克隆项目
运行问题 链接错误 · Issue #8 · yocover/start-learning-opengl (github.com)
可参考视频
VsCode下配置OpenGL开发环境_哔哩哔哩_bilibili
环境配置
使用MinGW(非64),安装的时候勾选base,g++和gdb
https://sourceforge.net/projects/mingw/
加入环境变量为安装路径/bin,如果以前安装过mingw64的记得要删除旧的环境变量
自己编译libglad.a 复制
项目里面的是不能直接用的
glad 在线服务 生成静态库
在压缩包执行下面的
gcc ./src/glad.c -c -I ./include/
ar -rc libglad.a glad.o
将生成的 libglad.a 复制到项目的 lib 文件下覆盖
测试运行
测试代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window))
{
processInput(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
执行命令 run 编译和运行.exe 程序
make run dir=01_test_glfw
执行命令 clean 删除 .exe 和对应文件夹下的.o 文件
make clean dir=01_test_glfw