Archived

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

TeTE

Problem with ... normals?

Recommended Posts

Look the picture at http://www4.uji.es/~al024870/misc/cube.jpg and you will see what''s my problem. It''s a cube with light and a green texture. I use vertex arrays with the vertices, the texture coordinates and the normals. All is read from an OBJ file. And the models are made with Nendo. What''s wrong? May be the normals? I think they are per vertex normals. I have tried more complex objects (airplanes, buildings). All look similar and no one is smooth even with GL_SMOOTH enabled.

Share this post


Link to post
Share on other sites
In order for GL_SMOOTH to take effect, you must calculate, and send the normals to OpenGL. Here is the function that I use to calculate a normal:


void ComputeNormal(VECTOR3D* v1, VECTOR3D* v2, VECTOR3D* v3)
{
VECTOR3D a(0.0f, 0.0f, 0.0f);
VECTOR3D b(0.0f, 0.0f, 0.0f);
VECTOR3D result(0.0f, 0.0f, 0.0f);
float length;

a.vertex[0]= v1->vertex[0] - v2->vertex[0];
a.vertex[1]= v1->vertex[1] - v2->vertex[1];
a.vertex[2]= v1->vertex[2] - v2->vertex[2];

b.vertex[0]= v1->vertex[0] - v3->vertex[0];
b.vertex[1]= v1->vertex[1] - v3->vertex[1];
b.vertex[2]= v1->vertex[2] - v3->vertex[2];

result.vertex[0]= (a.vertex[1]*b.vertex[2]) - (b.vertex[1]*a.vertex[2]);
result.vertex[1]= (b.vertex[0]*a.vertex[2]) - (a.vertex[0]*b.vertex[2]);
result.vertex[2]= (a.vertex[0]*b.vertex[1]) - (b.vertex[0]*a.vertex[1]);

//Calculate the length of the normal
length= (float)sqrt(SQUARE(result.vertex[0]) +
SQUARE(result.vertex[1]) +
SQUARE(result.vertex[2]));

//Normalize, and send to OpenGL
glNormal3f(result.vertex[0]/length, result.vertex[1]/length, result.vertex[2]/length);


Note that this function is *REALLY* a speed hazard. Especially the call to sqrt(...) . Owwww

------------------------------
Trent (ShiningKnight)
E-mail me
OpenGL Game Programming Tutorials

Edited by - ShiningKnight on June 21, 2001 4:26:55 PM

Share this post


Link to post
Share on other sites
I know that I have to send the normals. I read them from the file.

But I don''t know why, I get this strange image. May be the program has a "bug" and doesn''t saves them correctly.

Share this post


Link to post
Share on other sites
I know that I have to send the normals. I read them from the file.

But I don''t know why, I get this strange image. May be the program has a "bug" and doesn''t saves them correctly.

Share this post


Link to post
Share on other sites
Maybe you could draw the normals.

VECTOR3 a,b,c; // the points for a triangle
VECTOR3 m = (a + b + c) / 3.0; // center of the triangle
VECTOR3 e = m + normal * 10.0; // end of normal line

glBegin(GL_LINES);
glVertex3f(m.x, m.y, m.z);
glVertex3f(e.x, e.y, e.z);
glEnd();

But from the look of the cube i would guess the x and z (or other combination) coordinates are swaped...

Share this post


Link to post
Share on other sites
The problem was that Nendo saves the normals incorrectly, because when using a converter I get the expected results. Well, not exactly what I expected ...

Now I have another problem. All is tinted in blue!! I don''t use alpha and nor the light nor the materials have a great blue component. Any idea of what is the error?

Thanks for the tip of drawing the normals!!

Share this post


Link to post
Share on other sites