OpenGL is a 3D graphics library so all coordinates that we specify in OpenGL are in 3D (x
, y
and z
coordinate), which are in a specific range between 1.0
and 1.0
→ NDC (Normalized Device Coordinates)
Once your vertex coordinates have been processed in the vertex shader, they should be in NDC which is a small space where the
x
,y
andz
values vary from-1.0
to1.0
. Any coordinates that fall outside this range will be discarded/clipped and won't be visible on your screen.
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
The triangle specified in the code clip above within NDC
<aside> 💡 OpenGL Vertex Coordinate System: [-1, +1]
</aside>
unsigned int VBO;
// 0. Generate a buffer with a buffer ID & stores into `VBO`
glGenBuffers(1, &VBO);
// 1. Bind the newly created buffer `VBO` to the GL_ARRAY_BUFFER target
// 1. buffer type of vertex buffer object -> GL_ARRAY_BUFFER
// 2. from this point on any buffer calls we make (on GL_ARRAY_BUFFER target)
// will be used to configure the currently bound buffer
// 3. we can bind to several buffers at once as long as they have a different buffer type
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// 2. Copy the previously defined `vertices` data into currently bound buffer's memory
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 3. Set the vertex attributes pointers
glVertexAttribPointer(attribIdx, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// 4. use our shader program when we want to render an object
glUseProgram(shaderProgram);
glBufferData
function specifically targeted to copy user-defined data into the currently bound buffer