Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

J2ME 3D rendering problem

843799Sep 17 2004 — edited Nov 10 2004
Hi,

I'm experimenting with 3D which is available in the WTK22, and have come accross an annoying problem. I can create a mesh and convert it to a Node no problem, however these are my issues:

1). The mesh can be converted to an Object3D, but then won't cast to a world.

myWorld = (World) myObject

results in a cast error.

2). So, I tried using the Graphics3D render method which just renders nodes, but using

g3d.render(MYOBJECT,null);

results in an IllegalStateException.

It seems therefore that the mesh is incorrect, but it's straight from the examples. I have attached the code in the hope it will be usefull, please ignore all of the variables at the top :P

ThreeD.java:

package Game;

import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.m3g.*;

public class ThreeD extends MIDlet implements CommandListener
{

LayerManager layers = new LayerManager();
TiledLayer tiles;
Calendar cal = Calendar.getInstance();
Date date;
Alert alert;
int LastKey = 0;
int GameTemp = 0;
Random generator = new Random( System.currentTimeMillis() );
Display display;
public Graphics g;
VideoControl videoControl;
Graphics3D g3d;
Background background = new Background();
Fog fog;
Image2D image2D;
Texture2D texture2D;
Transform tTransform = new Transform();

World MYWORLD = new World();
Node MYOBJECT;

// Rem This is called when your application starts up, use it for setting variables etc.
Canvas MYCANVAS;
Timer timer0;

public ThreeD()
{

CUBE MYOBJECTa = new CUBE();
MYOBJECT = (Node) MYOBJECTa.getObject();
}


// Rem This is your main function, your application will run from this.
public void startApp()
{
display = Display.getDisplay(this);

// Rem This is the main canvas, you can create more.
MYCANVAS = new Canvas()
{
protected void showNotify(){ startTimer(); repaint(); }
protected void hideNotify(){ timer0.cancel(); }
// **;

// Rem This is where the main drawing operations are done.
protected void paint(Graphics b)
{
g = b;

// Rem This must be performed for any 3D commands to work
g3d = Graphics3D.getInstance();
g3d.bindTarget(b);

// Rem Clear our 3D viewport
background.setColor(0xf54588);
g3d.clear(background);

if(g3d == null){ System.out.println("Cannot get device"); }
if(MYWORLD == null){ System.out.println("World is null!"); }

// Rem Render the world
//g3d.render(MYWORLD);
g3d.render(MYOBJECT,null);


// Rem Close the 3D command set
g3d.releaseTarget();
}


// Rem This is called when a key is pressed.
protected void keyPressed(int keyCode)
{
date = new Date(); LastKey = Math.abs((int) date.getTime());
// Rem Update the screen.
repaint();
}


// Rem This is a loop with a sync rate of 1 second (1000 milli seconds)
private void startTimer()
{
timer0 = new Timer();
TimerTask updateTask = new TimerTask()
{
public void run()
{
// Rem Update the screen.
repaint();
} };
long interval = 1000;
timer0.schedule(updateTask, interval,interval);}
};


// Rem Show the canvas on the screen.
display.setCurrent(MYCANVAS);


// Rem Give it a listener so that the form can respond to user input (Action Function).
MYCANVAS.setCommandListener(this);

}

// Rem This is active when your application is paused.
public void pauseApp() { }


// Rem This is called when a button is pressed, c is the name of your button.
public void commandAction(Command C, Displayable s) { }


// Rem This is called when your application is closing.
public void destroyApp(boolean unconditional) { }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CUBE.java:

package Game;

import javax.microedition.m3g.*;

public class CUBE {
public CUBE(){}

public static Mesh getObject(){

int[][] aaStripLengths = {{4}, {4}, {4}, {4}, {4}, {4}};

// These are the vertex positions
short[] aPos =
{
// Front
-1, -1, 1, // B
1, -1, 1, // C
-1, 1, 1, // A
1, 1, 1, // D
// Bottom
-1, -1, -1, // F
1, -1, -1, // G
-1, -1, 1, // B
1, -1, 1, // C
// Top
-1, 1, 1, // A
1, 1, 1, // D
-1, 1, -1, // E
1, 1, -1, // H
// Right
1, 1, 1, // D
1, -1, 1, // C
1, 1, -1, // H
1, -1, -1, // G
// Left
-1, -1, 1, // B
-1, 1, 1, // A
-1, -1, -1, // F
-1, 1, -1, // E
// Back
1, -1, -1, // G
-1, -1, -1, // F
1, 1, -1, // H
-1, 1, -1 // E
};

// These are the colors for the vertices
byte[] aCol =
{
// Front
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
// Bottom
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// Top
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// Right
-1, -1, 0,
-1, -1, 0,
-1, -1, 0,
-1, -1, 0,
// Left
-1, 0, -1,
-1, 0, -1,
-1, 0, -1,
-1, 0, -1,
// Back
0, -1, -1,
0, -1, -1,
0, -1, -1,
0, -1, -1,
};

// Calculate the number of submeshes and vertices directly from the sizes
// of the arrays. This prevents us getting a mismatch if we decide to change
// the cells to a different shape later.
int cSubmeshes = aaStripLengths.length;
int cVertices = aPos.length/3;

// We will share a default appearance between all the faces on the cube. Each
// face is a separate "submesh" - it can have a separate appearance if we wish.
Appearance app = new Appearance();

// We need to specify an appearance and the submesh data for each face.
Appearance[] appa = new Appearance[cSubmeshes];
IndexBuffer[] iba = new IndexBuffer[cSubmeshes];

int startIndex=0;
for(int i=0;i<cSubmeshes;i++)
{
// We use the same apppearance for each.
appa=app;

// And we create a new triangle strip array for each submesh.
// The start index for each one just follows on from previous submeshes.
iba[i] = new TriangleStripArray(startIndex, aaStripLengths[i]);
for(int j=0; j<aaStripLengths[i].length;j++)
startIndex+=aaStripLengths[i][j];
}

// Now we create a new vertex buffer that contains all the above information
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.setDefaultColor(0xFFFFFFFF); // white

{
// Copy the vertex positions into a VertexArray object
VertexArray vaPos = new VertexArray(cVertices, 3, 2);
vaPos.set(0, cVertices, aPos);
vertexBuffer.setPositions(vaPos, 0.40f, null);
}

{
// Copy the vertex colors into a VertexArray object
VertexArray vaCols = new VertexArray(cVertices, 3, 1);
vaCols.set(0, cVertices, aCol);
vertexBuffer.setColors(vaCols);
}

// Create the mesh, set its translation...
Mesh m = new Mesh(vertexBuffer, iba, appa);
m.setTranslation(0,0,0);

// ...and finally return
return m;

}

}


Thanks,
John
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 8 2004
Added on Sep 17 2004
1 comment
216 views