J2ME - Where is my 3D cube?
Hello!I have just started to develope some apps for J2ME. Before I have explored Java3D so perhaps I tries to merge these two technics and there for run into this problem.
My plan for my first bigger app would consist of 4 classes.
CubeGame - the midlet
CubeCanvas - the canvas to display and render 3D objects.
Cube - a class that holds everything for a 3D cube-object.
RefreshTask - a timer class that i hope will be replaced by letting CubeCanvas implemets Runnable instead.
I also use a image for the texture for the cube, that one can be found at:
http://developers.sun.com/techtopics/mobility/apis/articles/3dgraphics/texture.png
The problem is that the cube never get's displayed when I start this app. I just see a black screen with my time counter. Perhasps it is there somewhere but is out of sight I do not know??
So if any one could tell me whats wrong and how to get the cube in the display please tell me!
Below is the source code.
Best regards
Fredrik Andersson
--------------------------
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;
public class CubeGame extends MIDlet implements CommandListener
{
static CubeGame instance;
CubeCanvas cubeCanvas = new CubeCanvas();
public CubeGame()
{
this.instance = this;
}
public void startApp()
{
Display.getDisplay(this).setCurrent(cubeCanvas);
Command exitCommand = new Command("Exit", Command.EXIT, 0);
cubeCanvas.addCommand(exitCommand);
cubeCanvas.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public static void quitApp()
{
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
public void commandAction(Command c, Displayable s)
{
if (c.getCommandType() == Command.EXIT)
{
notifyDestroyed();
}
}
}
--------------------
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
import javax.microedition.midlet.*;
public class CubeCanvas extends Canvas
{
private Graphics3D graphics3D;
private Camera camera = new Camera();
private Light light = new Light();
private Transform transform = new Transform();
private Background background = new Background();
private World world = new World();
private byte time = 100;
private Group group = new Group();
private Timer timer = new Timer();
private TimerTask timerTask = null;
private Cube cube = new Cube();
public CubeCanvas()
{
init();
}
public void init()
{
graphics3D = Graphics3D.getInstance();
background.setColor(0x33ccff);
world.addChild(group);
group.setOrientation(15.0f, 1.0f, 0.0f, 0.0f);
group.addChild(cube);
world.addChild(camera);
world.setActiveCamera(camera);
camera.setParallel((1<<3)*1.5f, 1.0f, -(1<<3), (1<<3));
camera.setPerspective( 60.0f, // field of view
(float)getWidth()/ (float)getHeight(), // aspectRatio
1.0f, // near clipping plane
1000.0f ); // far clipping plane
// create a light
light.setColor(0xffffff); // white light
light.setIntensity(1.25f); // overbright
world.addChild(light);
}
public void rotateCube(short cubeId, short dircetion)
{
}
public void connectCubes(short cubeId, short dircetion)
{
}
public void paint(Graphics g)
{
if(this == null || world == null)
{
return;
}
if(timerTask != null)
{
timerTask.cancel();
timerTask=null;
}
graphics3D.bindTarget(g);
graphics3D.clear(background);
Transform transform = new Transform();
transform.postTranslate(0.0f, 0.0f, 10.0f);
graphics3D.setCamera(camera, transform);
// set up a "headlight": a directional light shining
// from the direction of the camera
graphics3D.resetLights();
graphics3D.addLight(light, transform);
try
{
graphics3D.render(world);
}
finally
{
graphics3D.releaseTarget();
}
g.setColor(0xFFFFFFFF);
g.drawString(time + " sec", 10, 30, Graphics.LEFT | Graphics.TOP);
timerTask = new RefreshTask(this);
timer.schedule(timerTask, 1000);
time--;
}
}
-----------------------
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;
public class Cube extends Group
{
private short size;
private Image[] images;
private VertexBuffer vertexBuffer; // positions, normals, colors, texcoords
private IndexBuffer indexBuffer; // indices to vertexBuffer, formingtriangle strips
private Appearance appearance; // material, texture, compositing, ...
private Material material = new Material();
private Mesh mesh;
private Image iImage;
short[] vert = {
1, 1, 1, -1, 1, 1, 1,-1, 1, -1,-1, 1, // front
-1, 1,-1, 1, 1,-1, -1,-1,-1, 1,-1,-1, // back
-1, 1, 1, -1, 1,-1, -1,-1, 1, -1,-1,-1, // left
1, 1,-1, 1, 1, 1, 1,-1,-1, 1,-1, 1, // right
1, 1,-1, -1, 1,-1, 1, 1, 1, -1, 1, 1, // top
1,-1, 1, -1,-1, 1, 1,-1,-1, -1,-1,-1 }; // bottom
byte[] norm = {
0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,
0, 0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127,
-127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0,
127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127, 0 };
short[] tex = {
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1 };
int[] stripLen = { 4, 4, 4, 4, 4, 4 };
//public Cube(short s, Image[] i)
public Cube()
{
try
{
//size = s;
//images = i;
VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
vertArray.set(0, vert.length/3, vert);
VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
normArray.set(0, norm.length/3, norm);
VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
texArray.set(0, tex.length/2, tex);
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(vertArray, 1.0f, null); // unit scale, zerobias
vertexBuffer.setNormals(normArray);
vertexBuffer.setTexCoords(0, texArray, 1.0f, null); // unit scale, zerobias
indexBuffer = new TriangleStripArray( 0, stripLen );
iImage = Image.createImage( "/images/texture.png" );
Image2D image2D = new Image2D( Image2D.RGB, iImage );
Texture2D texture = new Texture2D( image2D );
texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
texture.setBlending(Texture2D.FUNC_MODULATE);
appearance = new Appearance();
appearance.setTexture(0, texture);
appearance.setMaterial(material);
material.setColor(Material.DIFFUSE, 0xFFFFFFFF); // white
material.setColor(Material.SPECULAR, 0xFFFFFFFF); // white
material.setShininess(100.0f);
mesh = new Mesh(vertexBuffer, indexBuffer, appearance);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public short getSize()
{
return size;
}
public Image[] getImages()
{
return images;
}
}
-------------------------------------------
import java.util.*;
public class RefreshTask extends TimerTask
{
CubeCanvas cubeCanvas;
public RefreshTask(CubeCanvas cc)
{
cubeCanvas = cc;
}
public void run()
{
cubeCanvas.repaint();
}
}
