OpenGL in VS 2019–1: Your Window

Homan Huang
5 min readAug 21, 2021

In the old fashion, we ran code in linear method among CPUs. With OpenGL, you can run code in parallel tasks among the GPUs. That’s much faster. Don’t you want to have a fast program under your design? Let’s work on OpenGL with C++. In this chapter, I will focus on setup and opening the first window.

— ==== Menu ==== —

📲 1. Download Requirements
🔨 2. Generate GLFW Project with CMake
🔧 3. VS2019 — Setup Project
✒️ 4. Main C++
😊 5. GLAD &. Set Background Color

📲 1. Download Requirements …… →Menu

Please download the source package.

  • Glad: Multi-Language GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.

Download the glad.zip file.

Unzip glad.zip to my OpenGL_Project\glad.

🔨 2. Generate GLFW Project with CMake …… →Menu

Download or Your saved folder

Unzip glfw-3.3.4.zip to “glfw-3.3.4” folder.

Unzip GLFW to its folder and create a “build” folder in it.

Build

Default is fine.

Generate

→ Done!

VS2019: Build GLFW Lib & Move into Libraries\lib

In glfw-3.3.4\build\

Open GLFW.sln in Visual Studio.

Build Solution

The project will generate a library file called glfw3.lib at build/src/debug.

If you encounter an error, you may have moved the folder from somewhere else. So you need to redo the CMake process.

Now, let’s move glfw3.lib to your project Libraries\lib folder:

User GLFW’s include as Your include

Import Glad

Download: Unzip glad.zip to glad

glad\include to Libraries\include

Source to main

glad\glad.c to C:\OpenGL_Project\MyOpenGL

🔧 3. VS2019 — Setup Project …… →Menu

Switch back to MyOpenGL project:

📟 64-Bit

Let’s choose x64 as our code standard.

🔧 Property

  • Platform + Configuration Manager
  • VC++ : Include
Include Directories → Edit
Add new folder
Libraries → include
  • VC++ → Library
Library Directories → Edit
Add new folder
Libraries → lib
  • Linker → Input

Two files:

opengl32.lib
glfw3.lib

Glad Source File

Drag glad.c to the folder of “Source Files”.

✒️ 4. Main C++ …… →Menu

Ctrl+Shift+A: Add main.cpp

As usual, main function:

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
int main() { return 0;
}

OpenGL Block: OpenGL code will insert in the block.

// Begin
glfwInit();
// End
glfwTerminate();

Hint:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

Create a window(w:h): 800x600

// window: w, h, name, full_screen, 
GLFWwindow* window = glfwCreateWindow(800, 600, "My OpenGL", NULL, NULL);
if (window == NULL) {
cout << "Failed to create GLFW window" << endl;
// End
glfwTerminate();
return -1;
}
// use window
glfwMakeContextCurrent(window);

Process window events:

// process window event
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
// close window
glfwDestroyWindow(window);

🏃🏼 — Let’s Run

Result →

It’s nothing but a pure white background.

😊 5. GLAD &. Set Background Color …… →Menu

Before the window event, we can use the GLAD(Multi-Language GL/GLES/EGL/GLX/WGL Loader-Generator) function.

// use window
glfwMakeContextCurrent(window);
// process window event

Load GLAD:

// use GLAD to load the function address of OpenGL
gladLoadGL();

Now, let’s set the background color and refresh the frame:

// windows: x, y, x=800, y=600
glViewport(0, 0, 800, 600);
// background color
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// clean back buffer and assign the new color
glClear(GL_COLOR_BUFFER_BIT);
// swap back buffer with front buffer
glfwSwapBuffers(window);

Result:

glClearColor() → Background color

The glCelarColor is using RGB color. For example,

// background color
//glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
float rValue = 184.0 / 255.0;
float gValue = 213.0 / 255.0;
float bValue = 238.0 / 255.0;
float alpha = 0.5f;
glClearColor(rValue, gValue, bValue, alpha);

Result:

That’s an easy task.

😉Have a nice day! …… →Menu

--

--

Homan Huang

Computer Science BS from SFSU. I studied and worked on Android system since 2017. If you are interesting in my past works, please go to my LinkedIn.