Compare commits

...

10 Commits

Author SHA1 Message Date
benkyd
aefe55e314 ok epic 2022-05-11 13:36:24 +00:00
Benjamin Kyd
ae8b69b96d Merge pull request #12 from benkyd/add-license-1
Create LICENSE
2021-03-02 17:46:50 +00:00
Benjamin Kyd
9c40baacb6 Create LICENSE 2021-03-02 17:46:42 +00:00
Ben
6e42ac9e8b voxel meshes perhaps 2021-03-02 17:42:24 +00:00
Ben
9f9daa3a6e renderer structure 2021-02-17 03:47:08 +00:00
Ben Kyd
fb75f6b8d6 welp picking this project back up 2021-02-12 00:43:19 +00:00
Ben
2872ac6268 face? 2020-05-28 17:10:02 +01:00
Ben
e4dbfa672c done barely anything smh 2020-05-20 01:12:18 +01:00
Ben
24d95d1ad4 fixed smh 2020-05-16 22:04:27 +01:00
Ben
05ff8543bb added old files which will work fine 2020-05-16 20:04:38 +01:00
29 changed files with 836 additions and 397 deletions

View File

@@ -15,6 +15,7 @@ add_definitions(-DMC_RESOURCES="${CMAKE_SOURCE_DIR}/resources/")
message(${CMAKE_SOURCE_DIR}/resources)
if (UNIX)
find_package(glm REQUIRED)
find_package(SDL2 REQUIRED)
endif(UNIX)
@@ -27,6 +28,9 @@ if (WIN32)
endif (WIN32)
set(THREADS_PREFER_PTHREAD_FLAD ON)
set(OpenGL_GL_PREFERENCE GLVND)
find_package(Threads REQUIRED)
find_package(OpenGL REQUIRED)
@@ -43,18 +47,22 @@ include_directories(${executable}
file(GLOB SourceFiles
${SrcDIR}/*
${SrcDIR}/ThirdParty/*
${SrcDIR}/Rendering/*
${SrcDIR}/Rendering/Platform/*
${SrcDIR}/World/*
)
add_executable(${executable} ${SourceFiles})
set_target_properties(${executable} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
CXX_STANDARD 20
CXX_EXTENSIONS ON
)
if (UNIX)
target_link_libraries(${executable}
SDL2
glm
OpenGL::OpenGL
OpenGL::GL
Threads::Threads

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Benjamin Kyd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,191 +0,0 @@
#include "camera.hpp"
Camera::Camera() {
projMatrix = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f);
Roll = 0.0f;
Pitch = 0.0f;
Yaw = 0.0f;
Position = {};
LookDirection = {};
viewMatrix = {};
UpdateView();
}
Camera::Camera(int w, int h) {
projMatrix = glm::perspective(glm::radians(45.0f), (float)w / float(h), 0.1f, 1000.0f);
Roll = 0.0f;
Pitch = 0.0f;
Yaw = 0.0f;
Position = {};
LookDirection = {};
viewMatrix = {};
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
// 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));
glm::mat4 rotate = matRoll * matPitch * matYaw;
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);
LookDirection.x = inverseView[2][0];
LookDirection.y = inverseView[2][1];
LookDirection.z = inverseView[2][2];
}
glm::mat4 Camera::GetViewMatrix() {
return viewMatrix;
}
glm::mat4 Camera::GetProjectionMatrix() {
return projMatrix;
}
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) {
if (e.type != SDL_MOUSEMOTION)
return;
float mouseDX = e.motion.xrel;
float mouseDY = e.motion.yrel;
glm::vec2 mouseDelta{ mouseDX, mouseDY };
MouseMoved(mouseDelta);
}
void Camera::MoveCamera(Uint8* state) {
float dx = 0;
float dz = 0;
float dy = 0;
// Rotate by camera direction
glm::mat2 rotate {
cos(Yaw), -sin(Yaw),
sin(Yaw), cos(Yaw)
};
glm::vec2 f(0.0, 1.0);
f = f * rotate;
if (state[SDL_SCANCODE_W]) {
dz -= f.y;
dx -= f.x;
}
if (state[SDL_SCANCODE_S]) {
dz += f.y;
dx += f.x;
}
if (state[SDL_SCANCODE_A]) {
dz += f.x;
dx += -f.y;
}
if (state[SDL_SCANCODE_D]) {
dz -= f.x;
dx -= -f.y;
}
if (state[SDL_SCANCODE_SPACE]) {
dy += 1;
}
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]);
// forward vector must be negative to look forward.
// read :http://in2gpu.com/2015/05/17/view-matrix/
Position.x += dx * CameraSpeed;
Position.z += dz * CameraSpeed;
Position.y += dy * CameraSpeed;
// update the view matrix
UpdateView();
}
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);
UpdateView();
}
void Camera::UpdatePosition(glm::vec3 position) {
Position = position;
UpdateView();
}
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);
UpdateView();
}
void Camera::UpdateLookDirection(glm::vec3 lookDirection) {
LookDirection = lookDirection;
Pitch = asin(-lookDirection.y);
Yaw = atan2(lookDirection.x, lookDirection.z);
UpdateView();
}

View File

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

View File

@@ -1,109 +0,0 @@
#include "shader.hpp"
Shader::Shader()
: m_fileReader() {
Program = 0;
m_frag = 0;
m_vert = 0;
m_logger = std::make_shared<Logger>();
}
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;
std::string fragmentLocation = path + ".frag";
Load(fragmentLocation, GL_FRAGMENT_SHADER);
*m_logger << LOGGER_INFO << "Fragment shader at '" << fragmentLocation << "' loaded..." << LOGGER_ENDL;
}
void Shader::Load(std::string path, GLenum type) {
GLuint activeShader = 0;
if (type == GL_VERTEX_SHADER)
m_vert = activeShader = glCreateShader(type);
if (type == GL_FRAGMENT_SHADER)
m_frag = activeShader = glCreateShader(type);
std::string loadedShaderSource = m_fileReader.LoadTextFromFile(path);
const char* shaderSource = loadedShaderSource.c_str();
int shaderSourceLength = loadedShaderSource.length();
glShaderSource(activeShader, 1, &shaderSource, &shaderSourceLength);
}
void Shader::Link() {
if (m_vert == 0 || m_frag == 0) {
*m_logger << 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(m_frag);
if (m_CheckShader(m_frag)) {
*m_logger << LOGGER_INFO << "Fragment shader '" << m_frag << "' compiled..." << LOGGER_ENDL;
}
Program = glCreateProgram();
glAttachShader(Program, m_vert);
glAttachShader(Program, m_frag);
glLinkProgram(Program);
glDeleteShader(m_vert);
glDeleteShader(m_frag);
*m_logger << LOGGER_INFO << "Program '" << Program << "' loaded..." << LOGGER_ENDL;
}
void Shader::Use() {
glUseProgram(Program);
}
bool Shader::m_CheckShader(GLuint uid) {
GLint status = GL_TRUE;
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;
delete buf;
return false;
}
return true;
}
Shader::~Shader() {
glDeleteProgram(Program);
glDeleteShader(m_vert);
glDeleteShader(m_frag);
}

View File

@@ -1,33 +0,0 @@
#ifndef MINECRAFT_RENDERER_SHADER_H_
#define MINECRAFT_RENDERER_SHADER_H_
#include <logger.h>
#include "../util/filereader.hpp"
#include "../common.hpp"
class Shader {
public:
Shader();
void Load(std::string path);
void Load(std::string path, GLenum type);
GLuint Program;
void Link();
void Use();
~Shader();
private:
std::shared_ptr<Logger> m_logger;
bool m_CheckShader(GLuint uid);
FileReader m_fileReader;
GLuint m_vert;
GLuint m_frag;
};
#endif

View File

@@ -1,11 +0,0 @@
#ifndef MINECRAFT_RENDERER_TEXTURE_H_
#define MINECRAFT_RENDERER_TEXTURE_H_
#include "../common.hpp"
class Texture {
public:
GLuint LoadTextures(std::vector<std::string> textures);
};
#endif

209
src/Rendering/camera.cpp Normal file
View File

@@ -0,0 +1,209 @@
#include "camera.hpp"
Camera::Camera()
{
projMatrix = glm::perspective( glm::radians( 45.0f ), 1.0f, 0.1f, 1000.0f );
Roll = 0.0f;
Pitch = 0.0f;
Yaw = 0.0f;
Position = {};
LookDirection = {};
viewMatrix = {};
UpdateView();
}
Camera::Camera( int w, int h )
{
projMatrix = glm::perspective( glm::radians( 45.0f ), (float) w / float( h ), 0.1f, 1000.0f );
Roll = 0.0f;
Pitch = 0.0f;
Yaw = 0.0f;
Position = {};
LookDirection = {};
viewMatrix = {};
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
// 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 ) );
glm::mat4 rotate = matRoll * matPitch * matYaw;
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 );
LookDirection.x = inverseView[2][0];
LookDirection.y = inverseView[2][1];
LookDirection.z = inverseView[2][2];
}
glm::mat4 Camera::GetViewMatrix()
{
return viewMatrix;
}
glm::mat4 Camera::GetProjectionMatrix()
{
return projMatrix;
}
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 )
{
if ( e.type != SDL_MOUSEMOTION )
return;
float mouseDX = e.motion.xrel;
float mouseDY = e.motion.yrel;
glm::vec2 mouseDelta { mouseDX, mouseDY };
MouseMoved( mouseDelta );
}
void Camera::MoveCamera( Uint8* state )
{
float dx = 0;
float dz = 0;
float dy = 0;
// Rotate by camera direction
glm::mat2 rotate {
cos( Yaw ), -sin( Yaw ),
sin( Yaw ), cos( Yaw )
};
glm::vec2 f( 0.0, 1.0 );
f = f * rotate;
if ( state[SDL_SCANCODE_W] )
{
dz -= f.y;
dx -= f.x;
}
if ( state[SDL_SCANCODE_S] )
{
dz += f.y;
dx += f.x;
}
if ( state[SDL_SCANCODE_A] )
{
dz += f.x;
dx += -f.y;
}
if ( state[SDL_SCANCODE_D] )
{
dz -= f.x;
dx -= -f.y;
}
if ( state[SDL_SCANCODE_SPACE] )
{
dy += 1;
}
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] );
// forward vector must be negative to look forward.
// read :http://in2gpu.com/2015/05/17/view-matrix/
Position.x += dx * CameraSpeed;
Position.z += dz * CameraSpeed;
Position.y += dy * CameraSpeed;
// update the view matrix
UpdateView();
}
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 );
UpdateView();
}
void Camera::UpdatePosition( glm::vec3 position )
{
Position = position;
UpdateView();
}
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 );
UpdateView();
}
void Camera::UpdateLookDirection( glm::vec3 lookDirection )
{
LookDirection = lookDirection;
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_
#ifndef MINECRAFT_RENDERING_CAMERA_H_
#define MINECRAFT_RENDERING_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:

128
src/Rendering/face.hpp Normal file
View File

@@ -0,0 +1,128 @@
#ifndef MINECRAFT_RENDERING_FACE_H_
#define MINECRAFT_RENDERING_FACE_H_
#include <vector>
#include <glm/glm.hpp>
namespace EFaceType {
enum Face : uint8_t {
Top,
Bottom,
Left,
Right,
Front,
Back,
};
}
static std::vector<glm::vec3> CubeTopFace = {
{ -0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f }
};
static std::vector<glm::vec2> CubeTopFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
static std::vector<glm::vec3> CubeBottomFace = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, -0.5f }
};
static std::vector<glm::vec2> CubeBottomFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
static std::vector<glm::vec3> CubeLeftFace = {
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f }
};
static std::vector<glm::vec2> CubeLeftFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
static std::vector<glm::vec3> CubeRightFace = {
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
};
static std::vector<glm::vec2> CubeRightFaceUVs = {
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f },
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }
};
static std::vector<glm::vec3> CubeFrontFace = {
{ -0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f }
};
static std::vector<glm::vec2> CubeFrontFaceUVs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f }
};
static std::vector<glm::vec3> CubeBackFace = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f }
};
static std::vector<glm::vec2> CubeBackFaceUVs = {
{ 1.0f, 1.0f },
{ 0.0f, 1.0f },
{ 0.0f, 0.0f },
{ 0.0f, 0.0f },
{ 1.0f, 0.0f },
{ 1.0f, 1.0f }
};
#endif

View File

@@ -0,0 +1,29 @@
#ifndef MINECRAFT_RENDERING_FRUSTRUM_H_
#define MINECRAFT_RENDERING_FRUSTRUM_H_
namespace EFrustrumPlanes
{
enum Planes
{
Right,
Left,
Top,
Bottom,
Far,
Near
};
};
class FrustrumPlane
{
public:
};
class Frustrum
{
public:
};
#endif

6
src/Rendering/mesh.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include "mesh.hpp"
Mesh::Mesh()
{
}

27
src/Rendering/mesh.hpp Normal file
View File

@@ -0,0 +1,27 @@
#ifndef MINECRAFT_RENDERING_MESH_H_
#define MINECRAFT_RENDERING_MESH_H_
#include "../common.hpp"
class Vertex
{
public:
glm::vec3 Position;
glm::vec3 SurfaceNormal;
};
class Mesh
{
public:
Mesh();
private:
GLuint mVAO;
GLuint mVBO;
};
#endif

View File

@@ -0,0 +1,66 @@
#include "renderable.hpp"
#include <algorithm>
Renderable::Renderable()
{
}
void Renderable::Init()
{
}
void Renderable::AddMesh( Mesh* mesh )
{
if (mesh == nullptr) return;
mMeshs.push_back( mesh );
}
void Renderable::RemoveMesh( Mesh* mesh )
{
// Renderable does not include mesh
if (std::find( mMeshs.begin(), mMeshs.end(), mesh ) == mMeshs.end())
return;
std::remove( mMeshs.begin(), mMeshs.end(), mesh );
}
void Renderable::SetActiveMesh( Mesh* mesh )
{
// Renderable does not include mesh
if (std::find( mMeshs.begin(), mMeshs.end(), mesh ) == mMeshs.end())
return;
mActiveMesh = mesh;
}
Mesh* Renderable::GetActiveMesh()
{
return mActiveMesh;
}
void Renderable::UpdateBuffer()
{
}
void Renderable::Load()
{
}
void Renderable::Unload()
{
}
Renderable::~Renderable()
{
Unload();
}

View File

@@ -0,0 +1,45 @@
#ifndef MINECRAFT_RENDERING_RENDERABLE_H_
#define MINECRAFT_RENDERING_RENDERABLE_H_
#include "../common.hpp"
class Mesh;
// Basically a model but thats effort
// perhaps sub-class?
class Renderable
{
public:
Renderable();
void Init();
// DOES NOT OWN MESH
void AddMesh( Mesh* );
void RemoveMesh( Mesh* );
void SetActiveMesh( Mesh* );
Mesh* GetActiveMesh();
void UpdateBuffer();
// GPU Load methods
void Load();
void Unload();
~Renderable();
private:
std::vector<glm::vec3> mBuff;
std::vector<Mesh*> mMeshs;
Mesh* mActiveMesh;
// Meshes have uniform uniforms
GLuint mUBO;
glm::mat4 mModelMatrix;
};
#endif

View File

@@ -0,0 +1,11 @@
#include "rendermaster.hpp"
RenderMaster::RenderMaster()
: mWorldRenderer(),
mMeshRenderer()
{
}

View File

@@ -0,0 +1,41 @@
#ifndef MINECRAFT_RENDERING_RENDERMASTER_H_
#define MINECRAFT_RENDERING_RENDERMASTER_H_
/**
* Renderer Structure
*
* Mesh -> Renderable
* Mesh -> VoxelMesh
* Renderable -> Model
* Renderable -> World (static(?))
* Renderable -> Entity (dynamic)
* Renderable -> Particle (dynamic)
*
* Kinda just winging it ngl
*/
class WorldRenderer
{
};
class MeshRenderer
{
};
class RenderMaster
{
public:
RenderMaster();
WorldRenderer mWorldRenderer;
MeshRenderer mMeshRenderer;
};
#endif

121
src/Rendering/shader.cpp Normal file
View File

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

30
src/Rendering/shader.hpp Normal file
View File

@@ -0,0 +1,30 @@
#ifndef MINECRAFT_RENDERING_SHADER_H_
#define MINECRAFT_RENDERING_SHADER_H_
#include <logger.h>
#include <glad/glad.h>
class Shader {
public:
Shader();
void Load(std::string path);
void Load(std::string path, GLenum type);
GLuint Program;
void Link();
void Use();
~Shader();
private:
std::shared_ptr<Logger> mLogger;
bool mCheckShader(GLuint uid);
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;

14
src/Rendering/texture.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef MINECRAFT_RENDERING_TEXTURE_H_
#define MINECRAFT_RENDERING_TEXTURE_H_
#include <vector>
#include <string>
#include <glad/glad.h>
class Texture {
public:
GLuint LoadTextures(std::vector<std::string> textures);
};
#endif

View File

@@ -0,0 +1,23 @@
#ifndef MINECRAFT_RENDERER_VOXELMESH_H_
#define MINECRAFT_RENDERER_VOXELMESH_H_
#include "mesh.hpp"
class VoxelMesh : public Mesh
{
public:
VoxelMesh();
int Width;
int Height;
int Depth;
// Size is w*h*d
std::vector<uint8_t> Voxels;
};
#endif

View File

@@ -5,7 +5,7 @@ Display::Display( int w, int h, std::string title )
{
mLogger << LOGGER_INFO << "Initializing display" << LOGGER_ENDL;
SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO );
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO );
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
@@ -60,7 +60,6 @@ void Display::Input( SDL_Event* e )
while ( SDL_PollEvent( e ) )
{
switch ( e->type )
{
@@ -83,8 +82,9 @@ void Display::Input( SDL_Event* e )
{
if ( e->window.event == SDL_WINDOWEVENT_RESIZED )
{
// CameraUpdateProjection( e->window.data1, e->window.data2 );
glViewport( 0, 0, e->window.data1, e->window.data2 );
mW = e->window.data1; mH = e->window.data2;
// CameraUpdateProjection( mW, mH );
glViewport( 0, 0, mW, mH );
}
break;
@@ -98,7 +98,7 @@ void Display::Input( SDL_Event* e )
}
// if ( IsMouseActive ) m_player->HandleMouseSDL( *e );
// if ( IsMouseActive ) HandleMouseSDL( *e );
}
// m_player->MoveSDL( state );

View File

@@ -13,18 +13,23 @@
#include "display.hpp"
#include "settings.hpp"
#include "Rendering/rendermaster.hpp"
#include "Rendering/texture.hpp"
#include "Rendering/camera.hpp"
#include "Rendering/mesh.hpp"
#define __DEBUG
static const int VERSION_MAJOR = 1;
static const int VERSION_MINOR = 1;
static const int VERSION_PATCH = 0;
static const int VERSION_MAJOR = 0;
static const int VERSION_MINOR = 0;
static const int VERSION_PATCH = 1;
void version()
{
std::stringstream version;
auto& container = []( std::string s ) { std::string r = ""; for ( auto& c : s ) { r += "-"; } return r; };
const auto& container = []( std::string s ) { std::string r = ""; for ( auto& c : s ) { r += "-"; } return r; };
version << "Minecraft ";
version << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
@@ -38,6 +43,9 @@ void Loop( Display* display )
{
SDL_Event e;
while ( display->IsWindowOpen )
{
display->PrepareFrame();
@@ -45,9 +53,12 @@ void Loop( Display* display )
// make framerate agnostic
display->Input( &e );
// rendering here
display->NextFrame();
}
@@ -75,4 +86,5 @@ int main( int argc, char** argv )
Loop( &display );
return 0;
}

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

3
src/threadpool.hpp Normal file
View File

@@ -0,0 +1,3 @@
// Threadpool for asset management and other such tasks

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;
}