3D noob

So far I’ve done a bit of 2D stuff with LWJGL. I like 2D because development is fast. However, I’d like to start incorporating some 3D effects in my 2D games. I think this is a good first step and eventually I might end up doing a fully 3D game.

I don’t really know where to start. For my first goal, I’d like to make a spinning spiked ball (like the ball from a mace). Should I start by making a box spin and then replace the box with a spiked ball? How would I make the spiked ball? I have Maya, is that a good tool to use for this? After I create my spiked ball (which should be a relatively easy model to make) how do I get it into LWJGL?

I should also note that I plan to do development for both the desktop and Android, so I’d like to keep the OpenGL bits as simple as possible. Eg, I don’t want to use something like jMonkeyEngine, since that is not available for Android (which, for the most part, only has OpenGL ES 1.0).

[quote]Should I start by making a box spin and then replace the box with a spiked ball?
[/quote]
I think it is a good idea to get use of the 3D functions. There is GLU function to create cube and sphere.

[quote]How would I make the spiked ball? I have Maya, is that a good tool to use for this?
[/quote]
Yes, Maya, Blender, Milkshape, … anything is good ;D

[quote]After I create my spiked ball (which should be a relatively easy model to make) how do I get it into LWJGL?
[/quote]
When done, export your spiked ball to “.obj” file format. It is a text file format that it is easy to do an importer.

[quote]I don’t want to use something like jMonkeyEngine, since that is not available for Android (which, for the most part, only has OpenGL ES 1.0).
[/quote]
I drop using JMonkey too. I do my own engine but it take a LOT of time (and it will be difficult to make all functionality). What I really want is a OpenGL ES binding in LWJGL, I will make an OpenGL/OpenGL ES engine easier :wink:

I created a simple spiked ball with Maya. I lifted some simple code to parse the OBJ text format and managed to get my model rendered with LWJGL. Sweet!

My render code looks like this:

FloatBuffer position = BufferUtils.createFloatBuffer(4);
position.put(new float[] {0.0f, 0.0f, 800.0f, 0.0f}).flip();
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, position);
GL11.glEnable(GL11.GL_LIGHT0);

FloatBuffer red = BufferUtils.createFloatBuffer(4);
red.put(new float[] {0.8f, 0.1f, 0.0f, 1.0f}).flip();
GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE, red);

GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_LIGHTING);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glFrustum(-1, 1, -1, 1, 30f, 500.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glTranslatef(0.0f, 0.0f, -290.0f);

FloatBuffer projection = BufferUtils.createFloatBuffer(128);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);

FloatBuffer modelview = BufferUtils.createFloatBuffer(128);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);

IntBuffer viewport = BufferUtils.createIntBuffer(128);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);

FloatBuffer winPos = BufferUtils.createFloatBuffer(3);
GLU.gluProject(800, 600, 1f, modelview, projection, viewport, winPos);
float xs = 800 / winPos.get(0);
float ys = 600 / winPos.get(1);
float zs = 1 / winPos.get(2);
GL11.glScalef(xs, ys, zs);

GL11.glRotatef(rotation, 0, 1, 1);

obj.draw();

I’m trying to use gluProject so (for example) I can model an object with a width of 60 and when I render it to the screen it will be 60 pixels wide. The above works, however I found the values for glFrustum mostly through trial and error. I found if I change zNear to 5f instead of 30f that my object is rendered 3 to 4 pixels too small. Can anyone help me understand why this occurs and also how I should know what values to plug in to glFrustum?

128 ? ???

Beside that you seem to use a amfully complicated solution :slight_smile:

   
    GL11.glViewport(viewport_x, viewport_y, viewport_w, viewport_h);
    GL11.glMatrixMode(GL11.GL_PROJECTION) ;
    GL11.glLoadIdentity();
    GLU.gluPerspective(fovy,(float)viewport_w/(float)viewport_h, zNear ,zFar);      
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();

For gluPerspective, you can use fovy = 45°, viewport_x = 0, viewport_y=0, viewport_w = 800, viewport_h = 600, zNear = 1, zFar=1000.

I didn’t try but give a try to :

  • scale the object by 100
  • translate by -100*zNear