ShaderWindow creada y carga de Shaders implementada

parent a41af10a
......@@ -16,6 +16,7 @@
PAG::GUI* PAG::GUI::instancia = nullptr;
BackgroundWindow PAG::GUI::background = BackgroundWindow();
LogWindow PAG::GUI::log = LogWindow();
ShaderWindow PAG::GUI::shader = ShaderWindow();
/**
* Constructor de la clase
......@@ -68,6 +69,7 @@ void PAG::GUI::prepararNuevoFrame() {
// Aquí van las instrucciones de dibujado de ventanas
background.render();
log.render();
shader.render();
}
/**
......
......@@ -14,6 +14,7 @@
#include <string>
#include "BackgroundWindow.h"
#include "LogWindow.h"
#include "ShaderWindow.h"
namespace PAG {
/**
......@@ -28,6 +29,7 @@ namespace PAG {
static GUI* instancia;
static BackgroundWindow background;
static LogWindow log;
static ShaderWindow shader;
GUI();
public:
......
......@@ -114,7 +114,7 @@ void PAG::Renderer::creaShaderProgram(std::string& nombre) {
}
// Leyendo VS
std::string nombreArchivoVS = "./";
std::string nombreArchivoVS = "";
nombreArchivoVS.append(nombre).append("-vs.glsl");
lectorArchivos.open(nombreArchivoVS);
if(!lectorArchivos.is_open()) {
......
......@@ -66,6 +66,8 @@ std::string ShaderException::getLog() {
aux.append(":\n").append(log).append("\n");
if (errorType == LECTURA) { aux.append("Please check your working directory."); }
return aux;
}
......
//
// Created by administrador on 2/10/25.
//
#include "ShaderWindow.h"
#include <fstream>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <sstream>
#include "GUI.h"
#include "ShaderException.h"
bool ShaderWindow::loadShaders() {
GLuint oldVS = idVS, oldFS = idFS, oldSP = idSP;
std::ifstream lectorArchivos;
// Creando VS
idVS = glCreateShader(GL_VERTEX_SHADER);
if (idVS == 0) {
throw ShaderException(GL_VERTEX_SHADER, CREACION, "");
}
// Leyendo VS
std::string nombreArchivoVS = "";
nombreArchivoVS.append(name).append("-vs.glsl");
lectorArchivos.open(nombreArchivoVS);
if(!lectorArchivos.is_open()) {
std::string error = "Couldn't open file ";
error.append(nombreArchivoVS);
// Restaurar estado anterior
restoreShader(idVS, oldVS);
throw ShaderException(GL_VERTEX_SHADER, LECTURA, error);
}
std::stringstream ssVS;
ssVS << lectorArchivos.rdbuf();
std::string stringVS = ssVS.str();
lectorArchivos.close();
const GLchar* fuenteVS = stringVS.c_str();
glShaderSource(idVS, 1, &fuenteVS, nullptr);
// Compilando VS
glCompileShader(idVS);
GLint resultadoCompilacion;
glGetShaderiv ( idVS, GL_COMPILE_STATUS, &resultadoCompilacion );
if ( resultadoCompilacion == GL_FALSE )
{ // Ha habido un error en la compilación.
// Para saber qué ha pasado, tenemos que recuperar el mensaje de error de OpenGL
GLint tamMsj = 0;
std::string mensaje = "";
glGetShaderiv ( idVS, GL_INFO_LOG_LENGTH, &tamMsj );
if ( tamMsj > 0 )
{ GLchar* mensajeFormatoC = new GLchar[tamMsj];
GLint datosEscritos = 0;
glGetShaderInfoLog ( idVS, tamMsj, &datosEscritos, mensajeFormatoC );
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
// Restaurar estado anterior
restoreShader(idVS, oldVS);
throw ShaderException(GL_VERTEX_SHADER, LECTURA, mensaje);
}
}
// Creando FS
idFS = glCreateShader(GL_FRAGMENT_SHADER);
if (idFS == 0) {
// Restaurar estado anterior
restoreShader(idVS, oldVS);
throw ShaderException(GL_FRAGMENT_SHADER, CREACION, "");
}
// Leyendo FS
std::string nombreArchivoFS = "./";
nombreArchivoFS.append(name).append("-fs.glsl");
lectorArchivos.open(nombreArchivoFS);
if(!lectorArchivos.is_open()) {
std::string error = "Couldn't open file ";
error.append(nombreArchivoFS);
// Restaurar estado anterior
restoreShader(idVS, oldVS);
restoreShader(idFS, oldFS);
throw ShaderException(GL_FRAGMENT_SHADER, LECTURA, error);
}
std::stringstream ssFS;
ssFS << lectorArchivos.rdbuf();
std::string stringFS = ssFS.str();
lectorArchivos.close();
const GLchar* fuenteFS = stringFS.c_str();
glShaderSource(idFS, 1, &fuenteFS, nullptr);
// Compilando FS
glCompileShader(idFS);
glGetShaderiv ( idFS, GL_COMPILE_STATUS, &resultadoCompilacion );
if ( resultadoCompilacion == GL_FALSE )
{ // Ha habido un error en la compilación.
// Para saber qué ha pasado, tenemos que recuperar el mensaje de error de OpenGL
GLint tamMsj = 0;
std::string mensaje = "";
glGetShaderiv ( idFS, GL_INFO_LOG_LENGTH, &tamMsj );
if ( tamMsj > 0 )
{ GLchar* mensajeFormatoC = new GLchar[tamMsj];
GLint datosEscritos = 0;
glGetShaderInfoLog ( idFS, tamMsj, &datosEscritos, mensajeFormatoC );
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
// Restaurar estado anterior
restoreShader(idVS, oldVS);
restoreShader(idFS, oldFS);
throw ShaderException(GL_FRAGMENT_SHADER, LECTURA, mensaje);
}
}
// Creando SP
idSP = glCreateProgram();
if(idSP == 0) {
// Restaurar estado anterior
restoreShader(idVS, oldVS);
restoreShader(idFS, oldFS);
throw ShaderException(0, CREACION, "");
}
glAttachShader(idSP, idVS);
glAttachShader(idSP, idFS);
// Enlazando SP
glLinkProgram(idSP);
GLint resultadoEnlazado = 0;
glGetProgramiv ( idSP, GL_LINK_STATUS, &resultadoEnlazado );
if ( resultadoEnlazado == GL_FALSE )
{ // Ha habido un error y hay que recuperar su descripción, para saber qué ha pasado
GLint tamMsj = 0;
std::string mensaje = "";
glGetProgramiv ( idSP, GL_INFO_LOG_LENGTH, &tamMsj );
if ( tamMsj > 0 )
{ GLchar* mensajeFormatoC = new GLchar[tamMsj];
GLint datosEscritos = 0;
glGetProgramInfoLog ( idSP, tamMsj, &datosEscritos, mensajeFormatoC );
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
// Restaurar estado anterior
restoreShader(idVS, oldVS);
restoreShader(idFS, oldFS);
glDeleteProgram(idSP);
idSP = oldSP;
throw ShaderException(0, ENLAZADO, mensaje);
}
}
}
void ShaderWindow::restoreShader(GLuint& var, GLuint oldId) {
glDeleteShader(var);
var = oldId;
}
ShaderWindow::ShaderWindow(): currentlyLoaded("NINGUNO"), idVS(0), idFS(0), idSP(0) {}
void ShaderWindow::warnListeners() {
}
void ShaderWindow::render() {
ImGui::SetNextWindowPos(ImVec2(510, 10), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(300, 80), ImGuiCond_Once);
if(ImGui::Begin("Shader Program") ) {
ImGui::SetWindowFontScale ( 1.0f ); // Escalamos el texto si fuera necesario
ImGui::InputText("##", &name, ImGuiInputTextFlags_AutoSelectAll);
if(ImGui::Button("Load")) {
try {
loadShaders();
warnListeners();
} catch(ShaderException ex) {
PAG::GUI::getInstancia().registrarMensaje(ex.getLog());
}
}
ImGui::SameLine(); ImGui::Text("Programa actual: %s", currentlyLoaded.c_str());
}
ImGui::End();
}
//
// Created by administrador on 2/10/25.
//
#ifndef PAG_P1_SHADERWINDOW_H
#define PAG_P1_SHADERWINDOW_H
#include <string>
#include "GuiElement.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class ShaderWindow: public GuiElement {
std::string name;
std::string currentlyLoaded;
GLuint idVS;
GLuint idFS;
GLuint idSP;
bool loadShaders();
void restoreShader(GLuint& var, GLuint oldId);
public:
ShaderWindow();
virtual ~ShaderWindow() = default;
void warnListeners() override;
void render() override;
};
#endif //PAG_P1_SHADERWINDOW_H
\ No newline at end of file
// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.)
// This is also an example of how you may wrap your own similar types.
// Changelog:
// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
// See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki:
// https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
#pragma once
#ifndef IMGUI_DISABLE
#include <string>
namespace ImGui
{
// ImGui::InputText() with std::string
// Because text input needs dynamic resizing, we need to setup a callback to grow the capacity
IMGUI_API bool InputText(const char* label, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void* user_data = nullptr);
IMGUI_API bool InputTextMultiline(const char* label, std::string* str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void* user_data = nullptr);
IMGUI_API bool InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = nullptr, void* user_data = nullptr);
}
#endif // #ifndef IMGUI_DISABLE
// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.)
// This is also an example of how you may wrap your own similar types.
// Changelog:
// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
// See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki:
// https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
#include "imgui.h"
#ifndef IMGUI_DISABLE
#include "imgui_stdlib.h"
// Clang warnings with -Weverything
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
#endif
struct InputTextCallback_UserData
{
std::string* Str;
ImGuiInputTextCallback ChainCallback;
void* ChainCallbackUserData;
};
static int InputTextCallback(ImGuiInputTextCallbackData* data)
{
InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
{
// Resize string callback
// If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
std::string* str = user_data->Str;
IM_ASSERT(data->Buf == str->c_str());
str->resize(data->BufTextLen);
data->Buf = (char*)str->c_str();
}
else if (user_data->ChainCallback)
{
// Forward to user callback, if any
data->UserData = user_data->ChainCallbackUserData;
return user_data->ChainCallback(data);
}
return 0;
}
bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
}
bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data);
}
bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
cb_user_data.ChainCallbackUserData = user_data;
return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif // #ifndef IMGUI_DISABLE
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