• Advertisement
Sign in to follow this  

Lighting and scaling, problems

This topic is 4175 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

I have a problem related to the combination of these. My geometry with a scale(using glScalef) of 1.0 appear correctly lighted, but objects above appear to dark and below they are way to bright. Is there some obvious thing that may be causing this?

Share this post


Link to post
Share on other sites
Advertisement
what exactly do you mean when you say "objects above appear to dark and below they are way to bright". Do you mean any object being drawn below (as in a lesser y-value) the original object is being drawn too brightly and objects drawn above (as in a greater y-value) are being drawn too dark?

This doesn't seem like it should have much to do with the scaling unless for some odd reason your normals are getting scaled as well. The most likely culprit is how you've set up your lighting model in OpenGL. Can you please post the code that initializes your lighting, and possibly some of the actual drawing code itself?

Thanks

Share this post


Link to post
Share on other sites
I mean scale above and below 1.0.

Here's the render function:


void drawScreen() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

camera.look();

float m[16];

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light.ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light.diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light.specular);
glLightfv(GL_LIGHT0, GL_POSITION, light.position);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

for(size_t i = 0; i < objects.size(); ++i) {
Object *obj = objects;
Geometry *g = obj->geometry;
Material *mat = obj->material;
Transform &t = obj->transform->world;

t.rotate.toMatrix(m);

glPushMatrix();

glTranslatef(t.translate.x, t.translate.y, t.translate.z);
glScalef(t.scale.x, t.scale.y, t.scale.z);
glMultMatrixf(m);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat->specular);
glMaterialfv(GL_FRONT, GL_EMISSION, mat->emission);
glMaterialf(GL_FRONT, GL_SHININESS, mat->shininess);

glVertexPointer(3, GL_FLOAT, 0, (float *)&g->vertices[0]);
glNormalPointer(GL_FLOAT, 0, (float *)&g->normals[0]);
glDrawElements(GL_TRIANGLES, g->indices.size(), GL_UNSIGNED_SHORT, &g->indices[0]);

glPopMatrix();
}

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

SDL_GL_SwapBuffers();
}

Share this post


Link to post
Share on other sites
If the current transformation matrix contains scaling then you need to call glEnable(GL_NORMALIZE) so that OpenGL makes all normals unit length.

Share this post


Link to post
Share on other sites
Keep in mind that you really shouldn't use glEnable(GL_NORMALIZE). It can really slow your program down. Instead, you'll want to either A) Don't use glScale(), just modify the location of the vertices yourself B) Re-normalize the normals yourself after scaling and save those, that way you only have to re-normalize once. Calling glEnable(GL_NORMALIZE) has to re-calculate the normals every frame (I believe).

Share this post


Link to post
Share on other sites
Quote:
Original post by Mantear
Calling glEnable(GL_NORMALIZE) has to re-calculate the normals every frame (I believe).


It normalizes the normals. Depending if you are vertex limited or not, it will slow down, or you won't notice.
There is another option, I think call AUTO_NORMALIZE.
If you use uniform scale, enable this one instead because it is faster.

Share this post


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

  • Advertisement