OpenGL编程-窗口初始化

    xiaoxiao2021-03-25  69

    本系列博客主要记录OpenGL可编程管线的学习,用的OpenGL4.3,自己的电脑显卡最高支持到4.4版本, 查看电脑显卡对OpenGL可用工具为OpenGL Extetions Viewer。练习中用到的窗口和OpenGL上下文创建用的是GLFW开源库,当然还有其他的GLUT,SDL,SFML库。这些库的源码编译以及安装可以自行百度或者谷歌。项目中需要用到opengl32.lib,如果你是Windows平台,opengl32.lib已经包含在Microsoft SDK里了,在VS开发环境添加就好。 到这里,我们仍然有一件事要做。因为OpenGL只是一个标准/规范,具体的实现是由驱动开发商针对特定显卡实现的。由于OpenGL驱动版本众多,它大多数函数的位置都无法在编译时确定下来,需要在运行时查询。任务就落在了开发者身上,开发者需要在运行时获取函数地址并将其保存在一个函数指针中供以后使用。GLEW是OpenGL Extension Wrangler Library的缩写,它简化此过这个过程,开发过程中只要我们加入GLEW,然后在初始化的过程中,初始化GLEW,我们就能使用OpenGL的各种高级特性了。 下面的代码用来初始化一个窗口,有些代码后面都有添加注释。

    int main(int argc, char** argv) { std::cout << "Starting GLFW context, OpenGL 4.3" << std::endl; glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); //GL版本号 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //可编程管线 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不能拉伸窗口 GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "LearnOpenGL", nullptr, nullptr); if (window == nullptr) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); //释放GLFW的资源 return -1; } glfwMakeContextCurrent(window); //当前窗口设为设备上下文 //glfwSetKeyCallback(window, key_callback); //响应键盘消息 glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { std::cout << "Failed to initialize GLEW" << std::endl; glfwTerminate(); return -1; } int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); init(); while (!glfwWindowShouldClose(window)) //检查GLFW是否退出 { glfwPollEvents(); glClearColor(0.0f, 0.0f, 0.3f, 1.0f); //设置颜色缓存区颜色 glClear(GL_COLOR_BUFFER_BIT); //glBindVertexArray(VAOs[Triangle]); //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); //glBindVertexArray(0); glfwSwapBuffers(window); } glfwTerminate(); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-33107.html

    最新回复(0)