title: How to use opengl glfw3 render in a imgui window?
body: <p>I am learning OpenGL by glfw3 lib and I am using imgui to make a interesting application.</p>
  
  <p>I want to make a little Engine or CAD tool.</p>
  
  <p><strong>Problem is I can not render what I want in a imgui window.</strong></p>
  
  <p>I searched google and found below links:</p>
  
  <ol>
  <li><a href="https://gamedev.stackexchange.com/questions/150214/render-in-a-imgui-window">https://gamedev.stackexchange.com/questions/150214/render-in-a-imgui-window</a></li>
  <li><a href="https://gamedev.stackexchange.com/questions/140693/how-can-i-render-an-opengl-scene-into-an-imgui-window">https://gamedev.stackexchange.com/questions/140693/how-can-i-render-an-opengl-scene-into-an-imgui-window</a></li>
  </ol>
  
  <p>I tried about two days and I have rendered a triangle in a imgui window.<br>
  <strong>However when I try to render content as this <a href="https://learnopengl.com/video/getting-started/coordinate_system_depth.mp4" rel="nofollow noreferrer">link</a>(<a href="https://learnopengl.com/Getting-started/Coordinate-Systems" rel="nofollow noreferrer">Tutorial</a> and <a href="https://learnopengl.com/code_viewer_gh.php?code=src/1.getting_started/6.2.coordinate_systems_depth/coordinate_systems_depth.cpp" rel="nofollow noreferrer">Code</a>) shows me.</strong></p>
  
  <p><strong>The box in imgui window won't spin!!!</strong></p>
  
  <p>I tried serial days and I am going to give up, I really need your help.</p>
  
  <p><code>#define __abc__</code> is a value to test.
  Xhader.h just is the header file in the tutorial, also .vs and .fs files.
  the difference is tutorial uses glad.h and I use glew.h</p>
  
  <pre><code>#define __abc__
  #define STB_IMAGE_IMPLEMENTATION
  #include &lt;iostream&gt;
  #include &lt;GL/glew.h&gt;
  #include &lt;GLFW/glfw3.h&gt;
  #include &lt;stb_image.h&gt;
  
  #include &lt;glm/glm.hpp&gt;
  #include &lt;glm/gtc/matrix_transform.hpp&gt;
  #include &lt;glm/gtc/type_ptr.hpp&gt;
  
  #include "imgui.h"
  #include "imgui_impl_glfw.h"
  #include "imgui_impl_opengl3.h"
  
  #include "Xhader.h"
  
  
  
  void framebuffer_size_callback(GLFWwindow* window, int width, int height);
  void processInput(GLFWwindow* window);
  
  GLFWwindow* Proinit();
  void imguiSetup(GLFWwindow* window);
  
  // settings
  const unsigned int SCR_WIDTH = 800;
  const unsigned int SCR_HEIGHT = 600;
  
  
  int main()
  {
      GLFWwindow* window = Proinit();
      imguiSetup(window);
  
      int view3d_width = 100;
      int view3d_height = 200;
  
  
  
      // configure global opengl state
      // -----------------------------
      glEnable(GL_DEPTH_TEST);
  
      // build and compile our shader zprogram
      // ------------------------------------
      Xhader ourShader("shader/vs.txt", "shader/fs.txt");
  
      // set up vertex data (and buffer(s)) and configure vertex attributes
      // ------------------------------------------------------------------
      float vertices[] = {
              -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
              0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
              0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
              0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
              -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
              -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
  
              -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
              0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
              0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
              0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
              -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
              -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
  
              -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
              -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
              -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
              -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
              -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
              -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  
              0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
              0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
              0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
              0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
              0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
              0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
  
              -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
              0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
              0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
              0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
              -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
              -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
  
              -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
              0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
              0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
              0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
              -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
              -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
      };
  
      GLuint VBO;
      glGenBuffers(1, &amp;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
      GLuint VAO;
      glGenVertexArrays(1, &amp;VAO);
      glBindVertexArray(VAO);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
      glEnableVertexAttribArray(1);
  
  
      //---------------------------------------    FBO    -----------------------------//
  #ifdef __abc__
      unsigned int fbo;
      glGenFramebuffers(1, &amp;fbo);
      glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  
      GLuint TOF;
      glGenTextures(1, &amp;TOF);
      glBindTexture(GL_TEXTURE_2D, TOF);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 400, 300, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  #else
      ;
  #endif
  
  
      // load and create a texture 
      // -------------------------
      unsigned int texture1, texture2;
      // texture 1
      // ---------
      glGenTextures(1, &amp;texture1);
      glBindTexture(GL_TEXTURE_2D, texture1);
      // set the texture wrapping parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      // set texture filtering parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      // load image, create texture and generate mipmaps
      int width, height, nrChannels;
      stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
      std::string s1 = "resources/textures/container.jpg";
      std::string s2 = "resources/textures/awesomeface.png";
      unsigned char* data = stbi_load(s1.c_str(), &amp;width, &amp;height, &amp;nrChannels, 0);
      if (data)
      {
          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
          glGenerateMipmap(GL_TEXTURE_2D);
      }
      else
      {
          std::cout &lt;&lt; "Failed to load texture" &lt;&lt; std::endl;
      }
      stbi_image_free(data);
      // texture 2
      // ---------
      glGenTextures(1, &amp;texture2);
      glBindTexture(GL_TEXTURE_2D, texture2);
      // set the texture wrapping parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      // set texture filtering parameters
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      // load image, create texture and generate mipmaps
      data = stbi_load(s2.c_str(), &amp;width, &amp;height, &amp;nrChannels, 0);
      if (data)
      {
          // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
          glGenerateMipmap(GL_TEXTURE_2D);
      }
      else
      {
          std::cout &lt;&lt; "Failed to load texture" &lt;&lt; std::endl;
      }
      stbi_image_free(data);
  
      // tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
      // -------------------------------------------------------------------------------------------
      ourShader.use();
      ourShader.setInt("texture1", 0);
      ourShader.setInt("texture2", 1);
  
  
      // render loop
      // -----------
      while (!glfwWindowShouldClose(window))
      {
  
  #ifdef __abc__
          glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, TOF, 0);
  #endif
          if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
              std::cout &lt;&lt; "FRAMEBUFFER SUCCESS" &lt;&lt; std::endl;
              glViewport(0, 0, 400, 300);
              // render
              // ------
              glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // also clear the depth buffer now!
              // bind textures on corresponding texture units
              glActiveTexture(GL_TEXTURE0);
              glBindTexture(GL_TEXTURE_2D, texture1);
              glActiveTexture(GL_TEXTURE1);
              glBindTexture(GL_TEXTURE_2D, texture2);
              // activate shader
              ourShader.use();
              // create transformations
              glm::mat4 model = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
              glm::mat4 view = glm::mat4(1.0f);
              glm::mat4 projection = glm::mat4(1.0f);
              float time = (float)glfwGetTime();
              std::cout &lt;&lt; time &lt;&lt; std::endl;
              model = glm::rotate(model,time , glm::vec3(0.5f, 1.0f, 0.0f));
  
              view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
              projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
              // retrieve the matrix uniform locations
              unsigned int modelLoc = glGetUniformLocation(ourShader.ID, "model");
              unsigned int viewLoc = glGetUniformLocation(ourShader.ID, "view");
              // pass them to the shaders (3 different ways)
              glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
              glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &amp;view[0][0]);
              // note: currently we set the projection matrix each frame, 
              //but since the projection matrix rarely changes it's often best practice 
              //to set it outside the main loop only once.
              ourShader.setMat4("projection", projection);
              // render box
              glBindVertexArray(VAO);
              glDrawArrays(GL_TRIANGLES, 0, 36);
  
  #ifdef __abc__
              glBindFramebuffer(GL_FRAMEBUFFER, 0);   
  #endif
          }
          else {
              std::cout &lt;&lt; "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" &lt;&lt; std::endl;
          }
  #ifdef __abc__
          // Start the Dear ImGui frame
          ImGui_ImplOpenGL3_NewFrame();
          ImGui_ImplGlfw_NewFrame();
          ImGui::NewFrame();
          bool View_3D = true;
          if (View_3D) {
              ImGui::Begin("3D View", &amp;View_3D);
              view3d_height = ImGui::GetWindowSize().x;
              view3d_width = ImGui::GetWindowSize().y;
              ImVec2 pos = ImGui::GetCursorScreenPos();
              ImGui::GetWindowDrawList()-&gt;AddImage(
                  (void*)TOF,
                  ImVec2(ImGui::GetCursorScreenPos()),
                  ImVec2(ImGui::GetCursorScreenPos().x + ImGui::GetWindowSize().x - 15,
                      ImGui::GetCursorScreenPos().y + ImGui::GetWindowSize().y - 35),
                  ImVec2(0, 1),
                  ImVec2(1, 0)
              );
              ImGui::End();
          }
  
  
  
          // input
          // -----
          processInput(window);
  
  
          ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
          // render
          ImGui::Render();
          int display_w, display_h;
          glfwGetFramebufferSize(window, &amp;display_w, &amp;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_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  #endif
  
          // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
          // -------------------------------------------------------------------------------
          glfwSwapBuffers(window);
          glfwPollEvents();
      }
  
      // optional: de-allocate all resources once they've outlived their purpose:
      // ------------------------------------------------------------------------
      glDeleteVertexArrays(1, &amp;VAO);
      glDeleteBuffers(1, &amp;VBO);
  
      // 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);
  }
  
  
  GLFWwindow* Proinit() {
  //glfwSetErrorCallback(glfw_error_callback);
      if (!glfwInit()) return (GLFWwindow*)1;
  #ifdef __APPLE__
      // GL 3.2 + GLSL 150
      const char* glsl_version = "#version 150";
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // Required on Mac
  #else
      const char* glsl_version = "#version 130";
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  // 3.2+ only
      //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // 3.0+ only
  #endif
      GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Shader3D", NULL, NULL);
      if (window == NULL) { std::cout &lt;&lt; "Failed to create GLFW window" &lt;&lt; std::endl; glfwTerminate(); return (GLFWwindow*)-1; }
      glfwMakeContextCurrent(window);
      //glfwSwapInterval(1); // Enable vsync
      //glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  #if defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
      bool err = glewInit() != GLEW_OK;
  #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
      bool err = gladLoadGL() == 0;
  #else
      bool err = false;
  #endif
      if (err) { fprintf(stderr, "Failed to initialize OpenGL loader!\n"); return (GLFWwindow*)1; }
  
  
      return window;
  }
  
  
  
  void imguiSetup(GLFWwindow* window) {
      // Setup Dear ImGui context  
      IMGUI_CHECKVERSION();
      ImGui::CreateContext();
      ImGuiIO&amp; io = ImGui::GetIO(); (void)io;
      //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls 
      //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls  
      ImGui::StyleColorsDark();
      ImGui_ImplGlfw_InitForOpenGL(window, true);         //Setup Platform bindings
  #ifdef __APPLE__
      ImGui_ImplOpenGL3_Init("#version 150");             //Setup Renderer bindings
  #else
      ImGui_ImplOpenGL3_Init("#version 130");
  #endif
  
  }
  
  </code></pre>