Understanding normals indices with Wavefront Obj

徘徊边缘 提交于 2019-12-04 13:03:51

Your problem is in the data structure. At least while loading an OBJ you need to load your faces into something like:

struct Vertex
{
    unsigned int vertex;
    unsigned int normal;
    unsigned int texturecoord;
};

struct Face
{
    // not dynamic, but you get the idea.
    Vertex vertexes[N];
};

Then you if you want an array of normals (and probably texturecoords) that matches the vertices, you need to create a new array that matches. The OBJ format is optimized for storage, not rendering.

The added benefit from this two step process, is that you can remove the restriction on homogene faces, by splinting each non triangle into a triangle.

I know that this is an old question, but I had the same problem earlier today while I was building an Obj parser. There were two problems in my code that resulted in a lighting that is very similar to what you've shown: 1- The first was when I incorrectly used the index number(1,2,3,...,n) I got from the Obj file to reference the normal in my array. 2- The other problem was taking the vt values instead of the vn values.

And this is how you pass normal values to OpenGL when using glBegin/glEnd:

glNormal3f(....);
glVertex3f(....);

You set a normal value before drawing each vertex. Whenever you set a normal, all following vertices will be affected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!