[SOLVED] disable + enable GL11.GL_LIGHTING => 3D Objects keep dark

Started by Darth Revan, September 14, 2012, 13:14:15

Previous topic - Next topic

Darth Revan

Hi, I try to write a GameWindow Class that I can use as base of my games. Now I have some problems:

1. If I render something in renderGame() it is dark. (If I remove GL11.glDisable(GL11.GL_LIGHTING); and GL11.glEnable(GL11.GL_LIGHTING); it is bright)
2. If there is something other that I should do in a different way, I would appreciate about any suggestion.

public GameWindow(final String title, final int width, final int height, final boolean fullscreen, final byte fps, final boolean vSync, final boolean showFps) {
	try {
		setDisplayMode(width, height, fullscreen);
	        Display.setTitle(title);
	        Display.setVSyncEnabled(vSync);
	        Display.create();
	} catch (LWJGLException e) {
		System.exit(0);
	}
	
	Mouse.setGrabbed(true);
	
	GL11.glViewport(0, 0, width, height);
		
	GL11.glEnable(GL11.GL_CULL_FACE);
	GL11.glEnable(GL11.GL_DEPTH_TEST);
	GL11.glShadeModel(GL11.GL_SMOOTH);
	
	// 3D
	GL11.glMatrixMode(GL11.GL_PROJECTION);
	GL11.glLoadIdentity();
	GLU.gluPerspective(67, (float) width / (float) height, 1, 100);
        
	GL11.glMatrixMode(GL11.GL_MODELVIEW);
	GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		
	create(); // loads Textures, Sounds...
	
	long time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
	int delta = (int) time;
	long lastFrame = time;
	
	while (!Display.isCloseRequested()) {
		time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
		delta = (int) (time - lastFrame);
		lastFrame = time;
 
		update(delta); // Logic
		
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glLoadIdentity();
	        
		renderGame(); // rendert 3D Objekte

		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL11.GL_LIGHTING);
	        
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glPushMatrix();
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, 0, height, 1, -1);
	        
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glPushMatrix();
		GL11.glLoadIdentity();

		renderHUD(); // render HUD

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glPopMatrix();
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glPopMatrix();
			
		GL11.glEnable(GL11.GL_LIGHTING);
		GL11.glEnable(GL11.GL_DEPTH_TEST);
	        
		Display.update();
		Display.sync(fps);
	}
	Display.destroy();
}


@Override
protected void renderHUD() {
	GL11.glDisable(GL11.GL_TEXTURE_2D);

	GL11.glColor3f(0, 0, 1);
		
	GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(0, 0);
		GL11.glVertex2f(200, 0);
		GL11.glVertex2f(200, 200);
		GL11.glVertex2f(0, 200);
	GL11.glEnd();
		
	GL11.glEnable(GL11.GL_TEXTURE_2D);
}


@Override
protected void renderGame() {
	camera.lookThrough();

	GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
        GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0.0f, 0.0f);
          	GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
            	GL11.glTexCoord2f(1.0f, 0.0f);
            	GL11.glVertex3f(1.0f, -1.0f, 1.0f);
            	GL11.glTexCoord2f(1.0f, 1.0f);
            	GL11.glVertex3f(1.0f, 1.0f, 1.0f);
            	GL11.glTexCoord2f(0.0f, 1.0f);
            	GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glEnd();
}

Fool Running

I would put your GL11.glEnable(GL11.GL_LIGHTING) and your GL11.glEnable(GL11.GL_DEPTH_TEST) calls before calling renderGame(). They are affecting renderGame() but only after the first run through the loop.
Also, the reason it's dark is because you are enabling lighting (with your GL11.glEnable(GL11.GL_LIGHTING)) call, but you are not specifying any light sources or normals for your model. I'd suggest Googling for examples on using lighting with OpenGL (I found this: http://www.glprogramming.com/red/chapter05.html but it uses glut so it may not help you a lot).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D