Apartado 1 practica 3: Dibujar triangulo

parent 0b1250b5
Showing with 98 additions and 5 deletions
......@@ -115,9 +115,11 @@ int main() {
glfwSetScrollCallback(window, scroll_callback);
renderer.inicializaOpenGL();
renderer.setClearColor(ventana_color->getColor());
renderer.enableDepthTest();
renderer.creaShaderProgram();
renderer.creaModelo();
while (!glfwWindowShouldClose(window)) {
renderer.clear();
......
#include "renderer.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "log.h"
#include "ventana_color.h"
......@@ -16,6 +17,12 @@ namespace PAG {
void Renderer::clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glUseProgram(idSP);
glBindVertexArray(idVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idIBO);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
}
void Renderer::setClearColor(std::array<float, 3> rgb) {
......@@ -30,7 +37,8 @@ namespace PAG {
glViewport(0, 0, width, height);
}
void Renderer::enableDepthTest() {
void Renderer::inicializaOpenGL() {
setClearColor(0, 0, 0, 0);
glEnable(GL_DEPTH_TEST);
}
......@@ -54,7 +62,77 @@ 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";
std::string miFragmentShader =
"#version 410\n"
"out vec4 colorFragmento;\n"
"void main() {\n"
" colorFragmento = vec4(1.0, .4, .2, 1.0);\n"
"}\n";
idVS = glCreateShader(GL_VERTEX_SHADER);
const GLchar *fuenteVS = miVertexShader.c_str();
glShaderSource(idVS, 1, &fuenteVS, nullptr);
glCompileShader(idVS);
idFS = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar *fuenteFS = miFragmentShader.c_str();
glShaderSource(idFS, 1, &fuenteFS, nullptr);
glCompileShader(idFS);
idSP = glCreateProgram();
glAttachShader(idSP, idVS);
glAttachShader(idSP, idFS);
glLinkProgram(idSP);
}
void Renderer::creaModelo() {
GLfloat vertices[] = {
-0.5, -0.5, 0,
0.5, -0.5, 0,
0 , 0.5, 0
};
GLuint indices[] = {0, 1, 2};
glGenVertexArrays(1, &idVAO);
glBindVertexArray(idVAO);
glGenBuffers(1, &idVBO);
glBindBuffer(GL_ARRAY_BUFFER, idVBO);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glGenBuffers(1, &idIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(GLuint), indices, GL_STATIC_DRAW);
}
Renderer::Renderer() {};
Renderer::~Renderer() {};
Renderer::~Renderer() {
if (idVS) {
glDeleteShader(idVS);
}
if (idFS) {
glDeleteShader(idFS);
}
if (idSP) {
glDeleteProgram(idSP);
}
if (idVBO) {
glDeleteBuffers(1, &idVBO);
}
if (idIBO) {
glDeleteBuffers(1, &idIBO);
}
if (idVAO) {
glDeleteVertexArrays(1, &idVAO);
}
};
}
......@@ -3,6 +3,8 @@
#include <string>
#include <array>
#include <glad/glad.h>
#include "ventana.h"
namespace PAG {
......@@ -20,7 +22,7 @@ namespace PAG {
void setClearColor(float, float, float, float);
void setViewport(int width, int height);
void enableDepthTest();
void inicializaOpenGL();
std::string getRendererName();
std::string getVendor();
......@@ -28,7 +30,18 @@ namespace PAG {
std::string getShadingLanguageVersion();
void wakeUp(Ventana &) override;
void creaShaderProgram();
void creaModelo();
private:
GLuint idVS = 0;
GLuint idFS = 0;
GLuint idSP = 0;
GLuint idVAO = 0;
GLuint idVBO = 0;
GLuint idIBO = 0;
Renderer();
~Renderer();
};
......
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