Ok, i try to explain my work:
i use vertex arrays to render the 3ds object. If i don't use shader, the model seems ok. The problem is when i use the shader.
OpenGL source:
I compute the normals in this way:
Vec3f triangle = obj->getShape().triangle.at(k);//Triangle vertices Vec3f v1 = obj->getShape().vertex.at(triangle.i1); Vec3f v2 = obj->getShape().vertex.at(triangle.i2); Vec3f v3 = obj->getShape().vertex.at(triangle.i3); Vec3f a, b; a = v3-v2; b = v1-v2; Vec3f normal = a^b; //cross product//Texture coordinates Vec2f st1 = obj->getShape().texture.at(triangle.i1); Vec2f st2 = obj->getShape().texture.at(triangle.i2); Vec2f st3 = obj->getShape().texture.at(triangle.i3); Vec2f c, d; c = st3-st2; d = st1-st2; float coef = 1/ (c.x * d.y - d.x * c.y); Vec3f tangent; tangent.x = coef * ((v1.x * d.y) + (v2.x * -c.y)); tangent.y = coef * ((v1.y * d.y) + (v2.y * -c.y)); tangent.z = coef * ((v1.z * d.y) + (v2.z * -c.y)); tangent.normalize(); normal.normalize(); Vec3f binormal = tangent^normal; //cross product
Then i put everything into an array. For example, the normal array will be:
normalArray[i++] = normal.x; normalArray[i++] = normal.y; normalArray[i++] = normal.z;
Then i call glNormalArray(... normalArray);
For tangents and binormals i use vertex attributes, declared into shader:
and in opengl i call glVertexAttribPointer(...);
To render the vertices:
glDrawElements(GL_TRIANGLES, shape.triangle.size()*3, GL_UNSIGNED_SHORT, indexPointer);
Thanks for the patience!
[Edited by - Simopi on February 18, 2010 1:26:22 PM]