diff --git a/CMakeLists.txt b/CMakeLists.txt index 4cb1666..24a0a20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ include_directories(${executable} file(GLOB SourceFiles ${SrcDIR}/* ${SrcDIR}/ThirdParty/* + ${SrcDIR}/Rendering/* ) add_executable(${executable} ${SourceFiles}) diff --git a/src/Rendering/camera.cpp b/src/Rendering/camera.cpp index 9c55d62..ed0f372 100644 --- a/src/Rendering/camera.cpp +++ b/src/Rendering/camera.cpp @@ -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,68 +36,74 @@ 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]; LookDirection.z = inverseView[2][2]; } -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(Pitch, -M_PI/2, M_PI/2); + Yaw += MouseSensitivity * (mouseDelta.x / 100); + Pitch += MouseSensitivity * (mouseDelta.y / 100); + Pitch = glm::clamp( 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(); diff --git a/src/Rendering/camera.hpp b/src/Rendering/camera.hpp index acbe1a0..239348a 100644 --- a/src/Rendering/camera.hpp +++ b/src/Rendering/camera.hpp @@ -1,7 +1,17 @@ #ifndef MINECRAFT_RENDERER_CAMERA_H_ #define MINECRAFT_RENDERER_CAMERA_H_ -#include "../common.hpp" +#include +#include +#define GLM_ENABLE_EXPERIMENTAL +#include +#include + +#if _WIN32 +#include +#else +#include +#endif class Camera { public: diff --git a/src/Rendering/frustrum.hpp b/src/Rendering/frustrum.hpp index cefa7e6..ebf0368 100644 --- a/src/Rendering/frustrum.hpp +++ b/src/Rendering/frustrum.hpp @@ -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 diff --git a/src/Rendering/shader.cpp b/src/Rendering/shader.cpp index c5ae645..a851d0e 100644 --- a/src/Rendering/shader.cpp +++ b/src/Rendering/shader.cpp @@ -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(); + mLogger = std::make_shared(); } -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); - - if (status == GL_FALSE) { + glGetShaderiv( uid, GL_COMPILE_STATUS, &status ); + + 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 ); } diff --git a/src/Rendering/shader.hpp b/src/Rendering/shader.hpp index 02e68d1..a5a746f 100644 --- a/src/Rendering/shader.hpp +++ b/src/Rendering/shader.hpp @@ -3,8 +3,7 @@ #include -#include "../util/filereader.hpp" -#include "../common.hpp" +#include class Shader { public: @@ -20,14 +19,12 @@ public: ~Shader(); private: - std::shared_ptr m_logger; + std::shared_ptr mLogger; - bool m_CheckShader(GLuint uid); + bool mCheckShader(GLuint uid); - FileReader m_fileReader; - - GLuint m_vert; - GLuint m_frag; + GLuint mVert; + GLuint mFrag; }; #endif diff --git a/src/Rendering/texture.cpp b/src/Rendering/texture.cpp index ac70556..7b91f10 100644 --- a/src/Rendering/texture.cpp +++ b/src/Rendering/texture.cpp @@ -2,16 +2,16 @@ #include -#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 textures) { Logger logger; - std::string basePath = GameConfig.ResourceBase + "textures/"; + std::string basePath = ResourceBase + "textures/"; int x = 16; int y = 16; diff --git a/src/Rendering/texture.hpp b/src/Rendering/texture.hpp index b83300f..dd6a123 100644 --- a/src/Rendering/texture.hpp +++ b/src/Rendering/texture.hpp @@ -1,7 +1,10 @@ #ifndef MINECRAFT_RENDERER_TEXTURE_H_ #define MINECRAFT_RENDERER_TEXTURE_H_ -#include "../common.hpp" +#include +#include + +#include class Texture { public: diff --git a/src/display.cpp b/src/display.cpp index f2b3dbf..5ce00fe 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -97,7 +97,7 @@ void Display::Input( SDL_Event* e ) } - // if ( IsMouseActive ) m_player->HandleMouseSDL( *e ); + // if ( IsMouseActive ) HandleMouseSDL( *e ); } // m_player->MoveSDL( state ); diff --git a/src/settings.hpp b/src/settings.hpp index 08c49d3..5ab11c0 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -1,10 +1,14 @@ #ifndef MINECRAFT_SETTINGS_H_ #define MINECRAFT_SETTINGS_H_ +#include + // 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 diff --git a/src/utilities.hpp b/src/utilities.hpp new file mode 100644 index 0000000..21e44ed --- /dev/null +++ b/src/utilities.hpp @@ -0,0 +1,10 @@ +#include +#include + +inline std::string LoadTextFromFile( std::string file ) +{ + std::ifstream t( file ); + std::string text( (std::istreambuf_iterator( t )), + std::istreambuf_iterator() ); + return text; +}