Everything required for a mouse driven look is in the program just troubleshooting now

This commit is contained in:
Warwick 2022-01-20 17:05:06 +00:00
parent c377630e17
commit a1e52621b8
No known key found for this signature in database
GPG key ID: A063045576839DB7
9 changed files with 81 additions and 21 deletions

View file

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
project(Game)
set(CMAKE_CXX_STANDARD 17)
file(GLOB_RECURSE SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/*.c

View file

@ -1,4 +1,4 @@
#version 330 core
#version 430 core
out vec4 FragColor;
in vec2 ourTexCoord;

View file

@ -1,11 +1,14 @@
#version 330 core
#version 430 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
// View
layout (location = 2) uniform mat4 mvp;
out vec2 ourTexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
gl_Position = mvp * vec4(aPos, 1.0);
ourTexCoord = aTexCoord;
};

View file

@ -14,5 +14,6 @@ void Error::warn(std::string msg) {
// throw object + ": " + msg;
}
void Error::log(std::string msg) {
std::cout << object << ": \n" << msg << std::endl;
std::cout << object << std::endl;
std::cout << msg << std::endl;
}

View file

@ -1,12 +1,41 @@
#include "PlayerCamera.h"
PlayerCamera::PlayerCamera() {
this->error = new Error("PlayerCamera");
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_GetRelativeMouseState(nullptr, nullptr);
}
PlayerCamera::~PlayerCamera() {}
PlayerCamera::~PlayerCamera() { delete this->error; }
void PlayerCamera::tick() {}
void PlayerCamera::tick() {
SDL_GetRelativeMouseState(&mouseX, &mouseY);
glm::mat4 PlayerCamera::getMVP() {}
// adjust rotation based off mouse input
pos.yaw -= mouseX * mouseSensitivity;
pos.pitch -= mouseY * mouseSensitivity;
if (pos.pitch > maxPitch)
pos.pitch = maxPitch;
if (pos.pitch < -maxPitch)
pos.pitch = -maxPitch;
// Get player forward vector and rotation matrix
glm::vec4 playerForward;
glm::mat4 playerRotation;
playerRotation = glm::rotate(playerRotation, pos.yaw, glm::vec3(0, 1, 0));
playerRotation = glm::rotate(playerRotation, pos.pitch, glm::vec3(1, 0, 0));
playerForward = glm::vec4(0, 0, -1, 0) * playerRotation;
// move camera
glm::mat4 view =
glm::lookAt(glm::vec3(pos.location),
glm::vec3(pos.location + playerForward), glm::vec3(0, 1, 0));
glm::mat4 projection =
glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f);
glm::mat4 transform;
MVP = projection * view * transform;
}
glm::mat4 PlayerCamera::getMVP() { return MVP; }

View file

@ -1,8 +1,9 @@
#pragma once
#include "Error.h"
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_stdinc.h>
#include <glm/ext.hpp>
#include <glm/glm.hpp>
#include <iostream>
#include <string>
class PlayerCamera {
@ -10,13 +11,14 @@ private:
// Constants for speed and sensitivity
const float movementSpeed = 0.05f;
const float mouseSensitivity = 0.05f;
const float maxPitch = glm::radians(89.0f);
// Player position
struct position {
struct CameraPosition {
glm::vec4 location;
float pitch;
float yaw;
};
} pos;
// Mouse position
int mouseX, mouseY;
@ -24,6 +26,9 @@ private:
// Camera position
glm::mat4 MVP;
// Error
Error *error = nullptr;
public:
PlayerCamera();

View file

@ -88,3 +88,4 @@ void ShaderLoader::setInt(const std::string &name, int value) const {
void ShaderLoader::setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
unsigned int ShaderLoader::getShaderProgramID() { return ID; }

View file

@ -21,6 +21,9 @@ public:
// use the shader
void use();
// Get shader id until I figure out uniforms
unsigned int getShaderProgramID();
// utility uniform functions
void setBool(const std::string &name, bool value) const;
void setInt(const std::string &name, int value) const;

View file

@ -11,10 +11,13 @@
//#include <glm/glm.hpp>
// File reader
#include <fstream>
#include <iostream>
#include <string>
// Shader
#include "ShaderLoader.h"
#include <iostream>
// Camera postioning
#include "PlayerCamera.h"
// Include error class
#include "Error.h"
@ -53,31 +56,34 @@ int main(int argc, char **argv) {
// TODO: Test that glContext was created successfully
// 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: " +
std::to_string(nrAttributes));
// Initialise Glew
if (glewInit() != GLEW_OK) {
GLenum glewStatus = glewInit();
if (glewStatus != GLEW_OK) {
// TODO make this work. I mean ffs if glew brakes how will I debug it.
// auto errorMsg = glewGetErrorString(glewStatus);
// error.log(errorMsg);
error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)");
}
// Create event handling struct
SDL_Event input;
ShaderLoader shader("../data/shaders"
ShaderLoader shader("./data/shaders"
"/vertex.glsl",
"../data/shaders"
"./data/shaders"
"/fragment.glsl");
float vertices[] = {
// positions // texture Co-ords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
0.5f, 0.5f, 1.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 1.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
@ -125,7 +131,7 @@ int main(int argc, char **argv) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load texture image
SDL_Surface *image = IMG_Load("../data"
SDL_Surface *image = IMG_Load("./data"
"/container.jpg");
if (image == nullptr) {
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
@ -151,6 +157,11 @@ int main(int argc, char **argv) {
SDL_FreeSurface(image);
image = nullptr;
// Finally create camera object
PlayerCamera camera;
GLuint mvpLocation = glGetUniformLocation(shader.getShaderProgramID(), "mvp");
// Game loop
bool running = true;
while (running) {
@ -162,12 +173,18 @@ int main(int argc, char **argv) {
}
// TODO: Do something with keys lol
};
// Clear screen ready for next loop
// Perform game logic
camera.tick();
// Clear screen ready for this loop
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Make every shader/rendering call from this point on use our shader
// glUseProgram(shaderProgram);
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE,
glm::value_ptr(camera.getMVP()));
// use our shader
shader.use();
// I think this is meant to be here but it breaks...