新建项目
-
首先建立一个文件夹作为工作区
-
在vscode中打开该文件夹,新建一个
main.cpp
文件
-
按下
F5
,然后选择C++ (GDB/LLDB)
,然后选择g++.exe
-
运行成功
我们发现目录下多了一个.vscode/tasks.json
文件,这个我们待会要配置。
配置GLFW
-
百度一下GLFW,进入官网,然后点击Download,根据需要选择对应版本(这里下载的是二进制文件,无需自己编译)
-
进入解压后的文件夹,找到我们将要用到的
include
文件夹和libglfw3.a
文件,复制一下
-
新建一个文件夹
depends
,将我们刚才复制的include
文件夹和libglfw3.a
文件粘贴进去 -
此时整个文件夹的目录树是这样的
5. 使用快捷键Ctrl+Shift+P
打开命令面板,搜索编辑配置
这时会生成一个.vscode/c_cpp_properties.json
文件,编辑该文件中的includePath
加入我们的depends下的include 文件夹
- 编写tasks.json以确保我们的项目能调用GLFW的库文件(自行加上带注释的那几句话,注意前后顺序)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "F:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}/depends/libglfw3.a", //链接至glfw库
"-lopengl32", //链接系统中的opengl32库
"-lgdi32", //链接系统中的gdi32库
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"--include-directory=${fileDirname}/depends/include" //确保头文件的包含
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
- 使用代码测试是否配置成功
#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);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
运行成功后结果如下:
GLEW
- 百度一下GLEW,进入官网下载二进制文件
- 下载解压后找到
glew32s.lib
文件和include
文件夹,复制粘贴进我们的depends文件夹下
3. 编写tasks.json以确保我们的项目能调用GLEW的库文件(自行加上带注释的那几句话,注意前后顺序)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "F:\\mingw64\\bin\\g++.exe",
"args": [
"-D GLEW_STATIC", //因为我们链接的是静态库,使用该宏告知glew
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}/depends/libglfw3.a", //链接至glfw库
"${fileDirname}/depends/glew32s.lib", //链接至glew静态库
"-lopengl32", //链接系统中的opengl32库
"-lgdi32", //链接系统中的gdi32库
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"--include-directory=${fileDirname}/depends/include" //确保头文件的包含
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
- 使用以下代码测试GLEW
#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);
if (glewInit() == GLEW_OK)
{
std::cout << "GLEW IS OK\n";
}
/* 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;
}
如图所示,GLEW IS OK
即为成功!