Modelling
The objectives of this TP are:
- Learn index buffers.
- Model your first 3D shape.
- Understand projections and transformations.
Vertex Buffer Objects
A Vertex Buffer Object (VBO) is an OpenGL object used to store vertex data in memory. It is used to efficiently store and transfer vertex data between the CPU and GPU. The VBO contains vertex attributes such as position, color, and texture coordinates, which are used to render the vertices on the screen.
To create a VBO, you can use the glGenBuffers function, which generates a unique ID for the VBO. You can then bind the VBO using glBindBuffer and specify the vertex data using glBufferData or glBufferSubData.
Vertex Buffer for our Triangle
Vertex Array Objects
A Vertex Array Object (VAO) is an OpenGL object that stores all the necessary information about vertices in a scene. It includes vertex attributes such as position, color, and texture coordinates. A VAO can be used to simplify the process of rendering multiple objects in a scene, as it stores all the vertex data in one place.
To create a VAO, you can use the glGenVertexArrays function, which generates a unique ID for the VAO. You can then bind the VAO using glBindVertexArray and specify the vertex attributes using glVertexAttribPointer. Finally, you can enable the vertex attributes using glEnableVertexAttribArray.
Vertex Array Object
Index Buffer Objects
An Index Buffer Object (IBO) is an OpenGL object used to store index data. It is used to reduce the amount of vertex data required to render a scene by allowing vertices to be shared between multiple triangles. The IBO stores indices that reference the vertices stored in the VBO.
To create an IBO, you can use the glGenBuffers function, which generates a unique ID for the IBO. You can use the same glBindBuffer and glBufferData for binding and initializing your buffer.
Parallelogram: with and without indexing
Hello Pyramid
Okay, enough theory. Create a new Pyramid class(you can use the skeleton from the Triangle class). Think what would be the VAO, VBO and IBOs for the pyramid. In the end you should get something like this.
Hello Pyramid
Perspective Projection
Okay we did a pyramid, but it still looks like a triangle. The answer to this is because our viewer(or camera) lacks perspective projection. A perspective projection is a technique used to create a sense of depth and distance in 3D scenes.
Perspective projection works by projecting 3D points in a scene onto a 2D plane (the screen) using a mathematical transformation called a perspective projection matrix. The projection matrix takes into account the position of the camera, the aspect ratio of the screen, and the field of view (FOV) of the camera.
The FOV determines how wide the camera's viewing frustum is, or how much of the scene is visible at once. A smaller FOV will make objects appear larger and closer, while a larger FOV will make objects appear smaller and farther away.
In perspective projection, objects that are closer to the camera will appear larger than objects that are farther away. This is because the projection matrix maps points in 3D space onto the screen in a way that takes into account their distance from the camera.
Perspective Projection
Transformations
For all the mathematics, we will use the GLM library. In total, you need initialise 4 matrices in your pyramid's draw - rotation, translation, scale and perspective projection. The view matrix is the matrix multiplication of the rotation, translation and scale while the perspective projection matrix transforms the view matrix to perspective view.
Transformation Matrices
To send these matrices to the GPU, we are going to use uniforms. Note that unlike a GLSL attribute(the position in our triangle's shader), a uniform is global and can be accessed from both vertex and fragment shaders. We need 2 uniforms in the vertex shader, the view matrix and the projection matrix.
In the end, your pyramid(with perspective view) should look something like this. To visualize the depth better, you can rotate it by an angle and give it a colour gradient.
Hello Pyramid 3D
Bonus: Other Shapes
Try other shapes like sphere, cone, cylinder. Look at the list of primitives in OpenGLwhich can be combined to create the new shapes.
Bonus: Mouse Control
It would also be nice to control our 3D scene with the mouse. To add mouse control to your code, you can use the mouse event handlers of glfw for this. For example, you can use the mouse to rotate the object or zoom in and out of the scene.