OpenGL Lighting Trouble

This topic is 2721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey,
I'm attempting to enable lighting on Android, OpenGL ES 1.x, right now, only on the Android Emulator.

I've successfully imported some OBJs into a scene and rendered them, moved the camera around etc... but when I add these lines to my GLSurfaceView :: onSurfaceCreated, there is no visible change

My normals are being imported as well from the OBJ, but even if those were incorrect, shouldn't the ambient lighting have a noticable difference?

 public void onSurfaceCreated(GL10 gl, EGLConfig config) { //.... gl.glEnable(GL10.GL_LIGHTING); float[] global_ambient = { 0.2f, 0.2f, 0.2f, 1.0f }; gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, global_ambient,0); gl.glEnable(GL10.GL_LIGHT0); float[] lightAmbient = {0.1f,0.1f,0.1f,1.0f}; float[] diffuseLight = { 0.8f, 0.8f, 0.8f, 1.0f }; float[] specularLight = { 0.5f, 0.5f, 0.5f, 1.0f }; float[] position = { 0f, -20f, 0f, 1.0f // Assign created components to GL_LIGHT0 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseLight, 0); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specularLight, 0); gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, position, 0); //.... } 

Is there anything glaringly incorrect with the above? Any pointed questions which could possibly help me figure out what's going on?

Thanks!

Share on other sites
Make sure objects have normals, and make sure such are properly normalized.

How are your objects positioned? If your objects a re positioned at x,y,z (0.0f, 0.0f,-12.0f) , and since your light is placed at -20.0f, I doubt much effect would be cast.

Share on other sites

Make sure objects have normals, and make sure such are properly normalized.

How are your objects positioned? If your objects a re positioned at x,y,z (0.0f, 0.0f,-12.0f) , and since your light is placed at -20.0f, I doubt much effect would be cast.

Thanks for the reply. I suppose the light is not intense enough to change the scene due to the positioning of my objects. My normals are normalized and such, they should be fine.

The object I'm trying to light (or show a lack thereof) is a textured terrain where the vertices are evenly spaced in the X/Y plane and the heights are controlled by a heightmap. The terrain spans the area between 0,0,0 and 300,300,H, where H is the maximum value within the heightmap.

Thus, I assume, if lighting is even enabled with a point light at 0,0,-20, then I should see most of the terrain as pitch black. But this is not true. The lighting is even and the scene is identical to if lighting was disabled.

What could be the problem?

Share on other sites
give a code snippet of your terrain initiation code. Based on how it's set up, I might have a simple trick for lighting it that involves hitting your keyboard three times to achieve desired result.

Share on other sites

give a code snippet of your terrain initiation code. Based on how it's set up, I might have a simple trick for lighting it that involves hitting your keyboard three times to achieve desired result.

Hey!
Thanks again for your reply. Here's the core of my terrain generation code. It basically steps through a grid of quads, breaks them into triangles and duplicates the vertices in order to connect the edges.

 // Traverse the entire area for(int xSide = 0; xSide < numTilesPerSide-1 ; xSide ++){ for(int zSide = 0 ; zSide < numTilesPerSide-1; zSide++){ // First corner short firstCornerInd = (short) (verts.size()/3); Vector3d firstCornerVert = new Vector3d(xSide*xStep, heightMap[xSide][zSide], zSide * zStep); verts.add(firstCornerVert.x); verts.add(firstCornerVert.y); verts.add(firstCornerVert.z); texs.add(0.0f); // x texs.add(0.0f); // y // Second corner short secondCornerInd = (short) (verts.size()/3); Vector3d secondCornerVert = new Vector3d((xSide+1)*xStep, heightMap[xSide+1][zSide], zSide * zStep); verts.add(secondCornerVert.x); verts.add(secondCornerVert.y); verts.add(secondCornerVert.z); texs.add(1.0f); // x texs.add(0.0f); // y // Third corner short thirdCornerInd = (short) (verts.size()/3); Vector3d thirdCornerVert = new Vector3d((xSide+1)*xStep, heightMap[xSide+1][zSide+1], (zSide+1) * zStep); verts.add(thirdCornerVert.x); verts.add(thirdCornerVert.y); verts.add(thirdCornerVert.z); texs.add(1.0f); // x texs.add(1.0f); // y // Fourth Corner short fourthCornerInd = (short) (verts.size()/3); Vector3d fourthCornerVert = new Vector3d((xSide)*xStep, heightMap[xSide][zSide+1], (zSide+1) * zStep); verts.add(fourthCornerVert.x); verts.add(fourthCornerVert.y); verts.add(fourthCornerVert.z); texs.add(0.0f); // x texs.add(1.0f); // y /** * Add indices in this order * Face1: First Corner, Second corner, Third Corner */ inds.add(firstCornerInd); inds.add(secondCornerInd); inds.add(thirdCornerInd); // Create the normal Vector3d face1Normal = Vector3d.cross( firstCornerVert.subtract(secondCornerVert), thirdCornerVert.subtract(secondCornerVert)); face1Normal.normalise(); // Each vertex needs a normal, which is the same for all three vertices in the triangle for(int i = 0 ; i < 3; i++){ norms.add(face1Normal.x); norms.add(face1Normal.y); norms.add(face1Normal.z); } /** * Face2: Fourth Corner, First Corner, Third Corner */ inds.add(fourthCornerInd); inds.add(firstCornerInd); inds.add(thirdCornerInd); Vector3d face2Normal = Vector3d.cross( fourthCornerVert.subtract(firstCornerVert), thirdCornerVert.subtract(firstCornerVert)); face2Normal.normalise(); // Each vertex needs a normal, which is the same for all three vertices in the triangle for(int i = 0 ; i < 3; i++){ norms.add(face2Normal.x); norms.add(face2Normal.y); norms.add(face2Normal.z); } } } 

Share on other sites

// Second corner
short secondCornerInd = (short) (verts.size()/3);
Vector3d secondCornerVert = new Vector3d((xSide+1)*xStep, heightMap[xSide+1][zSide], zSide * zStep)
[/quote]

Many terrain use similar ways to construct and generate their structure. Subtract 50 from like so "heightMap[xSide+1][zSide - 50.0f".... tell me what you see.

Share on other sites

Many terrain use similar ways to construct and generate their structure. Subtract 50 from like so "heightMap[xSide+1][zSide - 50.0f".... tell me what you see.

This
 Vector3d firstCornerVert = new Vector3d(xSide*xStep, heightMap[xSide][zSide] - 50.0f, zSide * zStep); Vector3d secondCornerVert = new Vector3d((xSide+1)*xStep, heightMap[xSide+1][zSide] - 50.0f, zSide * zStep); Vector3d thirdCornerVert = new Vector3d((xSide+1)*xStep, heightMap[xSide+1][zSide+1] - 50.0f, (zSide+1) * zStep); Vector3d fourthCornerVert = new Vector3d((xSide)*xStep, heightMap[xSide][zSide+1] - 50.0f, (zSide+1) * zStep); 
is what I assume you meant, but the entire terrain map was translated, the lighting issues remain. That is, the texture remains fully lit.

Here are some screens.

Lighting Enabled, with textures

Lighting Disabled, with textures

Lighting Enabled, no textures

Lighting Disabled, no textures

I understand the reason why the scene is uniform color, because the light position is behind me. But shouldn't there be a difference between lighting being enabled and disabled?!

Share on other sites
Are you checking for glGetError?

Share on other sites
Well, that's what I was about to do after finishing my particle system class. Oddly enough, after activating my particle system, lighting began working.

I'll update the thread when I isolate the problem. It's going to be something stupid and obvious in hindsight.

Share on other sites
Silly me.
 gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE); // This seems to be causing the behaviour // !!!! // gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, // GL10.GL_MODULATE); 

Well, that's what I was about to do after finishing my particle system class. Oddly enough, after activating my particle system, lighting began working.

I'll update the thread when I isolate the problem. It's going to be something stupid and obvious in hindsight.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631429
• Total Posts
3000035
×