-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16f400c
commit b3a6769
Showing
103 changed files
with
4,951 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright (c) 2020 by Stan Fortoński */ | ||
|
||
#include "Engine.hpp" | ||
|
||
namespace Engine | ||
{ | ||
void Engine::initGLFW() const | ||
{ | ||
glfwInit(); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, Config::get().getMajorVersion()); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, Config::get().getMinorVersion()); | ||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
|
||
#ifdef __APPLE__ | ||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | ||
#endif | ||
} | ||
|
||
void Engine::initGLEW() const | ||
{ | ||
glewExperimental = GL_TRUE; | ||
if (glewInit() != GLEW_OK) | ||
throw std::runtime_error("Can\'t init GLEW"); | ||
} | ||
|
||
void Engine::initDefaultOptionsGL() const | ||
{ | ||
if (Config::get().getSamples() > 0) | ||
glEnable(GL_MULTISAMPLE); | ||
glEnable(GL_DEPTH_TEST); | ||
glDepthFunc(GL_LESS); | ||
glEnable(GL_CULL_FACE); | ||
glEnable(GL_BLEND); | ||
glEnable(GL_CLIP_DISTANCE0); | ||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
} | ||
|
||
void Engine::initDeltaTime() | ||
{ | ||
float currentTime = glfwGetTime(); | ||
deltaTime = currentTime - lastTime; | ||
lastTime = currentTime; | ||
} | ||
|
||
void Engine::calcFPS() | ||
{ | ||
float currentTime = glfwGetTime(); | ||
if ((currentTime - lastTimeFromShow) > 1) | ||
{ | ||
actualFPS = std::to_string(frames)+" FPS"; | ||
frames = 0; | ||
lastTimeFromShow = currentTime; | ||
} | ||
else ++frames; | ||
} | ||
|
||
#if DEBUG_ENGINE == 1 | ||
void Engine::showErrors() | ||
{ | ||
GLenum err; | ||
while ((err = glGetError()) != GL_NO_ERROR) | ||
std::cout<<"GL error code: "<<err<<std::endl; | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright (c) 2020 by Stan Fortoński */ | ||
/* This project is using below libraries */ | ||
/* GLEW library - http://glew.sourceforge.net */ | ||
/* GLFW library - https://www.glfw.org */ | ||
/* GLM library - https://glm.g-truc.net */ | ||
/* Assimp library - https://github.com/assimp/assimp */ | ||
/* FreeType library - https://www.freetype.org */ | ||
/* STB_IMAGE library - https://github.com/nothings/stb */ | ||
|
||
#ifndef ENGINE_HPP | ||
#define ENGINE_HPP 1 | ||
#include <GL/glew.h> | ||
#include <stdexcept> | ||
#include <GLFW/glfw3.h> | ||
#include "support/Singleton.hpp" | ||
#include "Config.hpp" | ||
|
||
namespace Engine | ||
{ | ||
class Engine: public Singleton<Engine> | ||
{ | ||
friend class Singleton<Engine>; | ||
float lastTimeFromShow = 0; | ||
unsigned frames = 0; | ||
float lastTime = 0; | ||
float deltaTime = 0; | ||
std::string actualFPS; | ||
|
||
Engine(){;} | ||
|
||
public: | ||
void initGLFW() const; | ||
void initGLEW() const; | ||
void initDefaultOptionsGL() const; | ||
void initDeltaTime(); | ||
void calcFPS(); | ||
|
||
#if DEBUG_ENGINE == 1 | ||
void showErrors(); | ||
#endif | ||
|
||
float getDeltaTime() const{return deltaTime;} | ||
std::string getFPS() const{return actualFPS;} | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* Copyright (c) 2019 by Stan Fortoński */ | ||
|
||
#include "Program.hpp" | ||
|
||
namespace Engine | ||
{ | ||
Program::Program() | ||
{ | ||
amount = new unsigned; | ||
*amount = 1; | ||
} | ||
|
||
Program::Program(const Shader & vertex, const Shader & fragment) | ||
{ | ||
programId = glCreateProgram(); | ||
glAttachShader(programId, vertex.getId()); | ||
glAttachShader(programId, fragment.getId()); | ||
link(); | ||
vertex.clear(); | ||
fragment.clear(); | ||
amount = new unsigned; | ||
*amount = 1; | ||
} | ||
|
||
Program::Program(const std::string & vertFileName, const std::string & fragFileName) | ||
{ | ||
programId = glCreateProgram(); | ||
Shader vertex = Shader::createVertexShader(vertFileName); | ||
Shader fragment = Shader::createFragmentShader(fragFileName); | ||
glAttachShader(programId, vertex.getId()); | ||
glAttachShader(programId, fragment.getId()); | ||
link(); | ||
vertex.clear(); | ||
fragment.clear(); | ||
amount = new unsigned; | ||
*amount = 1; | ||
} | ||
|
||
Program::Program(const Program & program) | ||
{ | ||
swap(program); | ||
} | ||
|
||
Program & Program::operator=(const Program & program) | ||
{ | ||
clear(); | ||
swap(program); | ||
return *this; | ||
} | ||
|
||
void Program::swap(const Program & program) | ||
{ | ||
programId = program.programId; | ||
amount = program.amount; | ||
*amount = *amount + 1; | ||
} | ||
|
||
void Program::clear() | ||
{ | ||
*amount = *amount - 1; | ||
if (*amount == 0) | ||
{ | ||
delete amount; | ||
if (programId != 0) | ||
glDeleteProgram(programId); | ||
} | ||
} | ||
|
||
Program::~Program() | ||
{ | ||
clear(); | ||
} | ||
|
||
std::string Program::getLinkMessageErrorAndClear() const | ||
{ | ||
int length; | ||
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &length); | ||
char* message = new char[length]; | ||
|
||
glGetProgramInfoLog(programId, length, &length, message); | ||
glDeleteProgram(programId); | ||
|
||
std::string finalMess = message; | ||
delete [] message; | ||
return finalMess; | ||
} | ||
|
||
void Program::create() | ||
{ | ||
if (programId != 0) | ||
throw std::runtime_error("Can't create exists program"); | ||
programId = glCreateProgram(); | ||
} | ||
|
||
void Program::attachShader(const Shader & shader) const | ||
{ | ||
if (programId == 0) | ||
throw std::runtime_error("Can't attach shader to empty program"); | ||
glAttachShader(programId, shader.getId()); | ||
shader.clear(); | ||
} | ||
|
||
void Program::link() const | ||
{ | ||
if (programId == 0) | ||
throw std::runtime_error("Can't link empty program"); | ||
glLinkProgram(programId); | ||
int success; | ||
glGetProgramiv(programId, GL_LINK_STATUS, &success); | ||
if (!success) | ||
throw std::runtime_error(getLinkMessageErrorAndClear()); | ||
} | ||
|
||
unsigned Program::getUniformId(const char * name) const | ||
{ | ||
return glGetUniformLocation(programId, name); | ||
} | ||
|
||
void Program::setInt(const char * name, int i) const | ||
{ | ||
glUniform1i(getUniformId(name), i); | ||
} | ||
|
||
void Program::setFloat(const char * name, float f) const | ||
{ | ||
glUniform1f(getUniformId(name), f); | ||
} | ||
|
||
void Program::setVec2(const char * name, const glm::vec2 & vec) const | ||
{ | ||
glUniform2fv(getUniformId(name), 1, glm::value_ptr(vec)); | ||
} | ||
|
||
void Program::setVec3(const char * name, const glm::vec3 & vec) const | ||
{ | ||
glUniform3fv(getUniformId(name), 1, glm::value_ptr(vec)); | ||
} | ||
|
||
void Program::setVec4(const char * name, const glm::vec4 & vec) const | ||
{ | ||
glUniform4fv(getUniformId(name), 1, glm::value_ptr(vec)); | ||
} | ||
|
||
void Program::setMat2(const char * name, const glm::mat2 & mat) const | ||
{ | ||
glUniformMatrix2fv(getUniformId(name), 1, GL_FALSE, glm::value_ptr(mat)); | ||
} | ||
|
||
void Program::setMat3(const char * name, const glm::mat3 & mat) const | ||
{ | ||
glUniformMatrix3fv(getUniformId(name), 1, GL_FALSE, glm::value_ptr(mat)); | ||
} | ||
|
||
void Program::setMat4(const char * name, const glm::mat4 & mat) const | ||
{ | ||
glUniformMatrix4fv(getUniformId(name), 1, GL_FALSE, glm::value_ptr(mat)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* Copyright (c) 2019 by Stan Fortoński */ | ||
|
||
#ifndef PROGRAM_HPP | ||
#define PROGRAM_HPP 1 | ||
#include "Shader.hpp" | ||
#include <glm/glm.hpp> | ||
#include <glm/gtc/matrix_transform.hpp> | ||
#include <glm/gtc/type_ptr.hpp> | ||
|
||
namespace Engine | ||
{ | ||
class Program | ||
{ | ||
unsigned programId = 0; | ||
unsigned * amount; | ||
|
||
std::string getLinkMessageErrorAndClear() const; | ||
unsigned getUniformId(const char * name) const; | ||
|
||
void swap(const Program & program); | ||
void clear(); | ||
|
||
public: | ||
Program(); | ||
Program(const Program & program); | ||
Program & operator=(const Program & program); | ||
Program(const Shader & vertex, const Shader & fragment); | ||
Program(const std::string & vertFileName, const std::string & fragFileName); | ||
~Program(); | ||
|
||
void create(); | ||
void link() const; | ||
void attachShader(const Shader & shader) const; | ||
|
||
void use() const {glUseProgram(programId);} | ||
|
||
void setInt(const char * name, int i) const; | ||
void setFloat(const char * name, float f) const; | ||
void setVec2(const char * name, const glm::vec2 & vec) const; | ||
void setVec3(const char * name, const glm::vec3 & vec) const; | ||
void setVec4(const char * name, const glm::vec4 & vec) const; | ||
void setMat2(const char * name, const glm::mat2 & mat) const; | ||
void setMat3(const char * name, const glm::mat3 & mat) const; | ||
void setMat4(const char * name, const glm::mat4 & mat) const; | ||
|
||
unsigned getId() const {return programId;} | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Copyright (c) 2019 by Stan Fortoński */ | ||
|
||
#include "Shader.hpp" | ||
|
||
namespace Engine | ||
{ | ||
Shader::Shader(const std::string & fileName, GLenum t): type(t) | ||
{ | ||
std::string source = getSource(fileName); | ||
const char* data = source.c_str(); | ||
shaderId = glCreateShader(type); | ||
glShaderSource(shaderId, 1, &data, nullptr); | ||
glCompileShader(shaderId); | ||
|
||
int success; | ||
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success); | ||
if (!success) | ||
throw std::runtime_error(getCompileMessageErrorAndClear()); | ||
} | ||
|
||
std::string Shader::getSource(const std::string & fileName) const | ||
{ | ||
std::ifstream file(fileName, std::ios::binary); | ||
if (!file.is_open()) | ||
throw std::runtime_error("Can\'t open shader file: "+fileName+"."); | ||
|
||
std::stringstream stream; | ||
stream<<file.rdbuf(); | ||
|
||
file.clear(); | ||
file.close(); | ||
return stream.str(); | ||
} | ||
|
||
std::string Shader::getCompileMessageErrorAndClear() const | ||
{ | ||
int length; | ||
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length); | ||
char* message = new char[length]; | ||
|
||
glGetShaderInfoLog(shaderId, length, &length, message); | ||
glDeleteShader(shaderId); | ||
|
||
std::string finalMess = message; | ||
delete [] message; | ||
return finalMess; | ||
} | ||
} |
Oops, something went wrong.