$25
Use the Gloom-rs project along with all your modifications from Graphics Lab 1 as your starting point.
Do not include any additional libraries apart from those provided with Gloom-rs.
Upload your report as a single PDF file.
Please use the included shell scripts to automatically pack up the source code correctly into a ZIP file.
All tasks must be completed using Rust.
Use only functions present in OpenGL revision 4.0 Core or higher. If possible, version 4.3 or higher is recommended.
The delivered code is taken into account with the evaluation. Ensure your code is documented and as readable as possible.
Currently MacOS support for OpenGL is spotty at best. We strongly advise you to use Windows or Linux instead, if at all possible.
Questions which should be answered in the report have been marked with a [report] tag.
Objective: Understand how multiplying a 4x4 matrix with single coordinates allows for a wide variety and combinations of transformations. Get a taste of combining transformations, and what the limits are of their possibilities.
In the previous lab we’ve seen how to draw geometric primitives using OpenGL, and had a taste of GLSL Shaders. This lab focuses on affine transformations, and their effects on a scene.
This lab starts from the point where lab 1 ended. As such you should use the code from the previous as your starting point of this one.
Task 1: Repetition
a) [0.3 points] In Lab 1, you created a function in Task 1a which converted a float (f32) and integer (u32) vector into a Vertex Array Object (VAO).
i) Extend this function to in addition take another float vector as a parameter. This additional vector should contain color values - one RGBA color value per vertex. This additional float vector should be put into a Vertex Buffer Object, and attached to the VAO.
Hint: This should only be a matter of copying and pasting sections of the original function and modifying some parameters. Bear in mind that the attribute pointers need to be specified while the buffer they’re referring to is bound.
Hint: Colors in OpenGL usually consist of 4 floats. Each float represents one channel in the order [red, green, blue, alpha]. The value of each channel should be within the range [0, 1]. In each channel, a value of 1 denotes the maximum value, while 0 represents the minimum value.
The alpha channel denotes the transparency of the color. In this case, a value of 1 represents an entirely opaque color, while a value of 0 means the color is completely transparent.
In Gloom-rs, transparency has been turned on by default. This is something that has to be explicitly enabled in an OpenGL project, and is done with the following lines:
gl::Enable(gl::BLEND);
gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
The gl::Enable() function enables a particular OpenGL feature. In this case, the parameter gl::BLEND specifies that you’d like to turn on alpha blending. The gl::BlendFunc() specifies how a transparent color should be mixed with colors from objects behind it. The values shown above are usually the ones you’ll want to use.
Blending in OpenGL is done by looking at the color currently present in the framebuffer, as well as its transparency, and combining it with the new color and its transparency as defined by the blending function specified in gl::BlendFunc().
In this case, a new color is calculated like this:
ColorNew = ColorSource · AlphaSource + ColorDestination · (1 − AlphaSource) (1) The source color and alpha are the color and transparency of the new pixel being drawn, respectively. The destination color is the color of the pixel already present at the same location in the framebuffer.
Unless you want to achieve some very specific effects, the gl::BlendFunc() parameters shown above are almost always the ones you’ll want to use.
ii) Modify the Vertex (“gloom-rs/shaders/simple.vert”) and Fragment (“gloomrs/shaders/simple.frag”) shaders to use colors from the new color buffer as the colors of triangle(s) in the buffer. If vertices in the same triangle have been assigned different colors, colors should be interpolated between these vertices.
Hint: both shaders require modification to accomplish this.
b) [0.2 points] [report] Render a scene containing at least 3 different triangles, where each vertex of each triangle has a different color. Put a screenshot of the result in your report. All triangles should be visible on the screenshot.
Task 2: Alpha Blending and Depth
[0.2 points] [report] For this task, we want to show you what happens to a blended color when multiple triangles overlap from the camera’s perspective (where the triangles are positioned at different distances from the camera).
To this end, draw at least 3 triangles which satisfy the following conditions:
There exists a section on the xy-plane on which all triangles overlap.
For any single triangle, the three vertices of that triangle have the same zcoordinate.
No two triangles share the same z-coordinate.
All triangles have a transparent color (alpha < 1).
All triangles have a different color.
Each triangle’s vertices have the same color.
I do not recommend drawing the exact same triangle 3 times at different depths; it will make the next question more difficult to solve.
Remember to turn on alpha blending, as described in the previous task.
Put a screenshot in your report.
[0.3 points] [report] First make sure your triangles are being drawn back to front. That is, the triangle furthest away from the screen is drawn first, the second-furthest one next, and so on. You can change the draw order of triangles by modifying the index buffer. Remember the coordinate space of the Clipbox here.Swap the colors of different triangles by modifying the VBO containing the color Vertex Attribute. Observe the effect of these changes on the blended color of the area in which all triangles overlap. What effects on the blended color did you observe, and how did exchanging triangle colors cause these changes to occur?
Swap the depth (z-coordinate) at which different triangles are drawn by modifying the VBO containing the triangle vertices. Again observe the effect of these changes on the blended color of the overlapping area. Which changes in the blended color did you observe, and how did the exchanging of z-coordinates cause these changes to occur? Why was the depth buffer the cause this effect?
Task 3: The Affine Transformation Matrix
[0.2 points] Modify your Vertex Shader so that each vertex is multiplied by a 4x4 matrix. The elements of this matrix should also be defined in the Vertex Shader.
Change the individual elements of the matrix so that it becomes an identity matrix. Render the scene from Task 1b to ensure it has not changed.
Note that matrices in GLSL are column major. As such, individual elements in a matrix can be addressed using: matrixVariable[column][row] = value;
It is also possible to assign a vec4 to a column, to set all 4 values at once: matrixVariable[column] = vec4(a, b, c, d); Refer to the guide for more information.
[0.4 points] [report] Individually modify each of the values marked with letters in the matrix in equation 2 below, one at a time. In each case use the identity matrix as a starting point.
Observe the effect of modifying the value on the resulting rendered image. Deduce which of the four distinct transformation types discussed in the lectures and the book modifying the value corresponds to. Also write down the direction (axis) the transformation applies to.
a
d 0
0
b e
0
0
0
0 1
0
c f
1
(2)
Hint: You can use a uniform variable to pass a floating point value slowly oscillating between -0.5 and 0.5 from the main loop in main.rs into the Vertex Shader. Use the elapsed variable in the main loop, and compute the sine of of it (elapsed.sin()) before sending it into the shader with a uniform variable. See the guide for how to do this.
It makes the effects of modifying individual values in the transformation matrix much easier to recognise. The guide includes a description on how to work with uniform variables.
[0.1 points] [report] Why can you be certain that none of the transformations observed were rotations?