Add imgui
This commit is contained in:
142
src/main.cpp
142
src/main.cpp
@@ -3,33 +3,31 @@
|
||||
#include <opengl-playground/openglshader.hpp>
|
||||
#include <opengl-playground/textured-rectangle.hpp>
|
||||
|
||||
#define WINDOW_WIDTH 800
|
||||
#define WINDOW_HEIGHT 640
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_sdl.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#define WINDOW_WIDTH 1200
|
||||
#define WINDOW_HEIGHT 800
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
MainWindow *window;
|
||||
GLuint triangle_vao;
|
||||
GLuint triangle_vbo;
|
||||
SdlMainWindow *window;
|
||||
SDL_Event event;
|
||||
bool run = true;
|
||||
bool got_event = false;
|
||||
glm::mat4 view_matrix;
|
||||
glm::mat4 projection_matrix;
|
||||
bool show_demo_window = true;
|
||||
|
||||
float nav_point[2] = {0.0f, 0.0f};
|
||||
float zoom = 1.0;
|
||||
|
||||
float triangle_vertices[] = { -0.5f, 0.0f,
|
||||
0.0f, 0.5f,
|
||||
0.0f, 0.0f,
|
||||
0.8f, 0.0f,
|
||||
0.8f, 0.5f,
|
||||
0.5f, 0.2f,
|
||||
};
|
||||
|
||||
|
||||
|
||||
window = new SdlMainWindow(WINDOW_HEIGHT, WINDOW_WIDTH);
|
||||
|
||||
window->show();
|
||||
@@ -40,73 +38,73 @@ int main(int argc, char **argv)
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 0.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glGenVertexArrays(1, &triangle_vao);
|
||||
glBindVertexArray(triangle_vao);
|
||||
glGenBuffers(1, &triangle_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangle_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
std::shared_ptr<OpenGlShaderProgram> textured_rect_prog =
|
||||
std::make_shared<OpenGlShaderProgram>("shaders/textured-rectangle-vertex.glsl", "",
|
||||
"shaders/textured-rectangle-fragment.glsl");
|
||||
|
||||
OpenGlShaderProgram prog = OpenGlShaderProgram("shaders/2d-zoom-translate-vertex.glsl", "shaders/triangle-gen-zoomed-geometry.glsl", "shaders/fixed-red-fragment.glsl");
|
||||
prog.compile();
|
||||
projection_matrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f);
|
||||
textured_rect_prog->setProjectionView(projection_matrix, glm::mat4(1.0f));
|
||||
textured_rect_prog->compile();
|
||||
|
||||
OpenGlShaderProgram textured_rect_prog = OpenGlShaderProgram("shaders/textured-rectangle-vertex.glsl", "", "shaders/textured-rectangle-fragment.glsl");
|
||||
textured_rect_prog.compile();
|
||||
|
||||
TexturedRectangle rect = TexturedRectangle(-0.1, -0.1, 0.1, 0.1, textured_rect_prog);
|
||||
TexturedRectangle rect = TexturedRectangle(-1, -1, 1, 1, textured_rect_prog);
|
||||
rect.realize();
|
||||
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
//ImGui::StyleColorsClassic();
|
||||
|
||||
// Setup Platform/Renderer bindings
|
||||
ImGui_ImplSDL2_InitForOpenGL(window->getWindow(), window->getContext());
|
||||
ImGui_ImplOpenGL3_Init("#version 330");
|
||||
|
||||
|
||||
while (run) {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// Draw the triangles
|
||||
prog.use();
|
||||
|
||||
prog.setUniformFloat("zoom", zoom);
|
||||
prog.setUniformVec2("center_pos", nav_point);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glBindVertexArray(triangle_vao);
|
||||
glDrawArrays(GL_POINTS, 0, sizeof(triangle_vertices)/2);
|
||||
|
||||
rect.setZoom(zoom);
|
||||
rect.setOffset(nav_point[0], nav_point[1]);
|
||||
rect.render();
|
||||
|
||||
window->swapBuffer();
|
||||
got_event = false;
|
||||
|
||||
while (!got_event) {
|
||||
if (SDL_WaitEvent(&event)) {
|
||||
if (event.type == SDL_KEYDOWN) {
|
||||
if (event.key.keysym.sym == SDLK_w) {
|
||||
nav_point[1] += 0.05;
|
||||
} else if (event.key.keysym.sym == SDLK_s) {
|
||||
nav_point[1] -= 0.05;
|
||||
} else if (event.key.keysym.sym == SDLK_a) {
|
||||
nav_point[0] -= 0.05;
|
||||
} else if (event.key.keysym.sym == SDLK_d) {
|
||||
nav_point[0] += 0.05;
|
||||
} else if (event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
std::cout << "Bye!" << std::endl;
|
||||
run = false;
|
||||
} else if (event.key.keysym.sym == SDLK_PLUS) {
|
||||
zoom += 0.01f;
|
||||
} else if (event.key.keysym.sym == SDLK_MINUS) {
|
||||
zoom -= 0.01f;
|
||||
if (zoom < 0.0f)
|
||||
zoom = 0.0f;
|
||||
}
|
||||
got_event = true;
|
||||
|
||||
}
|
||||
}
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||
if (event.type == SDL_QUIT)
|
||||
run = false;
|
||||
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window->getWindow()))
|
||||
run = false;
|
||||
}
|
||||
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame(window->getWindow());
|
||||
ImGui::NewFrame();
|
||||
|
||||
|
||||
view_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(zoom, zoom, zoom));
|
||||
view_matrix = glm::translate(view_matrix, glm::vec3(-nav_point[0], -nav_point[1], 0.0f));
|
||||
|
||||
textured_rect_prog->setProjectionView(projection_matrix, view_matrix);
|
||||
rect.render();
|
||||
|
||||
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
window->swapBuffer();
|
||||
|
||||
}
|
||||
|
||||
textured_rect_prog.unload();
|
||||
prog.unload();
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
textured_rect_prog->unload();
|
||||
delete window;
|
||||
}
|
||||
|
@@ -2,12 +2,23 @@
|
||||
|
||||
|
||||
|
||||
OpenGlGraphics::OpenGlGraphics(OpenGlShaderProgram &prog) : shaderprog(prog)
|
||||
OpenGlGraphics::OpenGlGraphics(std::shared_ptr<OpenGlShaderProgram>shader_prog)
|
||||
{
|
||||
|
||||
this->shaderprog = shader_prog;
|
||||
this->setModelMatrix(glm::mat4(1.0f));
|
||||
}
|
||||
|
||||
OpenGlGraphics::~OpenGlGraphics()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OpenGlGraphics::setModelMatrix(const glm::mat4 &model_matrix)
|
||||
{
|
||||
this->model_matrix = model_matrix;
|
||||
}
|
||||
|
||||
glm::mat4 OpenGlGraphics::getModelMatrix()
|
||||
{
|
||||
return this->model_matrix;
|
||||
}
|
||||
|
@@ -9,11 +9,29 @@ OpenGlShaderProgram::OpenGlShaderProgram(std::string vertex_file, std::string ge
|
||||
this->geometry_file = geometry_file;
|
||||
this->fragment_file = fragment_file;
|
||||
this->compiled = false;
|
||||
this->setProjectionView(glm::mat4(1.0f), glm::mat4(1.0f));
|
||||
}
|
||||
|
||||
OpenGlShaderProgram::~OpenGlShaderProgram()
|
||||
{
|
||||
this->unload();
|
||||
}
|
||||
|
||||
void OpenGlShaderProgram::setProjectionView(const glm::mat4 &projection, const glm::mat4 &view)
|
||||
{
|
||||
this->proj = projection;
|
||||
this->view = view;
|
||||
this->proj_view = projection * view;
|
||||
}
|
||||
|
||||
glm::mat4 OpenGlShaderProgram::getProjection()
|
||||
{
|
||||
return this->proj;
|
||||
}
|
||||
|
||||
glm::mat4 OpenGlShaderProgram::getView()
|
||||
{
|
||||
return this->view;
|
||||
}
|
||||
|
||||
std::string load_shader_program_from_file(std::string &file_name)
|
||||
@@ -154,6 +172,8 @@ int OpenGlShaderProgram::use()
|
||||
return -1;
|
||||
|
||||
glUseProgram(this->shader_program);
|
||||
this->setUniformMat4("projection_view_matrix", this->proj_view);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -173,6 +193,14 @@ void OpenGlShaderProgram::setUniformVec2(const std::string &uniform, float *vect
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGlShaderProgram::setUniformMat4(const std::string &uniform, const glm::mat4 &matrix)
|
||||
{
|
||||
int loc = this->getUniformLoc(uniform);
|
||||
if (loc >= 0) {
|
||||
glUniformMatrix4fv(loc, 1, GL_FALSE, &matrix[0][0]);
|
||||
}
|
||||
}
|
||||
|
||||
int OpenGlShaderProgram::getUniformLoc(const std::string &uniform)
|
||||
{
|
||||
if (!this->compiled)
|
||||
|
@@ -39,3 +39,13 @@ void SdlMainWindow::activateGlContext()
|
||||
else
|
||||
SDL_GL_MakeCurrent(this->window, this->context);
|
||||
}
|
||||
|
||||
SDL_Window *SdlMainWindow::getWindow()
|
||||
{
|
||||
return this->window;
|
||||
}
|
||||
|
||||
SDL_GLContext SdlMainWindow::getContext()
|
||||
{
|
||||
return this->context;
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
#include <opengl-playground/textured-rectangle.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
|
||||
|
||||
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, OpenGlShaderProgram &shaderprog) :
|
||||
TexturedRectangle::TexturedRectangle(float x0, float y0, float x1, float y1, std::shared_ptr<OpenGlShaderProgram> shaderprog) :
|
||||
OpenGlGraphics(shaderprog)
|
||||
{
|
||||
this->pos1[0] = x0;
|
||||
@@ -45,14 +45,9 @@ void TexturedRectangle::realize()
|
||||
|
||||
void TexturedRectangle::render()
|
||||
{
|
||||
float offset[2];
|
||||
|
||||
this->shaderprog.use();
|
||||
offset[0] = this->getOffsetX();
|
||||
offset[1] = this->getOffsetY();
|
||||
this->shaderprog.setUniformVec2("pos_offset", offset);
|
||||
this->shaderprog.setUniformFloat("zoom", this->zoom);
|
||||
this->shaderprog->use();
|
||||
|
||||
this->shaderprog->setUniformMat4("model_matrix", this->model_matrix);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glBindVertexArray(this->vao);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
@@ -62,6 +57,7 @@ void TexturedRectangle::render()
|
||||
void TexturedRectangle::setZoom(float zoom)
|
||||
{
|
||||
this->zoom = zoom;
|
||||
this->calculateModelMatrix();
|
||||
}
|
||||
|
||||
float TexturedRectangle::getZoom()
|
||||
@@ -73,6 +69,7 @@ void TexturedRectangle::setOffset(float x_off, float y_off)
|
||||
{
|
||||
this->x_offset = x_off;
|
||||
this->y_offset = y_off;
|
||||
this->calculateModelMatrix();
|
||||
}
|
||||
|
||||
float TexturedRectangle::getOffsetX()
|
||||
@@ -84,3 +81,9 @@ float TexturedRectangle::getOffsetY()
|
||||
{
|
||||
return this->y_offset;
|
||||
}
|
||||
|
||||
void TexturedRectangle::calculateModelMatrix()
|
||||
{
|
||||
glm::mat4 moved_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(-this->x_offset, -this->y_offset, 0.0f));
|
||||
this->model_matrix = glm::scale(moved_matrix, glm::vec3(this->zoom, this->zoom, 0.0f));
|
||||
}
|
||||
|
Reference in New Issue
Block a user