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,6 +1,7 @@
#include "camera.hpp"
Camera::Camera() {
Camera::Camera()
{
projMatrix = glm::perspective( glm::radians( 45.0f ), 1.0f, 0.1f, 1000.0f );
@@ -17,7 +18,8 @@ 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 );
@@ -34,7 +36,8 @@ 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;
@@ -62,25 +65,29 @@ 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 );
}
void Camera::HandleMouse(SDL_Event e) {
void Camera::HandleMouse( SDL_Event e )
{
if ( e.type != SDL_MOUSEMOTION )
return;
@@ -95,7 +102,8 @@ void Camera::HandleMouse(SDL_Event e) {
}
void Camera::MoveCamera(Uint8* state) {
void Camera::MoveCamera( Uint8* state )
{
float dx = 0;
float dz = 0;
@@ -110,26 +118,32 @@ void Camera::MoveCamera(Uint8* state) {
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;
}
@@ -149,7 +163,8 @@ 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
@@ -161,7 +176,8 @@ void Camera::MouseMoved(glm::vec2 mouseDelta) {
}
void Camera::UpdatePosition(glm::vec3 position) {
void Camera::UpdatePosition( glm::vec3 position )
{
Position = position;
@@ -169,7 +185,8 @@ 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 );
@@ -180,7 +197,8 @@ void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) {
}
void Camera::UpdateLookDirection(glm::vec3 lookDirection) {
void Camera::UpdateLookDirection( glm::vec3 lookDirection )
{
LookDirection = lookDirection;
Pitch = asin( -lookDirection.y );

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 {
class FrustrumPlane
{
public:
};
class Frustrum {
class Frustrum
{
public:
};
#endif

View File

@@ -1,42 +1,46 @@
#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;
*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;
*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);
mVert = activeShader = glCreateShader( type );
if ( type == GL_FRAGMENT_SHADER )
m_frag = activeShader = glCreateShader(type);
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();
@@ -44,54 +48,61 @@ void Shader::Load(std::string path, GLenum type) {
}
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 );
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 );
}
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) {
if ( status == GL_FALSE )
{
char buf[512];
glGetShaderInfoLog( uid, 512, NULL, buf );
*m_logger << LOGGER_ERROR << buf << LOGGER_ENDL;
*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);
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;
}