Opengl bad lighting

Started by
6 comments, last by Shawn619 10 years, 10 months ago

I'm getting bad lighting and was woundering if anyone can tell me why.

I use a shader, and import/render models in obj format(normal,verts). I don't know what GL_SMOOTH vs. GL_FLAT does, because I used both and neither have any effect. I'm 90% sure my .obj model exported from blender gives me per-vertex normal and not per-face normal, so I don't think that's the problem. Why doesn't my opengl look like my blender render(with GL_SMOOTH on)?

Opengl program:

j9sruq.jpg

Blender render (with GL_SMOOTH on):

16ifuxz.jpg

Advertisement
Specular highlights (shininess)? Different shadowing opacity?

[edit] And you're not interpolating the normals of shared vertices.

Specular highlights (shininess)? Different shadowing opacity?

[edit] And you're not interpolating the normals of shared vertices.

Sorry, i'm only dealing with the specular component right now, even though blender produces all 3 components. There are no shadows.

How would I go about interpolating the normals of shared vertices?

If they're at various distances from each other you'd need Gouraud shading/interpolation. Haven't tried it.

http://www.gamedev.net/blog/906/entry-2256401-lightmapping-curved-csg-surfaces/

But you can get away with making a list of all the face normals shared by a vertex and then get the average. Don't forget to renormalize.

I'm using a shader which I believe does per-pixel shading(Gourand) on its own.

The way I'm rendering vertex and normal in an .obj model is like this, which associates 1 normal with all 3 vertices, does that look like the problem?

for all faces in model{
    
glBegin(GL_TRIANGLES);
glColor4f(colors[faces->colorNum-1]->r,colors[faces->colorNum-1]->g,colors[faces->colorNum-1]->b,1.0);
glNormal3f(normals[faces->facenum-1]->x,normals[faces->facenum-1]->x,normals[faces->facenum-1]->z);
glVertex3f(vertex[faces->faces[0]-1]->x,vertex[faces->faces[0]-1]->y,vertex[faces->faces[0]-1]->z);
glVertex3f(vertex[faces->faces[1]-1]->x,vertex[faces->faces[1]-1]->y,vertex[faces->faces[1]-1]->z);
glVertex3f(vertex[faces->faces[2]-1]->x,vertex[faces->faces[2]-1]->y,vertex[faces->faces[2]-1]->z);
glEnd();
}
It doesn't matter if the shader does per-pixel shading if the normals are per-face. You'll need to blend them yourself. If you have attenuation or something though that will show up in per-pixel.

GL_SMOOTH and GL_FLAT are old fixed function commands for set-ups that don't have user defined shader programs written using either GLSL, or CG, if you are using a programmable shader those commands will do nothing. They will be by-passed. There is a program for loading .obj models on the following site. http://www.dhpoware.com/demos/glObjViewer.html It handles normals correctly when exported from Blender and can handle generating them when the normals are not present in the .obj. Also the problem that you are having may be related to the default export settings in Blender. The default Blender .obj export settings do not export normals. You do look ambitious about solving this programmatically so I wish you luck with that and bid you adieu.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

GL_SMOOTH and GL_FLAT are old fixed function commands for set-ups that don't have user defined shader programs written using either GLSL, or CG, if you are using a programmable shader those commands will do nothing. They will be by-passed.

There is a program for loading .obj models on the following site.
http://www.dhpoware.com/demos/glObjViewer.html

It handles normals correctly when exported from Blender and can handle generating them when the normals are not present in the .obj.

Also the problem that you are having may be related to the default export settings in Blender. The default Blender .obj export settings do not export normals.

You do look ambitious about solving this programmatically so I wish you luck with that and bid you adieu.

Oh ok, FLAT and SMOOTH make sense now. I have figured out that the .obj loader that I had written is incapable of rendering smooth models, only flat. Thanks!

This topic is closed to new replies.

Advertisement