Normal question (well, kinda weird actualy)

I wrote a program that can load and render .obj files with textures, but the .obj file does not have normals data, is there a way to calculate normals?

how can i draw an .obj with lighting?

thanks!

The normals is an equation to calculate it for each verticy, i don’t know it. I use DirectX calls to calculate all my normals for me.

im trying to stick with opengl, is there a way to calculate normals in opengl?

See appendix E from the red book, also recommended to zix99 so he does not have to depend on some helper functions.
If you really do not want to make the effort is low level libraries like OpenGL and Direct3D not what you want.

You probably need to calculate the cross product. Here is two libraries with that function and others. http://plib.sourceforge.net/sg/ http://www-2.cs.cmu.edu/~ajw/doc/svl.html

Once you have the normals can you use the standard OpenGL lights.

the normal of a vertex is the normalized sum of the normal of the triangles that share the vertex.

for each triangle
normal[triangle.vertex[0]] += triangle.normal
normal[triangle.vertex[1]] += triangle.normal
normal[triangle.vertex[2]] += triangle.normal
end for

it’s strange that there is not normals in obj file! i using maya’s exported obj models and they contain normals data!
like this: vn 1 0 0

thanks some guy, i’ll try not to use DX this time. Its a pain to use DX for just ONE call in my whole program.

simple math question, it says that the normal = [v1 - v2] × [v2 - v3].

How do you subtract 2 vectors? is it: (x1+y1+z1 - x2+y2+z2) or: (x1-x2 y1-y2 z1-z2)
i think its (x1-x2 y1-y2 z1-z2) because its the only one resulting in a vector, but imnot sure… and also, how do you muliply?

The cross product of 2 given vectors v1 and v2 is as follows:
v1 x v2 = ( v1.xv1.z - v1.zv2.y , v1.zv2.x - v1.xv2.z, v1.xv2.y - v1.yv2.x )

I think it would be better for you if you get a math book and take a look at vector math, so you can understand where this formula comes from.

Here is a description on how to calculate the cross product, its really very simple. http://www.lighthouse3d.com/opengl/maths/index.php3?crossproduct
Note that the x means cross pruduct and not multiplication.

If you need some reference material about matrices could perhaps the matrix FAQ be used. http://www.j3d.org/matrix_faq/

Uh, I forgot something :P.
Yeah you’re right, the second formula is the correct for vector substraction.

don’t get to mathy on me, im only in ninth grade!

but now i jsut realsized somthing, im working with triangles… is it done the same way?

while exporting OBJ files from your favorite 3D-modeller app it is possible, in the most apps, to “configure” if you want to have normals.

and yes, you have to calculate the cross-product of the vectors, describing a triangle. this is called “the normal vector”; it’s that vector which “stands” perpendicular on a triangle.
by applying this normal to a polygon, you get lightning - but keep in mind that, if you use this way, you have so called “face normals” - this means, all vertices of a triangle have the same normal; if you want to do vertex normals, this means different normals per each vertex, you have to calculate the “average” normal for each vertex by calculating the average values of all faces, which are connected to this vertex.

[This message has been edited by DJSnow (edited 07-23-2003).]

no, i mean im drawing in triangles, not quads… can you explain the formula for find a normal of a triangle??

thanks!

It’s not subtracting vectors, it’s subtracting points to produce edge vectors.

The cross product of two triangle edge vectors will produce the normal of the triangle.

so i would use this formula for any 2 points on the triangle?? :
v1 x v2 = ( v1.xv1.z - v1.zv2.y , v1.zv2.x - v1.xv2.z, v1.xv2.y - v1.yv2.x )

No.

You need two vectors, generated from 3 points. You then do a cross product on the vectors.

Triangle consisting of 3 points p1 p2 p3

v1 = p2 - p1
v2 = p3 - p2

This subtraction is simply (x2-x1, y2-y1, z2-z1)

normal = v1 x v2

Now this x means a vector cross product.
http://mathworld.wolfram.com/CrossProduct.html

The code would look something like this:

normalx = v2.yv1.z - v2.zv1.y;
normaly = v2.zv1.x - v2.xv1.z;
normalz = v2.xv1.y - v2.yv1.x;

Finally you need to normalize, using pythagoras theorem to compute the length of the vector then divide x, y and z by the length of the vector.

length = sqrtf( normalx * normaly + normaly * normaly + normaly * normaly )

normalx /= length
normaly /= length
normalz /= length

This is winding dependent (the normal direction (out or in) will depend on the vertex winding order, clockwise vs anti-clockwise. If this is wrong for your data then reverse the vector directions with the subtractions.

P.S. there is a typo in your cross product equation.

[This message has been edited by dorbie (edited 07-24-2003).]

Originally posted by dorbie:

length = sqrtf( normalx * normaly + normaly * normaly + normaly * normaly )

Excellent explanation but the above should be
length = sqrtf( normalx * normalx + normaly * normaly + normalz * normalz )

Originally posted by dorbie:
[b]No.

You need two vectors, generated from 3 points. You then do a cross product on the vectors.

Triangle consisting of 3 points p1 p2 p3

v1 = p2 - p1
v2 = p3 - p2

This subtraction is simply (x2-x1, y2-y1, z2-z1)

normal = v1 x v2

Now this x means a vector cross product.
http://mathworld.wolfram.com/CrossProduct.html

The code would look something like this:

normalx = v2.yv1.z - v2.zv1.y;
normaly = v2.zv1.x - v2.xv1.z;
normalz = v2.xv1.y - v2.yv1.x;

Finally you need to normalize, using pythagoras theorem to compute the length of the vector then divide x, y and z by the length of the vector.

length = sqrtf( normalx * normaly + normaly * normaly + normaly * normaly )

normalx /= length
normaly /= length
normalz /= length

This is winding dependent (the normal direction (out or in) will depend on the vertex winding order, clockwise vs anti-clockwise. If this is wrong for your data then reverse the vector directions with the subtractions.

P.S. there is a typo in your cross product equation.

[This message has been edited by dorbie (edited 07-24-2003).][/b]

The website that you posted has a different equation then you! on theirs, there is no x’s… you don’t have to give me the code, jsut the equation!

thanks!

*bump