Implementado ventana de selección de color de fondo

parent a0c58661
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "gui.h" #include "gui.h"
#include "log.h" #include "log.h"
#include "ventana_log.h" #include "ventana_log.h"
#include "ventana_color.h"
// Helper que muestra un mensaje y cierra el programa // Helper que muestra un mensaje y cierra el programa
#define panic(message) { \ #define panic(message) { \
...@@ -53,33 +54,12 @@ void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) ...@@ -53,33 +54,12 @@ void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
} }
} }
const double gradient_step = 0.05;
const double gradient_colors[][3] = {{0.6, 0.6, 0.6}, {1, 0.65, 0}};
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
static double position_in_gradient = 0; static double position_in_gradient = 0;
PAG::log << "Movida la rueda del raton " PAG::log << "Movida la rueda del raton "
<< xoffset << " unidades en horizontal y " << xoffset << " unidades en horizontal y "
<< yoffset << " unidades en vertical" << std::endl; << yoffset << " unidades en vertical" << std::endl;
position_in_gradient += yoffset * gradient_step;
if (position_in_gradient > 1) {
position_in_gradient = 1;
} else if (position_in_gradient < 0) {
position_in_gradient = 0;
}
double a = (1 - position_in_gradient);
double b = position_in_gradient;
PAG::Renderer::getInstance().setClearColor(
gradient_colors[0][0] * a + gradient_colors[1][0] * b,
gradient_colors[0][1] * a + gradient_colors[1][1] * b,
gradient_colors[0][2] * a + gradient_colors[1][2] * b,
1.0
);
} }
int main() { int main() {
...@@ -115,6 +95,10 @@ int main() { ...@@ -115,6 +95,10 @@ int main() {
PAG::GUI::getInstance().init(window); PAG::GUI::getInstance().init(window);
PAG::GUI::getInstance().addVentana(std::make_shared<PAG::VentanaLog>(PAG::VentanaLog())); PAG::GUI::getInstance().addVentana(std::make_shared<PAG::VentanaLog>(PAG::VentanaLog()));
auto ventana_color = std::make_shared<PAG::VentanaColor>(PAG::VentanaColor());
ventana_color->addListener(&PAG::Renderer::getInstance());
PAG::GUI::getInstance().addVentana(ventana_color);
auto &renderer = PAG::Renderer::getInstance(); auto &renderer = PAG::Renderer::getInstance();
PAG::log << renderer.getRendererName() << std::endl; PAG::log << renderer.getRendererName() << std::endl;
...@@ -129,7 +113,7 @@ int main() { ...@@ -129,7 +113,7 @@ int main() {
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
renderer.setClearColor(0.6, 0.6, 0.6, 1.0); renderer.setClearColor(ventana_color->getColor());
renderer.enableDepthTest(); renderer.enableDepthTest();
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "ventana_color.h"
namespace PAG { namespace PAG {
// Basado en https://stackoverflow.com/a/1008289 // Basado en https://stackoverflow.com/a/1008289
Renderer &Renderer::getInstance() { Renderer &Renderer::getInstance() {
...@@ -16,6 +18,10 @@ namespace PAG { ...@@ -16,6 +18,10 @@ namespace PAG {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }
void Renderer::setClearColor(std::array<float, 3> rgb) {
setClearColor(rgb[0], rgb[1], rgb[2], 1);
}
void Renderer::setClearColor(float r, float g, float b, float a) { void Renderer::setClearColor(float r, float g, float b, float a) {
glClearColor(r, g, b, a); glClearColor(r, g, b, a);
} }
...@@ -41,6 +47,13 @@ namespace PAG { ...@@ -41,6 +47,13 @@ namespace PAG {
return std::string((const char *) glGetString(GL_SHADING_LANGUAGE_VERSION)); return std::string((const char *) glGetString(GL_SHADING_LANGUAGE_VERSION));
} }
void Renderer::wakeUp(Ventana &ventana) {
if (dynamic_cast<VentanaColor*>(&ventana)) {
auto ventana_color = dynamic_cast<VentanaColor&>(ventana);
setClearColor(ventana_color.getColor());
}
}
Renderer::Renderer() {}; Renderer::Renderer() {};
Renderer::~Renderer() {}; Renderer::~Renderer() {};
......
#pragma once #pragma once
#include <string> #include <string>
#include <array>
#include "ventana.h"
namespace PAG { namespace PAG {
class Renderer { class Renderer : public PAG::Ventana::Listener {
public: public:
static Renderer &getInstance(); static Renderer &getInstance();
...@@ -13,6 +16,7 @@ namespace PAG { ...@@ -13,6 +16,7 @@ namespace PAG {
// Funciones de opengl // Funciones de opengl
void clear(); void clear();
void setClearColor(std::array<float, 3> rgb);
void setClearColor(float, float, float, float); void setClearColor(float, float, float, float);
void setViewport(int width, int height); void setViewport(int width, int height);
...@@ -22,6 +26,8 @@ namespace PAG { ...@@ -22,6 +26,8 @@ namespace PAG {
std::string getVendor(); std::string getVendor();
std::string getVersion(); std::string getVersion();
std::string getShadingLanguageVersion(); std::string getShadingLanguageVersion();
void wakeUp(Ventana &) override;
private: private:
Renderer(); Renderer();
~Renderer(); ~Renderer();
......
#pragma once #pragma once
#include <vector>
namespace PAG { namespace PAG {
class Ventana { class Ventana {
public: public:
virtual void render() = 0; virtual void render() = 0;
virtual ~Ventana() = default; virtual ~Ventana() = default;
class Listener {
public:
virtual void wakeUp(Ventana &) = 0;
virtual ~Listener() = default;
};
void addListener(Listener *listener) {
listeners.push_back(listener);
};
protected:
void notifyListeners() {
for (auto listener : listeners) {
listener->wakeUp(*this);
}
};
std::vector<Listener *> listeners;
}; };
} }
#include "ventana_color.h"
#include <imgui.h>
namespace PAG {
void VentanaColor::render() {
ImGui::SetNextWindowPos(ImVec2(670, 110), ImGuiCond_FirstUseEver);
ImGui::ColorPicker3("Color", color.data(), ImGuiColorEditFlags_PickerHueWheel);
if (ImGui::IsItemEdited()) {
notifyListeners();
}
}
std::array<float, 3> VentanaColor::getColor() {
return color;
}
}
#pragma once
#include "ventana.h"
#include <array>
namespace PAG {
class VentanaColor : public Ventana {
public:
void render() override;
std::array<float, 3> getColor();
private:
std::array<float, 3> color = {0.6, 0.6, 0.6};
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment