How do I get indexed vertex positions AND indexed vertex normals from wavefront .obj file into an OpenGL vertex shader?

StackOverflow https://stackoverflow.com/questions/14147055

  •  12-01-2022
  •  | 
  •  

Question

I was trying to use two Vertex Buffer Objects (VBO) and two Index Buffer Objects (IBO) in a Vertex Array Object (VAO) for rendering data from a wavefront .Obj file containing a 3D model. The .obj file was using the vertex/normal face definition:

f v1//vn1 v2//vn2 v3//vn3

I realized that I couldn't use two IBOs for indexing with OpenGL's VAO since only one buffer object (at a time) can be bound to the GL_ELEMENT_ARRAY_BUFFER binding target. This means that I can't just define vertex position indices AND vertex normal indices which is a problem when I need both as input to a GLSL vertex shader.

I thought about using a "Vertex" struct encapsulating vertex position and vertex normals, but I don't know how to feed OpenGL the data as anything but one long array and when I have all my vertex position data in different objects. I guess I could use some loops to copy the data to a new array containing all the position data.

When loading the .obj file I guess I could process the data so that the normals are matched with the corresponding vertex position values.

How should I go about solving this problem?

I'm using C++ and Visual Studio 2012 with OpenGL 3.3.

Was it helpful?

Solution

One thing you must understand is, that a "vertex" is not synnonymous with "position". A vertex is the whole set of attributes; position, normal, texture coordinate, etc. OpenGL processes geometry one primitive at a time, where a primitive consists of 1 (point), 2 (line) or 3 vertices (triangle). You can not process position and normal separately, they always must go together.

Before you can process it in OpenGL you must expand the data in the .obj file. For each unique tuple of attributes introduce a new vertex with a new index and replace indexed attributes from the .obj with the indexed vertex OpenGL requires.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top