🚀 Knowing how to draw geometric shapes with code is one of the foundations for learning OpenGL and Vulkan.
In the future, I intend to bring more content about OpenGL and Vulkan, however, one of the best tools to “prepare” for these upcoming realities is SFML.
By the way, SFML is also interesting to enter the world of game and graphics development, besides you getting to better understand other libraries, such as: SDL2, Raylib, Allegro, besides also facilitating the learning of graphics engines such as: Godot, Unity and among others.
And one of the interesting tools of SFML, to use, is sf::Vertex.
sf::Vertex is a class (or more precisely, a struct) in SFML that represents a single vertex in 2D space and stores information such as:
position;
color;
and texture coordinates.
01. The Basics of the Basics
Starting from scratch to understand how sf::Vertex really works.
1.1. This only shows the black background, but note that in draw it is necessary to pass 3 parameters, in addition to &vertex as a reference, since it is not drawable.
Note that we used the model: sf::Points, it only draws a single point. There are other models, we will see later. > It is appearing on the screen, however, at position: 0, 0, that is, you will not be able to notice it.
1.2. To notice it, position it further to the right and down. To do this, use the union structure: .position, example:
Note that a small white dot (1 single vertex) will appear on the screen!
This basic example could be replaced using only the constructor:
1.3. You can also set color for the vertex using the union structure.color
1.4. You noticed that the constructor is overloaded like most declarations in SFML. And there is one more argument for the constructor which is the union structure: .texCoords.
As the name suggests, it is used to define coordinates for a possible textture that you want to “fit” to your vertex. In addition to being able to pass it directly in the constructor, you can also define it later:
Drawing just one vertex may be useful, depending on what you are doing, but it is generally not useful in most cases. In the next topic we will see how to use multiple vertex(s).
02. Multiple vertex(s)
The correct way to use vertex is together. In this example we will place one at each point of a possible square:
And now, since it is not a single vertex but 4, we no longer use reference to draw:
It will render like this:
Models for sf::Vertex
We use and talk about sf::Points, logically, it only draws points, but there are others. They are:
sf::Lines, it connects the points that are on the same line:
sf::LineStrip, similar to sf::Lines, but it makes an additional connection on one of the verticals:
sf::Quads, it fills the content taking the vertices as coordinates:
sf::Triangles, similar to sf::Quads, but it is for triangles, of course your array needs to have 3 elements now: sf::Vertex vertex[3]:
sf::TriangleStrip, we return to the 4 elements for the array, and notice that it fills, except for a triangle shape:
sf::TriangleFan, ends at the same vertex as the beginning, generating an effect similar to sf::Quads with fill:
Now there is a vertex with 5 vertices:
Examples
01. Drawing colored triangle like the basic example of OpenGL
There is another type that is also widely used, which is sf::VertexArray, it makes it easier to use and implement, not to mention that it is drawable, for example:
This renders a square of lines with no fill. In this video we create a gradient background with sf::VertexArray.