Add Basic Camera
This commit is contained in:
@@ -20,6 +20,7 @@ add_executable(Fall-Fever
|
||||
ShaderProgram.cpp
|
||||
VertexBuffer.cpp
|
||||
Texture.cpp
|
||||
Camera.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
|
||||
29
Camera.cpp
Normal file
29
Camera.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "Camera.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera(float fov, int width, int height) {
|
||||
//projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f);
|
||||
this->fov = fov;
|
||||
projectionMatrix = glm::perspective(this->fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f);
|
||||
viewMatrix = glm::mat4(1.0f);
|
||||
updateVPM();
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getViewProj() {
|
||||
return viewProjectionMatrix;
|
||||
}
|
||||
|
||||
void Camera::updateVPM() {
|
||||
viewProjectionMatrix = projectionMatrix * viewMatrix;
|
||||
}
|
||||
|
||||
void Camera::updateAspectRatio(int width, int height) {
|
||||
projectionMatrix = glm::perspective(fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f);
|
||||
updateVPM();
|
||||
}
|
||||
|
||||
void Camera::translate(glm::vec3 vector) {
|
||||
viewMatrix = glm::translate(viewMatrix, vector * -1.0f);
|
||||
}
|
||||
28
Camera.h
Normal file
28
Camera.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Camera {
|
||||
|
||||
public:
|
||||
|
||||
Camera(float fov, int width, int height);
|
||||
~Camera() = default;
|
||||
|
||||
glm::mat4 getViewProj();
|
||||
void updateVPM();
|
||||
void updateAspectRatio(int width, int height);
|
||||
|
||||
void translate(glm::vec3 vector);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
glm::mat4 viewMatrix;
|
||||
glm::mat4 projectionMatrix;
|
||||
|
||||
glm::mat4 viewProjectionMatrix;
|
||||
|
||||
float fov;
|
||||
|
||||
};
|
||||
@@ -13,6 +13,7 @@
|
||||
#endif
|
||||
|
||||
#include "Controller.h"
|
||||
#include "Camera.h"
|
||||
#include "Texture.h"
|
||||
|
||||
Controller::Controller() {
|
||||
@@ -70,6 +71,10 @@ void Controller::run() {
|
||||
|
||||
Texture tex1("res/tex.png", shaderProgram.getShaderProgramId());
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
Camera cam1(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
|
||||
cam1.translate(glm::vec3(0.0f, 0.0f, 1.5f));
|
||||
|
||||
// This is the game loop
|
||||
while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
|
||||
// Timing
|
||||
@@ -85,9 +90,10 @@ void Controller::run() {
|
||||
|
||||
// Update game
|
||||
// ...
|
||||
glm::mat4 trans_mat = glm::mat4(1.0f);
|
||||
trans_mat = glm::rotate(trans_mat, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
shaderProgram.setUniform("transform", trans_mat);
|
||||
model = glm::rotate(model, (float)this->deltaTime, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
cam1.updateVPM();
|
||||
glm::mat4 modelViewProj = cam1.getViewProj() * model;
|
||||
shaderProgram.setUniform("u_modelViewProj", modelViewProj);
|
||||
|
||||
// Render and buffer swap
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
@@ -102,6 +108,13 @@ void Controller::run() {
|
||||
|
||||
// Check events, handle input
|
||||
gameEventHandler->handleEvents();
|
||||
|
||||
// Update window size
|
||||
int new_window_width, new_window_height;
|
||||
glfwGetFramebufferSize(gameWindow->getGLFWwindow(), &new_window_width, &new_window_height);
|
||||
gameWindow->setWindowDimensions(new_window_width, new_window_height);
|
||||
glViewport(0, 0, new_window_width, new_window_height);
|
||||
cam1.updateAspectRatio(new_window_width, new_window_height);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ ShaderProgram::ShaderProgram(const char* vertexShaderPath, const char* fragmentS
|
||||
shaderProgramId = createShader(vertexShaderPath, fragmentShaderPath);
|
||||
|
||||
// Set transformation matrix as default to identity matrix
|
||||
this->bind();
|
||||
bind();
|
||||
glm::mat4 identity_matrix = glm::mat4(1.0f);
|
||||
this->setUniform("transform", identity_matrix);
|
||||
this->unbind();
|
||||
setUniform("transform", identity_matrix);
|
||||
unbind();
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
|
||||
@@ -30,7 +30,8 @@ Window::Window() {
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
// Tell GLFW which function to call when window is resized
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
// Currently not used...
|
||||
// glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
}
|
||||
|
||||
|
||||
5
Window.h
5
Window.h
@@ -11,6 +11,11 @@ public:
|
||||
|
||||
GLFWwindow * getGLFWwindow() { return window; }
|
||||
|
||||
void setWindowDimensions(int width, int height) {this->width = width; this->height = height;}
|
||||
|
||||
int getWindowWidth() { return width; }
|
||||
int getWindowHeight() { return height; }
|
||||
|
||||
private:
|
||||
static void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
static void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
|
||||
|
||||
@@ -7,10 +7,10 @@ layout(location = 2) in vec4 a_color;
|
||||
out vec4 v_color;
|
||||
out vec2 v_texCoord;
|
||||
|
||||
uniform mat4 transform;
|
||||
uniform mat4 u_modelViewProj;
|
||||
|
||||
void main() {
|
||||
gl_Position = transform * vec4(a_position, 1.0f);
|
||||
gl_Position = u_modelViewProj * vec4(a_position, 1.0f);
|
||||
v_texCoord = a_texCoord;
|
||||
v_color = a_color;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user