0% found this document useful (0 votes)
72 views

Laboratorio #7 - OpenGL - Programacion Grafica 2024

Guia Opengl

Uploaded by

Donovan Toledo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Laboratorio #7 - OpenGL - Programacion Grafica 2024

Guia Opengl

Uploaded by

Donovan Toledo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Para este laboratorio y los siguientes es necesario trabajar con una librería que se llama “glm”.

Esta
librería se puede descargar en el siguiente:
https://glm.g-truc.net/0.9.9/index.html

Por lo tanto, el código del archivo Main.cpp quedara de la siguiente forma en la parte superior con la
nueva librería incorporada:

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<stb/stb_image.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

#include"Texture.h"
#include"shaderClass.h"
#include"VAO.h"
#include"VBO.h"
#include"EBO.h"

Las librerías que se encuentran resaltadas son las que nos ayudaran a trabajar con las matrices para brindar
un ambiente en 3D, además de incluir el eje Z para manipulación de los objetos y elementos que se quieran
dibujar en la ventana utilizando OpenGL.

Texture.h

#ifndef TEXTURE_CLASS_H
#define TEXTURE_CLASS_H

#include<glad/glad.h>
#include<stb/stb_image.h>

#include"shaderClass.h"

class Texture
{
public:
GLuint ID;
GLenum type;
Texture(const char* image, GLenum texType, GLenum slot, GLenum format, GLenum
pixelType);

// Assigns a texture unit to a texture


void texUnit(Shader& shader, const char* uniform, GLuint unit);
// Binds a texture
void Bind();
// Unbinds a texture
void Unbind();
// Deletes a texture
void Delete();
};
#endif
Texture.cpp

#include"Texture.h"

Texture::Texture(const char* image, GLenum texType, GLenum slot, GLenum format, GLenum
pixelType)
{
// Assigns the type of the texture ot the texture object
type = texType;

// Stores the width, height, and the number of color channels of the image
int widthImg, heightImg, numColCh;
// Flips the image so it appears right side up
stbi_set_flip_vertically_on_load(true);
// Reads the image from a file and stores it in bytes
unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 0);

// Generates an OpenGL texture object


glGenTextures(1, &ID);
// Assigns the texture to a Texture Unit
glActiveTexture(slot);
glBindTexture(texType, ID);

// Configures the type of algorithm that is used to make the image smaller or
bigger
glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Configures the way the texture repeats (if it does at all)


glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);

// Extra lines in case you choose to use GL_CLAMP_TO_BORDER


// float flatColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
// glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, flatColor);

// Assigns the image to the OpenGL Texture object


glTexImage2D(texType, 0, GL_RGBA, widthImg, heightImg, 0, format, pixelType,
bytes);
// Generates MipMaps
glGenerateMipmap(texType);

// Deletes the image data as it is already in the OpenGL Texture object


stbi_image_free(bytes);

// Unbinds the OpenGL Texture object so that it can't accidentally be modified


glBindTexture(texType, 0);
}

void Texture::texUnit(Shader& shader, const char* uniform, GLuint unit)


{
// Gets the location of the uniform
GLuint texUni = glGetUniformLocation(shader.ID, uniform);
// Shader needs to be activated before changing the value of a uniform
shader.Activate();
// Sets the value of the uniform
glUniform1i(texUni, unit);
}

void Texture::Bind()
{
glBindTexture(type, ID);
}

void Texture::Unbind()
{
glBindTexture(type, 0);
}

void Texture::Delete()
{
glDeleteTextures(1, &ID);
}

default.vert

#version 330 core

// Positions/Coordinates
layout (location = 0) in vec3 aPos;
// Colors
layout (location = 1) in vec3 aColor;
// Texture Coordinates
layout (location = 2) in vec2 aTex;

// Outputs the color for the Fragment Shader


out vec3 color;
// Outputs the texture coordinates to the fragment shader
out vec2 texCoord;

// Controls the scale of the vertices


uniform float scale;

uniform mat4 model;


uniform mat4 view;
uniform mat4 proj;

void main()
{
// Outputs the positions/coordinates of all vertices
gl_Position = proj * view * model * vec4(aPos, 1.0);
// Assigns the colors from the Vertex Data to "color"
color = aColor;
// Assigns the texture coordinates from the Vertex Data to "texCoord"
texCoord = aTex;
}
VBO.cpp

#include"VBO.h"

// Constructor that generates a Vertex Buffer Object and links it to vertices


VBO::VBO(GLfloat* vertices, GLsizeiptr size)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
}

// Binds the VBO


void VBO::Bind()
{
glBindBuffer(GL_ARRAY_BUFFER, ID);
}

// Unbinds the VBO


void VBO::Unbind()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

// Deletes the VBO


void VBO::Delete()
{
glDeleteBuffers(1, &ID);
}

shaderClass.cpp

#include"shaderClass.h"

std::string get_file_contents(const char* filename)


{
std::ifstream in(filename, std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);

}
throw(errno);
}

//Procedemos a contruir el constructor de sombras

Shader::Shader(const char* vertexFile, const char* fragmentFile)


{
std::string vertexCode = get_file_contents(vertexFile);
std::string fragmentCode = get_file_contents(fragmentFile);

const char* vertexSource = vertexCode.c_str();


const char* fragmentSource = fragmentCode.c_str();

//Create Vertex Shader Object and get its reference


GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

//Attach Vertex Shader source to the Vertex Shader Object


glShaderSource(vertexShader, 1, &vertexSource, NULL);

//Compile the Vertex Shader into machine code


glCompileShader(vertexShader);

//Create Fragment Shader Object and get its reference


GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

//Attach Fragment Shader source to the Fragment Shader Object


glShaderSource(fragmentShader, 1, &fragmentSource, NULL);

//Compile the Vertex Shader into machine code


glCompileShader(fragmentShader);

//Create Shader Program Object and get its reference


ID = glCreateProgram();

//Attach the Vertex and Fragment Shaders to the Shader Program


glAttachShader(ID, vertexShader);
glAttachShader(ID, fragmentShader);

//Link all the shaders together into the Shader Program


glLinkProgram(ID);

//Delete the now useless Vertex and Fragment Shader Objects


glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}

void Shader::Activate()
{
glUseProgram(ID);
}

void Shader::Delete()
{
glUseProgram(ID);
}

EBO.cpp

#include"EBO.h"

// Constructor that generates a Elements Buffer Object and links it to indices


EBO::EBO(GLuint* indices, GLsizeiptr size)
{
glGenBuffers(1, &ID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
}

// Binds the EBO


void EBO::Bind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ID);
}

// Unbinds the EBO


void EBO::Unbind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

// Deletes the EBO


void EBO::Delete()
{
glDeleteBuffers(1, &ID);
}

VBO.h

#ifndef VBO_CLASS_H
#define VBO_CLASS_H

#include<glad/glad.h>

class VBO
{
public:
// Reference ID of the Vertex Buffer Object
GLuint ID;
// Constructor that generates a Vertex Buffer Object and links it to vertices
VBO(GLfloat* vertices, GLsizeiptr size);

// Binds the VBO


void Bind();
// Unbinds the VBO
void Unbind();
// Deletes the VBO
void Delete();
};

#endif

VAO.h

#ifndef VAO_CLASS_H
#define VAO_CLASS_H

#include<glad/glad.h>
#include"VBO.h"

class VAO
{
public:
// ID reference for the Vertex Array Object
GLuint ID;
// Constructor that generates a VAO ID
VAO();

// Links a VBO Attribute such as a position or color to the VAO


void LinkAttrib(VBO& VBO, GLuint layout, GLuint numComponents, GLenum type,
GLsizeiptr stride, void* offset);
// Binds the VAO
void Bind();
// Unbinds the VAO
void Unbind();
// Deletes the VAO
void Delete();
};

#endif

shaderClass.h

#ifndef SHADER_CLASS_H
#define SHADER_CLASS_H

#include<glad/glad.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<iostream>
#include<cerrno>

std::string get_file_contents(const char* filename);

class Shader {

public:
GLuint ID;
Shader(const char* vertexFile, const char* fragmentFile);

void Activate();
void Delete();
};

#endif

EBO.h

#ifndef EBO_CLASS_H
#define EBO_CLASS_H

#include<glad/glad.h>

class EBO
{
public:
// ID reference of Elements Buffer Object
GLuint ID;
// Constructor that generates a Elements Buffer Object and links it to indices
EBO(GLuint* indices, GLsizeiptr size);

// Binds the EBO


void Bind();
// Unbinds the EBO
void Unbind();
// Deletes the EBO
void Delete();
};

#endif

default.frag

#version 330 core

// Outputs colors in RGBA


out vec4 FragColor;

// Inputs the color from the Vertex Shader


in vec3 color;
// Inputs the texture coordinates from the Vertex Shader
in vec2 texCoord;

// Gets the Texture Unit from the main function


uniform sampler2D tex0;

void main()
{
FragColor = texture(tex0, texCoord);
}

Main.cpp

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<stb/stb_image.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

#include"Texture.h"
#include"shaderClass.h"
#include"VAO.h"
#include"VBO.h"
#include"EBO.h"

const unsigned int width = 800;


const unsigned int height = 800;

//Coordenadas de Vertices

GLfloat vertices[] =
{
// COORDENADAS COLORES COORDENADAS
PARA LA TEXTURE
-0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f,
-0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f,
0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f,
0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f,
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f
};

// Indices for vertices order


GLuint indices[] =
{
0, 1, 2,
0, 2, 3,
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4

};

int main()
{
// Initialize GLFW
glfwInit();

// Tell GLFW what version of OpenGL we are using


// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Create a GLFWwindow object of 800 by 800 pixels


GLFWwindow* window = glfwCreateWindow(width, height, "Bienvenidos al 3D usando
OpenGL", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);

//Load GLAD so it configures OpenGL


gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
glViewport(0, 0, width, height);

// Generates Shader object using shaders defualt.vert and default.frag


Shader shaderProgram("default.vert", "default.frag");

// Generates Vertex Array Object and binds it


VAO VAO1;
VAO1.Bind();

// Generates Vertex Buffer Object and links it to vertices


VBO VBO1(vertices, sizeof(vertices));
// Generates Element Buffer Object and links it to indices
EBO EBO1(indices, sizeof(indices));

// Links VBO attributes such as coordinates and colors to VAO


VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 8 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 8 * sizeof(float), (void*)(3 *
sizeof(float)));
VAO1.LinkAttrib(VBO1, 2, 2, GL_FLOAT, 8 * sizeof(float), (void*)(6 *
sizeof(float)));

// Unbind all to prevent accidentally modifying them


VAO1.Unbind();
VBO1.Unbind();
EBO1.Unbind();

//Gets ID of uniform called "scale"


GLuint uniID = glGetUniformLocation(shaderProgram.ID, "scale");

//Texture

int widthImg, heightImg, numColCh;


stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load("redbrickwall.jpg", &widthImg, &heightImg,
&numColCh, 0);
//Texture dog("dog.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGBA, GL_UNSIGNED_BYTE);
//dog.texUnit(shaderProgram, "tex0", 0);

GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//Para ubicar la textura


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthImg, heightImg, 0, GL_RGB,
GL_UNSIGNED_BYTE, bytes);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

//stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);

GLuint tex0Uni = glGetUniformLocation(shaderProgram.ID, "tex0");


shaderProgram.Activate();
glUniform1i(tex0Uni, 0);

// Main while loop


while (!glfwWindowShouldClose(window))
{
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT);
// Tell OpenGL which Shader Program we want to use
shaderProgram.Activate();

glm::mat4 model = glm::mat4(1.0f);


glm::mat4 view = glm::mat4(1.0f);
glm::mat4 proj = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, -0.5f, -2.0f));
proj = glm::perspective(glm::radians(45.05f), (float)(width / height),
0.1f, 100.0f);//Posicion de la Camara

int modelLoc = glGetUniformLocation(shaderProgram.ID, "model");


glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

int viewLoc = glGetUniformLocation(shaderProgram.ID, "view");


glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

int projLoc = glGetUniformLocation(shaderProgram.ID, "proj");


glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));

// Assigns a value to the uniform


shaderProgram.Activate();
//Assigns a value to the uniform
glUniform1f(uniID, 0.5f);
glBindTexture(GL_TEXTURE_2D, texture);
// Bind the VAO so OpenGL knows to use it
VAO1.Bind();
// Draw primitives, number of indices, datatype of indices, index of
indices
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(int), GL_UNSIGNED_INT,
0);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}

// Delete all the objects we've created


VAO1.Delete();
VBO1.Delete();
EBO1.Delete();
glDeleteTextures(1, &texture);
shaderProgram.Delete();
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}

Resultado de Incorporación de Textura sin activar la vista ni proyección.

Pregunta de Análisis: ¿por qué la textura aparece con esas líneas oscuras si la imagen original que se
está utilizando es la siguiente?:
Pregunta de Análisis: ¿Por qué con esta textura el resultado es diferente al de la ejecución anterior?:
Procediendo a modificar el archivo Main.cpp para agregar la funcionalidad de la rotación y activar la
perspectiva. La pirámide ya posee la textura incorporada y lo que se necesita es visualizar todos los lados
teniendo una rotación especifica. Por lo tanto, el resultado es el siguiente:

Main.cpp

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<stb/stb_image.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>

#include"Texture.h"
#include"shaderClass.h"
#include"VAO.h"
#include"VBO.h"
#include"EBO.h"

const unsigned int width = 800;


const unsigned int height = 800;

//Coordenadas de Vertices

GLfloat vertices[] =
{
// COORDENADAS COLORES COORDENADAS
PARA LA TEXTURE
-0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f,
-0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f,
0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f,
0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f,
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f
};

// Indices for vertices order


GLuint indices[] =
{
0, 1, 2,
0, 2, 3,
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4

};

int main()
{
// Initialize GLFW
glfwInit();
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// Create a GLFWwindow object of 800 by 800 pixels, naming it "YoutubeOpenGL"


GLFWwindow* window = glfwCreateWindow(width, height, "Bienvenidos al 3D usando
OpenGL", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);

//Load GLAD so it configures OpenGL


gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
glViewport(0, 0, width, height);

// Generates Shader object using shaders defualt.vert and default.frag


Shader shaderProgram("default.vert", "default.frag");

// Generates Vertex Array Object and binds it


VAO VAO1;
VAO1.Bind();

// Generates Vertex Buffer Object and links it to vertices


VBO VBO1(vertices, sizeof(vertices));
// Generates Element Buffer Object and links it to indices
EBO EBO1(indices, sizeof(indices));

// Links VBO attributes such as coordinates and colors to VAO


VAO1.LinkAttrib(VBO1, 0, 3, GL_FLOAT, 8 * sizeof(float), (void*)0);
VAO1.LinkAttrib(VBO1, 1, 3, GL_FLOAT, 8 * sizeof(float), (void*)(3 *
sizeof(float)));
VAO1.LinkAttrib(VBO1, 2, 2, GL_FLOAT, 8 * sizeof(float), (void*)(6 *
sizeof(float)));

// Unbind all to prevent accidentally modifying them


VAO1.Unbind();
VBO1.Unbind();
EBO1.Unbind();

//Gets ID of uniform called "scale"


GLuint uniID = glGetUniformLocation(shaderProgram.ID, "scale");

//Texture
int widthImg, heightImg, numColCh;
stbi_set_flip_vertically_on_load(true);
unsigned char* bytes = stbi_load("redbrickwall.png", &widthImg, &heightImg,
&numColCh, 0);
//Texture dog("dog.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGBA, GL_UNSIGNED_BYTE);
//dog.texUnit(shaderProgram, "tex0", 0);

GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//Para ubicar la textura


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthImg, heightImg, 0, GL_RGBA,
GL_UNSIGNED_BYTE, bytes);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);

//stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);

GLuint tex0Uni = glGetUniformLocation(shaderProgram.ID, "tex0");


shaderProgram.Activate();
glUniform1i(tex0Uni, 0);

//Variables para controlar la rotacion


float rotation = 0.0f;
double prevTime = glfwGetTime();

glEnable(GL_DEPTH_TEST);

// Main while loop


while (!glfwWindowShouldClose(window))
{
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Tell OpenGL which Shader Program we want to use
shaderProgram.Activate();

//Control de la Rotation de la Piramide con la textura incorporada por


medio de la funcion del tiempo.
double crntTime = glfwGetTime();
if (crntTime - prevTime >= 1/60)
{

rotation += 0.5f;
prevTime = crntTime;

//Inicializacion de las matrices para ser utilizadas


glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 proj = glm::mat4(1.0f);

//Asignamos diferentes transformaciones a las matrices que tenemos


previamente declaradas
//Para controlar la rotacion de la piramide
model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 1.0f,
0.0f));

//Para establecer la matriz de traslacion


view = glm::translate(view, glm::vec3(0.0f, -0.5f, -2.0f));

//Para establecer la proyeccion en perspectiva


proj = glm::perspective(glm::radians(45.05f), (float)(width / height),
0.1f, 100.0f);//Posicion de la Camara

//Entradas de las matrices para ser usadas en el Vertex Shader


int modelLoc = glGetUniformLocation(shaderProgram.ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

int viewLoc = glGetUniformLocation(shaderProgram.ID, "view");


glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

int projLoc = glGetUniformLocation(shaderProgram.ID, "proj");


glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));

// Assigns a value to the uniform


shaderProgram.Activate();
//Assigns a value to the uniform
glUniform1f(uniID, 0.5f);
glBindTexture(GL_TEXTURE_2D, texture);
// Bind the VAO so OpenGL knows to use it
VAO1.Bind();
// Draw primitives, number of indices, datatype of indices, index of
indices
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(int), GL_UNSIGNED_INT,
0);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}

// Delete all the objects we've created


VAO1.Delete();
VBO1.Delete();
EBO1.Delete();
glDeleteTextures(1, &texture);
shaderProgram.Delete();
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}}

You might also like