VS2015 OpenGL开发环境简单配置 glfw + glad

来源:互联网 发布:如何查看手机网络制式 编辑:程序博客网 时间:2024/05/22 04:35

学习OpenGL参考的一个教程是https://learnopengl-cn.github.io
此教程是基于glfw(窗口) + glad(跨平台),下面介绍VS2015一种简单的环境配置方法:

1. 新建项目

简单的建一个空项目即可,添加一个空白的cpp文件。

2. 添加NupenGL Core库

添加方法为:VS->工具->NuGet包管理器->程序包管理控制台
然后在下方的程序包管理控制台中输入Install-Package nupengl.core指令回车即可
此时,你的这个项目就已经配置好了glut glfw glew

3. 配置glad

如果不需要glad就可以跳过这一步,但是上文的教程用到了这个glad,所以也要顺便配置一下:

打开GLAD的在线服务,将语言(Language)设置为C/C++,在API选项中,选择3.3以上的OpenGL(gl)版本(我们的教程中将使用3.3版本,但更新的版本也能正常工作)。之后将模式(Profile)设置为Core,并且保证生成加载器(Generate
a
loader)的选项是选中的。现在可以先(暂时)忽略拓展(Extensions)中的内容。都选择完之后,点击生成(Generate)按钮来生成库文件。

GLAD现在应该提供给你了一个zip压缩文件,右键另存到本地,然后讲解压得到的include里面的两个头文件目录(glad和KHR)放入以下目录:刚才新建的工程目录\packages\nupengl.core.0.1.0.1\build\native\include,然后将src中的glad.c加入工程:
这里写图片描述

经过前面的这些步骤之后,其实环境就已经配置好了

4. 测试

#include <glad/glad.h>#include <GLFW/glfw3.h>#include <iostream>void framebuffer_size_callback(GLFWwindow* window, int width, int height);void processInput(GLFWwindow *window);// settingsconst unsigned int SCR_WIDTH = 800;const unsigned int SCR_HEIGHT = 600;int main(){    // glfw: initialize and configure    // ------------------------------    glfwInit();    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);#ifdef __APPLE__    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X#endif    // glfw window creation    // --------------------    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);    if (window == NULL)    {        std::cout << "Failed to create GLFW window" << std::endl;        glfwTerminate();        return -1;    }    glfwMakeContextCurrent(window);    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);    // glad: load all OpenGL function pointers    // ---------------------------------------    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))    {        std::cout << "Failed to initialize GLAD" << std::endl;        return -1;    }        // render loop    // -----------    while (!glfwWindowShouldClose(window))    {        // input        // -----        processInput(window);        // render        // ------        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);        glClear(GL_COLOR_BUFFER_BIT);        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)        // -------------------------------------------------------------------------------        glfwSwapBuffers(window);        glfwPollEvents();    }    // glfw: terminate, clearing all previously allocated GLFW resources.    // ------------------------------------------------------------------    glfwTerminate();    return 0;}// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly// ---------------------------------------------------------------------------------------------------------void processInput(GLFWwindow *window){    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)        glfwSetWindowShouldClose(window, true);}// glfw: whenever the window size changed (by OS or user resize) this callback function executes// ---------------------------------------------------------------------------------------------void framebuffer_size_callback(GLFWwindow* window, int width, int height){    // make sure the viewport matches the new window dimensions; note that width and     // height will be significantly larger than specified on retina displays.    glViewport(0, 0, width, height);}

如果配置成功,应该会出现一个
这里写图片描述
这样的窗口,大功告成!

不过我不知道如何将图中的黑色控制台窗口关掉,如有知道请告知,谢谢。