Ejercicio 3 agregado

parent e1ffe576
......@@ -9,8 +9,8 @@
#include "Renderer.h"
#include <cstdarg>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "ShaderException.h"
#include "glad/glad.h"
......@@ -98,29 +98,35 @@ void PAG::Renderer::refrescar_tamanio_framebuffer(int width, int height) {
/**
* Función para crear, compilar y enlazar el shader program
* @note No se incluye ninguna comprobación de errores
* @param nombre Nombre de los archivos a cargar
* @note Se cargarán los archivos <nombre>-vs.glsl y <nombre>-fs.glsl
*/
void PAG::Renderer::creaShaderProgram() {
std::string miVertexShader =
"#version 410\n"
"layout (location = 0) in vec3 posicion;\n"
"void main()\n"
"{ gl_Position = vec4 ( posicion, 1 );\n"
"}\n";
std::string miFragmentShader =
"#version 410\n"
"out vec4 colorFragmento;\n"
"void main()\n"
"{ colorFragmento = vec4 ( 1.0, .4, .2, 1.0 );\n"
"}\n";
void PAG::Renderer::creaShaderProgram(std::string& nombre) {
std::ifstream lectorArchivos;
// Creando VS
idVS = glCreateShader(GL_VERTEX_SHADER);
if (idVS == 0) {
throw ShaderException(GL_VERTEX_SHADER, true, "");
throw ShaderException(GL_VERTEX_SHADER, CREACION, "");
}
// Leyendo VS
std::string nombreArchivoVS = "./";
nombreArchivoVS.append(nombre).append("-vs.glsl");
lectorArchivos.open(nombreArchivoVS);
if(!lectorArchivos.is_open()) {
std::string error = "Couldn't open file ";
error.append(nombreArchivoVS);
throw ShaderException(GL_VERTEX_SHADER, LECTURA, error);
}
const GLchar* fuenteVS = miVertexShader.c_str();
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 );
......@@ -137,16 +143,33 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
throw ShaderException(GL_VERTEX_SHADER, false, mensaje);
throw ShaderException(GL_VERTEX_SHADER, LECTURA, mensaje);
}
}
// Creando FS
idFS = glCreateShader(GL_FRAGMENT_SHADER);
if (idFS == 0) {
throw ShaderException(GL_FRAGMENT_SHADER, true, "");
throw ShaderException(GL_FRAGMENT_SHADER, CREACION, "");
}
const GLchar* fuenteFS = miFragmentShader.c_str();
// Leyendo FS
std::string nombreArchivoFS = "./";
nombreArchivoFS.append(nombre).append("-fs.glsl");
lectorArchivos.open(nombreArchivoFS);
if(!lectorArchivos.is_open()) {
std::string error = "Couldn't open file ";
error.append(nombreArchivoFS);
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 )
......@@ -162,16 +185,19 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
throw ShaderException(GL_FRAGMENT_SHADER, false, mensaje);
throw ShaderException(GL_FRAGMENT_SHADER, LECTURA, mensaje);
}
}
// Creando SP
idSP = glCreateProgram();
if(idSP == 0) {
throw ShaderException(0, true, "");
throw ShaderException(0, CREACION, "");
}
glAttachShader(idSP, idVS);
glAttachShader(idSP, idFS);
// Enlazando SP
glLinkProgram(idSP);
GLint resultadoEnlazado = 0;
glGetProgramiv ( idSP, GL_LINK_STATUS, &resultadoEnlazado );
......@@ -187,7 +213,7 @@ void PAG::Renderer::creaShaderProgram() {
mensaje.assign ( mensajeFormatoC );
delete[] mensajeFormatoC;
mensajeFormatoC = nullptr;
throw ShaderException(0, false, mensaje);
throw ShaderException(0, ENLAZADO, mensaje);
}
}
}
......
......@@ -12,6 +12,7 @@
#include "Listener.h"
#include <glm/vec3.hpp>
#include "glad/glad.h"
#include <string>
/**
* Espacio de nombres para las prácticas de Programación de Aplicaciones
......@@ -46,7 +47,7 @@ namespace PAG {
void refrescar();
void refrescar_tamanio_framebuffer(int width, int height);
void creaShaderProgram();
void creaShaderProgram(std::string& nombre);
void creaModelo();
void wakeUp(WindowType t, ...) override;
......
......@@ -17,23 +17,23 @@
* @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)) {}
ShaderException::ShaderException(int shaderType, TipoError errorType, std::string log):
shaderType(shaderType), errorType(errorType), 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) {}
shaderType(other.shaderType), errorType(other.errorType), 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->shaderType = other.shaderType;
this->errorType = other.errorType;
this->log = other.log;
return *this;
}
......@@ -44,15 +44,23 @@ ShaderException & ShaderException::operator=(ShaderException const &other) {
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 "); }
switch(errorType) {
case CREACION:
aux.append("create ");
break;
case LECTURA:
aux.append("read ");
break;
case COMPILACION:
aux.append("compile ");
break;
case ENLAZADO:
aux.append("link ");
break;
}
if(type == GL_FRAGMENT_SHADER) { aux.append("fragment shader"); }
else if(type == GL_VERTEX_SHADER) { aux.append("vertex shader"); }
if(shaderType == GL_FRAGMENT_SHADER) { aux.append("fragment shader"); }
else if(shaderType == GL_VERTEX_SHADER) { aux.append("vertex shader"); }
else { aux.append("shader program"); }
aux.append(":\n").append(log).append("\n");
......
......@@ -11,17 +11,24 @@
#define SHADEREXCEPTION_H
#include <string>
enum TipoError {
CREACION,
LECTURA,
COMPILACION,
ENLAZADO
};
/**
* 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
int shaderType = 0; ///< Tipo de shader que ha causado el error
TipoError errorType = CREACION; ///< 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(int shaderType, TipoError errorType, std::string log);
ShaderException(ShaderException &other);
virtual ~ShaderException() = default;
......
......@@ -122,7 +122,8 @@ int main() {
PAG::GUI::getInstancia().registrarMensaje(std::string((const char*)glGetString ( GL_SHADING_LANGUAGE_VERSION )));
try {
PAG::Renderer::getInstancia().creaShaderProgram();
std::string nombre = "pag03";
PAG::Renderer::getInstancia().creaShaderProgram(nombre);
}catch(ShaderException e) {
PAG::GUI::getInstancia().registrarMensaje(e.getLog());
}
......
#version 410
out vec4 colorFragmento;
void main() {
colorFragmento = vec4 ( 1.0, .4, .2, 1.0 );
}
#version 410
layout (location = 0) in vec3 posicion;
void main() {
gl_Position = vec4 ( posicion, 1 );
}
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