[SOLVED] OpenGL not rendering properly

This code is straight from OpenGL Superbible 4th edition and I cant seem to get it to work in LWJGL.
Can someone please help me.



import general.OGLStarter;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.opengl.Display;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import org.lwjgl.util.glu.Sphere;

public class Solar extends OGLStarter {
    ByteBuffer bBuffer = ByteBuffer.allocateDirect(1024 + 128);
    FloatBuffer lightPos = bBuffer.asFloatBuffer();
    FloatBuffer whiteLight = bBuffer.asFloatBuffer();
    FloatBuffer sourceLight = bBuffer.asFloatBuffer();
    
    Sphere glSphere = new Sphere();
    
    public Solar() {
        // Lighting Values
        float light[] = { 0.0f, 0.0f, 0.0f, 1.0f };
        float  whiteLight2[] = { 0.2f, 0.2f, 0.2f, 1.0f };
        float  sourceLight2[] = { 0.8f, 0.8f, 0.8f, 1.0f };
        
        // Add lighing values to buffers
        lightPos.put(light);
        lightPos.flip();
        whiteLight.put(whiteLight2);
        whiteLight.flip();
        sourceLight.put(sourceLight2);
        sourceLight.flip();
        
    }
    
    // Called to draw scene
    public void renderScene() {
        // Earth and Moon angle of revolution
        float fMoonRot = 0.0f;
        float fEarthRot = 0.0f;

        // Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Save the matrix state and do the rotations
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();

        // Translate the whole scene out and into view	
        glTranslatef(0.0f, 0.0f, -300.0f);	

        // Set material color, Red
        // Sun
        glDisable(GL_LIGHTING);
        glColor3ub((byte)255, (byte)255, (byte)0);
        glSphere.draw(15.0f, 30, 17);
        glEnable(GL_LIGHTING);

        // Move the light after we draw the sun!
        glLight(GL_LIGHT0,GL_POSITION,lightPos);

        // Rotate coordinate system
        glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);

        // Draw the Earth
        glColor3ub((byte)0,(byte)0,(byte)255);
        glTranslatef(105.0f,0.0f,0.0f);
        glSphere.draw(15.0f, 30, 17);


        // Rotate from Earth based coordinates and draw Moon
        glColor3ub((byte)200,(byte)200,(byte)200);
        glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
        glTranslatef(30.0f, 0.0f, 0.0f);
        fMoonRot+= 15.0f;
        if(fMoonRot > 360.0f)
                fMoonRot = 0.0f;

        glSphere.draw(6.0f, 30, 17);

        // Restore the matrix state
        glPopMatrix();	// Modelview matrix


        // Step earth orbit 5 degrees
        fEarthRot += 5.0f;
        if(fEarthRot > 360.0f)
                fEarthRot = 0.0f;

        // Show the image
        Display.update();
        Display.sync(10);
    }


    // This function does any needed initialization on the rendering
    // context. 
    public void setupRC() {
	// Light values and coordinates
	glEnable(GL_DEPTH_TEST);	// Hidden surface removal
	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
	glEnable(GL_CULL_FACE);		// Do not calculate inside of jet

	// Enable lighting
	glEnable(GL_LIGHTING);

	// Setup and enable light 0
	glLightModel(GL_LIGHT_MODEL_AMBIENT,whiteLight);
	glLight(GL_LIGHT0,GL_DIFFUSE,sourceLight);
	glLight(GL_LIGHT0,GL_POSITION,lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	// Black blue background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    }

    public void checkSize(int w, int h) {
        float fAspect;

        // Prevent a divide by zero
        if(h == 0)
            h = 1;

        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);

        // Calculate aspect ratio of the window
        fAspect = (float)w/(float)h;

        // Set the perspective coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        // field of view of 45 degrees, near and far planes 1.0 and 425
        gluPerspective(45.0f, fAspect, 1.0f, 425.0f);

        // Modelview matrix reset
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    public static void main(String[] args) {
        new Solar().run();
    }
}


This program is a mini solar system of just the Sun, the Earth and the Moon.
The Sun is in the center of the screen while the earth rotates around the Sun, similarly the Moon rotate around the Earth.

The Output that I get is only the Sun. What am I doing wrong?

Try disabling lighting. Sun is the only one with lights disabled, so maybe that has something to do with it.

Tried disabling lighting but now my Sun is gone.


// Light values and coordinates
	glEnable(GL_DEPTH_TEST);	// Hidden surface removal
	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
	glEnable(GL_CULL_FACE);		// Do not calculate inside of jet

	// Enable lighting
	glEnable(GL_LIGHTING);

	// Setup and enable light 0
	glLightModel(GL_LIGHT_MODEL_AMBIENT,whiteLight);
	glLight(GL_LIGHT0,GL_DIFFUSE,sourceLight);
	glLight(GL_LIGHT0,GL_POSITION,lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

I commented the lighting code out and now I see the Earth and Moon but no rotation.
What am I doing wrong about the lighting and why is there no rotation?

This code is copied straight out of the book except for the FloatBuffer but thats how its done in java.

So i figured out why my Earth and Moon were not showing up ;D, I need to add this to my ByteBuffer


    ByteBuffer bBuffer = ByteBuffer.allocateDirect(1024 + 128).order(ByteOrder.nativeOrder());
    FloatBuffer lightPos = bBuffer.asFloatBuffer();
    FloatBuffer whiteLight = bBuffer.asFloatBuffer();
    FloatBuffer sourceLight = bBuffer.asFloatBuffer();

I am not 100% sure why this is a requirement but I think what it does is it sets the order of the bytes either to little endian or big endian depending on your system.
I guess without this it may cause a mismatch with which order your system uses thus leading to no visual of the objects.

Now the last problem I need to solve is the rotation.
Anyone?

You create 1 bytebuffer (which is is wrongly ordered), and create 3 floatbuffer views of it - that means that they all refer to the same memory address. So you overwrite the same memory with three different values, and them reading them later, expecting to find three different values.

I’ts better to use LWJGL utilities for this:


    FloatBuffer lightPos = BufferUtils.createFloatBuffer(...);
    FloatBuffer whiteLight =BufferUtils.createFloatBuffer(...);
    FloatBuffer sourceLight = BufferUtils.createFloatBuffer(...);

Riven thank you so much, that actually works much better ;D.

Now onto rotation.

Nevermind I figured it out. The problem is that I have both fEarthRot and fMoonRot declared and initialized in the renderScene() method which is in the game loop. So the value of both variables never increase. Dumb move on my part.

Thanks for all the help :D.