Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Drawing a Square using OpenGL and GLAD

--

Now that I have set up SDL I have been learning how to use OpenGL to do graphics rendering. In this article I will go over setting up GLAD and OpenGL in visual studio and then demonstrate how to display a square to the screen.

Setup

To start we are going to download GLAD. To do this head over to the GLAD website.

You will come to a website that looks like this. Copy the settings for the language, specification, and profile settings. Under API you can select whichever version you are using. this will show multiple options in the extensions box. Select add all to add them. Make sure generate a loader is selected and click generate. This will download a zip file that you will extract and copy into your project file where you .cpp files are located.

The glad folder should contain an include and src folder. In your project settings you will want to go to properties and add the include to the include directories. You will also add opengl32.lib to the additional dependencies under the linker. Last you will open the src folder and add the glad.c file to your project. You will also want to include it at the top.

Creating a Window

Creating a window with OpenGL is similar to creating a window with SDL. The big difference is instead of using the SDL renderer we are going to create an OpenGL context.

After initializing SDL we will set the attributes that the window will have.

When creating the window instead of using the SDL_WINDOW_SHOWN flag we are using the SDL_WINDOW_OPENGL flag.

Now we set the context and load glad.

In the game loop we will create a viewport and then display the window

This will allow you to display an OpenGL window.

Shaders

To create a square we are going to render two triangles next to each other. To do this we are going to need to create a vertex shader and fragment shaders. These will be defined at the top.

It is important to have the correct version here. I am using 4.6 so it is version 460 core.

Next we will replace the source code of the shader object with are own and then compile it.

We will do the same with the fragment shader and switch the vertex shader tags with the fragment shader.

Now we link the shaders to a shader program and free the individual vertex and fragment shaders.

Vertex Input

To actually draw to the screen we need the vertex data of the objects we wish to draw. In are case this is the three vertices of the triangles. These will be normalized to fit the coordinate plane OpenGl uses which spans from -1 to 1.

These will be stored in vertex buffer objects. We will then keep track of these vertex buffer objects with a vertex array object.

Now we just have to draw the object to the screen. After the game loop is over we will free the resources and close the program.

And with that we will get a square with two different colors.

If you would like to learn more check out LearnOpenGL for more information on the topics discussed in this article.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Matthew Clark
Matthew Clark

No responses yet