Shared vertex normals
Hey guys.... I know this is kinda off topic, please dont flame me too much.
Anyway, I''m writing a per pixel lighting engine using vertex/pixel shaders in both OpenGL & D3d 8.1b, thing is, I''m using indices to reference my vertices, but the 3d models I''m using do not contain normal data.
My program calculates the normals based on two given vertcies in a triangle. But since alot of my vertices are shared, this normal generating algorith will not work particularly well will it.
Any ideas at all guys?
Thanks for any feedback,
Dan
Transformers Rulez!!
Transformers Rulez!!
I don''t understand your problem to well. First of all, unless you are using 2d, you cant get a normal from two points. Secondly, why wouldn''t normal generation work if the vertecies are shared? I used indecies in my model format and it works perfectly fine.
True, for generating the normals you need all 3 points.
But anyway, the way to do it, is to first generate the normals for each triangle, and then run through all the shared vertices, and average the normals from the triangles that share it, to get the vertex-normal.
Although, you probably don''t want to generate the normals every time you start the program. This works fine for a small mesh, but once you start going into bigger ones, it takes too much time. Store the normals in the file instead.
--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
But anyway, the way to do it, is to first generate the normals for each triangle, and then run through all the shared vertices, and average the normals from the triangles that share it, to get the vertex-normal.
Although, you probably don''t want to generate the normals every time you start the program. This works fine for a small mesh, but once you start going into bigger ones, it takes too much time. Store the normals in the file instead.
--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
xiros:
Sorry, kinda had a long day at work! Brain not working! I was refering to the two vectors from a triangle for a cross product! Doh!
The normal generation would work, but not correctly, as generating the normal for a vertex using the metod I''m using gives the vertex the face normal. If I''m sharing the vertex in several faces, can you see the problem?
Anyway, thanks for the help tok_junior. I''m implementing it now. Fingers crossed.
Transformers Rulez!!
Sorry, kinda had a long day at work! Brain not working! I was refering to the two vectors from a triangle for a cross product! Doh!
The normal generation would work, but not correctly, as generating the normal for a vertex using the metod I''m using gives the vertex the face normal. If I''m sharing the vertex in several faces, can you see the problem?
Anyway, thanks for the help tok_junior. I''m implementing it now. Fingers crossed.
Transformers Rulez!!
Transformers Rulez!!
THe problem is very clear.. and just as tok_junior said.. just check what triangles share the vertice you want the normal for.. and average those triangles normals into the vertex normal..
what i did is that first i looped through all my triangles doing this to get the triangle normals...
Then i loop through all the vretices calculating the vertex normals
what i did is that first i looped through all my triangles doing this to get the triangle normals...
v1[0] = SCM_Header.Tris[j].Vert[0][0] - SCM_Header.Tris[j].Vert[1][0];
v1[1] = SCM_Header.Tris[j].Vert[0][1] - SCM_Header.Tris[j].Vert[1][1];
v1[2] = SCM_Header.Tris[j].Vert[0][2] - SCM_Header.Tris[j].Vert[1][2];
v2[0] = SCM_Header.Tris[j].Vert[2][0] - SCM_Header.Tris[j].Vert[1][0];
v2[1] = SCM_Header.Tris[j].Vert[2][1] - SCM_Header.Tris[j].Vert[1][1];
v2[2] = SCM_Header.Tris[j].Vert[2][2] - SCM_Header.Tris[j].Vert[1][2];
TNorm[j].TNorm[0] = v1[1] * v2[2] - v1[2] * v2[1];
TNorm[j].TNorm[1] = v1[2] * v2[0] - v1[0] * v2[2];
TNorm[j].TNorm[2] = v1[0] * v2[1] - v1[1] * v2[0];
SCL_Vector3D_Normalize (TNorm[j].TNorm);
Then i loop through all the vretices calculating the vertex normals
vert = &SCM_Header.Tris[0].Vert[0][0];
for (i=0, a=vert; i {
count = 0;
normal[0] = 0;
normal[1] = 0;
normal[2] = 0;
for (j=0, b=vert; j {
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
{
k = j / 3;
normal[0] += TNorm[k].TNorm[0];
normal[1] += TNorm[k].TNorm[1];
normal[2] += TNorm[k].TNorm[2];
count++;
}
}
scale = 1.0f / count;
k = i / 3;
l = i % 3;
SCM_Header.Norms[k].Norm[l][0] = normal[0] * scale;
SCM_Header.Norms[k].Norm[l][1] = normal[1] * scale;
SCM_Header.Norms[k].Norm[l][2] = normal[2] * scale;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement