测试代码
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
using std::string;
void processInput(GLFWwindow* window);
void cbFramebufferSize(GLFWwindow* window, int width, int height);
void InitOpenGL();
GLFWwindow* CreateOpenGLWindow(int width, int height, const string& title);
bool LoadFunctionPointers();
void RenderLoop(GLFWwindow* window);
int main()
{
InitOpenGL();
GLFWwindow* window = CreateOpenGLWindow(800, 600, "Test OpenGL");
if (!window)
return -1;
if (!LoadFunctionPointers())
return -1;
RenderLoop(window);
return 0;
}
void processInput(GLFWwindow* window)
{
if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE))
glfwSetWindowShouldClose(window, true);
}
void cbFramebufferSize(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void InitOpenGL()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
GLFWwindow* CreateOpenGLWindow(int width, int height, const string& title)
{
GLFWwindow* window = glfwCreateWindow(800, 600, title.c_str(), NULL, NULL);
if (!window)
{
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, cbFramebufferSize);
return window;
}
bool LoadFunctionPointers()
{
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
glfwTerminate();
return false;
}
return true;
}
void RenderLoop(GLFWwindow* window)
{
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.1f, 0.2f, 0.3f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}