Fixed camera looking at last frame's forward target.
This commit is contained in:
parent
cf964bf235
commit
a89a5745ea
3 changed files with 29 additions and 46 deletions
|
|
@ -8,13 +8,27 @@ PlayerCamera::PlayerCamera() {
|
||||||
PlayerCamera::~PlayerCamera() {}
|
PlayerCamera::~PlayerCamera() {}
|
||||||
|
|
||||||
void PlayerCamera::tick() {
|
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
|
// get camera right
|
||||||
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||||
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward));
|
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
|
// TODO Handle movement speed based on delta time
|
||||||
// handle keyboard input
|
// handle keyboard input
|
||||||
const Uint8 *keyboardState = SDL_GetKeyboardState(nullptr);
|
const Uint8 *keyboardState = SDL_GetKeyboardState(nullptr);
|
||||||
|
|
@ -27,22 +41,8 @@ void PlayerCamera::tick() {
|
||||||
if (keyboardState[SDL_SCANCODE_D])
|
if (keyboardState[SDL_SCANCODE_D])
|
||||||
cameraPosition -= cameraRight * movementSpeed;
|
cameraPosition -= cameraRight * movementSpeed;
|
||||||
|
|
||||||
// handle mouse
|
// get position to look at
|
||||||
SDL_GetRelativeMouseState(&mouseX, &mouseY);
|
glm::vec3 cameraTarget = cameraPosition + cameraForward;
|
||||||
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);
|
|
||||||
|
|
||||||
// MVP stuff
|
// MVP stuff
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,8 @@ private:
|
||||||
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
|
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
|
||||||
glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f);
|
glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f);
|
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
float cameraYaw = -90.0f;
|
float cameraYaw = glm::radians(-90.0f);
|
||||||
float cameraPitch = 0.0f;
|
float cameraPitch = glm::radians(0.0f);
|
||||||
|
|
||||||
// Mouse position
|
// Mouse position
|
||||||
int mouseX, mouseY;
|
int mouseX, mouseY;
|
||||||
|
|
|
||||||
33
src/main.cpp
33
src/main.cpp
|
|
@ -3,12 +3,7 @@
|
||||||
// Make sure Glew is loaded first
|
// Make sure Glew is loaded first
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
// Not used yet
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
// File reader
|
// File reader
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
// Shader
|
// Shader
|
||||||
#include "ShaderLoader.h"
|
#include "ShaderLoader.h"
|
||||||
|
|
@ -18,7 +13,6 @@
|
||||||
// Objects
|
// Objects
|
||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
// Include error class
|
// Include error class
|
||||||
#include "Error.h"
|
#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)
|
// Make OpenGL use double buffering (Render game first then shove to output)
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
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);
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||||||
|
|
||||||
// TODO: Discover if this is necessary for linux and what values they need be
|
// 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
|
// Create glContext
|
||||||
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
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
|
// Tell us the number of vertex attributes allowed in bug reports
|
||||||
//
|
|
||||||
int nrAttributes;
|
int nrAttributes;
|
||||||
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
|
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
|
||||||
error.log("Maximum num of vertex attributes supported: " +
|
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",
|
ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl",
|
||||||
ROOT_DIR "data/shaders/fragment.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");
|
Model cube(ROOT_DIR "data/models/cube/cube.obj");
|
||||||
cube.translate(glm::vec3(3.0f, 0.0f, -1.0f));
|
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
|
// Create player camera object
|
||||||
PlayerCamera camera;
|
PlayerCamera camera;
|
||||||
|
|
||||||
|
|
@ -112,8 +100,9 @@ int main(int argc, char **argv) {
|
||||||
if (keys[SDL_SCANCODE_ESCAPE])
|
if (keys[SDL_SCANCODE_ESCAPE])
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
// TODO: Do something with keys lol
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: Run game here lol
|
||||||
camera.tick();
|
camera.tick();
|
||||||
|
|
||||||
// Clear screen ready for next loop
|
// Clear screen ready for next loop
|
||||||
|
|
@ -131,12 +120,6 @@ int main(int argc, char **argv) {
|
||||||
// Finally render everything
|
// Finally render everything
|
||||||
shader.use();
|
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);
|
SDL_GL_SwapWindow(window);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue