Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
José Pardo Madera
/
pag_practicas_2025
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f185c956
authored
Sep 25, 2025
by
José Pardo Madera
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Apartado 3 practica 3: Movido código de los shaders a archivos aparte
parent
28fcc7e6
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
17 deletions
main.cpp
pag03-fs.glsl
pag03-vs.glsl
renderer.cpp
renderer.h
main.cpp
View file @
f185c956
...
...
@@ -118,7 +118,7 @@ int main() {
renderer
.
inicializaOpenGL
();
renderer
.
setClearColor
(
ventana_color
->
getColor
());
renderer
.
creaShaderProgram
();
renderer
.
creaShaderProgram
(
"pag03"
);
renderer
.
creaModelo
();
while
(
!
glfwWindowShouldClose
(
window
))
{
...
...
pag03-fs.glsl
0 → 100644
View file @
f185c956
#version 410
out
vec4
colorFragmento
;
void
main
()
{
colorFragmento
=
vec4
(
1
.
0
,
.
4
,
.
2
,
1
.
0
);
}
pag03-vs.glsl
0 → 100644
View file @
f185c956
#version 410
layout
(
location
=
0
)
in
vec3
posicion
;
void
main
()
{
gl_Position
=
vec4
(
posicion
,
1
);
}
renderer.cpp
View file @
f185c956
#include "renderer.h"
#include <glad/glad.h>
#include <fstream>
#include <stdexcept>
#include "log.h"
...
...
@@ -69,6 +71,23 @@ static bool enlazarPrograma(
return
true
;
}
/// Helper que devuelve el contenido de un archivo como un string
/// @throws std::runtime_error si no puede cargar el archivo
static
std
::
string
leerArchivo
(
std
::
string
nombre
)
{
std
::
ifstream
archivo
;
archivo
.
open
(
nombre
);
if
(
!
archivo
)
{
throw
std
::
runtime_error
(
"Cannot open file"
);
}
std
::
stringstream
streamContenidos
;
streamContenidos
<<
archivo
.
rdbuf
();
archivo
.
close
();
return
streamContenidos
.
str
();
}
namespace
PAG
{
// Basado en https://stackoverflow.com/a/1008289
Renderer
&
Renderer
::
getInstance
()
{
...
...
@@ -125,27 +144,32 @@ namespace PAG {
}
}
void
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
"
;
void
Renderer
::
creaShaderProgram
(
std
::
string
prefijo
)
{
std
::
string
nombreVertexShader
=
prefijo
+
"-vs.glsl"
;
std
::
string
nombreFragmentShader
=
prefijo
+
"-fs.glsl"
;
std
::
string
miFragmentShader
=
"#version 410
\n
"
"out vec4 colorFragmento;
\n
"
"void main() {
\n
"
" colorFragmento = vec4(1.0, .4, .2, 1.0);
\n
"
"}
\n
"
;
std
::
string
vertexShader
;
try
{
vertexShader
=
leerArchivo
(
nombreVertexShader
);
}
catch
(
std
::
runtime_error
&
e
)
{
PAG
::
log
<<
"No se pudo leer el archivo
\"
"
<<
nombreVertexShader
<<
"
\"
"
<<
std
::
endl
;
return
;
}
std
::
string
fragmentShader
;
try
{
fragmentShader
=
leerArchivo
(
nombreFragmentShader
);
}
catch
(
std
::
runtime_error
&
e
)
{
PAG
::
log
<<
"No se pudo leer el archivo
\"
"
<<
nombreFragmentShader
<<
"
\"
"
<<
std
::
endl
;
return
;
}
idVS
=
glCreateShader
(
GL_VERTEX_SHADER
);
if
(
idVS
==
0
)
{
PAG
::
log
<<
"No se pudo crear vertex shader."
<<
std
::
endl
;
return
;
}
if
(
!
compilarShader
(
idVS
,
"vertex-shader"
,
miV
ertexShader
))
{
if
(
!
compilarShader
(
idVS
,
nombreVertexShader
,
v
ertexShader
))
{
return
;
}
...
...
@@ -154,7 +178,7 @@ namespace PAG {
PAG
::
log
<<
"No se pudo crear fragment shader"
<<
std
::
endl
;
return
;
}
if
(
!
compilarShader
(
idFS
,
"fragment-shader"
,
miF
ragmentShader
))
{
if
(
!
compilarShader
(
idFS
,
nombreFragmentShader
,
f
ragmentShader
))
{
return
;
}
...
...
renderer.h
View file @
f185c956
...
...
@@ -31,7 +31,7 @@ namespace PAG {
void
wakeUp
(
Ventana
&
)
override
;
void
creaShaderProgram
();
void
creaShaderProgram
(
std
::
string
prefijo_archivos
);
void
creaModelo
();
private
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment