PDA

View Full Version : Lighting issues (very basic)



davoring
08-10-2014, 12:21 PM
Hello,

I'm getting started with opengl, and I'm facing issues with the lighting. My problem is that I'm drawing a red shape, but it is rendered white. I'm sure I missing something elementary...

I'm developing in Java using the JOGL libraries.

Here is how I setup the lighting in the init method:



gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, new float[]{.5f, .5f, .5f, .5f}, 0);
gl.glEnable(GL2ES1.GL_LIGHTING);
gl.glEnable(GL2ES1.GL_LIGHT0);


and here is how I draw a red triangle:



gl.glBegin(GL.GL_TRIANGLES);

gl.glColor3f(1, 0, 0);

gl.glNormal3f(0f, 0f, 1f);
gl.glVertex3f(0, 0, -5);
gl.glVertex3f(1, 0, -5);
gl.glVertex3f(0, 1, -5);

gl.glEnd();



Like I said, the triangle is rendered completely white. What am I doing wrong?


I'm attaching the entire code if it can help.

Any help would be appreciated.

carsten neumann
08-10-2014, 12:57 PM
When using lighting the reflection properties of surfaces are described with materials (see glMaterial (http://www.opengl.org/sdk/docs/man2/xhtml/glMaterial.xml)) and vertex colors are ignored. You can specify that vertex colors be used as a material property (e.g. as diffuse color) with glColorMaterial (http://www.opengl.org/sdk/docs/man2/xhtml/glColorMaterial.xml) and the corresponding glEnable(GL_COLOR_MATERIAL).

davoring
08-10-2014, 01:11 PM
Perfect, thanks!

davoring
08-15-2014, 09:24 AM
Adding glEnable(GL_COLOR_MATERIAL) has improved things in that the brightness of my triangles now depends on their angle. However, I still have a problem: all triangles appear as if they were drawn at the world's origin. It's a bit difficult to explain, but what I mean is that if two triangles have the same angle compated to the world, but one is much further away from the light source than the other, both triangles are lit exactyl the same, when I would expect the further triangle to be darker, since its angle compared to the light source is smaller.

Any ideas?

Here is the relevant code:



public void display(GLAutoDrawable drawable) {
catchControls();

GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
gl.glLoadIdentity();

gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_AMBIENT, new float[]{ambientLightIntensity, ambientLightIntensity, ambientLightIntensity, ambientLightIntensity}, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, new float[]{diffuseLightIntensity, diffuseLightIntensity, diffuseLightIntensity, diffuseLightIntensity}, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR, new float[]{0, 0, 0, 0}, 0);
gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, new float[]{lightPositionX, lightPositionY, lightPositionZ, 0}, 0);

drawWorld(gl);
}


public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context
glu = new GLU(); // get GL Utilities
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL.GL_DEPTH_TEST); // enables depth testing
gl.glDepthFunc(GL.GL_LEQUAL); // the type of depth test to do
gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // best perspective correction
gl.glShadeModel(GLLightingFunc.GL_SMOOTH); // blends colors nicely, and smooths out lighting


gl.glColorMaterial(GL.GL_FRONT, GL2ES1.GL_DIFFUSE);
gl.glEnable(GL2ES1.GL_COLOR_MATERIAL);
gl.glEnable(GL2ES1.GL_LIGHTING);

gl.glEnable(GL2ES1.GL_LIGHT0);
}

carsten neumann
08-15-2014, 10:00 AM
You are specifying a directional light source (because last component w==0 for the position). If you want a point light source you need to specify a position (i.e. w!=0) and possibly configure the attenuation factors.

davoring
08-15-2014, 11:49 AM
Good stuff, thanks again. :)