Windows10 OpenGL安装及配置

一、环境

  • Windows 10 64位家庭版
  • vs版本:Visual Studio 2017

二、配置OpenGL所需

  • GLFW
  • GLAD

三、步骤

  1.配置GLFW

  • 进入官网下载GLFW并解压:GLFW下载
  • 这里选择32位的,因为64位的可能有些问题
  • Windows10 OpenGL安装及配置
  • 进入VS2017创建空项目,在项目文件夹下创建includes和libs两个文件夹(如创建项目OpenGL,则为OpenGL/OpenGL/includes)
  • 将解压文件中lib-vc2017文件夹下的.lib文件拷贝进刚刚创建的libs下,同时将include下的.h文件拷贝进刚刚创建的includes下
  • 在项目名处右键->属性->VC++目录->引用目录->编辑:粘贴项目文件夹下的.h文件所在目录
  • Windows10 OpenGL安装及配置
  • 同理,库目录->编辑:粘贴项目文件夹下的.lib文件所在目录
  • Windows10 OpenGL安装及配置
  • 接着将为项目添加依赖项:仍然是属性->链接器->输入->附加依赖项->编辑,将opengl32.lib,glfw3.lib,msvcrt.lib添加进去
  • Windows10 OpenGL安装及配置

  2.GLAD

  •  进入GLAD官网:GLAD下载
  • Language选C\C++,Profile选core,version根据需要选择(3.3版本及以上),然后GRNERATE,之后选择zip文件下载并解压
  • Windows10 OpenGL安装及配置
  • 将include下的.h文件全部拷贝进项目文件夹下的includes下,将src下的glad.c拷贝进项目文件夹下
  • 然后进入vs,添加源文件->现有项,将glad.c添加进项目
  • Windows10 OpenGL安装及配置
  • 至此库文件配置完成,接下来测试是否能正常使用OpenGL

  3.测试OpenGL

  • 创建源文件,首先需要引用glad.h和glfw3.h文件
  • 因为使用的是自己添加的文件,这里引用需要使用“ “而非<>,使用” “时,它会自动出现你所添加的目录

#include"includes/glad.h" #include"includes/glfw3.h" #include <iostream> void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); // settings const 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); // 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); }

  • 出现图示窗口即可正常使用OpenGL
  • Windows10 OpenGL安装及配置
  • 若出现报错:默认库MSVCRTD与……冲突,属性->链接器->输入->忽略特定默认库->编辑,将MSVCRTD.lib添加进去即可

最后推荐一个学习OpenGL的网站,教程很舒服,本博客的测试源码也出自此:

https://learnopengl-cn.github.io/

Windows10 OpenGL安装及配置

上一篇:初次使用Dubbo,zookeeper(windows单机版)


下一篇:C#编码规范