mid's site

you're logged in as loser

🌍 Go Paperful

🔗 Subscribe via RSS

Journey into OpenGL: the First Triangle

JiGL

  1. The Series
  2. the First Triangle
  3. the Framebuffer and Depth Buffer
  4. Transformations
  5. Spaces
  6. the Cube?
  7. Vertex Arrays
  8. Index Arrays
  9. ...

We must use glad to generate the OpenGL API definitions. This is possible via the glad command line program, or via the web interface. Among the APIs we shall need only gl. You may as well pick the highest version, because we should also select the "Compatibility" profile. After pressing "Generate", you will be given the source code necessary to use OpenGL.

Take note of the extension list, which we shall touch later. Over the years, some extensions become part of the main API, but there remains hardware that is just almost capable of a high main version, yet fails to get there. Additionally, many OpenGL features are available both as part of an extension and as part of a main version. This leaves us with a choice: either use a low main version and implement the rest with extensions, or use a higher main version. You may mix and match as necessary. In my latest program (as of writing), I use a baseline version of 1.3, and all of my chosen extensions are optional.

Should you choose to use an extension, you will need to add it into glad and generate again.

#include<GLFW/glfw3.h>

int main() {
	if(!glfwInit()) {
		return 1;
	}
	
	GLFWwindow *window = glfwCreateWindow(640, 480, "Penis", NULL, NULL);
	if(!window) {
		glfwTerminate();
		return 1;
	}
	
	glfwMakeContextCurrent(window);
	
	while(!glfwWindowShouldClose(window)) {
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	
	glfwTerminate();
	return 0;
}

If you name this source file "main.c" as I have, it may be compiled like so: cc -std=c99 -o TestProgram -lglfw main.c.

Let's go over each call:

  • glfwInit and glfwTerminate are both self-explanatory.
  • glfwCreateWindow takes in a width, height and window title. It may optionally take in a monitor argument, in which case it will go full-screen in that same monitor. The fifth argument is for shared OpenGL contexts, too advanced for now.
  • glfwMakeContextCurrent makes the OpenGL context associated with the window "current". This is necessary, as the OpenGL state is completely global.
  • After that, we enter the main loop. This will run forever (ideally), until the user tries Alt+F4 or presses the close button, at which point the glfwWindowShouldClose function will begin outputting a truthy value.
  • We shall touch upon glfwSwapBuffers later.
  • glfwPollEvents allows GLFW to check whether certain events have occured, e.g. key presses or mouse clicks. Were we to register some event handlers, they would be called here.

If you try running this program as-is, you will come upon a black screen. Congratulations! You may technically call this your first OpenGL program.

We shall now insert the files glad generated for us, so that we may begin actually using OpenGL. glad gives us a source and several header files, neatly organized into separate src and include directories, but I usually plop source and header files together. After this, we shall include glad/gl.h. This include must be put before the GLFW include, as glad tries to "override" the traditional OpenGL header, which GLFW uses.

Now, we amend. Here is a simple triangle:

glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);

while(!glfwWindowShouldClose(window)) {
	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT);
	
	glBegin(GL_TRIANGLES);
		glColor3f(1, 1, 1);
		glVertex2f(-0.2, -0.2);
		glVertex2f(+0.2, -0.2);
		glVertex2f(0, +0.2);
	glEnd();
	
	glfwSwapBuffers(window);
	glfwPollEvents();
}

gladLoadGL is what assigns OpenGL functions to the correct addresses. It takes a procedure loading function as an argument, which GLFW offers.

Within the loop, we blacken the window by first setting the clear color with glClearColor, then actually clear with glClear. The latter function can accept multiple items. The loop forces the program to keep clearing and drawing the same frame over and over. This will be useful once get into animation and more complicated logic.

The glBegin function tells OpenGL that it should begin accepting vertices. We chose the triangle as our primitive, but there are others: lines, quads, convex polygons, etc. Triangles, however, will be the main mode we shall be working with. This is because triangles are considered a fundamental shape - every polygon can be decomposed into a set of triangles. It is so widespread, that, at a point, graphics accelerators basically stopped supporting every other shape, and programmers began drawing everything with only triangles. Even your virtual desktop could be using triangles underneath if graphics acceleration is turned on.

Within drawing mode, we feed OpenGL vertices with the glVertex set of functions. They may accept different types, specified with the suffix. In our case we are passing two floats for two dimensions: 2f. We may assign attributes to vertices. In our case, we are setting the color of all vertices to (1, 1, 1), which corresponds to white. Colors are typically also passed as unsigned bytes (3ub). One may also pass a fourth coordinate, which corresponds to the alpha channel (4f or 4ub). Otherwise it is 1 for opaque.

Each vertex has its own attributes. We can test this by, say, setting a different color for each vertex. Attributes are always interpolated across a primitive shape. You can see it mix the three colors, causing a grey color in the center of the triangle.

Blue corresponds to quads mode, green - triangles mode, red - triangle strip mode, pink - polygon mode.

glBegin(GL_TRIANGLES);
	glColor3f(0, 0, 1);
	glVertex2f(-0.2, -0.2);
	glColor3f(0, 1, 0);
	glVertex2f(+0.2, -0.2);
	glColor3f(1, 0, 0);
	glVertex2f(0, +0.2);
glEnd();

Under triangle mode, each three vertices passed correspond to one triangle. In quad mode, every four vertices correspond to one quad. There is also "triangle strip mode", in which each subsequent vertex is joined with two points of the previous triangle. There are also triangle fans, but their use case is quite fringe. In each mode, you can feed OpenGL multiple such primitive shapes, by simply calling glVertex more, and you should always "batch" vertices like this to a reasonable degree before calling glEnd.

A vertex position is defined in what is known as clip space: the center of the window is at (0, 0), the bottom-left corner is at (-1, -1) and the top-right is at (1, 1). The third dimension defines the depth of a point. At -1 it is the near plane of clip space, which is the closest a point may be while still visible. At +1 it is the far plane. This way OpenGL defines a sort of cube, inside of which all shapes are visible and, if partially outside, are "clipped". If completely outside, they are thrown away. The choice of the range (-1, 1) for the depth dimension is generally considered poor, as it puts zero, where floating-point numbers are most accurate, at the center of clip space. We define the notion of clip space, because we shall be working with other spaces later.

Here you see a stretched, animated triangle. Its projection onto the screen remains constant.

You can try changing the third coordinate by using glVertex3f, but you will not notice a difference, because OpenGL uses an "orthographic projection". You can imagine each pixel on the window shooting out light rays into the scene. Wherever that ray lands, that is how the color of the pixel is chosen. Under an orthographic projection, each ray is parallel to another, and for this reason depth doesn't cause any distortion as it would under perspective projection.

Here is a small test in the form of virtual flashcards, to make sure you've grasped the concepts. I have no way of verifying your knowledge, so this test is purely for your own benefit. Try to write down your answers on a piece of paper before revealing mine, as when done in your head you are unlikely to "finalize" that information.

What is the purpose of GLFW?

To create a window, in which to draw and receive events.

What is the purpose of glad?

To give us access to the full OpenGL API.

What is clip space? What is its purpose?

The cube defined by extents (-1, -1, -1) and (1, 1, 1). All shapes within clip space are drawn to the screen.

What is the third dimension in clip space?

The third dimension is the depth of a point. The plane defined by z = -1 is the near plane, and it holds points closest to the screen. The plane defined by z = +1 is the far plane, and its points are the farthest that can be before being clipped.

What is a vertex attribute?

A vertex attribute is data assigned to a particular vertex, such as its position or color. When a shape is drawn, vertex attributes are interpolated between its vertices.