OpenGL学习笔记-2:基于GLFW在windows系统下创建一个OpenGL窗口
一、打开VS新建一个空工程
二、引入GLFW库
头文件: glfw3native.h
glfw3.h
库文件:glfw3.lib
三、引入GLEW库
头文件:glew.h
库文件:glew32s.lib
四、要引入 opengl32.lib 库
否则VS工程会报错:
1>glew32s.lib(glew.obj) : error LNK2019: unresolved external symbol __imp__glGetIntegerv@8 referenced in function _glewContextInit@0 1>glew32s.lib(glew.obj) : error LNK2019: unresolved external symbol __imp__glGetString@4 referenced in function _glewContextInit@0 1>glew32s.lib(glew.obj) : error LNK2019: unresolved external symbol __imp__wglGetCurrentDC@0 referenced in function _wglewGetExtension@4 1>glew32s.lib(glew.obj) : error LNK2019: unresolved external symbol __imp__wglGetProcAddress@4 referenced in function __glewInit_GL_3DFX_tbuffer
五、main.cpp代码:
#define GLEW_STATIC
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>
using namespace std;
int main()
{ //init glfw
glfwInit();
//glfwWindowHint funciton
//@param 1 : settings
//@param 2 : setting value int
//set opengl version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//set the opengl use core-profil mode
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//The OS only supports forward-compatible core profile contexts for OpenGL versions 3.2 and later. Before creating an OpenGL context of version 3.2 or later you must set the GLFW_OPENGL_FORWARD_COMPAT and GLFW_OPENGL_PROFILE hints accordingly. OpenGL 3.0 and 3.1 contexts are not supported at all on macOS.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//glfwCreateWindow function
//@param 1 : the width of window
//@param 2 : the height of window
//@param 3 : UTF-8 encoded window title
//@param 4 : The monitor to use for full screen mode, or NULL for windowed mode.
//@param 5 : The window whose context to share resources with, or NULL to not share resources.
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
//glfwCreateWindow function
//makes the OpenGL or OpenGL ES context of the specified window current on the calling thread.
glfwMakeContextCurrent(window);
//有些显卡的驱动程序不能正确给出所支持的扩展信息,导致GLEW不能正确获取某些函数的入口地址
glewExperimental = GLU_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to glewInit" << std::endl;
return -1;
}
//左下角(0.0)
glViewport(0, 0, 800, 600);
while (!glfwWindowShouldClose(window))
{
//glfwSwapBuffers function
//This function swaps the front and back buffers of the specified window when rendering with OpenGL or OpenGL ES.
glfwSwapBuffers(window);
//glfwPollEvents function
//This function processes only those events that are already in the event queue and then returns immediately.
glfwPollEvents();
}
//glfwTerminate function
//This function destroys all remaining windows and cursors, restores any modified gamma ramps and frees any other allocated resources.
glfwTerminate();
return 0;
}
六、编译执行会显示一个黑画面
