Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

OpenGL Untitled

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

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


Link to post
Share on other sites
Advertisement
Read this page, hopefully it will help with the colors messing up when you enable lighting.
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 this post


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

Share this post


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

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!

Share this post


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


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


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


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


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


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


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!