OpenGL学习记录01
一、OpenGL介绍
OpenGL(开放图形库)是用于渲染2D、3D矢量图形的跨语言、跨平台的应用程序编程接口(API)。
OpenGL的高效实现(利用了图形加速硬件)存在于Windows,部分UNIX平台和Mac OS。这些实现一般由显示设备厂商提供,而且非常依赖于该厂商提供的硬件。
本文接下来将使用GLFW库
进行代码的编写。
二、GLFW的安装与配置
1、下载
进入GLFW官网,依次点击Download
->64-bit Windows binaries
(下载与自己系统版本一致的,我这以64位为例)。
2、解压文件
下载完之后解压文件。
3、打开Visual Studio创建项目
在Viusal Studio中创建一个c++
的空项目
。
创建完成后再解决方案中添加一个文件夹,命名为src
,并创建一个名为Application.cpp
的c++文件。
4、配置环境
添加如下代码。
#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;
}
再添加完代码之后,会出现报错,是因为我们还没有配置好依赖。
首先我们现将依赖文件移动到项目目录下。
在解决方案路径下,创建Dependencies
文件夹和GLFW
文件夹,并从之前解压的GLFW文件中复制include
文件夹和符合你vs版本的lib-vs
文件夹。
然后我们删除lib-vs
文件夹中的dll
相关文件,只剩下glfw3.lib
文件。
接下来我们右键解决方案
,点击属性
。
依次点击C/C++
->常规
,修改附加包含目录
为我们之前复制的include
文件位置。
$(solutionDir)Dependencies\GLFW\include
填写上述路径。
依次点击链接器
->常规
,修改附加库目录
,添加路径。
$(SolutionDir)Dependencies\GLFW\lib-vc2022
最后修改输入
的附加依赖项
。
glfw3.lib;opengl32.lib;User32.lib;Gdi32.lib;Shell32.lib
接下里,我们运行,就会出现一个黑色的窗口。
三、简单绘制三角形
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
//绘制三角形
glBegin(GL_TRIANGLES);
glVertex2f(-0.5,0);
glVertex2f(0, 1);
glVertex2f(0.5, 0);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
修改上述代码,再次运行,就可以看到在我们的窗口中绘制出了一个白色的三角形。
以上内容均为个人学习记录,如有错误,可以一起学习探讨。
更多内容请关注我的个人博客