Lighting and Other Conundrums!

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

Recommended Posts

In making my height-map thinky, I have run into a couple problems. Hopefully you can help! I have a height map that has a single light shining on it. The light is located above and in front of the height map. Here is an example screenshot: As well, I have a camera that circles around the height map. However, the lighting apperas to change when I move the camera, like so: (same height map, rotated about 30 degree counter-clockwise compared to first) Why dost this happen?! And lastly, if I rotate a lot around the image, back 'things' start to appear. Like this for example: (rotated about 60 degree clockwise compared to first image) And idea with this happens? Thanks for the help!

Share on other sites
You have to be careful about when you specify the location of your light. Specifying your light position before transforming your terrain will probably give you incorrect results. What do you mean by back things? I don't notice anything unusual about your third screenshot.

Share on other sites
Well, I don't actually transform the terrain. I just draw the terrain, set up the light, and then move the camera around.

And sorry, spelling mistake in the post... was supposed to say 'black' things. Basically some parts of the height map are being xed out by black portions. As I rotate even more about the image, the entire height map is almost consumed by the black pieces. I have no idea why this or the lighting thing is happening.

Any more ideas? (THanks for the help though).

Share on other sites
Quote:
 Original post by NietsnieWell, I don't actually transform the terrain. I just draw the terrain, set up the light, and then move the camera around.

Can you post your rendering loop? The way you've described your rendering seems a really strange way of doing things, so I want to see your code so I can understand exactly what you're doing. That will help me figure out what is wrong.

Regarding the black spots: It looks a lot like backface culling is causing those faces to be discarded. You can fix this either by turning off backface culling or by making sure all backfaces face downwards.

Share on other sites
Ok.

This is my setup for OpenGL:
// Setup for OpenGLint InitGL(GLvoid){    if (!LoadGLTexture()) return false;    glEnable(GL_TEXTURE_2D);    glShadeModel(GL_SMOOTH);        // Enable smooth shading    glClearColor(0.0f,0.0f,0.0f,0.0f); // Black background    glClearDepth(1.0f);             // Depth buffer setup    glEnable(GL_DEPTH_TEST);        // Enable depth testing    glDepthFunc(GL_LEQUAL);         // Type of depth test    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Quality of persective     glEnable(GL_NORMALIZE);    LoadFile("Terrain.txt", MAP_SIZE*MAP_SIZE, g_HeightMap);    moveCamera(0.0f);    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);    glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);        glEnable(GL_LIGHT1);    glEnable(GL_LIGHTING);    return true;}

And this is the the rendering segment:
// OpenGL Drawingint DrawGLScene(GLvoid){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer    glLoadIdentity();               // Reset current modelview matrix    gluLookAt(cx,cy,cz,  xf,yf,zf,  0,1,0); // Position, View, Up Vector = Camera's position and view    glScalef(scaleValue,scaleValue,scaleValue);        glBindTexture(GL_TEXTURE_2D, texture[0]);    map.drawMap();    glColor4f(1.0f,1.0f,1.0f,1.0f);    return true;}

(Note that the camereas position (cx,cy,cz) are changed through keyboard input. The cameras focus (xf,yf,zf) remains constant.

And this is the code that map.drawMap() eventually reaches:
void CTriangle::drawTriangle(bool first) {    glNormal3f (product.v[0], product.v[1], product.v[2]);    if (!first) {        glTexCoord2f(0.0f, 0.0f);        glVertex3f(vertex[0].x, vertex[0].y, vertex[0].z);        glTexCoord2f(0.0f, 1.0f);        glVertex3f(vertex[1].x, vertex[1].y, vertex[1].z);        glTexCoord2f(1.0f, 1.0f);        glVertex3f(vertex[2].x, vertex[2].y, vertex[2].z);    }    else {        glTexCoord2f(1.0f, 1.0f);        glVertex3f(vertex[0].x, vertex[0].y, vertex[0].z);        glTexCoord2f(1.0f, 0.0f);        glVertex3f(vertex[1].x, vertex[1].y, vertex[1].z);        glTexCoord2f(0.0f, 0.0f);        glVertex3f(vertex[2].x, vertex[2].y, vertex[2].z);    }}

Am I doing anything wrong? Thanks for the help so far!

Share on other sites
I just thought of something... is the lights position affected by moving the camere? I never actually change the light position, but is it affected indirectly by moving the camera? Because it seems that whatever the camera is pointing at is always lighter than what it is not pointing at...

Share on other sites
The light's position is affected by the modelview matrix when you send it (the light position) to OpenGL. So if you set the light position before the camera view matrix (your gluLookAt() call) then the light will always be positioned in the same place relative to the view. To have the camera appear in the correct world-space position no matter where the camera moves, send the light position after you call gluLookAt().

This is all mentioned in the Red Book and various web sites (many times here on gamedev). I know it can be tough to find things if you don't know exactly what to search for, but please try to do a little more research before posting here in the future.

Happy coding [smile]

EDIT: As for your second question, it would be good if we could see the drawMap() function as well.

Share on other sites
Thanks for the help Kalidor. What you said fixed the lighting :) I had actually checked these forums using the javascript search thing (I think that benryves made) and couldn't find anything. But I never even thought of the Red Book :)

As for the second question, here is the drawMap code:
void CMap::drawMap(){    for (int i=0;i<polygons.size();i++)        polygons.drawPolygon();}void CPolygon::drawPolygon(bool points){    glBegin(GL_TRIANGLES);       triangle[0].drawTriangle(true);       triangle[1].drawTriangle(false);    glEnd();}(Draw triangle is listed above).

As for making the triangles, here is the triangle class that makes the vertexs, calculates the vectors, and the normal (product) of the triangle:
class CTriangle {  public:    vector<CVertex> vertex;    vector<CVector> vectors;    CVector product;    CTriangle(int left, int back, int right, int front, int *heights, bool first);    ~CTriangle();    void crossProduct();       void drawTriangle(bool first); };CTriangle::CTriangle(int left, int back, int right, int front, int *heights, bool first) {    if (first) {        vertex.push_back(CVertex (left,front,heights[3]));   // Front left        vertex.push_back(CVertex (left,back,heights[0]));    // Back left point        vertex.push_back(CVertex (right,back,heights[1]));   // Back right    }    else {        vertex.push_back(CVertex (right,back,heights[1]));    // Back right point        vertex.push_back(CVertex (right,front,heights[2]));   // Front right        vertex.push_back(CVertex (left,front,heights[3]));   // Front left    }    vectors.push_back(CVector(vertex[0],vertex[1]));             // Calculate side vectors    vectors.push_back(CVector(vertex[1],vertex[2]));    vectors.push_back(CVector(vertex[2],vertex[0]));    crossProduct();                                             // Find the normal}CTriangle::~CTriangle() {}void CTriangle::crossProduct() {    product.v[0] = (vectors[1].v[1] * vectors[0].v[2]) - (vectors[1].v[2] * vectors[0].v[1]);    product.v[1] = (vectors[1].v[2] * vectors[0].v[0]) - (vectors[1].v[0] * vectors[0].v[2]);    product.v[2] = (vectors[1].v[0] * vectors[0].v[1]) - (vectors[1].v[1] * vectors[0].v[0]);    product.normalize();}

This is really bugging me, so any help is greatly appreciated!

[Edited by - Nietsnie on March 15, 2005 2:59:40 PM]

Share on other sites
Quote:
 Original post by NietsnieThanks for the help Kalidor. What you said fixed the lighting :) I had actually checked these forums using the javascript search thing (I think that benryves made) and couldn't find anything. But I never even thought of the Red Book :)

Glad I could help. And don't worry, I know it's tough searching for things sometimes. It was more a friendly reminder just in case you didn't search first, because it seems a lot of people think of gamedev like it's a 24/7 tech support hotline instead of a community of people spending their valuable time helping those in need (for free!).

Quote:
 if (first) { vertex.push_back(CVertex (left,front,heights[3])); // Front left vertex.push_back(CVertex (left,back,heights[0])); // Back left point vertex.push_back(CVertex (right,back,heights[1])); // Back right } else { vertex.push_back(CVertex (right,back,heights[1])); // Back left point vertex.push_back(CVertex (right,front,heights[2])); // Back right vertex.push_back(CVertex (left,front,heights[3])); // Front left }

That doesn't look right. Front left, back left, back right, then back left, back right, front left?

EDIT: Nevermind, I see it does look right in the actual code. This seems like an overly complicated way to create your mesh though. Is there anyway you could upload a demo of your program somewhere for us to check out so we can see more clearly how it is those black spots come up? And just since I didn't see you answer it before, you have tried disabling face culling, right?

Share on other sites
Well, I have tried sticking a
glDisable(GL_CULL_FACE);
in the InitGL section, but it didn't do anything.

I'll be able to get a video of whats happening in about 20 minutes. Thanks again for the help.

• 10
• 17
• 9
• 14
• 41