LearnOpenGL1.0创建窗口

来源:互联网 发布:php酒店管理系统 编辑:程序博客网 时间:2024/05/18 07:33

source : 1 2

一、准备

 可使用GLFW库、SDL库或者freeGLUT库。 其中可用glew库或glad库setup the OpenGL Function pointers

二、初始化并配置GLFW

glfwInit()glfwWindowHint()

三、创建窗口对象

glfwWindow *windowglfwCreatWindow()glfwTerminate()glfwMakeContextCurrent(window)

四、设置glew管理方式并初始化

glewExperimental = GL_TRUE;glewInit()

五、定义视窗(Viewport)尺寸

glfwGetFramebufferSize()glViewport()

六、Game loop

 // 程序循环while(!glfwWindowShouldClose(window)){    // 检查事件    glfwPollEvents();    // 渲染指令    ...    // 交换缓冲    glfwSwapBuffers(window);}

七、设置窗口回调

glfwSetKeyCallback()void key_callback(GLFWwindow* window, int key,                   int scancode, int action, int mode);

八、源代码

#include <iostream>#include "GL\glew.h"#include "GLFW\glfw3.h"// Function prototypesvoid key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);// Window dimensionsconst GLuint WIDTH = 800, HEIGHT = 600;// The MAIN function, from here we start the application and run the game loopint main(){    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;    // Init GLFW    glfwInit();    // Set all the required options for GLFW    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //核心模式    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //不可调整大小    // Create a GLFWwindow object that we can use for GLFW's functions    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);    if (window == nullptr)    {        std::cout << "Failed to create GLFW window" << std::endl;        glfwTerminate();        return -1;    }    glfwMakeContextCurrent(window);    // Set the required callback functions    glfwSetKeyCallback(window, key_callback);    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions    glewExperimental = GL_TRUE;    // Initialize GLEW to setup the OpenGL Function pointers    if (glewInit() != GLEW_OK)    {        std::cout << "Failed to initialize GLEW" << std::endl;        return -1;    }    // Define the viewport dimensions    int width, height;    glfwGetFramebufferSize(window, &width, &height);    glViewport(0, 0, width, height);    // Game loop    while (!glfwWindowShouldClose(window))    {        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions        glfwPollEvents();        // Render        // Clear the colorbuffer        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);        glClear(GL_COLOR_BUFFER_BIT);        // Swap the screen buffers        glfwSwapBuffers(window);    }    // Terminate GLFW, clearing any resources allocated by GLFW.    glfwTerminate();    return 0;}// Is called whenever a key is pressed/released via GLFWvoid key_callback(GLFWwindow* window, int key, int scancode, int action, int mode){    std::cout << key << std::endl;    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)        glfwSetWindowShouldClose(window, GL_TRUE);}