Rust and OpenGL from scratch - Colored Triangle
Welcome back!
Previously, we have filled the VBO, configured VAO, and painted triangle in a window.
This time, we will add different color for every triangle corner.
To avoid repeating the same things other OpenGL tutorials already teach, we are loosely following learnopengl.com lessons, and we are now at Hello Triangle page.
More of the same
We can extend our data to include red, green and blue for each color, respectively:
(main.rs, modified previous code)
let vertices: = vec!;
Then we change the stride in data layout from 3 to 6, because we’ve added 3 additional floating point value for each vertex:
(main.rs, modified previous code)
EnableVertexAttribArray; // this is "layout (location = 0)" in vertex shader
VertexAttribPointer;
// continue here
We will configure the vertex shader to pick up this color information at location 1:
(continued)
EnableVertexAttribArray; // this is "layout (location = 0)" in vertex shader
VertexAttribPointer;
Let’s modify vertex shader to pick up the color as vec3:
layout in vec3 Position;
layout in vec3 Color;
out VS_OUTPUT OUT;
void
And then the fragment shader:
in VS_OUTPUT IN;
out vec4 Color;
void
If you follow learnopengl.com, you may notice that we changed up a bit the way
we pass color from vertex to fragment shader. The VS_OUTPUT struct groups output and
input variables nicely, and we don’t need to add suffixes for our values such as aColor.
Drawing
Drawing code remains the same, and we get the expected result:

Discussion
While is is quite possible and educational to use OpenGL this way,
it is incredibly error-prone. If we were to forget to modify stride for the
VAO configured in previous lesson, we would get some garbage on screen,
and would have to spend some time searching for bug and double-checking
everything.
In future, we will begin building some abstractions to encapsulate error-prone things related to VAO setup.
There are additional topics discussed in learnopengl.com lesson that we are going to skip for now, such as passing uniforms to shader or Element Buffer Objects. It is a bit tedious, but not that hard to rewrite C code to Rust, and I feel that we have discussed enough examples of that. I am going to leave these things as “exercises to the reader”.
As always, the code for this part is available on github.
For the next part, we will rework our OpenGL setup a bit, before we have written too many things.