Join Pablo Colapinto for an in-depth discussion in this video Generating Vertex Array Objects, part of Learning OpenGL.
-The shader we've built and compiled in the last lesson applies to every vertex that we're going to send to the GPU. There are still some steps to let the GPU know exactly where our data is and how we expect it to handle it. We exchange data with the GPU with vertex buffer objects. We exchange data with the GPU about where those buffer objects are with vertex array objects.
We tell the GPU how to navigate that data with vertex attribute pointers. Let's take a look at how this all works. The vertex array object allows us to find a particular number, a "glu int" to a whole set of data on the GPU. On the CPU side, we can refer to a whole buffer of data on the GPU by just binding this particular number. The vertex array object is a number which represents a whole set of vertex data.
Here on lines 29 to 40, we're using our GL extension wrangler "glu" to make sure that we're calling the correct GL gen vertex arrays and GL bind vertex array methods appropriate to our particular platform. On a Mac, we'll typically want to call GL gen vertex arrays "apple." Whereas, on a PC, we'll typically call GL gen vertex arrays. This macro encapsulates that discrepancy for us.
Lines 44 to 60 contains our shader code from before. On lines 128, 130 and 132, we've defined three GL, unsigned ints which are handles into our GPUs data array. The first step is to assign our position ID to the address of our position variable in our shader source code up at the top here. The second step will be to assign our array ID on line 132 to a new vertex array object.
The third step will be to assign our buffer ID to the actual buffer of data that we send over to the GPU. Finally, we're going to tell the GPU how to navigate the data we've sent to it by specifying the type of data we've sent and the stride between each data block. On line 179, we assign our position ID to the "GL get attrib" location of the position variable in our shader. Now we can use the position ID to refer to the attribute position in our shader source.
On lines 187 and 188, we generate a new vertex array object and bind it. With the array object bound, we generate a new buffer of data and send it over. We'll get to exactly how that works in a second. On line 204, we make sure that the position attribute in our shader source has been enabled. On line 207, we specify exactly how the GPU should navigate the buffer of data that we've sent to it earlier on lines 194 to 198.
GL vertex attribute pointer tells the GPU how to navigate the buffer of memory. It specifies a handle into the buffer of data, the position ID in this case, the number of elements that we are sending in our data, in this case we have a two dimensional coordinate. We're sending over two floats. The type of data, in this case a GL float. Whether we want the GPU to normalize the data for us, we do not, so we set GL "False." The stride of the data so that the GPU knows how big each data block is.
In this case its size of vec two. And then offset into the memory buffer, in this case zero. With our data sent, along with instructions on how to handle that data, we can unbind our vertex array object. Now, in our own draw loop, all we need to do to reference that data array, on the GPU, is called "bind vertex array" and pass it the array ID. At that point, the GPU knows which buffer of data we're referring to and also how to navigate it. It also knows what attributes in the shader source code it's supposed to map to that data.
In this case, the position attribute. With all that information bound, we can call GL "Draw Arrays" passing it the type of primitive we'd like to draw, in this case GL triangles, an offset into memory, in this case zero, and the number of vertices we'd like it to draw, in this case three. Now we can unbind our vertex array object and unbind our shader.
- What is OpenGL?
- Setting up an OpenGL workflow
- Creating a window with GLFW
- Working with geometric primitives
- Rotating, translating, and scaling
- Understanding matrix transformations
- Creating mesh data structures
- Buffering mesh data
- Getting mouse and keyboard input
Skill Level Intermediate
Q: Why am I getting errors saying I don’t have GLEW installed when I try to create the Xcode projects with Cmake?
1) Download, Install, and Run Xcode
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install glew
./bootstrap && make && make install
brew install cmake
cd Exercise Filesmkdir xcodecd xcodecmake -G "Xcode" ..