Dear ImGui is a bloat-free graphical user interface library for C++. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline-enabled application. It is fast, portable, renderer agnostic, and self-contained (no external dependencies).

https://github.com/ocornut/imgui
    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                          ImGUI                           <|
    // |>----------<>----------<>----------<>----------<>----------<|

    ImGui::CreateContext();
    ImGui::StyleColorsDark();
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 440");
    //ImGui variables: 
    //bool show_demo_window = true;
    //bool show_another_window = false;
    float clear_color[]{ 0.45f, 0.55f, 0.60f, 1.00f };
    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                       RENDER LOOP                        <|
    // |>----------<>----------<>----------<>----------<>----------<|

        // render imgui
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        ImGui::Begin("test");
        ImGui::ColorEdit4("bg color", clear_color);
        ImGui::End();

        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

#include <glad/glad.h> 
#include <GLFW/glfw3.h> 
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

#include <string>
#include <iostream>

#include "Shader.h"
#include "VertexArray.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"


#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"



void processInput(GLFWwindow* window);
void size_callback(GLFWwindow* window, int width, int height);
GLFWwindow* create_GLFWwindow(float width, float height, std::string title);
void init_glad();


Shader shader; 

float SCR_WIDTH = 640, SCR_HEIGHT = 480;
std::string SCR_TITLE = "My 3D App";

glm::mat4 proj, view, model;
float currentFrame, lastFrame, deltaTime;


int main() {
    GLFWwindow* window = create_GLFWwindow(SCR_WIDTH, SCR_HEIGHT, SCR_TITLE);
    init_glad();

    shader.Compile();
    shader.Bind();

    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                      DEFINE VERTICES                     <|
    // |>----------<>----------<>----------<>----------<>----------<|

    float vertices[]{
        -100.0f, -100.0f, 0.0f, 0.0f,// 0 - bottom left 
         100.0f, -100.0f, 1.0f, 0.0f,// 1 - bottom right
         100.0f,  100.0f, 1.0f, 1.0f,// 2 - top right
        -100.0f,  100.0f, 0.0f, 1.0f,// 3 - top left

    };
    //for (float& i : vertices) i *= 200; // we scale our shape 
    unsigned int indices[] = { 
        0, 1, 2,  // first Triangle
        0, 2, 3   // second Triangle
    };
    unsigned int layout[]{ 2, GL_FLOAT,2,GL_FLOAT };

    VertexArray va; va.Bind();
    VertexBuffer vb(&vertices[0], sizeof(vertices));
    IndexBuffer ib(indices, sizeof(indices));

    vb.setLayout(layout,sizeof(layout));
    va.Unbind();
        

    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                         TEXTURE                          <|
    // |>----------<>----------<>----------<>----------<>----------<|

    // load and create a texture 
    unsigned int texture1;
    glGenTextures(1, &texture1);
    glBindTexture(GL_TEXTURE_2D, texture1);
    // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// set texture wrapping to GL_REPEAT (default wrapping method)
    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.
    // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
    unsigned char* data = stbi_load("res/horse-face.png", &width, &height, &nrChannels, 0);
    if (data) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else {
        std::cout << "Failed to load texture" << std::endl;
    }
    stbi_image_free(data);
    shader.SetUniform1i("texture1", 0);

    // enable blending 
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                          CAMERA                          <|
    // |>----------<>----------<>----------<>----------<>----------<|
    // set our initial values 
    float w = SCR_WIDTH;
    float h = SCR_HEIGHT;
    proj = glm::ortho(0.0f, w, 0.0f, h, -1.0f, 1.0f);
    view = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0));
    model = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0));
    glm::mat4 mvp = proj * view * model;
    shader.setUniformMat4f("u_MVP", mvp); // update our uniform 




    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                          ImGUI                           <|
    // |>----------<>----------<>----------<>----------<>----------<|

    ImGui::CreateContext();
    ImGui::StyleColorsDark();
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 440");
    //ImGui variables: 
    //bool show_demo_window = true;
    //bool show_another_window = false;
    float clear_color[]{ 0.45f, 0.55f, 0.60f, 1.00f };




    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                       RENDER LOOP                        <|
    // |>----------<>----------<>----------<>----------<>----------<|

    va.Bind();

    while (!glfwWindowShouldClose(window)) {
        // per-frame time logic
        // --------------------
        float currentFrame = static_cast<float>(glfwGetTime());
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        processInput(window);
        
        // clear
        glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
        glClear(GL_COLOR_BUFFER_BIT); 



        // update and redraw model matrix 9 times
        for(int i = 0; i<3; i++)
            for (int j = 0; j < 3; j++) {
                model = glm::translate(glm::mat4(1.0f), glm::vec3(i * 250, j * 250, 0));
                glm::mat4 mvp = proj * view * model;
                shader.setUniformMat4f("u_MVP", mvp);
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

            }



        // render imgui
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        ImGui::Begin("test");
        ImGui::ColorEdit4("bg color", clear_color);
        ImGui::End();

        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());




        glfwSwapBuffers(window);
        glfwPollEvents();
    }

}


void processInput(GLFWwindow* window){
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);


    float cameraSpeed = static_cast<float>(250 * deltaTime);

    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        view = glm::translate(view, glm::vec3(0, -1, 0) * cameraSpeed);
    else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        view = glm::translate(view, glm::vec3(0, 1, 0) * cameraSpeed);

    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        view = glm::translate(view, glm::vec3(1, 0, 0) * cameraSpeed);
    else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        view = glm::translate(view, glm::vec3(-1, 0, 0) * cameraSpeed);

    
    glm::mat4 mvp = proj * view * model;
    shader.setUniformMat4f("u_MVP", mvp);
}


void size_callback(GLFWwindow* window, int width, int height) {
    std::cout << "New width: " << width << "\theight: " << height << std::endl;

    // update the projection matrix 
    float f = width, h = height;
    proj = glm::ortho(0.0f, f, 0.0f, h, -1.0f, 1.0f);
    glm::mat4 mvp = proj * view * model;
    shader.setUniformMat4f("u_MVP", mvp);

    glViewport(0, 0, width, height);
}

GLFWwindow* create_GLFWwindow(float width, float height, std::string title){
    // |>----------<>----------<>----------<>----------<>----------<|
    // |>                    CREATE GLFW WINDOW                    <|
    // |>----------<>----------<>----------<>----------<>----------<|

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        //return EXIT_FAILURE;
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); 

    glfwSetFramebufferSizeCallback(window, size_callback); // we bind callback to our size_callback func. 

    return window;
}

void init_glad(){
    // |>----------<>----------<>----------<>----------<>----------<|
    // |>               LOAD OPENGL FUNCTION POINTERS              <|
    // |>----------<>----------<>----------<>----------<>----------<|

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        exit(EXIT_FAILURE);
    }

}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *