hnOsmium0001

imgui + gl test main

Jan 28th, 2020
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void resizeCallback(GLFWwindow* handle, int32_t width, int32_t height) {
  2.     HOEngine::Window::HandleResize(handle, width, height);
  3.     glViewport(0, 0, width, height);
  4. }
  5.  
  6. void keyCallback(GLFWwindow* handle, int32_t key, int32_t scancode, int32_t action, int32_t modss) {
  7. }
  8.  
  9. void charCallback(GLFWwindow* handle, uint32_t codepoint) {
  10. }
  11.  
  12. void cursorPosCallback(GLFWwindow* handle, double x, double y) {
  13. }
  14.  
  15. void cursorButtonCallback(GLFWwindow* handle, int32_t button, int32_t action, int32_t mods) {
  16. }
  17.  
  18. void scrollCallback(GLFWwindow* handle, double x, double y) {
  19. }
  20.  
  21. class Application : public HOEngine::ApplicationBase {
  22. public:
  23.     void Run() {
  24.         auto window = HOEngine::Window::New({ 1024, 768 }, "Test window", {
  25.             .resizeCallback = resizeCallback,
  26.             .keyCallback = keyCallback,
  27.             .charCallback = charCallback,
  28.             .cursorPosCallback = cursorPosCallback,
  29.             .cursorButtonCallback = cursorButtonCallback,
  30.             .scrollCallback = scrollCallback
  31.         });
  32.         if (window == nullptr) {
  33.             std::cerr << "Unable to create GLFW window, aborting\n";
  34.             return;
  35.         }
  36.  
  37.         if (gl3wInit()) {
  38.             std::cerr << "Unable to initialize OpenGL, aborting\n";
  39.             return;
  40.         }
  41.  
  42.         // Allocation
  43.         GLuint vao;
  44.         glGenVertexArrays(1, &vao);
  45.         HOEngine::ScopeGuard vaoRelease([&]() { glDeleteVertexArrays(1, &vao); });
  46.  
  47.         GLuint vbo;
  48.         glGenBuffers(1, &vbo);
  49.         HOEngine::ScopeGuard vboRelease([&]() { glDeleteBuffers(1, &vbo); });
  50.  
  51.         auto vshSource = HOEngine::ReadFileAsStr("example/resources/triangle.vsh").value_or("");
  52.         auto fshSource = HOEngine::ReadFileAsStr("example/resources/triangle.fsh").value_or("");
  53.         auto program = HOEngine::ShaderProgram::New(vshSource, fshSource);
  54.         if (!program) {
  55.             std::cerr << "Unable to create shader program, aborting\n";
  56.             return;
  57.         }
  58.  
  59.         // Initialization
  60.         glBindVertexArray(vao);
  61.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  62.         HOEngine::VertexAttributes<float[3], float[3]>::SetupPointers();
  63.         glBindVertexArray(0);
  64.  
  65.         // Data
  66.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  67.         HOEngine::VertexAttributes<float[3], float[3]> verts[3];
  68.         verts[0].Attr<0>() << 0.0f, 1.0f, 0.0f;
  69.         verts[0].Attr<1>() << 1.0f, 0.0f, 0.0f;
  70.         verts[1].Attr<0>() << -1.0f, -1.0f, 0.0f;
  71.         verts[1].Attr<1>() << 0.0f, 1.0f, 0.0f;
  72.         verts[2].Attr<0>() << 1.0f, -1.0f, 0.0f;
  73.         verts[2].Attr<1>() << 0.0f, 0.0f, 1.0f;
  74.         glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
  75.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  76.  
  77.         IMGUI_CHECKVERSION();
  78.         ImGui::CreateContext();
  79.  
  80.         // ImGui::StyleColorsClassic();
  81.         // ImGui::StyleColorsLight();
  82.         ImGui::StyleColorsDark();
  83.  
  84.         ImGui_ImplGlfw_InitForOpenGL(*window, true);
  85.         ImGui_ImplOpenGL3_Init(nullptr);
  86.  
  87.         // Data for ImGui application rendering
  88.         bool showDemoWindow = false;
  89.         ImVec2 fboDim(128, 96);
  90.         // Data for triangle rendering
  91.         float time = 0;
  92.  
  93.         GLuint tex;
  94.         glGenTextures(1, &tex);
  95.         HOEngine::ScopeGuard texRelease([&]() { glDeleteTextures(1, &tex); });
  96.         glBindTexture(GL_TEXTURE_2D, tex);
  97.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window->dim().width, window->dim().height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  98.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100.  
  101.         GLuint fbo;
  102.         glGenFramebuffers(1, &fbo);
  103.         HOEngine::ScopeGuard fboRelease([&]() { glDeleteFramebuffers(1, &fbo); });
  104.         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  105.         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
  106.  
  107.         while (!glfwWindowShouldClose(*window)) {
  108.             glfwPollEvents();
  109.  
  110.             // Render our triangle
  111.             glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  112.             {
  113.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.                 glClearColor(175.0f / 255.0f, 175.0f / 255.0f, 175.0f / 255.0f, 1.0f);
  115.  
  116.                 glUseProgram(*program);
  117.                 GLuint coefRUniform = glGetUniformLocation(*program, "coefR");
  118.                 GLuint coefGUniform = glGetUniformLocation(*program, "coefG");
  119.                 GLuint coefBUniform = glGetUniformLocation(*program, "coefB");
  120.                 glUniform1f(coefRUniform, static_cast<float>(std::sin(time)) * 0.5f + 0.5f);
  121.                 glUniform1f(coefGUniform, static_cast<float>(std::sin(time + 3.1415926535f / 2)) * 0.5f + 0.5f);
  122.                 glUniform1f(coefBUniform, static_cast<float>(std::sin(time + 3.1415926535f)) * 0.5f + 0.5f);
  123.  
  124.                 glBindVertexArray(vao);
  125.                 glDrawArrays(GL_TRIANGLES, 0, 1 * 3);
  126.                 glBindVertexArray(0);
  127.             }
  128.             glBindFramebuffer(GL_FRAMEBUFFER, 0);
  129.  
  130.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  131.             glClearColor(175.0f / 255.0f, 175.0f / 255.0f, 175.0f / 255.0f, 1.0f);
  132.  
  133.             ImGui_ImplOpenGL3_NewFrame();
  134.       ImGui_ImplGlfw_NewFrame();
  135.             ImGui::NewFrame();
  136.  
  137.             if (showDemoWindow) {
  138.                 ImGui::ShowDemoWindow();
  139.             }
  140.  
  141.             ImGui::Begin("ImGui Demo Window");
  142.             ImGui::Text("Hello, world!");
  143.             ImGui::Checkbox("Demo window", &showDemoWindow);
  144.             ImGui::SliderFloat("Width", &fboDim.x, 16.0f, 1024.0f);
  145.             ImGui::SliderFloat("Height", &fboDim.y, 12.0f, 768.0f);
  146.             ImGui::End();
  147.  
  148.             ImGui::Begin("Rendering framebuffer");
  149.             ImGui::Image((void*) tex, fboDim);
  150.             ImGui::End();
  151.  
  152.             glClear(GL_COLOR_BUFFER_BIT);
  153.             glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
  154.             ImGui::Render();
  155.             ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  156.  
  157.             glfwSwapBuffers(*window);
  158.             time += 0.01f;
  159.         }
  160.     }
  161. };
  162.  
  163. int main(void) {
  164.     Application app;
  165.     app.Run();
  166.     return 0;
  167. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×