Everything required for a mouse driven look is in the program just troubleshooting now
This commit is contained in:
parent
c377630e17
commit
a1e52621b8
9 changed files with 81 additions and 21 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
|
||||||
|
|
||||||
project(Game)
|
project(Game)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
file(GLOB_RECURSE SOURCE_FILES
|
file(GLOB_RECURSE SOURCE_FILES
|
||||||
${CMAKE_SOURCE_DIR}/src/*.c
|
${CMAKE_SOURCE_DIR}/src/*.c
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#version 330 core
|
#version 430 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
in vec2 ourTexCoord;
|
in vec2 ourTexCoord;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
#version 330 core
|
#version 430 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec2 aTexCoord;
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
// View
|
||||||
|
layout (location = 2) uniform mat4 mvp;
|
||||||
|
|
||||||
out vec2 ourTexCoord;
|
out vec2 ourTexCoord;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(aPos, 1.0);
|
gl_Position = mvp * vec4(aPos, 1.0);
|
||||||
ourTexCoord = aTexCoord;
|
ourTexCoord = aTexCoord;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -14,5 +14,6 @@ void Error::warn(std::string msg) {
|
||||||
// throw object + ": " + msg;
|
// throw object + ": " + msg;
|
||||||
}
|
}
|
||||||
void Error::log(std::string msg) {
|
void Error::log(std::string msg) {
|
||||||
std::cout << object << ": \n" << msg << std::endl;
|
std::cout << object << std::endl;
|
||||||
|
std::cout << msg << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,41 @@
|
||||||
#include "PlayerCamera.h"
|
#include "PlayerCamera.h"
|
||||||
|
|
||||||
PlayerCamera::PlayerCamera() {
|
PlayerCamera::PlayerCamera() {
|
||||||
|
this->error = new Error("PlayerCamera");
|
||||||
SDL_SetRelativeMouseMode(SDL_TRUE);
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||||
SDL_GetRelativeMouseState(nullptr, nullptr);
|
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; }
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Error.h"
|
||||||
#include <SDL2/SDL_mouse.h>
|
#include <SDL2/SDL_mouse.h>
|
||||||
#include <SDL2/SDL_stdinc.h>
|
#include <SDL2/SDL_stdinc.h>
|
||||||
|
#include <glm/ext.hpp>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class PlayerCamera {
|
class PlayerCamera {
|
||||||
|
|
@ -10,13 +11,14 @@ private:
|
||||||
// Constants for speed and sensitivity
|
// Constants for speed and sensitivity
|
||||||
const float movementSpeed = 0.05f;
|
const float movementSpeed = 0.05f;
|
||||||
const float mouseSensitivity = 0.05f;
|
const float mouseSensitivity = 0.05f;
|
||||||
|
const float maxPitch = glm::radians(89.0f);
|
||||||
|
|
||||||
// Player position
|
// Player position
|
||||||
struct position {
|
struct CameraPosition {
|
||||||
glm::vec4 location;
|
glm::vec4 location;
|
||||||
float pitch;
|
float pitch;
|
||||||
float yaw;
|
float yaw;
|
||||||
};
|
} pos;
|
||||||
|
|
||||||
// Mouse position
|
// Mouse position
|
||||||
int mouseX, mouseY;
|
int mouseX, mouseY;
|
||||||
|
|
@ -24,6 +26,9 @@ private:
|
||||||
// Camera position
|
// Camera position
|
||||||
glm::mat4 MVP;
|
glm::mat4 MVP;
|
||||||
|
|
||||||
|
// Error
|
||||||
|
Error *error = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PlayerCamera();
|
PlayerCamera();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,3 +88,4 @@ void ShaderLoader::setInt(const std::string &name, int value) const {
|
||||||
void ShaderLoader::setFloat(const std::string &name, float value) const {
|
void ShaderLoader::setFloat(const std::string &name, float value) const {
|
||||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
unsigned int ShaderLoader::getShaderProgramID() { return ID; }
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,9 @@ public:
|
||||||
// use the shader
|
// use the shader
|
||||||
void use();
|
void use();
|
||||||
|
|
||||||
|
// Get shader id until I figure out uniforms
|
||||||
|
unsigned int getShaderProgramID();
|
||||||
|
|
||||||
// utility uniform functions
|
// utility uniform functions
|
||||||
void setBool(const std::string &name, bool value) const;
|
void setBool(const std::string &name, bool value) const;
|
||||||
void setInt(const std::string &name, int value) const;
|
void setInt(const std::string &name, int value) const;
|
||||||
|
|
|
||||||
39
src/main.cpp
39
src/main.cpp
|
|
@ -11,10 +11,13 @@
|
||||||
//#include <glm/glm.hpp>
|
//#include <glm/glm.hpp>
|
||||||
// File reader
|
// File reader
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
// Shader
|
// Shader
|
||||||
#include "ShaderLoader.h"
|
#include "ShaderLoader.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Camera postioning
|
||||||
|
#include "PlayerCamera.h"
|
||||||
|
|
||||||
// Include error class
|
// Include error class
|
||||||
#include "Error.h"
|
#include "Error.h"
|
||||||
|
|
@ -53,31 +56,34 @@ int main(int argc, char **argv) {
|
||||||
// TODO: Test that glContext was created successfully
|
// TODO: Test that glContext was created successfully
|
||||||
|
|
||||||
// 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: " +
|
||||||
std::to_string(nrAttributes));
|
std::to_string(nrAttributes));
|
||||||
|
|
||||||
// Initialise Glew
|
// 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)");
|
error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create event handling struct
|
// Create event handling struct
|
||||||
SDL_Event input;
|
SDL_Event input;
|
||||||
|
|
||||||
ShaderLoader shader("../data/shaders"
|
ShaderLoader shader("./data/shaders"
|
||||||
"/vertex.glsl",
|
"/vertex.glsl",
|
||||||
"../data/shaders"
|
"./data/shaders"
|
||||||
"/fragment.glsl");
|
"/fragment.glsl");
|
||||||
|
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
// positions // texture Co-ords
|
// positions // texture Co-ords
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
|
0.5f, 0.5f, 1.0f, 1.0f, 1.0f, // top right
|
||||||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
|
0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // bottom right
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
|
-0.5f, -0.5f, 1.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, 0.0f, 1.0f // top left
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int indices[] = {
|
unsigned int indices[] = {
|
||||||
|
|
@ -125,7 +131,7 @@ int main(int argc, char **argv) {
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
|
||||||
// Load texture image
|
// Load texture image
|
||||||
SDL_Surface *image = IMG_Load("../data"
|
SDL_Surface *image = IMG_Load("./data"
|
||||||
"/container.jpg");
|
"/container.jpg");
|
||||||
if (image == nullptr) {
|
if (image == nullptr) {
|
||||||
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
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);
|
SDL_FreeSurface(image);
|
||||||
image = nullptr;
|
image = nullptr;
|
||||||
|
|
||||||
|
// Finally create camera object
|
||||||
|
PlayerCamera camera;
|
||||||
|
|
||||||
|
GLuint mvpLocation = glGetUniformLocation(shader.getShaderProgramID(), "mvp");
|
||||||
|
|
||||||
// Game loop
|
// Game loop
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
|
|
@ -162,12 +173,18 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
// TODO: Do something with keys lol
|
// 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);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
// Make every shader/rendering call from this point on use our shader
|
// Make every shader/rendering call from this point on use our shader
|
||||||
// glUseProgram(shaderProgram);
|
// glUseProgram(shaderProgram);
|
||||||
|
|
||||||
|
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE,
|
||||||
|
glm::value_ptr(camera.getMVP()));
|
||||||
|
// use our shader
|
||||||
shader.use();
|
shader.use();
|
||||||
|
|
||||||
// I think this is meant to be here but it breaks...
|
// I think this is meant to be here but it breaks...
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue