Clase ShaderException creada

parent 7f4579cb
......@@ -7,30 +7,32 @@ file( GLOB MY_FILES *.cpp )
file( GLOB GLAD_FILES glad/src/*.c )
file( GLOB IMGUI_FILES imgui/src/*.cpp)
add_executable(PAG_p1 ${MY_FILES} ${GLAD_FILES} ${IMGUI_FILES})
add_executable(PAG_p1 ${MY_FILES} ${GLAD_FILES} ${IMGUI_FILES}
ShaderException.cpp
ShaderException.h)
target_include_directories(PAG_p1 PUBLIC glad/include imgui/include )
# Uncomment the following when using Conan, comment (# at line start) otherwise
find_package(opengl_system)
find_package(glfw3)
find_package(glm)
#find_package(opengl_system)
#find_package(glfw3)
#find_package(glm)
# Then, link your executable or library with the corresponding package targets:
target_link_libraries(PAG_p1 opengl::opengl)
target_link_libraries(PAG_p1 glfw)
target_link_libraries(PAG_p1 glm::glm)
#target_link_libraries(PAG_p1 opengl::opengl)
#target_link_libraries(PAG_p1 glfw)
#target_link_libraries(PAG_p1 glm::glm)
#===========================================
# Uncomment the following when using Linux, comment (# at line start) otherwise
#find_package(glfw3 3.3 REQUIRED)
#find_package(OpenGL REQUIRED)
find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)
#target_include_directories(PAG_p1 PUBLIC
# $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
# $<INSTALL_INTERFACE:include>)
#target_link_libraries(PAG_p1 PUBLIC glfw OpenGL::GL ${CMAKE_DL_LIBS})
target_include_directories(PAG_p1 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(PAG_p1 PUBLIC glfw OpenGL::GL ${CMAKE_DL_LIBS})
......@@ -11,6 +11,8 @@
#include <cstdarg>
#include <iostream>
#include <string>
#include "ShaderException.h"
#include "glad/glad.h"
PAG::Renderer* PAG::Renderer::instancia = nullptr;
......@@ -115,8 +117,7 @@ void PAG::Renderer::creaShaderProgram() {
idVS = glCreateShader(GL_VERTEX_SHADER);
if (idVS == 0) {
std::cout << "Cannot create vertex shader object." << std::endl;
return;
throw ShaderException(GL_VERTEX_SHADER, true, "");
}
const GLchar* fuenteVS = miVertexShader.c_str();
glShaderSource(idVS, 1, &fuenteVS, nullptr);
......@@ -136,15 +137,13 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
throw std::runtime_error(mensaje);
throw ShaderException(GL_VERTEX_SHADER, false, mensaje);
}
}
idFS = glCreateShader(GL_FRAGMENT_SHADER);
if (idFS == 0) {
std::cout << "Cannot create fragment shader object." << std::endl;
// TODO: Cambiar por excepción
return;
throw ShaderException(GL_FRAGMENT_SHADER, true, "");
}
const GLchar* fuenteFS = miFragmentShader.c_str();
glShaderSource(idFS, 1, &fuenteFS, nullptr);
......@@ -163,14 +162,13 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
throw std::runtime_error(mensaje);
throw ShaderException(GL_FRAGMENT_SHADER, false, mensaje);
}
}
idSP = glCreateProgram();
if(idSP == 0) {
std::cout << "Cannot create shader program" << std::endl;
return;
throw ShaderException(0, true, "");
}
glAttachShader(idSP, idVS);
glAttachShader(idSP, idFS);
......@@ -189,8 +187,7 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
std::cout << "Cannot link shader program" << std::endl;
std::cout << mensaje << std::endl;
throw ShaderException(0, false, mensaje);
}
}
}
......
/**
* @file ShaderException.cpp
* @author dpp00022
*
* @date 2025/08/25
*
* @brief Implementación de la clase ShaderException
*/
#include "ShaderException.h"
#include "glad/glad.h"
#include <utility>
/**
* Constructor parametrizado de la clase
* @param type Tipo de shader que ha causado el error
* @param creating Si el error ha ocurrido durante la creación o no
* @param log Descripción del error
*/
ShaderException::ShaderException(int type, bool creating, std::string log):
type(type), creating(creating), log(std::move(log)) {}
/**
* Constructor copia de la clase
* @param other Excepción que se desea copiar
*/
ShaderException::ShaderException(ShaderException &other):
type(other.type), creating(other.creating), log(other.log) {}
/**
* Operador copia de la clase
* @param other Excepción que se desea copiar
*/
ShaderException & ShaderException::operator=(ShaderException const &other) {
this->type = other.type;
this->creating = other.creating;
this->log = other.log;
return *this;
}
/**
* Devuelve un string con el log completo
*/
std::string ShaderException::getLog() {
std::string aux = "Cannot ";
if(creating) { aux.append("create "); }
else {
if(type == GL_FRAGMENT_SHADER ||
type == GL_VERTEX_SHADER) { aux.append("compile "); }
else { aux.append("link "); }
}
if(type == GL_FRAGMENT_SHADER) { aux.append("fragment shader"); }
else if(type == GL_VERTEX_SHADER) { aux.append("vertex shader"); }
else { aux.append("shader program"); }
aux.append(":\n").append(log).append("\n");
return log;
}
/**
* Mét0do what() de excepción. Solo devuelve la descripción, sin la cabecera.
*/
const char * ShaderException::what() const noexcept {
return log.c_str();
}
/**
* @file ShaderException.h
* @author dpp00022
*
* @date 2025/09/25
*
* @brief Declaración de la clase ShaderException
*/
#ifndef SHADEREXCEPTION_H
#define SHADEREXCEPTION_H
#include <string>
/**
* Clase que representa una excepción personalizada para manejar
* los errores que puedan suceder con los Shaders.
*/
class ShaderException: public std::exception{
int type = 0; ///< Tipo de shader que ha causado el error
bool creating = false; ///< Si el error ha ocurrido durante la creación o no
std::string log; ///< Descripción del error
public:
ShaderException() = default;
ShaderException(int type, bool creating, std::string log);
ShaderException(ShaderException &other);
virtual ~ShaderException() = default;
ShaderException& operator=(ShaderException const& other);
std::string getLog();
const char * what() const noexcept override;
};
#endif //SHADEREXCEPTION_H
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