OpenGL

OpenGL VS2017 开发环境搭建(GLFW+GLEW)

2018-11-16  本文已影响78人  Lee_09
这个博客已经写的非常好了:OpenGL 开发环境配置:Visual Studio 2017 + GLFW + GLEW

我把上面的链接内容摘要一下:

1.CMake下载地址:https://cmake.org/download/
2.GLFW下载地址 :http://www.glfw.org/download.html
3.GLEW下载地址 :http://glew.sourceforge.net/

如果没有下载预编译文件,那么需要使用CMake来生成对应.sln并在vs2017中编译生成相应静态链接库.lib。
执行过程:打开CMake -> source code一栏里填包含CMakeList.txt的目标文件夹 -> 填写build目录 -> 点Config -> 点Generate -> 打开对应.sln -> 设置为生成静态链接库编译生成.lib。

1.头文件:将GLFW和GLEW中include内的文件(夹)拷贝到D:\Program Files\OpenGL\headers;
2.lib文件:将GLFW和GLEW编译生成的静态链接库拷贝到D:\Program Files\OpenGL\libs;

1.VC++目录->包含目录:添加 D:\Program Files\OpenGL\headers;
2.VC++目录->库目录:添加D:\Program Files\OpenGL\libs;
3.链接器->输入:添加opengl32.lib,glfw3.lib和glew32d.lib。

#include <iostream>

// GLEW
#define GLEW_STATIC    
#include <GL/glew.h>

//GLFW    
#include <GLFW/glfw3.h>    

// 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    
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, "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    
    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 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);
}
上一篇下一篇

猜你喜欢

热点阅读