Laboratorio #9 - OpenGL - Programacion Grafica 2024
Laboratorio #9 - OpenGL - Programacion Grafica 2024
Objetivo: Implementar el Modelo de Phong en Graficas de Computadoras para tener iluminación en una
escena desarrollador en OpenGL.
VAO.cpp
#include"VAO.h"
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
light.vert
void main()
{
gl_Position = camMatrix * model * vec4(aPos, 1.0f);
}
light.frag
void main()
{
FragColor = lightColor;
}
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;
// Normals (not necessarily normalized)
layout (location = 3) in vec3 aNormal;
void main()
{
// calculates current position
crntPos = vec3(model * vec4(aPos, 1.0f));
// Outputs the positions/coordinates of all vertices
gl_Position = camMatrix * vec4(crntPos, 1.0);
default.frag
void main()
{
// ambient lighting
float ambient = 0.6f;
// diffuse lighting
vec3 normal = normalize(Normal);
vec3 lightDirection = normalize(lightPos - crntPos);
float diffuse = max(dot(normal, lightDirection), 0.0f);
// specular lighting
float specularLight = 1.50f;
vec3 viewDirection = normalize(camPos - crntPos);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specAmount = pow(max(dot(viewDirection, reflectionDirection), 0.0f), 5);
float specular = specAmount * specularLight;
VBO.cpp
#include"VBO.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
Camera.cpp
#include"Camara.h"
// Makes camera look in the right direction from the right position
view = glm::lookAt(Position, Position + Orientation, Up);
// Adds perspective to the scene
projection = glm::perspective(glm::radians(FOVdeg), (float)width / height,
nearPlane, farPlane);
// Normalizes and shifts the coordinates of the cursor such that they begin
in the middle of the screen
// and then "transforms" them into degrees
float rotX = sensitivity * (float)(mouseY - (height / 2)) / height;
float rotY = sensitivity * (float)(mouseX - (width / 2)) / width;
// Sets mouse cursor to the middle of the screen so that it doesn't end up
roaming around
glfwSetCursorPos(window, (width / 2), (height / 2));
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
// Unhides cursor since camera is not looking around anymore
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
// Makes sure the next time the camera looks around it doesn't jump
firstClick = true;
}
}
Camara.h
#ifndef CAMERA_CLASS_H
#define CAMERA_CLASS_H
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/type_ptr.hpp>
#include<glm/gtx/rotate_vector.hpp>
#include<glm/gtx/vector_angle.hpp>
#include"shaderClass.h"
class Camera
{
public:
// Stores the main vectors of the camera
glm::vec3 Position;
glm::vec3 Orientation = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 Up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 cameraMatrix = glm::mat4(1.0f);
// Prevents the camera from jumping around when first clicking left click
bool firstClick = true;
// Adjust the speed of the camera and it's sensitivity when looking around
float speed = 0.1f;
float sensitivity = 100.0f;
// Camera constructor to set up initial values
Camera(int width, int height, glm::vec3 position);
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
stb.cpp
#define STB_IMAGE_IMPLEMENTATION
#include<stb/stb_image.h>
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);
void Texture::Bind()
{
glBindTexture(type, ID);
}
void Texture::Unbind()
{
glBindTexture(type, 0);
}
void Texture::Delete()
{
glDeleteTextures(1, &ID);
}
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"
#include"Camara.h"
//Coordenadas de Vertices
GLfloat vertices[] =
{ // COORDINATES / COLORS / TexCoord / NORMALS //
-0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
// Bottom side
-0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 5.0f, 0.0f, -1.0f, 0.0f,
// Bottom side
0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 5.0f, 0.0f, -1.0f, 0.0f,
// Bottom side
0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f,
// Bottom side
-0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f, -0.8f, 0.5f, 0.0f,
// Left Side
-0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f, -0.8f, 0.5f, 0.0f,
// Left Side
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f, -0.8f, 0.5f, 0.0f,
// Left Side
-0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f, 0.0f, 0.5f, -0.8f,
// Non-facing side
0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f, 0.0f, 0.5f, -0.8f,
// Non-facing side
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f, 0.0f, 0.5f, -0.8f,
// Non-facing side
0.5f, 0.0f, -0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f, 0.8f, 0.5f, 0.0f,
// Right side
0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f, 0.8f, 0.5f, 0.0f,
// Right side
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f, 0.8f, 0.5f, 0.0f,
// Right side
0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 5.0f, 0.0f, 0.0f, 0.5f, 0.8f,
// Facing side
-0.5f, 0.0f, 0.5f, 0.83f, 0.70f, 0.44f, 0.0f, 0.0f, 0.0f, 0.5f, 0.8f,
// Facing side
0.0f, 0.8f, 0.0f, 0.92f, 0.86f, 0.76f, 2.5f, 5.0f, 0.0f, 0.5f, 0.8f
// Facing side
};
GLfloat lightVertices[] =
{
-0.1f, -0.1f, 0.1f,
-0.1f, -0.1f, -0.1f,
0.1f, -0.1f, -0.1f,
0.1f, -0.1f, 0.1f,
-0.1f, 0.1f, 0.1f,
-0.1f, 0.1f, -0.1f,
0.1f, 0.1f, -0.1f,
0.1f, 0.1f, 0.1f
};
GLuint lightIndices[] =
{
0, 1, 2,
0, 2, 3,
0, 4, 7,
0, 7, 3,
3, 7, 6,
3, 6, 2,
2, 6, 5,
2, 5, 1,
1, 5, 4,
1, 4, 0,
4, 5, 6,
4, 6, 7
};
int main()
{
// Initialize GLFW
glfwInit();
lightVAO.Unbind();
lightVBO.Unbind();
lightEBO.Unbind();
lightShader.Activate();
glUniformMatrix4fv(glGetUniformLocation(lightShader.ID, "model"), 1, GL_FALSE,
glm::value_ptr(lightModel));
glUniform4f(glGetUniformLocation(lightShader.ID, "lightColor"), lightColor.x,
lightColor.y, lightColor.z, lightColor.w);
shaderProgram.Activate();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE,
glm::value_ptr(pyramidModel));
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x,
lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "light"), lightPos.x, lightPos.y,
lightPos.z);
//Texture
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);
camera.Matrix(shaderProgram, "camMatrix");
//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);
lightShader.Activate();
camera.Matrix(lightShader, "camMatrix");
lightVAO.Bind();
glDrawElements(GL_TRIANGLES, sizeof(lightIndices) / sizeof(int),
GL_UNSIGNED_INT, 0);
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);
EBO.cpp
#include"EBO.h"
shaderClass.cpp
#include"shaderClass.h"
}
throw(errno);
}
void Shader::Activate()
{
glUseProgram(ID);
}
void Shader::Delete()
{
glUseProgram(ID);
}
shaderClass
#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
RESULTADO DE LA EJECUCION
NOTA: Recuerde sustituir la textura de su polígono colocando la que usted considere para su diseño.