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?