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…
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?
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.
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;