I dunno why it doesn't work currently be we should have a working camera

This commit is contained in:
Warwick 2022-01-27 15:30:40 +00:00
parent f364da6272
commit 19ad21dd7e
No known key found for this signature in database
GPG key ID: A063045576839DB7
4 changed files with 77 additions and 23 deletions

View file

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
project(Game)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_CLANG_TIDY)
file(GLOB_RECURSE SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/*.c

View file

@ -7,6 +7,51 @@ PlayerCamera::PlayerCamera() {
PlayerCamera::~PlayerCamera() {}
void PlayerCamera::tick() {}
void PlayerCamera::tick() {
pos.vectorPos = glm::vec4(0.0f, 0.0f, 5.0f, 1.0f);
// Get the mouse movement
int mouseX, mouseY;
SDL_GetRelativeMouseState(&mouseX, &mouseY);
glm::mat4 PlayerCamera::getMVP() {}
// Calculate rotation
pos.yaw -= mouseX * mouseSensitivity;
pos.pitch -= mouseX * mouseSensitivity;
// Stop camera from looking too far up/down
const float maxPitch = glm::radians(89.0f);
if (pos.pitch > maxPitch)
pos.pitch = maxPitch;
if (pos.pitch < -maxPitch)
pos.pitch = -maxPitch;
glm::vec4 playerForward(0.0f, 0.0f, -1.0f, 0.0f);
glm::mat4 playerRotation;
// Rotate yaw
playerRotation =
glm::rotate(playerRotation, pos.yaw, glm::vec3(0.0f, 1.0f, 0.0f));
// Rotate pitch
playerRotation =
glm::rotate(playerRotation, pos.pitch, glm::vec3(1.0f, 0.0f, 0.0f));
playerForward = playerRotation * playerForward;
// until we add keyboard stuff set the position to a static value
// create MVP
glm::mat4 view = glm::lookAt(
glm::vec3(pos.vectorPos),
glm::vec3(pos.vectorPos +
playerForward /* glm::vec4(0.0f, 0.0f, -1.0f, 0.0f) */),
glm::vec3(0.0f, 1.0f, 0.0f));
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units
float width = 800;
float height = 600;
glm::mat4 projection = glm::perspective(
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 transform = glm::mat4(1.0f);
MVP = projection * view * transform;
}
glm::mat4 PlayerCamera::getMVP() { return MVP; }

View file

@ -1,22 +1,26 @@
#pragma once
#include "Error.h"
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_stdinc.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <string>
class PlayerCamera {
private:
// Error reporting class
Error error = Error("PlayerCamera");
// Constants for speed and sensitivity
const float movementSpeed = 0.05f;
const float mouseSensitivity = 0.05f;
const float movementSpeed = 0.005f;
const float mouseSensitivity = 0.005f;
// Player position
struct position {
glm::vec4 location;
glm::vec4 vectorPos;
float pitch;
float yaw;
};
} pos;
// Mouse position
int mouseX, mouseY;

View file

@ -1,6 +1,4 @@
// Include Config header generated by GNU autotools
//#include "../config.h"
//#include "../config_file_paths.h"
#include <GL/glew.h>
// Make sure Glew is loaded first
#include <GL/gl.h>
@ -16,6 +14,8 @@
#include <string>
// Shader
#include "ShaderLoader.h"
// Camera
#include "PlayerCamera.h"
// Include error class
#include "Error.h"
@ -155,24 +155,26 @@ int main(int argc, char **argv) {
// Mess with perspective
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units
float width = 4;
float height = 3;
float width = 800;
float height = 600;
glm::mat4 Projection = glm::perspective(
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
//// Camera matrix
// glm::mat4 View = glm::lookAt(
// glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space
// glm::vec3(0, 0, 0), // and looks at the origin
// glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
//);
//// Model matrix : an identity matrix (model will be at the origin)
// glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 mvp =
Projection * View *
Model; // Remember, matrix multiplication is the other way around
//// Our ModelViewProjection : multiplication of our 3 matrices
// glm::mat4 mvp =
// Projection * View *
// Model; // Remember, matrix multiplication is the other way around
PlayerCamera camera;
// Game loop
bool running = true;
@ -185,6 +187,8 @@ int main(int argc, char **argv) {
}
// TODO: Do something with keys lol
};
camera.tick();
// Clear screen ready for next loop
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -192,7 +196,7 @@ int main(int argc, char **argv) {
// glUseProgram(shaderProgram);
// Send our glsl shader our mvp
shader.setMat4("MVP", mvp);
shader.setMat4("MVP", camera.getMVP());
shader.use();
// I think this is meant to be here but it breaks...