1

Let's say I am rendering a 3d GL_TRIANGLE. The object requires 3 vertices for it to be defined: A,B,C. I place such data into a buffer and bind it to the shader through glVertexAttribPointer.

Now I want to pass in the normal to the shader. For every triangle there should be 1 normal vector but if I try to pass it in through a glVertexAttribPointer, I would need to define the same normal 3 times for points A,B,C. Is it possible to pass in 1 vertex every 3 other vertices in a glVertexAttribPointer to avoid this?

Or is it a good idea to pass it in for all vertices?

sgtHale
  • 1,507
  • 1
  • 16
  • 28
  • 1
    That's not easily possible. If the arrays for the different attributes don't match up, you would need separate index arrays for each attribute to assign the attributes to vertices. This is not supported. My answer to a similar question here elaborates on this: http://stackoverflow.com/questions/25349620/use-one-gl-element-array-buffer-to-reference-each-attribute-from-0/25352461#25352461. – Reto Koradi Sep 04 '14 at 04:30

1 Answers1

3

Now I want to pass in the normal to the shader. For every triangle there should be 1 normal vector

No, there are 3 normals. One for each vertex.

Is it possible to pass in 1 vertex every 3 other vertices in a glVertexAttribPointer to avoid this?

No, because vertex attributes belong together and can not be separated.

Or is it a good idea to pass it in for all vertices?

Definitely. Makes life much easier for everybody.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Okay. Also one last small relevant question. I'm trying to use an element buffer for a heightmap (define all points once and draw them with GL_ELEMENT_ARRAY). However, It seems that with this method there is no room for calculating and placing the normal into the vertex buffer. How would I be able to store and pass in the normal for such a structure? – sgtHale Sep 04 '14 at 19:40
  • 1
    @sgtHale: For a heightmap you don't want to have face normals anyway. You want per vertex normals, where each vertex' normal is the mean of the normals of the faces the vertex is part of. – datenwolf Sep 04 '14 at 21:15
  • @sgtHale: Note that if you want a flat shaded effect (which resulted in a faceted look) you can specify, that certain vertex attributes shall not be interpolated over the face; with that set, only the value as specified for the first vertex of a face is considered. If you think about it, there's a strict offset between vertices and faces, which means that you can simply assign per face normals to vertices and things work as expected. – datenwolf Sep 04 '14 at 21:24