[toc]html
#搭建ImGui例子中的第一個OpenGl2環境 ##一丶搭建OpenGL2環境git
咱們學習ImGui.能夠看到.有幾個例子 分別是:github
OpenGl2 OpenGl3 Directx11 Directx10 Directx9 由於要學習例子或者什麼.最好移植到本身的工程中進行學習.app
因此先把第一個OpenGl2的移植寫一下.後面會將5個例子都會移植過來.而後進行學習.框架
首先下載 OpenGl須要的庫. 也就是說GLFW.oop
###1.下載GLFW庫學習
官網下載: https://www.glfw.org/download.html 以下圖:測試
根據你當前項目的選擇.下載32位或者64位.ui
###2.解壓GLFW庫.命令行
下載以後解壓. 以下圖:
裏面咱們須要關注的是 include 頭文件 以及 lib-vcxxxx 其中跟你你VS的環境.進行選擇. 好比咱們的是VS2013. 我就選擇vs2013.
###3.將GLFW庫拷貝到你的工程文件下.
這一步就重要了. 首先咱們須要的是 include目錄 以及 lib-vcxxx目錄. 咱們拷貝到咱們的目錄下.
這裏我新建一個文件夾.名字就叫作GLFW. 咱們裏面就是存放的咱們拷貝的文件.
###4.配置到Vs2013中.
####4.1配置屬性中的附加包含目錄 這一步很重要了.
首先打開工程項目屬性.找到C/C++一欄.看到附加包含目錄. 設置爲咱們剛剛的 文件夾下的include
這一步重要的地方就在於.咱們的文件是放在哪裏.若是你放在跟我同樣的目錄下.直接這樣引用. 可是上面還有一層目錄. 若是你放在了上一層目錄. 你這裏就要寫成 ..\GLFW\include 總的來講就是路徑配置問題.
####4.2配置屬性中的鏈接器常規屬性的附加庫目錄
這一個跟咱們同樣.設置lib所在的目錄便可.
以下
####4.3配置屬性中的輸入中的附加依賴項.
配置的命令行爲:
opengl32.lib;glfw3.lib;%(AdditionalDependencies)
以下:
此時咱們的OpenGl就搭建好了.使用咱們的代碼進行測試.
###5.測試OpenGl是否搭建成功
#include <stdio.h> #include <Windows.h> #include <tchar.h> #ifdef __APPLE__ #define GL_SILENCE_DEPRECATION #endif #include <GLFW/glfw3.h> //注意這個位置.你設置了目錄直接這樣引用就行.GLFW不是咱們創建的文件夾.而是include裏面的GLFW static void glfw_error_callback(int error, const char* description) { fprintf(stderr, "Glfw Error %d: %s\n", error, description); } int WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) //int main() { // Setup window //glfwSetErrorCallback(glfw_error_callback); //初始化OpenGl if (!glfwInit()) return 1; //建立OpenGl窗口 GLFWwindow* window = glfwCreateWindow(1280, 720, "IBinary", NULL, NULL); if (window == NULL) return 1; //設置上下文.以及進行交換. glfwMakeContextCurrent(window); glfwSwapInterval(1); // Enable vsync //開始進行循環.判斷gl窗口是否關閉.不關閉一直循環. while (!glfwWindowShouldClose(window)) { glfwPollEvents(); //建立接收事件,至關於消息循環.若是不加則不響應消息. //這些代碼能夠省略.加了這些代碼是設置Open內部的窗口顏色什麼的 int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glClearColor(0.45f, 0.55f, 0.60f, 1.00f); glClear(GL_COLOR_BUFFER_BIT); glfwMakeContextCurrent(window); glfwSwapBuffers(window); // Cleanup } glfwDestroyWindow(window); glfwTerminate(); return 0; }
運行:
這樣咱們的一個OpenGl2的窗口就已經搭建好了.
可是咱們說了.是要配合ImGui.因此咱們繼續進行配置ImGui.
##二丶OpenGL2 跟ImGui相結合.
###2.1下載ImGui Imgui是一個很簡單的庫.若是想使用.咱們須要去Github進行下載. https://github.com/ocornut/imgui 上面也有使用說明
以下:
他告訴了咱們ImGui須要的CPP以及頭文件. 以及Imgui如何編寫繪製代碼啊
ImGui::Text("HelloWorld");
這樣咱們就在窗口上繪製了一段話.
###2.2.配置ImGui到OpenGl2中.
首先,先把你解壓的Imgui下的須要的頭文件都拷貝到咱們工程下面.固然你能夠配置文件夾. 跟上面同樣.配置咱們的目錄.
以下:
而後將.h文件跟.cpp文件加載到咱們工程中.
查看Imgui OpenGui2的例子.看看還須要哪些頭文件.
能夠看到.還須要4個支持OpenGl2的額外文件.咱們也拷貝過來加載到工程中.
拷貝到工程以後能夠拷貝ImGui例子代碼進行測試了
代碼以下:
#include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl2.h" #include <stdio.h> #include <Windows.h> #include <tchar.h> #ifdef __APPLE__ #define GL_SILENCE_DEPRECATION #endif #include <GLFW/glfw3.h> #if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) #pragma comment(lib, "legacy_stdio_definitions") #endif int WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { if (!glfwInit()) //初始化OpenGl return 1; //建立OpenGl窗口 GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL2 example", NULL, NULL); if (window == NULL) return 1; //設置OpenGl山下文 glfwMakeContextCurrent(window); glfwSwapInterval(1); // Enable vsync // 設置ImGui舌下文. IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; //設置顏色風格 ImGui::StyleColorsDark(); // Setup Platform/Renderer bindings ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL2_Init(); ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); // Main loop while (!glfwWindowShouldClose(window)) { glfwPollEvents(); // Start the Dear ImGui frame 啓動IMgui Frame框架. ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); { //開始繪製ImGui ImGui::Begin("IBinary Windows"); // Create a window called "Hello, world!" and append into it. ImGui::Text("IBinary Blog"); //ImGui::SameLine(); ImGui::Indent(); //另起一行製表符開始繪製Button ImGui::Button("2222", ImVec2(100, 50)); ImGui::End(); } // 3. Show another simple window. // Rendering ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); //必須在繪製完Open以後接着繪製Imgui //glUseProgram(last_program); glfwMakeContextCurrent(window); glfwSwapBuffers(window); } // Cleanup ImGui_ImplOpenGL2_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(window); glfwTerminate(); return 0; }
啓動以後
若是你啓動ImGui的例子.會發現一直有個控制檯窗口.緣由就是它是以main啓動.咱們改爲窗口程序啓動便可.
原文出處:https://www.cnblogs.com/iBinary/p/10888911.html