pyramidCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:5k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import javax.microedition.m3g.*;
- class pyramidCanvas 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
- public pyramidCanvas(){
- super(false);
- setFullScreenMode(true);
- g3d = Graphics3D.getInstance();
- world = new World();
- camera = new Camera();
- world.addChild(camera); // add the camera to the world.
- float w = getWidth();
- float h = getHeight();
- // Constructs a perspective projection matrix and sets that as the current projection matrix.
- camera.setPerspective(60.0f, w / h, 0.1f, 50f);
- pyramidMesh = createpyramid(); // create our pyramid.
- pyramidMesh.setTranslation(0.0f, 0.0f, -3.0f); // move the pyramid 3 units into the screen.
- world.addChild(pyramidMesh); // add the pyramid to the world
- world.setActiveCamera(camera);
- Thread t = new Thread(this);
- t.start();
- }
- public void draw3D(Graphics g){
- 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();
- }
- }
- public void run() {
- Graphics g = getGraphics();
- while(true){
- // rotate the pyramid 1 degree around the Y-axis.
- pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
- draw3D(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, // point 1
- 1, -1, 1, // point 2
- 1, -1, -1, // point 3
- -1, -1, -1, // point 4
- 0, 1, 0}; // point 5, top
- // The points sequence.
- int []INDICES = new int[] {
- 0, 1, 4,
- 1, 2, 4,
- 2, 3, 4,
- 3, 0, 4,
- 2, 1, 0,
- 2, 0, 3};
- byte []COLORS = new byte[] {127, 0, 0, //R
- 0, 127, 0, //G
- 0, 0, 127, //B
- 127, 0, 127, //B
- 0, 127, 127};//B
- // The length of each sequence in the indices array.
- int []LENGTH = new int[] {3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles
- VertexArray POSITION_ARRAY, COLOR_ARRAY;
- IndexBuffer INDEX_BUFFER;
- // Create a VertexArray to be used by the VertexBuffer
- POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);
- POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);
- COLOR_ARRAY = new VertexArray(COLORS.length / 3, 3, 1);
- COLOR_ARRAY.set(0, COLORS.length / 3, COLORS);
- INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);
- // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,
- // and texture coordinates for a set of vertices
- VertexBuffer vertexBuffer = new VertexBuffer();
- vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);
- vertexBuffer.setColors(COLOR_ARRAY);
- // Create the 3D object defined as a polygonal surface
- Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);
- Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh
- PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
- polygonMode.setPerspectiveCorrectionEnable(true);
- polygonMode.setCulling(PolygonMode.CULL_BACK); // By using CULL_NONE all faces of the pyramid will be shown.
- polygonMode.setShading(PolygonMode.SHADE_SMOOTH); // use a smooth shading of the colors on the pyramid.
- appearance.setPolygonMode(polygonMode);
- mesh.setAppearance(0, appearance); // Set the appearance to the 3D object
- return mesh;
- }
- }