fixed smh

This commit is contained in:
Ben
2020-05-16 22:04:27 +01:00
parent 05ff8543bb
commit 24d95d1ad4
11 changed files with 170 additions and 121 deletions

View File

@@ -43,6 +43,7 @@ include_directories(${executable}
file(GLOB SourceFiles
${SrcDIR}/*
${SrcDIR}/ThirdParty/*
${SrcDIR}/Rendering/*
)
add_executable(${executable} ${SourceFiles})

View File

@@ -1,8 +1,9 @@
#include "camera.hpp"
Camera::Camera() {
Camera::Camera()
{
projMatrix = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f);
projMatrix = glm::perspective( glm::radians( 45.0f ), 1.0f, 0.1f, 1000.0f );
Roll = 0.0f;
Pitch = 0.0f;
@@ -17,9 +18,10 @@ Camera::Camera() {
}
Camera::Camera(int w, int h) {
Camera::Camera( int w, int h )
{
projMatrix = glm::perspective(glm::radians(45.0f), (float)w / float(h), 0.1f, 1000.0f);
projMatrix = glm::perspective( glm::radians( 45.0f ), (float) w / float( h ), 0.1f, 1000.0f );
Roll = 0.0f;
Pitch = 0.0f;
@@ -34,27 +36,28 @@ Camera::Camera(int w, int h) {
}
void Camera::UpdateView() {
void Camera::UpdateView()
{
// roll can be removed
glm::mat4 matRoll = glm::mat4(1.0f); //identity matrix;
glm::mat4 matPitch = glm::mat4(1.0f);//identity matrix
glm::mat4 matYaw = glm::mat4(1.0f); //identity matrix
glm::mat4 matRoll = glm::mat4( 1.0f ); //identity matrix;
glm::mat4 matPitch = glm::mat4( 1.0f );//identity matrix
glm::mat4 matYaw = glm::mat4( 1.0f ); //identity matrix
// roll, pitch and yaw
matRoll = glm::rotate(matRoll, Roll, glm::vec3(0.0f, 0.0f, 1.0f));
matPitch = glm::rotate(matPitch, Pitch, glm::vec3(1.0f, 0.0f, 0.0f));
matYaw = glm::rotate(matYaw, Yaw, glm::vec3(0.0f, 1.0f, 0.0f));
matRoll = glm::rotate( matRoll, Roll, glm::vec3( 0.0f, 0.0f, 1.0f ) );
matPitch = glm::rotate( matPitch, Pitch, glm::vec3( 1.0f, 0.0f, 0.0f ) );
matYaw = glm::rotate( matYaw, Yaw, glm::vec3( 0.0f, 1.0f, 0.0f ) );
glm::mat4 rotate = matRoll * matPitch * matYaw;
glm::mat4 translate = glm::mat4(1.0f);
translate = glm::translate(translate, -Position);
glm::mat4 translate = glm::mat4( 1.0f );
translate = glm::translate( translate, -Position );
viewMatrix = rotate * translate;
// Work out Look Vector
glm::mat4 inverseView = glm::inverse(viewMatrix);
glm::mat4 inverseView = glm::inverse( viewMatrix );
LookDirection.x = inverseView[2][0];
LookDirection.y = inverseView[2][1];
@@ -62,40 +65,45 @@ void Camera::UpdateView() {
}
glm::mat4 Camera::GetViewMatrix() {
glm::mat4 Camera::GetViewMatrix()
{
return viewMatrix;
}
glm::mat4 Camera::GetProjectionMatrix() {
glm::mat4 Camera::GetProjectionMatrix()
{
return projMatrix;
}
void Camera::UpdateProjection(int width, int height) {
void Camera::UpdateProjection( int width, int height )
{
projMatrix = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f);
projMatrix = glm::perspective( glm::radians( 45.0f ), (float) width / (float) height, 0.1f, 1000.0f );
}
void Camera::HandleMouse(SDL_Event e) {
void Camera::HandleMouse( SDL_Event e )
{
if (e.type != SDL_MOUSEMOTION)
if ( e.type != SDL_MOUSEMOTION )
return;
float mouseDX = e.motion.xrel;
float mouseDY = e.motion.yrel;
glm::vec2 mouseDelta{ mouseDX, mouseDY };
glm::vec2 mouseDelta { mouseDX, mouseDY };
MouseMoved(mouseDelta);
MouseMoved( mouseDelta );
}
void Camera::MoveCamera(Uint8* state) {
void Camera::MoveCamera( Uint8* state )
{
float dx = 0;
float dz = 0;
@@ -103,40 +111,46 @@ void Camera::MoveCamera(Uint8* state) {
// Rotate by camera direction
glm::mat2 rotate {
cos(Yaw), -sin(Yaw),
sin(Yaw), cos(Yaw)
cos( Yaw ), -sin( Yaw ),
sin( Yaw ), cos( Yaw )
};
glm::vec2 f(0.0, 1.0);
glm::vec2 f( 0.0, 1.0 );
f = f * rotate;
if (state[SDL_SCANCODE_W]) {
if ( state[SDL_SCANCODE_W] )
{
dz -= f.y;
dx -= f.x;
}
if (state[SDL_SCANCODE_S]) {
if ( state[SDL_SCANCODE_S] )
{
dz += f.y;
dx += f.x;
}
if (state[SDL_SCANCODE_A]) {
if ( state[SDL_SCANCODE_A] )
{
dz += f.x;
dx += -f.y;
}
if (state[SDL_SCANCODE_D]) {
if ( state[SDL_SCANCODE_D] )
{
dz -= f.x;
dx -= -f.y;
}
if (state[SDL_SCANCODE_SPACE]) {
if ( state[SDL_SCANCODE_SPACE] )
{
dy += 1;
}
if (state[SDL_SCANCODE_LSHIFT]) {
if ( state[SDL_SCANCODE_LSHIFT] )
{
dy -= 1;
}
// get current view matrix
glm::mat4 mat = GetViewMatrix();
glm::vec3 forward(mat[0][2], mat[1][2], mat[2][2]);
glm::vec3 strafe(mat[0][0], mat[1][0], mat[2][0]);
glm::vec3 forward( mat[0][2], mat[1][2], mat[2][2] );
glm::vec3 strafe( mat[0][0], mat[1][0], mat[2][0] );
// forward vector must be negative to look forward.
// read :http://in2gpu.com/2015/05/17/view-matrix/
@@ -149,19 +163,21 @@ void Camera::MoveCamera(Uint8* state) {
}
void Camera::MouseMoved(glm::vec2 mouseDelta) {
void Camera::MouseMoved( glm::vec2 mouseDelta )
{
// note that yaw and pitch must be converted to radians.
// this is done in UpdateView() by glm::rotate
Yaw += MouseSensitivity * (mouseDelta.x/100);
Pitch += MouseSensitivity * (mouseDelta.y/100);
Pitch = glm::clamp<float>(Pitch, -M_PI/2, M_PI/2);
Yaw += MouseSensitivity * (mouseDelta.x / 100);
Pitch += MouseSensitivity * (mouseDelta.y / 100);
Pitch = glm::clamp<float>( Pitch, -M_PI / 2, M_PI / 2 );
UpdateView();
}
void Camera::UpdatePosition(glm::vec3 position) {
void Camera::UpdatePosition( glm::vec3 position )
{
Position = position;
@@ -169,22 +185,24 @@ void Camera::UpdatePosition(glm::vec3 position) {
}
void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) {
void Camera::UpdateEulerLookDirection( float roll, float pitch, float yaw )
{
Roll = roll; Pitch = pitch; Yaw = yaw;
LookDirection.x = cos(Yaw) * cos(Pitch);
LookDirection.y = sin(Yaw) * cos(Pitch);
LookDirection.z = sin(Pitch);
LookDirection.x = cos( Yaw ) * cos( Pitch );
LookDirection.y = sin( Yaw ) * cos( Pitch );
LookDirection.z = sin( Pitch );
UpdateView();
}
void Camera::UpdateLookDirection(glm::vec3 lookDirection) {
void Camera::UpdateLookDirection( glm::vec3 lookDirection )
{
LookDirection = lookDirection;
Pitch = asin(-lookDirection.y);
Yaw = atan2(lookDirection.x, lookDirection.z);
Pitch = asin( -lookDirection.y );
Yaw = atan2( lookDirection.x, lookDirection.z );
UpdateView();

View File

@@ -1,7 +1,17 @@
#ifndef MINECRAFT_RENDERER_CAMERA_H_
#define MINECRAFT_RENDERER_CAMERA_H_
#include "../common.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <glm/glm.hpp>
#if _WIN32
#include <SDL.h>
#else
#include <SDL2/SDL.h>
#endif
class Camera {
public:

View File

@@ -1,35 +1,29 @@
#ifndef MINECRAFT_RENDERER_FRUSTRUM_H_
#define MINECRAFT_RENDERER_FRUSTRUM_H_
#include "../common.hpp"
namespace EFrustrumPlanes {
enum Planes {
namespace EFrustrumPlanes
{
enum Planes
{
Right,
Left,
Top,
Bottom,
Far,
Near
};
};
class FrustrumPlane
{
public:
};
class FrustrumPlane {
class Frustrum
{
public:
};
class Frustrum {
public:
};
#endif

View File

@@ -1,97 +1,108 @@
#include "shader.hpp"
#include "../utilities.hpp"
Shader::Shader()
: m_fileReader() {
{
Program = 0;
m_frag = 0;
m_vert = 0;
mFrag = 0;
mVert = 0;
m_logger = std::make_shared<Logger>();
mLogger = std::make_shared<Logger>();
}
void Shader::Load(std::string path) {
void Shader::Load( std::string path )
{
std::string vertexLocation = path + ".vert";
Load(vertexLocation, GL_VERTEX_SHADER);
*m_logger << LOGGER_INFO << "Vertex shader at '" << vertexLocation << "' loaded..." << LOGGER_ENDL;
Load( vertexLocation, GL_VERTEX_SHADER );
*mLogger << LOGGER_INFO << "Vertex shader at '" << vertexLocation << "' loaded..." << LOGGER_ENDL;
std::string fragmentLocation = path + ".frag";
Load(fragmentLocation, GL_FRAGMENT_SHADER);
*m_logger << LOGGER_INFO << "Fragment shader at '" << fragmentLocation << "' loaded..." << LOGGER_ENDL;
Load( fragmentLocation, GL_FRAGMENT_SHADER );
*mLogger << LOGGER_INFO << "Fragment shader at '" << fragmentLocation << "' loaded..." << LOGGER_ENDL;
}
void Shader::Load(std::string path, GLenum type) {
void Shader::Load( std::string path, GLenum type )
{
GLuint activeShader = 0;
if (type == GL_VERTEX_SHADER)
m_vert = activeShader = glCreateShader(type);
if ( type == GL_VERTEX_SHADER )
mVert = activeShader = glCreateShader( type );
if (type == GL_FRAGMENT_SHADER)
m_frag = activeShader = glCreateShader(type);
if ( type == GL_FRAGMENT_SHADER )
mFrag = activeShader = glCreateShader( type );
std::string loadedShaderSource = m_fileReader.LoadTextFromFile(path);
std::string loadedShaderSource = LoadTextFromFile( path );
const char* shaderSource = loadedShaderSource.c_str();
int shaderSourceLength = loadedShaderSource.length();
glShaderSource(activeShader, 1, &shaderSource, &shaderSourceLength);
glShaderSource( activeShader, 1, &shaderSource, &shaderSourceLength );
}
void Shader::Link() {
void Shader::Link()
{
if (m_vert == 0 || m_frag == 0) {
*m_logger << LOGGER_ERROR << "Failed to link programs: Both programs not present" << LOGGER_ENDL;
if ( mVert == 0 || mFrag == 0 )
{
*mLogger << LOGGER_ERROR << "Failed to link programs: Both programs not present" << LOGGER_ENDL;
return;
}
glCompileShader(m_vert);
if (m_CheckShader(m_vert)) {
*m_logger << LOGGER_INFO << "Vertex shader '" << m_vert << "' compiled..." << LOGGER_ENDL;
glCompileShader( mVert );
if ( mCheckShader( mVert ) )
{
*mLogger << LOGGER_INFO << "Vertex shader '" << mVert << "' compiled..." << LOGGER_ENDL;
}
glCompileShader(m_frag);
if (m_CheckShader(m_frag)) {
*m_logger << LOGGER_INFO << "Fragment shader '" << m_frag << "' compiled..." << LOGGER_ENDL;
glCompileShader( mFrag );
if ( mCheckShader( mFrag ) )
{
*mLogger << LOGGER_INFO << "Fragment shader '" << mFrag << "' compiled..." << LOGGER_ENDL;
}
Program = glCreateProgram();
glAttachShader(Program, m_vert);
glAttachShader(Program, m_frag);
glAttachShader( Program, mVert );
glAttachShader( Program, mFrag );
glLinkProgram(Program);
glLinkProgram( Program );
glDeleteShader(m_vert);
glDeleteShader(m_frag);
glDeleteShader( mVert );
glDeleteShader( mFrag );
*m_logger << LOGGER_INFO << "Program '" << Program << "' loaded..." << LOGGER_ENDL;
*mLogger << LOGGER_INFO << "Program '" << Program << "' loaded..." << LOGGER_ENDL;
}
void Shader::Use() {
void Shader::Use()
{
glUseProgram(Program);
glUseProgram( Program );
}
bool Shader::m_CheckShader(GLuint uid) {
bool Shader::mCheckShader( GLuint uid )
{
GLint status = GL_TRUE;
glGetShaderiv(uid, GL_COMPILE_STATUS, &status);
glGetShaderiv( uid, GL_COMPILE_STATUS, &status );
if (status == GL_FALSE) {
if ( status == GL_FALSE )
{
char buf[512];
glGetShaderInfoLog(uid, 512, NULL, buf);
*m_logger << LOGGER_ERROR << buf << LOGGER_ENDL;
glGetShaderInfoLog( uid, 512, NULL, buf );
*mLogger << LOGGER_ERROR << buf << LOGGER_ENDL;
delete buf;
return false;
}
@@ -100,10 +111,11 @@ bool Shader::m_CheckShader(GLuint uid) {
}
Shader::~Shader() {
Shader::~Shader()
{
glDeleteProgram(Program);
glDeleteShader(m_vert);
glDeleteShader(m_frag);
glDeleteProgram( Program );
glDeleteShader( mVert );
glDeleteShader( mFrag );
}

View File

@@ -3,8 +3,7 @@
#include <logger.h>
#include "../util/filereader.hpp"
#include "../common.hpp"
#include <glad/glad.h>
class Shader {
public:
@@ -20,14 +19,12 @@ public:
~Shader();
private:
std::shared_ptr<Logger> m_logger;
std::shared_ptr<Logger> mLogger;
bool m_CheckShader(GLuint uid);
bool mCheckShader(GLuint uid);
FileReader m_fileReader;
GLuint m_vert;
GLuint m_frag;
GLuint mVert;
GLuint mFrag;
};
#endif

View File

@@ -2,16 +2,16 @@
#include <logger.h>
#include "../config.hpp"
#include "../settings.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "../util/stb_image.hpp"
#include "../ThirdParty/stb_image.hpp"
GLuint Texture::LoadTextures(std::vector<std::string> textures) {
Logger logger;
std::string basePath = GameConfig.ResourceBase + "textures/";
std::string basePath = ResourceBase + "textures/";
int x = 16;
int y = 16;

View File

@@ -1,7 +1,10 @@
#ifndef MINECRAFT_RENDERER_TEXTURE_H_
#define MINECRAFT_RENDERER_TEXTURE_H_
#include "../common.hpp"
#include <vector>
#include <string>
#include <glad/glad.h>
class Texture {
public:

View File

@@ -97,7 +97,7 @@ void Display::Input( SDL_Event* e )
}
// if ( IsMouseActive ) m_player->HandleMouseSDL( *e );
// if ( IsMouseActive ) HandleMouseSDL( *e );
}
// m_player->MoveSDL( state );

View File

@@ -1,10 +1,14 @@
#ifndef MINECRAFT_SETTINGS_H_
#define MINECRAFT_SETTINGS_H_
#include <string>
// TODO: import settings and stuff
// for now this works
static const int WindowWidth = 1000;
static const int WindowHeight = 600;
static const std::string ResourceBase = MC_RESOURCES;
#endif

10
src/utilities.hpp Normal file
View File

@@ -0,0 +1,10 @@
#include <fstream>
#include <string>
inline std::string LoadTextFromFile( std::string file )
{
std::ifstream t( file );
std::string text( (std::istreambuf_iterator<char>( t )),
std::istreambuf_iterator<char>() );
return text;
}