#### Archived

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

# I know it's getting old but... problems with vertex normals

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

## Recommended Posts

Hey guys, my problem is this: although i know exactly how 2 calculate the face normals AND the vertex normals on my terrain generator, i''m having trouble implementing it with a 2d array of vertices(that i like 2 use heh). i''m sure it''s possible to calculate the normals properly, coz i managed to count the polygons shared by each vertex. i think that my main problem is that i somehow get a negative value on my vNormal.y, althought trying to use an absolute value DIDn''T help so mayb it''s something else. any ideas?

##### Share on other sites
put in code to visualize the normals. it''s the easiest way to work out whats screwed up.

##### Share on other sites
ok these r some code snippets that relate 2 normal generation:

struct GLvector
{
GLfloat x, y, z;
};

struct GLvertex
{
GLvector pos, normal;
GLfloat diffuse;
GLuint nTimesShared;
};

GLvertex TerrainVertices[NumHills+1][NumHills+1];

GLvoid InitGLGeometry(GLvoid)
{
int i, j;

GLvector FaceNormals[NumHills][NumHills*2];

// initializing terrain vertices and normals

for (i=0; i<=NumHills; i++)
for (j=0; j<=NumHills; j++)
{
TerrainVertices[j].pos.x = GLfloat(i-(GLfloat(NumHills)/2.0f));
TerrainVertices[i][j].pos.y = GLfloat(0);
TerrainVertices[i][j].pos.z = GLfloat(j-(GLfloat(NumHills)/2.0f));

TerrainVertices[i][j].nTimesShared = 0;

TerrainVertices[i][j].normal.x = 0;
TerrainVertices[i][j].normal.y = 0;
TerrainVertices[i][j].normal.z = 0;
}

GenerateHeightMap(); // works fine, will not b included

for (i=0; i for (j=0; j {
FaceNormals[i][j*2].x = float((TerrainVertices[i+1][j].pos.x-TerrainVertices[i][j].pos.x)*
(TerrainVertices[i][j].pos.x-TerrainVertices[i][j+1].pos.x));

FaceNormals[i][j*2].y = float((TerrainVertices[i+1][j].pos.y-TerrainVertices[i][j].pos.y)*
(TerrainVertices[i][j].pos.y-TerrainVertices[i][j+1].pos.y));

FaceNormals[i][j*2].z = float((TerrainVertices[i+1][j].pos.z-TerrainVertices[i][j].pos.z)*
(TerrainVertices[i][j].pos.z-TerrainVertices[i][j+1].pos.z));

length = (float)sqrt(((FaceNormals[i][j*2].x*FaceNormals[i][j*2].x)+
(FaceNormals[i][j*2].y*FaceNormals[i][j*2].y)+
(FaceNormals[i][j*2].z*FaceNormals[i][j*2].z)));

if (FaceNormals[i][j*2].y<0)
{
FaceNormals[i][j*2].x /= -length;
FaceNormals[i][j*2].y /= -length;
FaceNormals[i][j*2].z /= -length;
}
else
{
FaceNormals[i][j*2].x /= length;
FaceNormals[i][j*2].y /= length;
FaceNormals[i][j*2].z /= length;
}

FaceNormals[i][(j*2)+1].x = float((TerrainVertices[i+1][j+1].pos.x-TerrainVertices[i+1][j].pos.x)*
(TerrainVertices[i+1][j].pos.x-TerrainVertices[i+1][j+1].pos.x));

FaceNormals[i][(j*2)+1].y = float((TerrainVertices[i+1][j+1].pos.y-TerrainVertices[i+1][j].pos.y)*
(TerrainVertices[i+1][j].pos.y-TerrainVertices[i+1][j+1].pos.y));

FaceNormals[i][(j*2)+1].z = float((TerrainVertices[i+1][j+1].pos.z-TerrainVertices[i+1][j].pos.z)*
(TerrainVertices[i+1][j].pos.z-TerrainVertices[i+1][j+1].pos.z));

length = (float)sqrt(((FaceNormals[i][(j*2)+1].x*FaceNormals[i][(j*2)+1].x)+
(FaceNormals[i][(j*2)+1].y*FaceNormals[i][(j*2)+1].y)+
(FaceNormals[i][(j*2)+1].z*FaceNormals[i][(j*2)+1].z)));

if (FaceNormals[i][(j*2)+1].y<0)
{
FaceNormals[i][(j*2)+1].x /= -length;
FaceNormals[i][(j*2)+1].y /= -length;
FaceNormals[i][(j*2)+1].z /= -length;
}
else
{
FaceNormals[i][(j*2)+1].x /= length;
FaceNormals[i][(j*2)+1].y /= length;
FaceNormals[i][(j*2)+1].z /= length;
}

}

// finding polygon sharing for each vertex

for (i=0; i for (j=0; j {
TerrainVertices[i][j].nTimesShared++;
TerrainVertices[i+1][j].nTimesShared++;
TerrainVertices[i][j+1].nTimesShared++;

TerrainVertices[i+1][j].nTimesShared++;
TerrainVertices[i][j+1].nTimesShared++;
TerrainVertices[i+1][j+1].nTimesShared++;

TerrainVertices[i][j].normal.x += FaceNormals[i][j*2].x;
TerrainVertices[i][j].normal.y += FaceNormals[i][j*2].y;
TerrainVertices[i][j].normal.z += FaceNormals[i][j*2].z;

TerrainVertices[i+1][j].normal.x += FaceNormals[i][j*2].x;
TerrainVertices[i+1][j].normal.y += FaceNormals[i][j*2].y;
TerrainVertices[i+1][j].normal.z += FaceNormals[i][j*2].z;

TerrainVertices[i][j+1].normal.x += FaceNormals[i][j*2].x;
TerrainVertices[i][j+1].normal.y += FaceNormals[i][j*2].y;
TerrainVertices[i][j+1].normal.z += FaceNormals[i][j*2].z;

TerrainVertices[i+1][j].normal.x += FaceNormals[i][(j*2)+1].x;
TerrainVertices[i+1][j].normal.y += FaceNormals[i][(j*2)+1].y;
TerrainVertices[i+1][j].normal.z += FaceNormals[i][(j*2)+1].z;

TerrainVertices[i][j+1].normal.x += FaceNormals[i][(j*2)+1].x;
TerrainVertices[i][j+1].normal.y += FaceNormals[i][(j*2)+1].y;
TerrainVertices[i][j+1].normal.z += FaceNormals[i][(j*2)+1].z;

TerrainVertices[i+1][j+1].normal.x += FaceNormals[i][(j*2)+1].x;
TerrainVertices[i+1][j+1].normal.y += FaceNormals[i][(j*2)+1].y;
TerrainVertices[i+1][j+1].normal.z += FaceNormals[i][(j*2)+1].z;
}

for (i=0; i<=NumHills; i++)
for (j=0; j<=NumHills; j++)
{
TerrainVertices[i][j].normal.x /= (float)TerrainVertices[i][j].nTimesShared;
TerrainVertices[i][j].normal.y /= (float)TerrainVertices[i][j].nTimesShared;
TerrainVertices[i][j].normal.z /= (float)TerrainVertices[i][j].nTimesShared;
}
}

##### Share on other sites
Umm nm guys... i rewrote my entire normals code and create some simple CrossProduct() and CalcVector() funcs and whoa it worked. yay for me. thanks anyway!

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632935
• Total Posts
3009304
• ### Who's Online (See full list)

There are no registered users currently online

×