OpenGL学习记录一(环境配置)
opengl 文档查询地址 : https://docs.gl/
参考教程 (文章):https://learnopengl-cn.github.io/
视频: https://www.patreon.com/thecherno
bilibili 翻译版 :最好的OpenGL教程之一
使用社区版vs studio ,安装时仅选择c++桌面开发,够用
学习记录对应的git地址 :OpenGLDemo
下载 glfw 、 glew 学习以32位
(界面基于 studio 2022)
1.创建empty c++项目
2.添加src 目录,添加cpp 文件
3.配置项目属性 (可跳过glfw)
下载glfw
下载glew
源目录创建依赖目录,并将相关依赖拷贝过去
将下载的glfw 或者 glew
glew
添加/修改配置
$(SolutionDir)Dependencies\GLEW\include
$(SolutionDir)Dependencies\GLEW\include
链接库,glew使用
输入(user、gdi等是因为前面的系统配置全部清掉,这里需要补充)
glfw的话,复制 https://www.glfw.org/documentation.html 的代码
#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; }
如果 #include <GLFW/glfw3.h> 引用爆红,调试环境改为下载的库-对应 x86或者64
glew
/*fatal error C1189: #error: gl.h included before 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); /*初始化 glew 需要放在 glfwMakeContextCurrent 创建上下文之后*/ if (glewInit() != GLEW_OK) std::cout << "Error " << std::endl; /*获取版本*/ std::cout << glGetString(GL_VERSION) << std::endl; /* 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; }
遇到的问题
fatal error C1189: #error: gl.h included before glew.h 需要放到GLFW前面/
#include <GL/glew.h>
#include <GLFW/glfw3.h>
无法解析的外部符号 __imp__glewInit@0,函数 _main 中引用了该符号
需要定义glew static(内部使用dll)
c++->预处理器->预处理器定义->添加GLEW_STATIC;
本文来自博客园,作者:西瓜皮不甜,转载请注明原文链接:https://www.cnblogs.com/Jieth/p/17197127.html