SpotLightDemoCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:8k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import javax.microedition.m3g.*;
- class SpotLightDemoCanvas extends GameCanvas implements Runnable{
- private Graphics3D g3d; // Graphics object used to render the world.
- private World world; // This world contains the camera and the pyramidMesh.
- private Camera camera; // the camera in the scene
- private Mesh pyramidMesh; // the pyramid in the scene
- private Mesh icosahedronMesh; // the icosahedron in the scene
- private Light light;
- private int meshIndex = 0;
- private javax.microedition.midlet.MIDlet midlet;
- private final int SCREEN_WIDTH;
- private final int SCREEN_HEIGHT;
- public SpotLightDemoCanvas(javax.microedition.midlet.MIDlet m){
- super(false);
- midlet = m;
- setFullScreenMode(true);
- SCREEN_WIDTH = getWidth();
- SCREEN_HEIGHT = getHeight();
- g3d = Graphics3D.getInstance();
- world = new World();
- camera = new Camera();
- world.addChild(camera); // add the camera to the world.
- light = new Light(); // Create a new light
- light.translate(0.0f, 0.0f, -1.0f); // The light position
- light.setMode(Light.SPOT); // Light Mode
- light.setColor(0xFFFFFF); // The color of the light 'WHITE'
- light.setSpotExponent(10.0f); // splot light concentration 0 to 128
- light.setSpotAngle(30.0f);
- world.addChild(light); // Add the light to the world to be rendered.
- // Constructs a perspective projection matrix and sets that as the current projection matrix.
- camera.setPerspective(60.0f, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 50f);
- pyramidMesh = createpyramid(); // create our pyramid.
- pyramidMesh.setTranslation(0.0f, 0.3f, -3.0f); // move the pyramid 3 units into the screen.
- world.addChild(pyramidMesh);
- world.setActiveCamera(camera);
- Thread t = new Thread(this);
- t.start();
- }
- public void keyPressed(int keyCode){
- switch(keyCode){
- }
- }
- /*
- * Change the Light Mode to be used.
- */
- public void draw3D(Graphics g){
- g.setColor(0x000000);
- g.fillRect(0, 0, getWidth(), getHeight());
- try{
- g3d.bindTarget(g); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
- g3d.render(world); // Render the world
- }finally{
- g3d.releaseTarget();
- }
- }
- private void draw2D(Graphics g){
- g.setColor(0xFFFFFF);
- /*switch(lightIndex){
- case 0:
- g.drawString("Ambient", 0, 0, 0);
- break;
- case 1:
- g.drawString("Directional", 0, 0, 0);
- break;
- case 2:
- g.drawString("Omni", 0, 0, 0);
- break;
- case 3:
- g.drawString("Spot", 0, 0, 0);
- break;
- }*/
- // g.drawImage(SwapImage, 0, SCREEN_HEIGHT, Graphics.LEFT | Graphics.BOTTOM);
- // g.drawImage(NextImage, SCREEN_WIDTH, SCREEN_HEIGHT, Graphics.RIGHT | Graphics.BOTTOM);
- }
- public void run() {
- Graphics g = getGraphics();
- while(true){
- /* switch(meshIndex){
- case 0:
- icosahedronMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);
- break;
- case 1:
- // rotate the pyramid 1 degree around the Y-axis.
- pyramidMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);
- break;
- }*/
- pyramidMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);
- light.postRotate(1.0f, 1.0f, 0.0f, 0.0f); //enable this to make the light rotate around it's x-axis
- draw3D(g);
- draw2D(g);
- flushGraphics();
- }
- }
- // this method creates a colored pyramid.
- private Mesh createpyramid(){
- // The vertices used by the pyramid. x, y, z
- short []POINTS = new short[] {-1, -1, 1, 1, -1, 1, 0, 1, 0, // front
- 1, -1, 1, 1, -1, -1, 0, 1, 0, // right
- 1, -1, -1, -1, -1, -1, 0, 1, 0, // back
- -1, -1, -1, -1, -1, 1, 0, 1, 0, // left
- -1, -1, 1, 1, -1, 1, 1, -1, -1, // bottom right
- -1, -1, 1, 1, -1, -1, -1, -1, -1}; // bottom left
- // The wall angle of the pyramid is 70