目录
笔者学习OPENGL,找寻多个教程,乱七八糟。最终找到一个相对完整的英文教程,并找到他的中文翻译版本。(笔者英语也不怎么样,没办法,唉~)
声明:本文虽为原创,确实是照着教程做的,教程给了几种方法,我选择了比较简单的,相信大家都能学会的,若有问题可以下方评论。
参考:LearnOpenGL
1. 做准备
1.1 GLFW官网下载GLFW库
下载地址:GLFW官网下载页
选择32位二进制版本
1.2 GLAD库在线生成
生成地址:GLAD在线生成地址
生成后下载glad.zip压缩包即可。
2. 配置
解压glad.zip,将里面文件(glad与KHR)全部复制到解压后的glfw-3.2.1.bin.WIN32下的include文件夹下面。
将glfw-3.2.1.bin.WIN32下的lib-vc2015的glfw3.dll粘贴到C:\Windows\SysWOW64中。(笔者的版本是vs2015, windows是64位,若读者版本不一致,请自行更换。glfw-3.2.1.bin.WIN32提供vs2010到vs2015, 若是32位系统,就放到C:\Window\System32下)
vs2015下新建空项目,粘贴glad文件夹里面的glad.c文件到源文件中,并建立main.cpp。
项目右键选择属性,VC++目录中,下拉编辑包含目录与库目录,分别选择glfw-3.2.1.bin.WIN32下的include与lib-vc2015文件夹(同样,读者若不是vs2015,请选择对应版本)
链接器-输入-附加依赖项-编辑 填入lib-vc2015下的三个文件名,注意换行。
为防止读者输入错误,特粘贴如下:
glfw3.lib
glfw3dll.lib
opengl32.lib
至此,配置完毕!
3. 运行检测
新建空项目,将glad.c文件放到源文件下,并建立main.cpp。
main.cpp文件中代码如下:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
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);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
运行截图:
4. 函数列表
glfwInit
参数:无
作用:初始化GLFW库
glfwCrateWindow
参数:宽、高、标题、剩下两个为NULL即可
作用:创建窗体
glfwWindowShouldClose
参数:窗体
作用:是否需要关闭窗体
glfwMakeContextCurrent
参数:窗体
作用:通过上下文创建窗体
glfwSwapBuffers
参数:窗体
作用:展示窗体
glfwPollEvents
参数:无
作用:检查操作系统返回的信息
processInput
参数:窗体
作用:键盘按键控制,例如,按Esc键关闭窗体
framebuffer_size_callback
参数:窗体
作用:改变窗体时的回调函数,窗体进行相应改变。
更多OpenGL知识:现代OpenGL入门教程
有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。