• GLFW+GLEW搭建opengl环境(备忘)


    使用VS2017社区版本(免费版)

    下载GLFW和GLEW源码。

    使用CMAKE生成工程文件

    打开右击GLFW和GLEW项目编译

    GLFW默认是静态库

    编译GLEW时调整为静态库。将生成的lib和源码中的include文件夹放好,新建空的C++项目。在项目属性设置好路径。

    opengl32.lib包含在window sdk 10中了。不需要单独编译。

     

    为了支持中文需要使用UTF-8无bom编码。测试代码如下。

    #include <iostream>    
    // GLEW    
    #define GLEW_STATIC    
    #include <GL/glew.h>    
    #include <Windows.h>
    // GLFW    
    #include <GLFW/glfw3.h>    
    #pragma comment(lib,"winmm.lib") // 告诉连接器与这个库连接,因为我们要播放多媒体声音 
    #pragma comment(linker, "/subsystem:"windows" /entry:"mainCRTStartup"" )
    //#define DEBUG
    // Function prototypes    
    void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
    
    // Window dimensions    
    const GLuint WIDTH = 800, HEIGHT = 600;
    
    // The MAIN function, from here we start the application and run the game loop
    //#ifdef DEBUG
    //int main()
    //#else
    //int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    //#endif
    int 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,"墨迹",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    
        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);
            Sleep(20);
        }
    
        // Terminate GLFW, clearing any resources allocated by GLFW.    
        glfwTerminate();
        return 0;
    }
    
    // Is called whenever a key is pressed/released via GLFW    
    void 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);
    }

    效果如下

    程序400kb,无需动态库。

    单独一个程序的话会比动态库要小。

  • 相关阅读:
    Effective C++ -----条款29:为“异常安全”而努力是值得的
    Effective C++ -----条款28:避免返回handles指向对象内部成分
    Effective C++ -----条款27:尽量少做转型动作
    Effective C++ -----条款26:尽可能延后变量定义式的出现时间
    Effective C++ -----条款25:考虑写出一个不抛异常的swap函数
    Effective C++ -----条款24:若所有参数皆需类型转换,请为此采用non-member函数
    HGE 第一个程序
    Help him http://acm.hdu.edu.cn/showproblem.php?pid=5059
    C 语言实例
    C 语言实例
  • 原文地址:https://www.cnblogs.com/pojdd/p/10073091.html
Copyright © 2020-2023  润新知