# OpenGL Untitled

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

## Recommended Posts

hello, i am very new to OpenGL and have some questions: i just activated lighting but now all my colors are gone and the polygons are from black to white depending on the lighting. i am rendering a heightfield and need to calculate normals for that. so far i get the slopes for each point, but how do i get the normals out of it? thanks!

##### Share on other sites
http://sjbaker.org/steve/omniv/opengl_lighting.html

Here is a good way to find the normals per vertex on a model.

for all vertices
vertex.norm = {0,0,0}

for all triangles in model{
temp_norm = trianlge.findNorm()
triangle.vertex1.norm += temp_norm
triangle.vertex2.norm += temp_norm
triangle.vertex3.norm += temp_norm
}

for all vertices
normalize(vertex.norm);

##### Share on other sites
use glEnable(GL_COLOR_MATERIAL); to be able to use glColor in conjunction with opengl-lighting :)

##### Share on other sites
thank you!

the glEnable(GL_COLOR_MATERIAL); worked!
but now on the other hand i dont get any shading on objects that have a texture on it? is there a similar thing i should enable?

im rendering a heightfield, so i basically have a twodimensional array with values from 0 to 1.
i dont really understand that algorithm.

thanks!

##### Share on other sites
Quote:
 Original post by ehmdjiithank you!the glEnable(GL_COLOR_MATERIAL); worked!but now on the other hand i dont get any shading on objects that have a texture on it? is there a similar thing i should enable?

Did this change when you turned on color material? Are you setting vertex color values for the textured faces (using color pointer or glcolor)?

The diffuse material color is combined with the light color for diffuse lighting, so if it were 0, or something else very dark, then you would see little or no diffuse shading from the lights. With GL_COLOR_MAERIAL on and the default settings, the diffuse material color comes from the vertex color. If you are using this functionality you need to supply colors for those verticies, if not you should turn off GL_COLOR_MATERIAL for those faces.

There are other possible problems that could cause lighting not to show up correctly. You might want to post your lighting/texture setup code.

Quote:
 about the normals:im rendering a heightfield, so i basically have a twodimensional array with values from 0 to 1.i dont really understand that algorithm.thanks!

Bascily if you want a smooth shaded object you will want to set the normal for each vertex to the average of the normals of the faces it touches.

The algorithim above will do that for a generic mesh by taking each face in the mesh and adding it's normal to a sum stored for each vertex used by that face. Then he normalizes those sums to get the final vertex normal for each vertex.

In the case of your terrain consider all of the triangles that touch a height point in the grid, these are your faces. For a non-edge vertex there should be 6:
+----+----+|\ 1 |\   || \  | \  ||  \ |  \ || 2 \| 3 \|+----+----+|\ 4 |\ 5 || \  | \  ||  \ |6 \ ||   \|   \|+----+----+

##### Share on other sites
thanks a lot for you reply!

Quote:
 Did this change when you turned on color material?

no, it didnt work before either.

Quote:
 Are you setting vertex color values for the textured faces (using color pointer or glcolor)?

no, i am not setting vertex colors.

Quote:
 bascily if you want a smooth shaded object you will want to set the normal for each vertex to the average of the normals of the faces it touches.

ok, but how do i get the normals of the surrounding faces?

thanks a lot!

##### Share on other sites
Quote:
 ok, but how do i get the normals of the surrounding faces?

The normal of your triangle, is the crossproduct of two sides.

normal = a.cross(b);

If you don't normalize the normal yourself, you should call glEnable(GL_NORMALIZE) - automatic normalization by opengl.

##### Share on other sites
ah thanks! that makes sense now!

but isnt this a lot of calculation? will performance decrease if my heightfield is 64x64?

##### Share on other sites
You only need to calculate the normals once (unless they are constantly changing, then you need to calculate after each change). Before you draw anything find the normal for each vertex and save it. Then just use the saved values every time you draw.

You shouldn't notice the time it takes to find the normals for a 64x64 feild, especially if you only do it once.

##### Share on other sites
Quote:
Original post by stein
Quote:
 ok, but how do i get the normals of the surrounding faces?

The normal of your triangle, is the crossproduct of two sides.

normal = a.cross(b);

If you don't normalize the normal yourself, you should call glEnable(GL_NORMALIZE) - automatic normalization by opengl.

Yep that's it, and just to make sure this is really clear, if you have 3 verts in the face v1, v2, v3, then you'll get the face normal with: cross_product(v2 - v1, v3- v1).

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 17
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632962
• Total Posts
3009506
• ### Who's Online (See full list)

There are no registered users currently online

×