Laboratorio #7 - OpenGL - Programacion Grafica 2024
Laboratorio #7 - OpenGL - Programacion Grafica 2024
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);
#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);
// 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);
void Texture::Bind()
{
glBindTexture(type, ID);
}
void Texture::Unbind()
{
glBindTexture(type, 0);
}
void Texture::Delete()
{
glDeleteTextures(1, &ID);
}
default.vert
// Positions/Coordinates
layout (location = 0) in vec3 aPos;
// Colors
layout (location = 1) in vec3 aColor;
// Texture Coordinates
layout (location = 2) in vec2 aTex;
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"
shaderClass.cpp
#include"shaderClass.h"
}
throw(errno);
}
void Shader::Activate()
{
glUseProgram(ID);
}
void Shader::Delete()
{
glUseProgram(ID);
}
EBO.cpp
#include"EBO.h"
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);
#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();
#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>
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);
#endif
default.frag
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"
//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
};
};
int main()
{
// Initialize GLFW
glfwInit();
//Texture
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
//stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);
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"
//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
};
};
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);
//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);
//stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_DEPTH_TEST);
rotation += 0.5f;
prevTime = crntTime;