Laboratorio #8 - OpenGL - Programacion Grafica 2024
Laboratorio #8 - OpenGL - Programacion Grafica 2024
Además, se agrega la
manipulación del teclado, así como el mouse para la manipulación de la cámara, la cual básicamente
representa el eje Z. Quedando el proyecto en Visual Community de la siguiente manera:
Para este laboratorio, los siguientes archivos fueron agregados y Main.cpp fue modificado para la
interacción de la camera por medio de teclado primeramente y luego la incorporación del mouse.
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;
void main()
{
// Outputs the positions/coordinates of all vertices
gl_Position = camMatrix * 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;
}
Camera.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:
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);
int width;
int height;
};
#endif // !CAMERA_CLASS_H
Camera.cpp
#include"Camara.h"
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const
char* uniform)
{
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
double mouseX;
double mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
IMPORTANTE: CADA CAPTURA DE PANTALLA REPRESENTA EL MOVIMIENTO PRESIONANDO LAS TECLAS “A,
S,D,W,CONTROL IZQUIERDO, BARRA ESPACIADORA”
Si al archivo Camera.cpp le agregamos la funcionalidad del uso del mouse para la manipulación de la
cámara que representa el eje z para los ojos del usuario, nos quedaría de la siguiente manera:
Camera.cpp
#include"Camara.h"
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const
char* uniform)
{
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
double mouseX;
double mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
Por lo tanto, agregando la función “clic” para posicionar el cursor del mouse en la ventana, los archivos
Camara.h y Camera.cpp quedan finalmente de la siguiente manera.
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);
// 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.cpp
#include"Camara.h"
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const
char* uniform)
{
// Initializes matrices since otherwise they will be the null matrix
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
// 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;
}
}
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[] =
{
// 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);
glEnable(GL_DEPTH_TEST);
camera.Inputs(window);
camera.Matrix(45.0f, 0.1f, 100.0f, shaderProgram, "camMatrix");
//rotation += 0.5f;
//prevTime = crntTime;
//}