• Advertisement
Sign in to follow this  

Lighting and Other Conundrums!

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Nietsnie
Well, 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 this post


Link to post
Share on other sites
Ok.

This is my setup for OpenGL:
// Setup for OpenGL
int 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 Drawing
int 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Nietsnie
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 :)

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nietsnie
Oops, thats just a mistake with the comments.


Heh, yeah I snuck in an edit right before your post.

Share this post


Link to post
Share on other sites
Any programs you recomend for caputreing screen output? I've tried three so far and they all either don't let me caputre more than 1fps or they just plain don't work properly. Any suggestions?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nietsnie
Any programs you recomend for caputreing screen output? I've tried three so far and they all either don't let me caputre more than 1fps or they just plain don't work properly. Any suggestions?


I've heard good things about Fraps, but I've never used it and I'm not sure of the video recording capabilities of the free version.

Share this post


Link to post
Share on other sites
Ack, I can't get any of the video recording stuff to work :(

How would I go about disabling all back face culling? That may work. As well, I remember reading in one of NeHe's tutorial (can't remember which one) that the order in which you draw vertices of a shape is important, as it can mess up how the shape looks. Could this have something to do with it? This is getting quite annoying?

Share this post


Link to post
Share on other sites
glDisable(GL_CULL_FACE); should turns off face culling, make sure it isnt being enabled anywhere else.

and yes, the winding order of the triangles matters, as depending on the order lets the gfx card/OGL work out if its front or back facing.
Generally front facing polygons are wound counter-clockwise (so, if it was drawing a square you'd start top left, then bottom left, then bottom right and finally top right) and when they become back facing they are wound clockwise.

Share this post


Link to post
Share on other sites
Quote:
and yes, the winding order of the triangles matters, as depending on the order lets the gfx card/OGL work out if its front or back facing.
Generally front facing polygons are wound counter-clockwise (so, if it was drawing a square you'd start top left, then bottom left, then bottom right and finally top right) and when they become back facing they are wound clockwise.
Haha fun... I've been doing it clockwise :). I'll check if that works...

EDIT: No, it doesn't, but I just figured out what is happening (although now how to fix it). If I look at the map from the front, everything is fine. However, if I rotate around to the back side, all the faces become translucent. As in, the polygons that are behind them but do not face the camera are being draw overtop of the ones that should be drawn. Odd.

Share this post


Link to post
Share on other sites
Heres an example:

First shot, taken from the front and just showing the layout of this height-map :
Image Hosted by ImageShack.us

Second shot, camera rotated around 180 degrees (and positioned higher) (importance is in the back right area):
Image Hosted by ImageShack.us

Third shot, moving down so the lighted polygon in back right is almsot gone:
Image Hosted by ImageShack.us

And fourth shot, showing the 'phenomenom', with the camera positioned even lower:
Image Hosted by ImageShack.us



EDIT: Aha! I tried enabling GL_CULL_FACE and it worked. But still, why would it not work when GL_CULL_FACE was disabled?
It looks almost as if the polygon that does not face the camera is overlapping infront of the polygon that is facing the camera. Any idea why this is happening?

EDIT2: Well, it kind of works. It doesnt work still when a polygon is facing the camera, but there is another polygon in front of that is also facing the camera. In that case, the polygon that is farther behind will overlap the one that is closer to the camera. And yes, depth testing is enabled.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement