added old files which will work fine
This commit is contained in:
191
src/Rendering/camera.cpp
Normal file
191
src/Rendering/camera.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
#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();
|
||||
|
||||
}
|
||||
45
src/Rendering/camera.hpp
Normal file
45
src/Rendering/camera.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef MINECRAFT_RENDERER_CAMERA_H_
|
||||
#define MINECRAFT_RENDERER_CAMERA_H_
|
||||
|
||||
#include "../common.hpp"
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
Camera();
|
||||
Camera(int w, int h);
|
||||
|
||||
void UpdateView();
|
||||
|
||||
glm::mat4 GetViewMatrix();
|
||||
glm::mat4 GetProjectionMatrix();
|
||||
glm::mat4 GetFrustrumMatrix();
|
||||
|
||||
void UpdateProjection(int width, int height);
|
||||
|
||||
// Keyboard
|
||||
void MoveCamera(Uint8* state);
|
||||
// Mouse
|
||||
void HandleMouse(SDL_Event e);
|
||||
// Mouse Delta
|
||||
void MouseMoved(glm::vec2 mouseDelta);
|
||||
|
||||
// Updatable by
|
||||
float MouseSensitivity = 0.1f;
|
||||
float CameraSpeed = 2.0f;
|
||||
|
||||
void UpdatePosition(glm::vec3 position);
|
||||
void UpdateEulerLookDirection(float roll, float pitch, float yaw);
|
||||
void UpdateLookDirection(glm::vec3 lookDirection);
|
||||
|
||||
glm::vec3 Position = {};
|
||||
float Roll, Pitch, Yaw;
|
||||
glm::vec3 LookDirection = {};
|
||||
|
||||
private:
|
||||
|
||||
glm::mat4 viewMatrix = {};
|
||||
glm::mat4 projMatrix = {};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
3
src/Rendering/frustrum.cpp
Normal file
3
src/Rendering/frustrum.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "frustrum.hpp"
|
||||
|
||||
|
||||
35
src/Rendering/frustrum.hpp
Normal file
35
src/Rendering/frustrum.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#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
|
||||
109
src/Rendering/shader.cpp
Normal file
109
src/Rendering/shader.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#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);
|
||||
|
||||
}
|
||||
33
src/Rendering/shader.hpp
Normal file
33
src/Rendering/shader.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#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
|
||||
60
src/Rendering/texture.cpp
Normal file
60
src/Rendering/texture.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "texture.hpp"
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
#include "../config.hpp"
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "../util/stb_image.hpp"
|
||||
|
||||
GLuint Texture::LoadTextures(std::vector<std::string> textures) {
|
||||
|
||||
Logger logger;
|
||||
|
||||
std::string basePath = GameConfig.ResourceBase + "textures/";
|
||||
|
||||
int x = 16;
|
||||
int y = 16;
|
||||
|
||||
GLsizei layers = textures.size();
|
||||
GLubyte* texels = (GLubyte*)malloc(x * y * 4 * layers * sizeof(GLubyte));
|
||||
|
||||
for (int i = 0; i < layers; i++) {
|
||||
|
||||
std::string path = basePath + textures[i];
|
||||
|
||||
int xR = 0;
|
||||
int yR = 0;
|
||||
int cR = 0;
|
||||
|
||||
unsigned char* texture = stbi_load(path.c_str(), &xR, &yR, &cR, STBI_rgb_alpha);
|
||||
|
||||
memcpy(texels + (i * x * y * 4), texture, x * y * 4);
|
||||
|
||||
stbi_image_free(texture);
|
||||
logger << LOGGER_INFO << "Texture at '" << path << "' Loaded..." << LOGGER_ENDL;
|
||||
|
||||
}
|
||||
|
||||
GLuint textureArray = 0;
|
||||
|
||||
glGenTextures(1, &textureArray);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
|
||||
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, x, y, layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
|
||||
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, x, y, layers, GL_RGBA, GL_UNSIGNED_BYTE, texels);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
||||
|
||||
free(texels);
|
||||
|
||||
return textureArray;
|
||||
|
||||
}
|
||||
11
src/Rendering/texture.hpp
Normal file
11
src/Rendering/texture.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MINECRAFT_RENDERER_TEXTURE_H_
|
||||
#define MINECRAFT_RENDERER_TEXTURE_H_
|
||||
|
||||
#include "../common.hpp"
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
GLuint LoadTextures(std::vector<std::string> textures);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -60,7 +60,6 @@ void Display::Input( SDL_Event* e )
|
||||
|
||||
while ( SDL_PollEvent( e ) )
|
||||
{
|
||||
|
||||
switch ( e->type )
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user