Fixed camera looking at last frame's forward target.

This commit is contained in:
Warwick 2022-06-14 15:01:27 +01:00
parent cf964bf235
commit a89a5745ea
3 changed files with 29 additions and 46 deletions

View file

@ -8,13 +8,27 @@ PlayerCamera::PlayerCamera() {
PlayerCamera::~PlayerCamera() {}
void PlayerCamera::tick() {
// handle mouse
SDL_GetRelativeMouseState(&mouseX, &mouseY);
cameraYaw += mouseX * glm::radians(mouseSensitivity);
cameraPitch -= mouseY * glm::radians(mouseSensitivity);
// lock pitch to certain range
if (cameraPitch > 89.0f)
cameraPitch = 89.0f;
if (cameraPitch < -89.0f)
cameraPitch = -89.0f;
// calculate camera rotation
glm::vec3 direction;
direction.x = cos(cameraYaw) * cos(cameraPitch);
direction.y = sin(cameraPitch);
direction.z = sin(cameraYaw) * cos(cameraPitch);
cameraForward = glm::normalize(direction);
// get camera right
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward));
// get position to look at
glm::vec3 cameraTarget = cameraPosition + cameraForward;
// TODO Handle movement speed based on delta time
// handle keyboard input
const Uint8 *keyboardState = SDL_GetKeyboardState(nullptr);
@ -27,22 +41,8 @@ void PlayerCamera::tick() {
if (keyboardState[SDL_SCANCODE_D])
cameraPosition -= cameraRight * movementSpeed;
// handle mouse
SDL_GetRelativeMouseState(&mouseX, &mouseY);
cameraYaw += mouseX * mouseSensitivity;
cameraPitch -= mouseY * mouseSensitivity;
// lock pitch to certain range
if (cameraPitch > 89.0f)
cameraPitch = 89.0f;
if (cameraPitch < -89.0f)
cameraPitch = -89.0f;
// calculate camera rotation
glm::vec3 direction;
direction.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
direction.y = sin(glm::radians(cameraPitch));
direction.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
cameraForward = glm::normalize(direction);
// get position to look at
glm::vec3 cameraTarget = cameraPosition + cameraForward;
// MVP stuff
glm::mat4 model = glm::mat4(1.0f);

View file

@ -20,8 +20,8 @@ private:
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f);
float cameraYaw = -90.0f;
float cameraPitch = 0.0f;
float cameraYaw = glm::radians(-90.0f);
float cameraPitch = glm::radians(0.0f);
// Mouse position
int mouseX, mouseY;

View file

@ -3,12 +3,7 @@
// Make sure Glew is loaded first
#include <GL/gl.h>
#include <SDL2/SDL.h>
// Not used yet
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// File reader
#include <fstream>
#include <iostream>
#include <string>
// Shader
#include "ShaderLoader.h"
@ -18,7 +13,6 @@
// Objects
#include "Mesh.h"
#include "Model.h"
#include <vector>
// Include error class
#include "Error.h"
@ -34,7 +28,7 @@ int main(int argc, char **argv) {
// Make OpenGL use double buffering (Render game first then shove to output)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// TODO: Understand what a depth buffer is
// Initialise depth buffer
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
// TODO: Discover if this is necessary for linux and what values they need be
@ -54,10 +48,11 @@ int main(int argc, char **argv) {
// Create glContext
SDL_GLContext glContext = SDL_GL_CreateContext(window);
// TODO: Test that glContext was created successfully
if (!glContext) {
error.crash("No SDL OpenGL Context initialised.");
}
// Tell us the number of vertex attributes allowed in bug reports
//
int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
error.log("Maximum num of vertex attributes supported: " +
@ -74,18 +69,11 @@ int main(int argc, char **argv) {
ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl",
ROOT_DIR "data/shaders/fragment.glsl");
Model backpack(ROOT_DIR "data/models/backpack/backpack.obj");
Model backpack(std::string(ROOT_DIR) +
std::string("data/models/backpack/backpack.obj"));
Model cube(ROOT_DIR "data/models/cube/cube.obj");
cube.translate(glm::vec3(3.0f, 0.0f, -1.0f));
// Mess with perspective
// 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);
// Create player camera object
PlayerCamera camera;
@ -112,8 +100,9 @@ int main(int argc, char **argv) {
if (keys[SDL_SCANCODE_ESCAPE])
running = false;
}
// TODO: Do something with keys lol
};
// TODO: Run game here lol
camera.tick();
// Clear screen ready for next loop
@ -131,12 +120,6 @@ int main(int argc, char **argv) {
// Finally render everything
shader.use();
// I think this is meant to be here but it breaks...
// shove vertex array into buffer
// glBindVertexArray(VAO);
// TODO: Run game here lol
SDL_GL_SwapWindow(window);
};