• ### Popular Now

• 9
• 16
• 15
• 12
• 9

#### Archived

This topic is now archived and is closed to further replies.

# Lighting Terrain

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

## Recommended Posts

Ok, I have a randomly generated terrain, and I want there to be like sunlight so I can see the curves in the terrain... What type of light would I use for that, and how would I do it, and everything... Oh, and I''m using GL_TRIANGLE_STRIPS to draw the terrain.... does glEnable(GL_NORMALIZE) calculate the normals for me? or is that a differnet function?

##### Share on other sites
to calculate the normal of a surface take the inner product of two lines on the surface (two of the edges of a triangle, or a quad for example). for each vertex take the average of the normals of the triangles/quads/(whatever poly''s) around it. glNormalize only makes all the normals to a unitvector, this means they all get a length of 1.0 .
I''d advice to calculate the normals before rendering the landscape yourself, then be sure there all unit vectors and disable glNormalize, because this slows down the rendering.
Use a ambient light and a diffuse (directional) light.

Marty

##### Share on other sites

    glColor3f(0,1,0);  for (int ty = 0; ty < 19; ty ++) {    glBegin(GL_TRIANGLE_STRIP);      glVertex3f(-10.0f,array[0][ty]-10.0f,ty-10.0f);      glVertex3f(-10.0f,array[0][ty+1]-10.0f,ty-9.0f);      for (int tx = 1; tx < 19; tx++) {        glVertex3f(tx-10.0f,array[tx+1][ty]-10.0f, ty -10.0f);          glVertex3f(tx-10.0f,array[tx+1][ty+1]-10.0f,   ty-9.0f);        }    glEnd();  }

that''s where i render it... Am i going to need to prepare some more arrays ahead of time that contains the normals? that would probably be faster... calculating once rather then once per loop... difficult stuff....

woops, missed part of your post. what are unit vectors and normals exactly? and if i calculate them and make them unit vectors how do i tell the rendering stuff the normal for the vertecies?

is it glNormalfv()?

##### Share on other sites
A unit vector is a vector of length one, that means for a 3d vector : sqrt(x*x+y*y+z*z) = 1.
To make a vector a unit vector do this:

float length = sqrt(x*x+y*y+z*z)
x = x/length
y = y/length
z = z/length

the innerproduct of two vectors in 3d:

x = y1*z2+z1*y2
y = z1*x2+x1*z2
z = x1*y2+y1*x2

this is your normal (of one a surface) (x1,y1,z1) is a line on the surface and (x2,y2,z2) another line on the same surface...

appoint a normal to each vertex (the average normal of the surrounding poly''s) and use glNormal to assign this normal to the vertex you''re sending to OGL next...

Marty

##### Share on other sites
Here''s an amazing algorithm for making extremely good lighting! I''m using it on my terrain engine and it looks GREAT! It''s a little hard to implement, but it''s woth it!

http://www.gamedev.net/reference/articles/article1817.asp