Forum Stats

  • 3,872,606 Users
  • 2,266,458 Discussions
  • 7,911,262 Comments

Discussions

How to create a cube with m3g

843851
843851 Member Posts: 19,484
edited Aug 30, 2010 6:42AM in Java ME SDK
Hi, I am new in jsr 184 (m3g) technology. In my practice, I want to create a simple cube. My code is something like this.
/
 To change this template, choose Tools | Templates
 *and open the template in the editor.*
/
package Chapter6;

import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.*;*
*import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;*

*/**

 @author paladin
*/*
*public class SimpleCubeCanvas extends Canvas {*

*    private Display display;*
*    private byte[] m_vertices = {0, 0, 0,*
*        1, 0, 0,*
*        1, 1, 0,*
*        0, 1, 0,*
*        0, 0, 1,*
*        1, 0, 1,*
*        1, 1, 1,*
*        0, 1, 1*
*    };*

*    private int[] m_indexData = {0, 1, 3, 2, 7, 6, 4, 5, 0, 1};*
*    private VertexArray vertexArray;*
*    private TriangleStripArray triangleStripArray;*
*    private VertexBuffer vertexBuffer;*
*    private Appearance appearance;*
*    private Mesh meshCube;*
*    private Graphics3D graph3D;*
*    private Background background;*
*    private Camera camera;*
*    private Light light;*

*    public SimpleCubeCanvas(Display dis) {*
*        super();*
*        display = dis;*

*        try {*

*            // create vertex array*
*            vertexArray = new VertexArray(m_vertices.length / 3, 3, 1);*
*            vertexArray.set(0, m_vertices.length / 3, m_vertices);*

*            // create vertex buffer*
*            vertexBuffer = new VertexBuffer();*
*            vertexBuffer.setPositions(vertexArray, 1.0f, null);*

*            // create the triangleStripArray and populate it*
*            triangleStripArray = new TriangleStripArray(m_indexData,*
*                    new int[]{m_indexData.length});*

*            // create appearance*
*            appearance = new Appearance();*
*            Material material = new Material();*
*            material.setColor(Material.DIFFUSE, 0x01FF0000);*
*            appearance.setMaterial(material);*
*            meshCube = new Mesh(vertexBuffer, triangleStripArray, appearance);*

*            background = new Background();*
*            background.setColor(0xFFFFF);*

*            light = new Light();*
*            light.setMode(Light.DIRECTIONAL);*
*            light.setColor(0xFCFCFC);*

*            camera = new Camera();*
*            camera.setPerspective(120.0f, (float) getWidth() / getHeight(),*
*                    0.1f, 1000f);*


*        } catch (Exception ex) {*
*            ex.printStackTrace();*
*        }*
*    }*

*    public void start() {*
*        display.setCurrent(this);*
*        repaint();*

*    }*

*    protected void paint(Graphics g) {*
*        graph3D = Graphics3D.getInstance();*
*        graph3D.addLight(light, null);*
*        graph3D.setCamera(camera,null);*

*        try {*
*            graph3D.bindTarget(g, true, 0);*
*            graph3D.clear(background);*
*            graph3D.render(meshCube, null);*
*        } catch (Exception ex) {*
*            ex.printStackTrace();*
*        } finally {*
*            graph3D.releaseTarget();*
*        }*
*    }*
*}*

*
*
and the midlet code is like this
*
*
*/*
 *To change this template, choose Tools | Templates*
 and open the template in the editor.
*/*

*package Chapter6;*

*import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;*

*/**
 *@author paladin*
/
public class SimpleCubeMIDLet extends MIDlet implements CommandListener{

    private SimpleCubeCanvas canvas;
    private Command exitCommand;

    public SimpleCubeMIDLet(){

        canvas = new SimpleCubeCanvas(Display.getDisplay(this));
        exitCommand = new Command("Exit",Command.EXIT,2);
        canvas.setCommandListener(this);
        canvas.addCommand(exitCommand);
    }

    public void startApp() {
        canvas.start();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d){
        if ( c == exitCommand){
            destroyApp(true);
            notifyDestroyed();
        }
    }
}
But what I get is a magenta screen and I could not see the cube. Please tell me my mistake. Thank you very much.

Comments

This discussion has been closed.