MrShoe

06-15-2001, 04:20 AM

Today I came up with an algorith for generating normals. It works fine for like a single surface and all. I then tried to apply it to my terrain generator to get lighting. But the results are not good and the terrain does not look like its lit at all. Here is the important code:

struct normals{

float x, y, z;

};

typedef struct normals normals;

normals normal[TERRAINWIDTH*TERRAINLENGTH];

...

void calculate_normal(int normal_number, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3){

float vec1_x, vec1_y, vec1_z, vec2_x, vec2_y, vec2_z;

float bigx, bigy, bigz;

vec1_x = x2 - x1;

vec1_y = y2 - y1;

vec1_z = z2 - z1;

vec2_x = x3 - x1;

vec2_y = y3 - y1;

vec2_z = z3 - z1;

bigx = (vec1_y*vec2_z) - (vec1_z*vec2_y);

bigy = (vec1_z*vec2_x) - (vec1_x*vec2_z);

bigz = (vec1_x*vec2_y) - (vec1_y*vec2_x);

normal[normal_number].x = bigx/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].y = bigy/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].z = bigz/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].x = abs(normal[normal_number].x);

normal[normal_number].y = abs(normal[normal_number].y);

normal[normal_number].z = abs(normal[normal_number].z);

}

void renderScene(void){

float height;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glBindTexture(GL_TEXTURE_2D, texture[0]);

glRotatef(angle, 0.0, 1.0, 0.0);

glRotatef(25, 1.0, 0.0, 0.0);

glTranslatef(-4.0, -2.0, zoom);

glColor3f(1.0, 1.0, 1.0);

for(mapx = 0; mapx < TERRAIN_LENGTH-1; mapx++){

for(mapz = 0; mapz < TERRAIN_WIDTH-1; mapz++){

glBegin(GL_TRIANGLE_STRIP);

calculate_normal((mapx*99)+mapz, mapx/DETAIL, height, mapz/DETAIL, mapx/DETAIL, height, (mapz+1)/DETAIL, (mapx+1)/DETAIL, height, mapz/DETAIL);

glNormal3f(normal[(mapx*99)+mapz].x, normal[(mapx*99)+mapz].y, normal[(mapx*99)+mapz].z);

height = map[mapx][mapz];

glTexCoord2f(0.0, 0.0);

glVertex3f(mapx/DETAIL, height, mapz/DETAIL);

height = map[mapx][mapz+1];

glTexCoord2f(0.0, 1.0);

glVertex3f(mapx/DETAIL, height, (mapz + 1)/DETAIL);

height = map[mapx+1][mapz];

glTexCoord2f(1.0, 0.0);

glVertex3f((mapx+1)/DETAIL, height, mapz/DETAIL);

height = map[mapx+1][mapz+1];

glTexCoord2f(1.0, 1.0);

glVertex3f((mapx+1)/DETAIL, height, (mapz+1)/DETAIL);

glEnd();

}

}

glPopMatrix();

glutSwapBuffers();

}

I just made the normal algorithm today so it might not be the "proper' or standard way to calculate normals but it works for a single polygon... Please help, thanks.

struct normals{

float x, y, z;

};

typedef struct normals normals;

normals normal[TERRAINWIDTH*TERRAINLENGTH];

...

void calculate_normal(int normal_number, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3){

float vec1_x, vec1_y, vec1_z, vec2_x, vec2_y, vec2_z;

float bigx, bigy, bigz;

vec1_x = x2 - x1;

vec1_y = y2 - y1;

vec1_z = z2 - z1;

vec2_x = x3 - x1;

vec2_y = y3 - y1;

vec2_z = z3 - z1;

bigx = (vec1_y*vec2_z) - (vec1_z*vec2_y);

bigy = (vec1_z*vec2_x) - (vec1_x*vec2_z);

bigz = (vec1_x*vec2_y) - (vec1_y*vec2_x);

normal[normal_number].x = bigx/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].y = bigy/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].z = bigz/sqrt(bigx*bigx + bigy*bigy + bigz*bigz);

normal[normal_number].x = abs(normal[normal_number].x);

normal[normal_number].y = abs(normal[normal_number].y);

normal[normal_number].z = abs(normal[normal_number].z);

}

void renderScene(void){

float height;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();

glBindTexture(GL_TEXTURE_2D, texture[0]);

glRotatef(angle, 0.0, 1.0, 0.0);

glRotatef(25, 1.0, 0.0, 0.0);

glTranslatef(-4.0, -2.0, zoom);

glColor3f(1.0, 1.0, 1.0);

for(mapx = 0; mapx < TERRAIN_LENGTH-1; mapx++){

for(mapz = 0; mapz < TERRAIN_WIDTH-1; mapz++){

glBegin(GL_TRIANGLE_STRIP);

calculate_normal((mapx*99)+mapz, mapx/DETAIL, height, mapz/DETAIL, mapx/DETAIL, height, (mapz+1)/DETAIL, (mapx+1)/DETAIL, height, mapz/DETAIL);

glNormal3f(normal[(mapx*99)+mapz].x, normal[(mapx*99)+mapz].y, normal[(mapx*99)+mapz].z);

height = map[mapx][mapz];

glTexCoord2f(0.0, 0.0);

glVertex3f(mapx/DETAIL, height, mapz/DETAIL);

height = map[mapx][mapz+1];

glTexCoord2f(0.0, 1.0);

glVertex3f(mapx/DETAIL, height, (mapz + 1)/DETAIL);

height = map[mapx+1][mapz];

glTexCoord2f(1.0, 0.0);

glVertex3f((mapx+1)/DETAIL, height, mapz/DETAIL);

height = map[mapx+1][mapz+1];

glTexCoord2f(1.0, 1.0);

glVertex3f((mapx+1)/DETAIL, height, (mapz+1)/DETAIL);

glEnd();

}

}

glPopMatrix();

glutSwapBuffers();

}

I just made the normal algorithm today so it might not be the "proper' or standard way to calculate normals but it works for a single polygon... Please help, thanks.