PDA

View Full Version : 3DS Model & normals

SimoPI
02-19-2010, 07:32 AM
Hi, i'm new. First, sorry for my english.

I have a problem coding the shape normals. I'm not sure about the code i'm writing, because i'm a beginner in opengl and glsl.

Assume a triangle: (i1, i2, i3)
Assume a vertex: (v1, v2, v3), obtained from triangle[i1]

In opengl, i compute the normals in this way:

vector3 a, b;
a = v3-v1;
b = v2-v1;

vector3 normal = a^b; //cross product

Is that right?

I must send these values to a GLSL bump mapping, and it wants a normal for every vector. But i have ONE normal, computed from 3 vectors.
I need a normalArray implementation where i can send a normal for every vertex. I think i must reply the computed normal in v1, v2 and v3, but i don't know how...

Someone can help me?

vivek vyas
02-19-2010, 08:49 AM
do you use lib3ds
if yes then i have solution for calculating normal as it have function for it

strattonbrazil
02-19-2010, 09:59 AM
By default OpenGL defines triangle orientation in counter-clockwise order so unless you change it, the front facing side is v1,v2,v3 going counterclockwise around the triangle. If you are looking at those vertices and they're clockwise, you're seeing the back of it. As such, the normal is going to be b^a using the two vectors you made and using the right-handed orientation. If you want a flat triangle, use this normal for all these vertices.

And when you post, you need to specify what the problem is. Are you seeing black?

SimoPI
02-19-2010, 11:25 AM
@strattonbrazil: you're right, sorry. I attach a screenshot to show what i see:

http://img202.imageshack.us/img202/1521/screencb.png

I think i understand what you say. While i wait your answer i try something. Thanks again.

@vivek: i found the code surfing on internet, i don't use a library. By the way the same problem appears if i use a Wavefront loader. Sorry if i didn't specify that.

SimoPI
02-20-2010, 06:44 AM
I have a stupid question. To render a 3d object i use indices to retrieve triangles. Then i call glDrawElements.
When i build the normal array:

If i have a triangle (i1, i2, i3). To compute the normal i must retrieve vertex[i1], vertex[i2], vertex[i3]. Then to store the result i must set normal for vertex "i":
normalArray[i] = normal.x;
normalArray[i+1] = normal.y;
normalArray[i+2] = normal.z;

This means that normalArray size is numVertices*3... is that right? Because doing that i have the problem in the previous post: http://img202.imageshack.us/img202/1521/screencb.png

I write to learn, sorry if it is stupid :P