FANDOM


On this page are notes of me following along on the Create a window page from Learn OpenGL.

Because operations are specific per operating system and OpenGL purposefully tries to abstract from operations used to create an OpenGL context and an application to draw in. Luckily there are quite a few libraries that already provide the functionality we need, some specifically aimed at OpenGL. These libraries save us all the operation-system specific work and gives us a window and an OpenGL context to render in. Some of the popular libraries include GLUT , SDL , SFML , and GLFW .

In this tutorial we are going to be using GLFW.

GLFW and SetupEdit

We are going to be using GLFW, which is an Open Source, multi-platform library for OpenGL development. We will also be using Visual Studio 2017 Community Edition to write our code, so the following steps are for setting up GLFW in Visual Studio.

Step 01Edit

Go to www.glfw.org then we need to go to the Download page and select the 32-bit Windows Binaries under Windows pre-compiled binaries. For this project we will only need 32-bit as we will be building a 32-bit application, maybe for more serious projects we could use 64-bit.

DownloadPage-GLFW












While this is downloading we can go into Visual Studio and set up our new project.

Step 02Edit

In Visual Studio we want to go to File -> New -> Project. From there we want to make sure that we are under Visual C++. Then we select Empty Project. Then just give it a name and a location and select OK.

VisualStudioProjectCreation


Now that we have a Visual Studio solution and project set up and we are in the project let's do a little maintenance.

So under the Solution Explorer we want to click the project name and then select  'Show All Files'. 


What the 'Show All Files' does is mimic our Windows Explorer file/directory structure.
VisualStudio showAllFiles



Then we want to right click the project name and select 'Add' and create a new folder and call it 'src'. Once that folder is created we want to right click on the src folder and add a new item. The item we are going to add is a .cpp file called 'Application'. This is going to be where our main function is going to live.

Now we are ready to actually link up GLFW to work in Visual Studio.

Step 03Edit

If we go back to www.glf.org and go to the Documentation page, we can see some example source code that tells how we can get GLFW running. So what we are going to do is copy this code and paste it into our Application.cpp file. Now if we try and run this code in VS it will not work as there are a bunch of broken links we need to fix, so let's do that now.

If we go back into the zipped file we downloaded earlier we need only concern ourselves with a few of the files here. So first we need to go into the lib-vc2015 folder and find the glfw3.lib file. Then go back into VS and right click on the project name and select 'Open Folder in File Explorer'. Once in the folder structure we should have the solution file <projectName>.sln, this is known as the Solution Directory. In here we want to make a new folder called 'Dependencies', then inside that new folder create another new folder called 'GLFW', and inside GLFW we are going to put all the files that we want.

So Inside the GLFW folder we just made we want to paste the 'include' folder and the 'lib-vc2015' folder from the zipped folder we downloaded earlier. Then in the lib-vc2015 folder we copied over to GLFW we can delete out glfw3.dll and glfw3dll.lib as we will not be needing them..

Glfw-libFileStructure












Step 04Edit

Now we need to tell our VS project to include the glfw include files we have and add our lib file to the project linker. To do this we need to go right click on the project in Solution Explorer and hit 'Properties'. In here you will find include stuff and the linker. An important thing to note here is that we make sure that in 'Configuration' up the top that we are set to 'All Configurations' and that 'Platform' is set to 'Win32'.

VisualStudioProperySettings













Setting 'All Configurations' is particularly important as if you make changes in another configuration you wont see those changes in your current project and you will get errors.

Now lets go under 'C/C++' then under 'General' we want to add 'Additional Include Directories'. To do this, we want to make the path of the include file relative to the current VS solution. VS has a handy macro for us that lets us get our current solution director (this can be found by clicking the 'Macros' button and searching 'solutiondir') but to save us from searching the macro is $(SolutionDir) then we can type the rest of the directory after it. So the final string should look something like this :

VS adittionalIncludes












Now that we have our glfw include file set up we can link up our library directory.

So under 'Linker' in the same Properties menu, then go to 'General', here we will find the 'Additional Library Directories'. So once again we want to use $(SolutionDir) then we want to add where our .lib file is located. So it should look something like the following :

VS adittionalLibraries













Once you have that we need to specify our additional dependencies. So we need to  go to 'Input' under 'Linker', clear out all that are there by default, then add the following :

VS additionalDependencies













The  glfw3.lib and opengl32.lib should be relatively obvious but the others were added after trial and error with running the project and experiencing various linking errors. So to save the hassle of going back and forth I have just added the needed dependencies in the screenshot above. All in all the dependencies required here are the following :

glfw3.lib;opengl32.lib;User32.lib;Gdi32.lib;Shell32.lib;

To find these dependencies yourself, checkout The Cherno Project's video Setting up OpenGL and Creating a Window in C++ video and go to 15:26, where he shows how he finds the dependencies required to solve these errors.


Now the Application.cpp file should successfully compile and build! If we hit F5 to run our code, you should get a black screen pop up. Exciting!

Step 05Edit

Let's draw a triangle! Let's do this with legacy OpenGL, it is ok to use Legacy for debugging and testing. So under glClear lets write a triangle with vertexs :

/* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f( 0.0f,  0.5f);
        glVertex2f( 0.5f, -0.5f);
        glEnd();

Now if we press F5 we can see that we have drawn a triangle to the screen!

FinalCap

















ReferenceEdit

Setting up OpenGL and Creating a Window in C++ - TheChernoProject

Community content is available under CC-BY-SA unless otherwise noted.