一.GLFW集成

  1. 官网下载二进制文件 GLFW
    x86平台请下载32-bit Windows binaries
    下载source package,提取当中的include文件夹
  2. 新建一个空的win32工程,将32-bit Windows binaries下载得到的压缩包内的glfw3.lib拷贝到你自己的工作目录.添加这些目录(需要VS搜索库和include文件的地方),我们首先进入Project Properties(工程属性,在解决方案窗口里右键项目),然后选择VC++ Directories(VC++ 目录)选项卡(如下图)。在下面的两栏添加目录:WIN10 OpenGL GLFW GLAD 开发环境搭建 - 图1

最后需要在Linker(链接器)选项卡里的Input(输入)选项卡里添加glfw3.lib这个文件:WIN10 OpenGL GLFW GLAD 开发环境搭建 - 图2

二.配置GLAD

GLAD是一个开源的库,它能解决我们上面提到的那个繁琐的问题。GLAD的配置与大多数的开源库有些许的不同,GLAD使用了一个在线服务。在这里我们能够告诉GLAD需要定义的OpenGL版本,并且根据这个版本加载所有相关的OpenGL函数。

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

GLAD现在应该提供给你了一个zip压缩文件,包含两个头文件目录,和一个glad.c文件。将两个头文件目录(glad和KHR)复制到你的Include文件夹中(或者增加一个额外的项目指向这些目录),并添加glad.c文件到你的工程中。

三.测试代码

  1. #include "stdafx.h"
  2. #include <glad/glad.h>
  3. #include <GLFW/glfw3.h>
  4. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  5. void processInput(GLFWwindow *window);
  6. // settings
  7. const unsigned int SCR_WIDTH = 800;
  8. const unsigned int SCR_HEIGHT = 600;
  9. int main()
  10. {
  11. // glfw: initialize and configure
  12. // ------------------------------
  13. glfwInit();
  14. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  15. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  16. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  17. #ifdef __APPLE__
  18. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
  19. #endif
  20. // glfw window creation
  21. // --------------------
  22. GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
  23. if (window == NULL)
  24. {
  25. std::cout << "Failed to create GLFW window" << std::endl;
  26. glfwTerminate();
  27. return -1;
  28. }
  29. glfwMakeContextCurrent(window);
  30. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  31. // glad: load all OpenGL function pointers
  32. // ---------------------------------------
  33. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  34. {
  35. std::cout << "Failed to initialize GLAD" << std::endl;
  36. return -1;
  37. }
  38. // render loop
  39. // -----------
  40. while (!glfwWindowShouldClose(window))
  41. {
  42. // input
  43. // -----
  44. processInput(window);
  45. // render
  46. // ------
  47. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
  50. // -------------------------------------------------------------------------------
  51. glfwSwapBuffers(window);
  52. glfwPollEvents();
  53. }
  54. // glfw: terminate, clearing all previously allocated GLFW resources.
  55. // ------------------------------------------------------------------
  56. glfwTerminate();
  57. return 0;
  58. }
  59. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
  60. // ---------------------------------------------------------------------------------------------------------
  61. void processInput(GLFWwindow *window)
  62. {
  63. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  64. glfwSetWindowShouldClose(window, true);
  65. }
  66. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
  67. // ---------------------------------------------------------------------------------------------
  68. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  69. {
  70. // make sure the viewport matches the new window dimensions; note that width and
  71. // height will be significantly larger than specified on retina displays.
  72. glViewport(0, 0, width, height);
  73. }

完整源代码可以在这里找到
完整测试工程可以在这里下载