Interaction with Shaders and Further Examples
- OpenGL has a generic system for allowing the user to pass
data in a VBO along to the shader.
- This is the VertexAttribute
- This is related to data that is associated with each vertex.
- Attributes are specified in the shaders
- In color.vert there are three
attributes specified
- vPosition, the vertex position
- vColor, the vertex color
- vScale, a per vertex scale factor
- Dumb, I know but I wanted to illustrate a point.
- Looking at the code in the shader, you can see how each is used
- We will begin looking at shaders more soon, but this will do for now.
- In the program color.C
- Attrib_IDs has changed to include the new attributes
- The names do not have to match, just the positions.
- The data that we stick in the VBO (vertices) has changed.
- It includes the new data.
- In the setup routine, we specify the two additional data values
- glVertexAttribPointer
- glEnableVertexAttribArray
- Note, the location is in terms of bytes.
- According to this source
- glVertexAttribPointer does two things
- State where (in the VBO) the data comes from
- Tells OpenGL how to interpret (the format of) that data.
- Sizes are limited between 1 and 4 elements.
- If the shader requires more components then the data supplies,
they are increased by (0,0,0,1)
- So (x,y) becomes (x,y,0,1) for vertex position)
- This is not true for doubles
- Storing the data
- They say we should use interleaved arrays whenever possible.
- But colors2.C shows another option
- A single struct with multiple arrays.
- Not a big fan of this method
- All three arrays are packed (in a contiguous manner) in the same struct.
- I think this would be hard to build nicely.
- It was a pain to initialize
- offsetof(type, field) returns the offset, in bytes of the field from the beginning of the structure.
- And here is with multiple VBOs
- colors3.C
- To do the initialization, I needed to compile with -std=c++11
- I used his vec.h class for my data types.
- He had sqrt and ceilf in the std library for this
- I removed it.
- But you do need to include <cmath>
- I would start using this, it has a bunch of utilities we
- Want to use
- Don't want to write.
- In twoThings.C I create multiple VAOs and draw them.