Windows10 OpenGL安裝及配置


一、環境

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

二、配置OpenGL所需

  • GLFW
  • GLAD

三、步驟

  1.配置GLFW

  • 進入官網下載GLFW並解壓:GLFW下載
  • 這里選擇32位的,因為64位的可能有些問題
  • 進入VS2017創建空項目,在項目文件夾下創建includes和libs兩個文件夾(如創建項目OpenGL,則為OpenGL/OpenGL/includes)
  • 將解壓文件中lib-vc2017文件夾下的.lib文件拷貝進剛剛創建的libs下,同時將include下的.h文件拷貝進剛剛創建的includes下
  • 在項目名處右鍵->屬性->VC++目錄->引用目錄->編輯:粘貼項目文件夾下的.h文件所在目錄
  • 同理,庫目錄->編輯:粘貼項目文件夾下的.lib文件所在目錄
  • 接着將為項目添加依賴項:仍然是屬性->鏈接器->輸入->附加依賴項->編輯,將opengl32.lib,glfw3.lib,msvcrt.lib添加進去

  2.GLAD

  •  進入GLAD官網:GLAD下載
  • Language選C\C++,Profile選core,version根據需要選擇(3.3版本及以上),然后GRNERATE,之后選擇zip文件下載並解壓
  • 將include下的.h文件全部拷貝進項目文件夾下的includes下,將src下的glad.c拷貝進項目文件夾下
  • 然后進入vs,添加源文件->現有項,將glad.c添加進項目
  • 至此庫文件配置完成,接下來測試是否能正常使用OpenGL

  3.測試OpenGL

  • 創建源文件,首先需要引用glad.h和glfw3.h文件
  • 因為使用的是自己添加的文件,這里引用需要使用“ “而非<>,使用” “時,它會自動出現你所添加的目錄
  •   1 #include"includes/glad.h"
      2 
      3 #include"includes/glfw3.h"
      4 
      5 #include <iostream>
      6 
      7 void framebuffer_size_callback(GLFWwindow* window, int width, int height);
      8 
      9 void processInput(GLFWwindow *window); // settings
     10 
     11 const unsigned int SCR_WIDTH = 800;
     12 
     13 const unsigned int SCR_HEIGHT = 600;
     14 
     15 int main() {
     16 
     17     // glfw: initialize and configure
     18 
     19     // ------------------------------
     20 
     21     glfwInit();
     22 
     23     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
     24 
     25     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     26 
     27     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     28 
     29 #ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
     30 
     31     // uncomment this statement to fix compilation on OS X
     32 
     33 #endif
     34 
     35 // glfw window creation
     36 
     37 // --------------------
     38 
     39     GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
     40 
     41     if (window == NULL)
     42 
     43     {
     44 
     45         std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1;
     46 
     47     }
     48 
     49     glfwMakeContextCurrent(window);
     50 
     51     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
     52 
     53     // glad: load all OpenGL function pointers
     54 
     55     // ---------------------------------------
     56 
     57     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
     58 
     59     {
     60 
     61         std::cout << "Failed to initialize GLAD" << std::endl; return -1;
     62 
     63     }
     64 
     65     // render loop
     66 
     67     // -----------
     68 
     69     while (!glfwWindowShouldClose(window))
     70 
     71     {
     72 
     73         // input // -----
     74 
     75         processInput(window);
     76 
     77         // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
     78 
     79         // -------------------------------------------------------------------------------
     80 
     81         glfwSwapBuffers(window);
     82 
     83         glfwPollEvents();
     84     }
     85 
     86     // glfw: terminate, clearing all previously allocated GLFW resources. 
     87     // ------------------------------------------------------------------ 
     88     glfwTerminate(); return 0; 
     89 } 
     90 // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly 
     91 // --------------------------------------------------------------------------------------------------------- 
     92 void processInput(GLFWwindow *window) 
     93 { 
     94     if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
     95         glfwSetWindowShouldClose(window, true); 
     96 }
     97  // glfw: whenever the window size changed (by OS or user resize) this callback function executes 
     98  // --------------------------------------------------------------------------------------------- 
     99 void framebuffer_size_callback(GLFWwindow* window, int width, int height) 
    100 { 
    101     // make sure the viewport matches the new window dimensions; note that width and 
    102     // height will be significantly larger than specified on retina displays. 
    103     glViewport(0, 0, width, height); 
    104 }

     

  • 出現圖示窗口即可正常使用OpenGL
  • 若出現報錯:默認庫MSVCRTD與……沖突,屬性->鏈接器->輸入->忽略特定默認庫->編輯,將MSVCRTD.lib添加進去即可

最后推薦一個學習OpenGL的網站,教程很舒服,本博客的測試源碼也出自此:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM