Configure GLFW
First create a C++ empty project with VS, and download GLFW from the official website. GLFW is a C language library specifically for OpenGL, which provides some minimal interfaces required for rendering objects. Download the precompiled binaries from the Download page of the official website. If there is an incompatibility problem, refer to the method in LearnOpenGL, first download the source code package, and then use CMAKE to generate the file.
To decompress the file, you need two folders include and lib-vc2019. I installed VS2019, and select the corresponding folder for other versions.
Create a new Dependencies folder in the root directory of the project, indicating all dependent files, create a new GLFW folder at the lower level, copy include and lib-vc2019, and the directory structure is as follows. The dll files in
lib-vc2019 do not need to be deleted
Right-click the solution in VS – Properties, select all configurations, and select x64 or Win32 as the platform.
Add $(SolutionDir)Dependencies\GLFW\include in C/C++ - General - Additional Include Directories, where $(SolutionDir) represents the root directory of the project.
Then add $(SolutionDir)Dependencies\GLFW\lib-vc2019 in Linker – General – Additional Library Directory
Add the two libraries glfw3.lib and opengl32.lib in the linker - input - additional dependencies, pay attention to the semicolon.
create window
Create a new src folder in the project, and add a script to write the code, copy the code under the Documentation page of the GLFW official website, right-click the solution, generate (Build) compile and run.
Application.cpp
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Finally, finally saw the window drawn with OpenGL.
draw triangle
Application.cpp
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* 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();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Configure GLEW
GLEW is a cross-platform open source C/C++ extension loading library. After downloading from the official website, extract it to the previous Dependencies folder, and change the name to GLEW for clarity.
The configuration process is similar to GLFW, except that a preprocessor definition GLEW_STATIC is added.
Application.cpp
//需要先引用GL/glew.h,不然会报错
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
//输出版本号
std::cout << glGetString(GL_VERSION) << std::endl;
//必须在创建上下文之后,初始化glew
GLenum err = glewInit();
if (GLEW_OK != err)
std::cout << err << std::endl;
//......
return 0;
}