wvufan87

10-15-2008, 10:39 PM

I'm still very new to opengl and had a question on using a height map. This is for a school asignment so I'd like to have some assistance learning rather than just getting an answer. This is an excerpt from the assignment.

The height values are

given by height(i, j) = 15.0 ∗ sin(10πi/256.0) ∗ sin(10πj/256.0). The 3D coordinates of points on

the terrain are given by (i, j, height(i, j)).

Render the terrain surface using triangle strips. Set the color of each vertex to (0.1∗height[i][j], 1.0, 0.1∗

height[i][j]). Compute the unit normal vector for each vertex, p, by using the cross product. If

p = (i, j, height(i, j)) then let a = (i + 1, j, height(i + 1, j)) and b = (i, j + 1, height(i, j + 1)).

Construct the vector v = a − p and w = b − p and n = v × w. The unit normal is given by N = n/||n||

This is what I've come up with so far:

glBegin(GL_TRIANGLE_STRIP);

int i,j;

for(i=0; i < 256; i++)

{

for(j=0; j < 256; j++)

{

glNormal3f(0, 0, 1);

glColor3f(0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0), 1.0, 0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0));

glVertex3f(i+1, j, 15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0));

glColor3f(0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0), 1.0, 0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0));

glVertex3f(i, j+1, 15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0));

glColor3f(0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0), 1.0, 0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0));

glVertex3f(i, j, 15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0));

glColor3f(0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0), 1.0, 0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0));

glVertex3f(i+1, j+1, 15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0));

}

}

glEnd();

I get a kind of nice image on the screen, but I don't think I'm doing it right. And I'm not really sure on normalization. Any pointers in the right direction would be greatly appreciated.

The height values are

given by height(i, j) = 15.0 ∗ sin(10πi/256.0) ∗ sin(10πj/256.0). The 3D coordinates of points on

the terrain are given by (i, j, height(i, j)).

Render the terrain surface using triangle strips. Set the color of each vertex to (0.1∗height[i][j], 1.0, 0.1∗

height[i][j]). Compute the unit normal vector for each vertex, p, by using the cross product. If

p = (i, j, height(i, j)) then let a = (i + 1, j, height(i + 1, j)) and b = (i, j + 1, height(i, j + 1)).

Construct the vector v = a − p and w = b − p and n = v × w. The unit normal is given by N = n/||n||

This is what I've come up with so far:

glBegin(GL_TRIANGLE_STRIP);

int i,j;

for(i=0; i < 256; i++)

{

for(j=0; j < 256; j++)

{

glNormal3f(0, 0, 1);

glColor3f(0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0), 1.0, 0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0));

glVertex3f(i+1, j, 15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*j)/256.0));

glColor3f(0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0), 1.0, 0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0));

glVertex3f(i, j+1, 15 * sin((10*PI*i)/256.0) * sin((10*PI*(j+1))/256.0));

glColor3f(0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0), 1.0, 0.1*15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0));

glVertex3f(i, j, 15 * sin((10*PI*i)/256.0) * sin((10*PI*j)/256.0));

glColor3f(0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0), 1.0, 0.1*15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0));

glVertex3f(i+1, j+1, 15 * sin((10*PI*(i+1))/256.0) * sin((10*PI*(j+1))/256.0));

}

}

glEnd();

I get a kind of nice image on the screen, but I don't think I'm doing it right. And I'm not really sure on normalization. Any pointers in the right direction would be greatly appreciated.