CMake refactor, fix warnings
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ res/textures
|
||||
.kdev4
|
||||
.cache
|
||||
imgui.ini
|
||||
CMakeUserPresets.json
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.24)
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(
|
||||
Fall-Fever
|
||||
@@ -13,12 +13,5 @@ find_package(glfw3 REQUIRED)
|
||||
find_package(glm REQUIRED)
|
||||
find_package(spdlog REQUIRED)
|
||||
|
||||
# Colored output with Ninja
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
||||
add_compile_options (-fdiagnostics-color=always)
|
||||
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
add_compile_options (-fcolor-diagnostics)
|
||||
endif()
|
||||
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
|
||||
|
||||
57
CMakePresets.json
Normal file
57
CMakePresets.json
Normal file
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"version": 2,
|
||||
"cmakeMinimumRequired": {
|
||||
"major": 3,
|
||||
"minor": 14,
|
||||
"patch": 0
|
||||
},
|
||||
"configurePresets": [
|
||||
{
|
||||
"name": "cmake-pedantic",
|
||||
"hidden": true,
|
||||
"warnings": {
|
||||
"dev": true,
|
||||
"deprecated": true,
|
||||
"uninitialized": true,
|
||||
"unusedCli": true,
|
||||
"systemVars": false
|
||||
},
|
||||
"errors": {
|
||||
"dev": true,
|
||||
"deprecated": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "dev-mode",
|
||||
"hidden": true,
|
||||
"inherits": "cmake-pedantic",
|
||||
"cacheVariables": {
|
||||
"CMAKE_COLOR_DIAGNOSTICS": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "ci-std",
|
||||
"description": "This preset makes sure the project actually builds with at least the specified standard",
|
||||
"hidden": true,
|
||||
"cacheVariables": {
|
||||
"CMAKE_CXX_EXTENSIONS": "OFF"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "flags-unix",
|
||||
"hidden": true,
|
||||
"cacheVariables": {
|
||||
"CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "ci-unix",
|
||||
"generator": "Unix Makefiles",
|
||||
"hidden": true,
|
||||
"inherits": ["flags-unix", "ci-std"],
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Release"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
[Project]
|
||||
CreatedFrom=CMakeLists.txt
|
||||
Manager=KDevCMakeManager
|
||||
Name=Fall-Fever
|
||||
@@ -1,7 +0,0 @@
|
||||
if(UNIX)
|
||||
set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE)
|
||||
endif(UNIX)
|
||||
|
||||
if(WIN32)
|
||||
set(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE)
|
||||
endif(WIN32)
|
||||
@@ -1,7 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
void main() {
|
||||
|
||||
// Empty as we aren't rendering to any color buffer
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 a_position;
|
||||
|
||||
uniform mat4 u_modelViewProjMatrix;
|
||||
|
||||
void main() {
|
||||
|
||||
gl_Position = u_modelViewProjMatrix * vec4(a_position, 1.0f);
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#version 330 core
|
||||
in vec4 v_fragmentPosition;
|
||||
|
||||
uniform vec3 v_lightPos;
|
||||
uniform float pointShadowDepthMapFarPlane;
|
||||
|
||||
void main() {
|
||||
|
||||
// Distance between fragment and light source
|
||||
float lightDistance = length(v_fragmentPosition.xyz - v_lightPos);
|
||||
|
||||
// map to [0;1] range
|
||||
lightDistance = lightDistance / pointShadowDepthMapFarPlane;
|
||||
|
||||
gl_FragDepth = lightDistance;
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#version 330 core
|
||||
layout (triangles) in;
|
||||
layout (triangle_strip, max_vertices=18) out;
|
||||
|
||||
uniform mat4 u_shadowMatrices[6];
|
||||
|
||||
out vec4 v_fragmentPosition;
|
||||
|
||||
void main() {
|
||||
|
||||
for(int face = 0; face < 6; face++) {
|
||||
|
||||
gl_Layer = face;
|
||||
for(int i = 0; i < 3; i++) {
|
||||
|
||||
v_fragmentPosition = gl_in[i].gl_Position;
|
||||
gl_Position = u_shadowMatrices[face] * v_fragmentPosition;
|
||||
EmitVertex();
|
||||
|
||||
}
|
||||
|
||||
EndPrimitive();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 a_position;
|
||||
|
||||
uniform mat4 u_modelMatrix;
|
||||
|
||||
void main() {
|
||||
|
||||
gl_Position = u_modelMatrix * vec4(a_position, 1.0f);
|
||||
}
|
||||
@@ -18,7 +18,6 @@ void Camera::updateVPM()
|
||||
|
||||
void Camera::updateAspectRatio(float aspectRatio)
|
||||
{
|
||||
// m_projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f);
|
||||
m_projectionMatrix = glm::perspective(m_fov / 2.F, aspectRatio, .1F, 1000.F);
|
||||
updateVPM();
|
||||
}
|
||||
@@ -71,12 +70,12 @@ void Camera::updateDirectionFromMouseInput(MouseCursorInput const &mouse_cursor_
|
||||
{
|
||||
auto [deltaX, deltaY] = mouse_cursor_input;
|
||||
|
||||
if (deltaX == 0 && deltaY == 0) {
|
||||
if (std::abs(deltaX) < std::numeric_limits<double>::epsilon() && std::abs(deltaY) < std::numeric_limits<double>::epsilon()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_yaw += deltaX;
|
||||
m_pitch += deltaY;
|
||||
m_yaw += static_cast<float>(deltaX);
|
||||
m_pitch += static_cast<float>(deltaY);
|
||||
|
||||
static constexpr float CLIP = 89.;
|
||||
|
||||
@@ -88,9 +87,9 @@ void Camera::updateDirectionFromMouseInput(MouseCursorInput const &mouse_cursor_
|
||||
}
|
||||
|
||||
glm::vec3 direction;
|
||||
direction.x = std::cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
direction.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
|
||||
direction.y = std::sin(glm::radians(m_pitch));
|
||||
direction.z = std::sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
direction.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
|
||||
m_front_vec = glm::normalize(direction);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Controller.h"
|
||||
#include "Camera.h"
|
||||
#include "Entity.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Helper.h"
|
||||
#include "Light.h"
|
||||
#include "Scene.h"
|
||||
@@ -18,15 +19,11 @@ Controller::Controller()
|
||||
: m_gameWindow(std::make_shared<Window>()), m_camera(std::make_shared<Camera>(90., m_gameWindow->aspectRatio()))
|
||||
{
|
||||
std::array shaderProgramPrototypes{
|
||||
ShaderProgram::Prototype{"defaultProgram", "data/shaders/basic.vert", "data/shaders/basic.frag", ""},
|
||||
ShaderProgram::Prototype{"lightProgram", "data/shaders/light.vert", "data/shaders/light.frag", ""},
|
||||
ShaderProgram::Prototype{"skyboxProgram", "data/shaders/skybox.vert", "data/shaders/skybox.frag", ""},
|
||||
ShaderProgram::Prototype{"defaultProgram", "data/shaders/basic.vert", "data/shaders/basic.frag"},
|
||||
ShaderProgram::Prototype{"lightProgram", "data/shaders/light.vert", "data/shaders/light.frag"},
|
||||
ShaderProgram::Prototype{"skyboxProgram", "data/shaders/skybox.vert", "data/shaders/skybox.frag"},
|
||||
ShaderProgram::Prototype{"postProcessingProgram", "data/shaders/postprocessing.vert",
|
||||
"data/shaders/postprocessing.frag", ""},
|
||||
ShaderProgram::Prototype{"directionalShadowDepthProgram", "data/shaders/directionalShadowDepth.vert",
|
||||
"data/shaders/directionalShadowDepth.frag", ""},
|
||||
ShaderProgram::Prototype{"pointShadowDepthProgram", "data/shaders/pointShadowDepth.vert",
|
||||
"data/shaders/pointShadowDepth.frag", "data/shaders/pointShadowDepth.geom"},
|
||||
"data/shaders/postprocessing.frag"},
|
||||
};
|
||||
|
||||
for (auto &prototype : shaderProgramPrototypes) {
|
||||
@@ -46,7 +43,7 @@ void Controller::run()
|
||||
updateExposure(*getShaderProgramByName("postProcessingProgram"));
|
||||
|
||||
auto lightSource = m_scene->getEntityByName("light");
|
||||
lightSource->setScale(.1);
|
||||
lightSource->setScale(.1F);
|
||||
lightSource->setRotation(glm::vec3(0.));
|
||||
lightSource->setPosition(glm::vec3(-2., 1.5, 2.));
|
||||
|
||||
@@ -116,7 +113,7 @@ void Controller::limit_framerate()
|
||||
|
||||
double frameTime = 1 / (double)MAX_FPS;
|
||||
if (frameTime > lastTime) {
|
||||
Helper::sleep((frameTime - lastTime) * 1000000);
|
||||
Helper::sleep(static_cast<unsigned>(frameTime - lastTime) * 1000000);
|
||||
}
|
||||
|
||||
m_deltaTime = glfwGetTime() - startingTime;
|
||||
|
||||
13
src/Entity.h
13
src/Entity.h
@@ -18,8 +18,8 @@ class Entity
|
||||
public:
|
||||
struct Prototype
|
||||
{
|
||||
Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale)
|
||||
: name(name), position(position), rotation(rotation), scale(scale)
|
||||
Prototype(const std::string &_name, glm::vec3 _position, glm::vec3 _rotation, float _scale)
|
||||
: name(_name), position(_position), rotation(_rotation), scale(_scale)
|
||||
{}
|
||||
virtual ~Prototype() = default;
|
||||
|
||||
@@ -66,17 +66,16 @@ class ModelEntity : public Entity
|
||||
public:
|
||||
struct Prototype : public Entity::Prototype
|
||||
{
|
||||
Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale,
|
||||
const std::string &modelName, const std::string &shaderProgramName)
|
||||
: Entity::Prototype(name, position, rotation, scale), modelName(modelName),
|
||||
shaderProgramName(shaderProgramName)
|
||||
Prototype(const std::string &_name, glm::vec3 _position, glm::vec3 _rotation, float _scale,
|
||||
std::string _modelName, std::string _shaderProgramName)
|
||||
: Entity::Prototype(_name, _position, _rotation, _scale), modelName(std::move(_modelName)),
|
||||
shaderProgramName(std::move(_shaderProgramName))
|
||||
{}
|
||||
std::string modelName;
|
||||
std::string shaderProgramName;
|
||||
};
|
||||
|
||||
ModelEntity(Prototype prototype, const Model *model, ShaderProgram *shaderProgram);
|
||||
~ModelEntity() = default;
|
||||
|
||||
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
|
||||
void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram);
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
#include <cstddef>
|
||||
|
||||
AbstractFrameBuffer::~AbstractFrameBuffer()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
void AbstractFrameBuffer::bind() const
|
||||
{
|
||||
@@ -66,7 +67,7 @@ void FrameBuffer::drawOnEntireScreen() const
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glBindVertexArray(0);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, wireframe);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, static_cast<GLenum>(wireframe));
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
@@ -88,13 +89,15 @@ void FrameBuffer::generateTextures(uint32_t width, uint32_t height)
|
||||
glGenRenderbuffers(1, &m_depthStencilBuffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, m_colorBuffer);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA,
|
||||
GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorBuffer, 0);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilBuffer);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, static_cast<GLsizei>(width),
|
||||
static_cast<GLsizei>(height));
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilBuffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
@@ -114,7 +117,8 @@ void FrameBuffer::setExposureCorrection(bool exposureCorrection) const
|
||||
}
|
||||
|
||||
AbstractDepthMap::~AbstractDepthMap()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
DepthMap::DepthMap(int RESOLUTION)
|
||||
{
|
||||
@@ -171,7 +175,8 @@ DepthMapCube::DepthMapCube(int resolution)
|
||||
}
|
||||
|
||||
DepthMapCube::~DepthMapCube()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
GLuint DepthMapCube::getCubeMapTextureId()
|
||||
{
|
||||
|
||||
14
src/Light.h
14
src/Light.h
@@ -14,8 +14,8 @@ class Light : public Entity
|
||||
public:
|
||||
struct Prototype
|
||||
{
|
||||
Prototype(const std::string &name, const std::string &parent, glm::vec3 color, float intensity)
|
||||
: name(name), parent(parent), color(color), intensity(intensity)
|
||||
Prototype(const std::string &_name, const std::string &_parent, glm::vec3 _color, float _intensity)
|
||||
: name(_name), parent(_parent), color(_color), intensity(_intensity)
|
||||
{}
|
||||
virtual ~Prototype() = default;
|
||||
|
||||
@@ -57,9 +57,9 @@ class PointLight : public Light
|
||||
public:
|
||||
struct Prototype : public Light::Prototype
|
||||
{
|
||||
Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 color,
|
||||
float intensity)
|
||||
: Light::Prototype(name, parent, color, intensity), position(position)
|
||||
Prototype(const std::string &_name, const std::string &_parent, glm::vec3 _position, glm::vec3 _color,
|
||||
float _intensity)
|
||||
: Light::Prototype(_name, _parent, _color, _intensity), position(_position)
|
||||
{}
|
||||
glm::vec3 position;
|
||||
};
|
||||
@@ -83,8 +83,8 @@ class DirectionalLight : public Light
|
||||
public:
|
||||
struct Prototype : public Light::Prototype
|
||||
{
|
||||
Prototype(const std::string &name, glm::vec3 direction, glm::vec3 color, float intensity)
|
||||
: Light::Prototype(name, "", color, intensity), direction(direction)
|
||||
Prototype(const std::string &_name, glm::vec3 _direction, glm::vec3 _color, float _intensity)
|
||||
: Light::Prototype(_name, "", _color, _intensity), direction(_direction)
|
||||
{}
|
||||
glm::vec3 direction;
|
||||
};
|
||||
|
||||
22
src/Mesh.cpp
22
src/Mesh.cpp
@@ -5,15 +5,17 @@
|
||||
#include "resources/Texture.h"
|
||||
|
||||
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<ResourceId> textures)
|
||||
: m_preInitializationVertexData{vertices, indices}, m_numElements(indices.size()), m_textures(textures)
|
||||
{}
|
||||
: m_preInitializationVertexData{vertices, indices}, m_numElements(static_cast<unsigned>(indices.size())),
|
||||
m_textures(textures)
|
||||
{
|
||||
}
|
||||
|
||||
void Mesh::initializeOnGPU()
|
||||
{
|
||||
m_vertexArray =
|
||||
new VertexArray(static_cast<void *>(m_preInitializationVertexData.vertices.data()),
|
||||
static_cast<void *>(m_preInitializationVertexData.indices.data()),
|
||||
m_preInitializationVertexData.vertices.size(), m_preInitializationVertexData.indices.size());
|
||||
m_vertexArray = new VertexArray(static_cast<void *>(m_preInitializationVertexData.vertices.data()),
|
||||
static_cast<void *>(m_preInitializationVertexData.indices.data()),
|
||||
static_cast<unsigned>(m_preInitializationVertexData.vertices.size()),
|
||||
static_cast<unsigned>(m_preInitializationVertexData.indices.size()));
|
||||
}
|
||||
|
||||
Mesh::~Mesh()
|
||||
@@ -26,12 +28,12 @@ void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
uint8_t typeNumberCount[static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS)]{0};
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
// Bind all textures in order to its texture unit
|
||||
int i = 0;
|
||||
std::size_t i = 0;
|
||||
for (auto it : m_textures) {
|
||||
auto texture = std::static_pointer_cast<Texture>(ResourceHandler::instance().resource(it));
|
||||
TextureType currentTextureType = texture->textureType();
|
||||
|
||||
texture->bind(i, shaderProgram, typeNumberCount[static_cast<int>(currentTextureType)]);
|
||||
texture->bind(static_cast<uint8_t>(i), shaderProgram, typeNumberCount[static_cast<int>(currentTextureType)]);
|
||||
|
||||
typeNumberCount[static_cast<int>(currentTextureType)] += 1;
|
||||
|
||||
@@ -40,7 +42,7 @@ void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
|
||||
// Draw elements
|
||||
m_vertexArray->bind();
|
||||
glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_numElements), GL_UNSIGNED_INT, 0);
|
||||
m_vertexArray->unbind();
|
||||
|
||||
// Unbind all textures
|
||||
@@ -53,7 +55,7 @@ void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
void Mesh::drawWithoutTextures()
|
||||
{
|
||||
m_vertexArray->bind();
|
||||
glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0);
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_numElements), GL_UNSIGNED_INT, 0);
|
||||
m_vertexArray->unbind();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "VertexArray.h"
|
||||
#include "definitions/models.h"
|
||||
#include "resources/Texture.h"
|
||||
#include "resources/Resource.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
void draw(ShaderProgram *shaderProgram);
|
||||
void drawWithoutTextures();
|
||||
|
||||
VertexArray *getVertexArray();
|
||||
auto getVertexArray() -> VertexArray *;
|
||||
|
||||
private:
|
||||
struct PreInitializationVertexData
|
||||
|
||||
126
src/Scene.cpp
126
src/Scene.cpp
@@ -1,13 +1,10 @@
|
||||
#include "Scene.h"
|
||||
#include "Camera.h"
|
||||
#include "Controller.h"
|
||||
#include "Entity.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Light.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "resources/Model.h"
|
||||
#include "resources/ResourceHandler.h"
|
||||
#include "resources/Texture.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <future>
|
||||
@@ -16,14 +13,7 @@
|
||||
|
||||
Scene::Scene(std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms)
|
||||
: m_shaderProgram(*Controller::getShaderProgramByName("defaultProgram", shaderPrograms))
|
||||
// m_depthMapDirectionalFBO(SHADOW_RES)
|
||||
{
|
||||
// // Create 4 depthMaps
|
||||
// for (int i = 0; i < 4; i++) {
|
||||
// DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES);
|
||||
// m_depthMapPointFBO.push_back(temp_depthMap);
|
||||
// }
|
||||
|
||||
// This will be removed in future when gloss maps are implemented
|
||||
m_shaderProgram.bind();
|
||||
m_shaderProgram.setUniform("u_material.shininess", 100.0f);
|
||||
@@ -43,7 +33,7 @@ Scene::Scene(std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms)
|
||||
|
||||
for (const auto &descriptor : modelDescriptors) {
|
||||
|
||||
auto loadModel = [=, &mutex]() {
|
||||
auto loadModel = [=, this, &mutex]() {
|
||||
ResourceId model = ResourceHandler::instance().registerResource<Model>(descriptor);
|
||||
|
||||
Log::logger().info("Loaded model \"{}\": {}", descriptor.name, descriptor.path);
|
||||
@@ -66,7 +56,7 @@ Scene::Scene(std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms)
|
||||
m_skybox->initializeOnGPU();
|
||||
|
||||
std::array entityPrototypes{
|
||||
ModelEntity::Prototype{"backpack", {0., 1., 0.}, {}, 0.6, "backpack", "defaultProgram"},
|
||||
ModelEntity::Prototype{"backpack", {0., 1., 0.}, {}, 0.6F, "backpack", "defaultProgram"},
|
||||
ModelEntity::Prototype{"container", {10., 1., 0.}, {45., 45., 45.}, 1., "container", "defaultProgram"},
|
||||
ModelEntity::Prototype{"ground", {}, {}, 1., "ground", "defaultProgram"},
|
||||
ModelEntity::Prototype{"light", {}, {}, 1., "cube", "lightProgram"},
|
||||
@@ -118,7 +108,7 @@ Scene::Scene(std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms)
|
||||
auto pointPrototype = dynamic_cast<PointLight::Prototype *>(prototype.get());
|
||||
if (pointPrototype) {
|
||||
currentLight = std::make_shared<PointLight>(*pointPrototype, &m_shaderProgram);
|
||||
}
|
||||
}
|
||||
|
||||
lights.push_back(currentLight);
|
||||
Log::logger().info("Loaded light \"{}\"", prototype->name);
|
||||
@@ -150,118 +140,8 @@ void Scene::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
(*it)->draw(viewProjMatrix, viewPosition);
|
||||
}
|
||||
|
||||
// Calculate shadows (unclear if it should belong here or somewhere else)
|
||||
// calculateShadows();
|
||||
}
|
||||
|
||||
// void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram)
|
||||
// {
|
||||
// // Get old viewport dimensions to reset them later...
|
||||
// GLint VIEWPORT[4];
|
||||
// glGetIntegerv(GL_VIEWPORT, VIEWPORT);
|
||||
|
||||
// glViewport(0, 0, SHADOW_RES, SHADOW_RES);
|
||||
// // Switch face culling (Peter panning)
|
||||
// glCullFace(GL_BACK);
|
||||
|
||||
// m_depthMapDirectionalFBO.bind();
|
||||
|
||||
// glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// // --- Directional shadows ---
|
||||
// glm::mat4 directionalLightView =
|
||||
// glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f,
|
||||
// 0.0f));
|
||||
// glm::mat4 directionalLightViewProjectionMatrix = m_directionalLightProjection * directionalLightView;
|
||||
|
||||
// // Draw scene from light perspective
|
||||
// // Draw all entities
|
||||
// for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
// (*it)->drawDirectionalShadows(directionalLightViewProjectionMatrix, directionalShaderProgram);
|
||||
// }
|
||||
|
||||
// m_depthMapDirectionalFBO.unbind();
|
||||
|
||||
// m_shaderProgram->bind();
|
||||
|
||||
// // Send lightViewProjMatrix to basic shader
|
||||
// m_shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix);
|
||||
|
||||
// // Send shadowMap to basic shader
|
||||
// int textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2;
|
||||
// m_shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit);
|
||||
// glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
// glBindTexture(GL_TEXTURE_2D, m_depthMapDirectionalFBO.getDepthMap());
|
||||
|
||||
// m_shaderProgram->unbind();
|
||||
|
||||
// // --- Point shadows ---
|
||||
// std::vector<PointLight *> pointLights = getPointLights();
|
||||
|
||||
// // 4 depthMaps for 4 point lights
|
||||
// for (int i = 0; i < 1; i++) {
|
||||
// m_depthMapPointFBO[i]->bind();
|
||||
|
||||
// glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// // Create 6 view matrices for every face of the cubeMap
|
||||
// std::vector<glm::mat4> viewProjMatrices;
|
||||
// glm::vec3 lightPos = pointLights[i]->getPosition();
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(1.0f, 0.0f, 0.0f),
|
||||
// glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(-1.0f, 0.0f, 0.0f),
|
||||
// glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
// glm::vec3(0.0f, 0.0f, 1.0f)));
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(0.0f, -1.0f, 0.0f),
|
||||
// glm::vec3(0.0f, 0.0f, -1.0f)));
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(0.0f, 0.0f, 1.0f),
|
||||
// glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
// lightPos + glm::vec3(0.0f, 0.0f, -1.0f),
|
||||
// glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
|
||||
// pointShaderProgram->bind();
|
||||
|
||||
// for (int i = 0; i < 6; i++) {
|
||||
// pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(),
|
||||
// viewProjMatrices[i]);
|
||||
// }
|
||||
|
||||
// pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint);
|
||||
// pointShaderProgram->setUniform("v_lightPos", lightPos);
|
||||
|
||||
// // Draw scene from light perspective
|
||||
// // Draw all entities
|
||||
// for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
// (*it)->drawPointShadows(pointShaderProgram);
|
||||
// }
|
||||
|
||||
// m_depthMapPointFBO[i]->unbind();
|
||||
|
||||
// m_shaderProgram->bind();
|
||||
|
||||
// m_shaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint);
|
||||
|
||||
// textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1;
|
||||
// m_shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit);
|
||||
// glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
// glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapTextureId());
|
||||
|
||||
// m_shaderProgram->unbind();
|
||||
// }
|
||||
|
||||
// // Reset viewport size
|
||||
// glViewport(VIEWPORT[0], VIEWPORT[1], VIEWPORT[2], VIEWPORT[3]);
|
||||
// glCullFace(GL_FRONT);
|
||||
// }
|
||||
|
||||
std::shared_ptr<ModelEntity> Scene::getEntityByName(const std::string &name)
|
||||
{
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
|
||||
19
src/Scene.h
19
src/Scene.h
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "FrameBuffer.h"
|
||||
#include "resources/Resource.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
@@ -9,14 +8,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Camera;
|
||||
class ModelEntity;
|
||||
class Light;
|
||||
class PointLight;
|
||||
class DirectionalLight;
|
||||
class ShaderProgram;
|
||||
class Skybox;
|
||||
class Model;
|
||||
|
||||
class Scene
|
||||
{
|
||||
@@ -34,7 +31,6 @@ public:
|
||||
std::shared_ptr<ModelEntity> getEntityById(uint32_t id);
|
||||
|
||||
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
|
||||
// void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram);
|
||||
|
||||
private:
|
||||
ShaderProgram &m_shaderProgram;
|
||||
@@ -45,19 +41,4 @@ private:
|
||||
|
||||
// Lights
|
||||
std::vector<std::shared_ptr<Light>> m_lights;
|
||||
|
||||
// // Shadows
|
||||
// const int SHADOW_RES = 4096 / 4;
|
||||
// DepthMap m_depthMapDirectionalFBO;
|
||||
// std::vector<DepthMapCube *> m_depthMapPointFBO;
|
||||
// // Shadow projection matrices
|
||||
// const float m_nearPlaneDirectional = 1.0f;
|
||||
// const float m_farPlaneDirectional = 15.0f;
|
||||
// glm::mat4 m_directionalLightProjection =
|
||||
// glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, m_nearPlaneDirectional, m_farPlaneDirectional);
|
||||
// const float m_aspectRatioPoint = 1.0f;
|
||||
// const float m_nearPlanePoint = 1.0f;
|
||||
// const float m_farPlanePoint = 25.0f;
|
||||
// glm::mat4 m_pointLightProjection =
|
||||
// glm::perspective(glm::radians(90.0f), m_aspectRatioPoint, m_nearPlanePoint, m_farPlanePoint);
|
||||
};
|
||||
|
||||
@@ -11,25 +11,18 @@ ShaderProgram::ShaderProgram(Prototype prototype) : m_uniqueName(prototype.name)
|
||||
|
||||
m_shaderProgramId = glCreateProgram();
|
||||
GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
GLuint gs;
|
||||
GLuint fs = compile(fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
glAttachShader(m_shaderProgramId, vs);
|
||||
glAttachShader(m_shaderProgramId, fs);
|
||||
|
||||
if (!prototype.geometryPath.empty()) {
|
||||
std::string geometryShaderSource = parse(prototype.geometryPath.c_str());
|
||||
gs = compile(geometryShaderSource, GL_GEOMETRY_SHADER);
|
||||
glAttachShader(m_shaderProgramId, gs);
|
||||
}
|
||||
|
||||
glLinkProgram(m_shaderProgramId);
|
||||
|
||||
GLint isLinked = 0;
|
||||
glGetProgramiv(m_shaderProgramId, GL_LINK_STATUS, &isLinked);
|
||||
if (!isLinked)
|
||||
Log::logger().critical("Failed to link shaderProgram \"{}\", \"{}\", \"{}\"", prototype.vertexPath,
|
||||
prototype.geometryPath, prototype.fragmentPath);
|
||||
Log::logger().critical("Failed to link shaderProgram \"{}\", \"{}\"", prototype.vertexPath,
|
||||
prototype.fragmentPath);
|
||||
|
||||
#ifdef _RELEASE
|
||||
glDetachShader(program, vs);
|
||||
@@ -37,11 +30,6 @@ ShaderProgram::ShaderProgram(Prototype prototype) : m_uniqueName(prototype.name)
|
||||
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
|
||||
if (!prototype.geometryPath.empty()) {
|
||||
glDetachShader(program, gs);
|
||||
glDeleteShader(gs);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -87,7 +75,7 @@ GLuint ShaderProgram::compile(const std::string &shaderSource, GLenum type)
|
||||
if (result != GL_TRUE) {
|
||||
int length;
|
||||
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);
|
||||
char *message = new char[length];
|
||||
char *message = new char[static_cast<unsigned>(length)];
|
||||
glGetShaderInfoLog(shaderId, length, &length, message);
|
||||
Log::logger().error("Shader compilation failed: {}", message);
|
||||
delete[] message;
|
||||
|
||||
@@ -13,7 +13,6 @@ public:
|
||||
std::string name;
|
||||
std::string vertexPath;
|
||||
std::string fragmentPath;
|
||||
std::string geometryPath;
|
||||
};
|
||||
|
||||
ShaderProgram(Prototype prototype);
|
||||
|
||||
@@ -55,21 +55,3 @@ void VertexArray::unbind()
|
||||
{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
std::vector<Vertex> VertexArray::createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates)
|
||||
{
|
||||
std::vector<Vertex> vertexVec;
|
||||
uint32_t i = 0;
|
||||
uint32_t k = 0;
|
||||
for (; i < numVertices; i += 3) {
|
||||
Vertex currentVertex = {};
|
||||
currentVertex.position.x = vertices[i];
|
||||
currentVertex.position.y = vertices[i + 1];
|
||||
currentVertex.position.z = vertices[i + 2];
|
||||
currentVertex.textureCoords.x = textureCoordinates[k];
|
||||
currentVertex.textureCoords.y = textureCoordinates[k + 1];
|
||||
k += 2;
|
||||
vertexVec.push_back(currentVertex);
|
||||
}
|
||||
return vertexVec;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "definitions/models.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <vector>
|
||||
|
||||
@@ -14,8 +12,6 @@ public:
|
||||
void bind();
|
||||
void unbind();
|
||||
|
||||
static std::vector<Vertex> createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates);
|
||||
|
||||
private:
|
||||
GLuint m_VAO;
|
||||
GLuint m_VBO;
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#include "Window.h"
|
||||
#include "Helper.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "definitions/window.h"
|
||||
#include "util/Log.h"
|
||||
|
||||
#include <glad/gl.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/gl.h>
|
||||
|
||||
Window::Window()
|
||||
{
|
||||
@@ -29,8 +28,9 @@ Window::Window()
|
||||
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
|
||||
#endif
|
||||
|
||||
m_glfw_window = std::shared_ptr<GLFWwindow>(glfwCreateWindow(m_width, m_height, "OpenGL", nullptr, nullptr),
|
||||
[](GLFWwindow *window) { glfwDestroyWindow(window); });
|
||||
m_glfw_window = std::shared_ptr<GLFWwindow>(
|
||||
glfwCreateWindow(static_cast<int>(m_width), static_cast<int>(m_height), "OpenGL", nullptr, nullptr),
|
||||
[](GLFWwindow *window) { glfwDestroyWindow(window); });
|
||||
if (!m_glfw_window) {
|
||||
Log::logger().critical("Failed to create window");
|
||||
}
|
||||
@@ -38,8 +38,13 @@ Window::Window()
|
||||
// Wait for window to maximize (in case)
|
||||
// Helper::sleep(1000);
|
||||
|
||||
glfwGetWindowPos(m_glfw_window.get(), &m_posX, &m_posY);
|
||||
glfwGetWindowSize(m_glfw_window.get(), &m_width, &m_height);
|
||||
int width{};
|
||||
int height{};
|
||||
|
||||
glfwGetWindowSize(m_glfw_window.get(), &width, &height);
|
||||
|
||||
m_width = static_cast<uint32_t>(width);
|
||||
m_height = static_cast<uint32_t>(height);
|
||||
|
||||
// Create OpenGL context
|
||||
glfwMakeContextCurrent(m_glfw_window.get());
|
||||
@@ -75,7 +80,7 @@ Window::Window()
|
||||
|
||||
set_catched_cursor(m_mouse_catched);
|
||||
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
glViewport(0, 0, static_cast<GLsizei>(m_width), static_cast<GLsizei>(m_height));
|
||||
|
||||
glfwSetWindowUserPointer(m_glfw_window.get(), this);
|
||||
glfwSetKeyCallback(m_glfw_window.get(), key_callback);
|
||||
@@ -96,15 +101,20 @@ auto Window::dimensions_changed() const -> bool
|
||||
glfwGetFramebufferSize(m_glfw_window.get(), &new_width, &new_height);
|
||||
glfwGetWindowPos(m_glfw_window.get(), &new_posx, &new_posy);
|
||||
|
||||
return !(new_width == m_width && new_height == m_height && new_posx == m_posX && new_posy == m_posY);
|
||||
return !(static_cast<uint32_t>(new_width) == m_width && static_cast<uint32_t>(new_height) == m_height);
|
||||
}
|
||||
|
||||
void Window::update_dimensions()
|
||||
{
|
||||
glfwGetFramebufferSize(m_glfw_window.get(), &m_width, &m_height);
|
||||
glfwGetWindowPos(m_glfw_window.get(), &m_posX, &m_posY);
|
||||
int width{};
|
||||
int height{};
|
||||
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
glfwGetFramebufferSize(m_glfw_window.get(), &width, &height);
|
||||
|
||||
m_width = static_cast<uint32_t>(width);
|
||||
m_height = static_cast<uint32_t>(height);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void Window::set_catched_cursor(bool value)
|
||||
@@ -173,7 +183,8 @@ void Window::mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double
|
||||
window.m_last_cursor_pos_y = ypos;
|
||||
|
||||
// Check if this is the first VALID mouse event after window being resized
|
||||
if (window.m_first_mouse_input && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
|
||||
if (window.m_first_mouse_input && !(std::abs(deltaCursorPosX) < std::numeric_limits<double>::epsilon() &&
|
||||
std::abs(deltaCursorPosY) < std::numeric_limits<double>::epsilon())) {
|
||||
window.m_first_mouse_input = false;
|
||||
deltaCursorPosX = 0.0;
|
||||
deltaCursorPosY = 0.0;
|
||||
|
||||
@@ -35,7 +35,7 @@ private:
|
||||
static void glfw_error_callback(int error, const char *description);
|
||||
static void framebuffer_size_callback(GLFWwindow *glfw_window, int width, int height);
|
||||
|
||||
static constexpr float MOUSE_SENSITIVITY = 0.15;
|
||||
static constexpr float MOUSE_SENSITIVITY = 0.15f;
|
||||
|
||||
void set_catched_cursor(bool value);
|
||||
|
||||
@@ -46,10 +46,8 @@ private:
|
||||
MouseButtonInput m_mouse_button_input;
|
||||
MouseCursorInput m_mouse_cursor_input;
|
||||
|
||||
int m_posX{};
|
||||
int m_posY{};
|
||||
int m_width{};
|
||||
int m_height{};
|
||||
uint32_t m_width{};
|
||||
uint32_t m_height{};
|
||||
double m_last_cursor_pos_x = 0.0;
|
||||
double m_last_cursor_pos_y = 0.0;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ public:
|
||||
{}
|
||||
|
||||
protected:
|
||||
int32_t m_textureWidth;
|
||||
int32_t m_textureHeight;
|
||||
int32_t m_numComponents;
|
||||
uint32_t m_textureWidth;
|
||||
uint32_t m_textureHeight;
|
||||
uint32_t m_numComponents;
|
||||
};
|
||||
|
||||
@@ -20,11 +20,19 @@ TextureCubeMap::TextureCubeMap(const TextureCubeMapDescriptor &descriptor) : Abs
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
int i = 0;
|
||||
std::size_t i = 0;
|
||||
for (const auto &faceName : FACE_NAMES) {
|
||||
std::string texturePath = descriptor.path + faceName;
|
||||
|
||||
auto textureBuffer = stbi_load(texturePath.c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0);
|
||||
int textureWidth{};
|
||||
int textureHeight{};
|
||||
int numComponents{};
|
||||
|
||||
auto textureBuffer = stbi_load(texturePath.c_str(), &textureWidth, &textureHeight, &numComponents, 0);
|
||||
|
||||
m_textureWidth = static_cast<unsigned>(textureWidth);
|
||||
m_textureHeight = static_cast<unsigned>(textureHeight);
|
||||
m_numComponents = static_cast<unsigned>(numComponents);
|
||||
|
||||
if (!textureBuffer) {
|
||||
Log::logger().warn("CubeMap texture {} could not be loaded", texturePath);
|
||||
@@ -53,7 +61,7 @@ void TextureCubeMap::initialize()
|
||||
|
||||
int i = 0;
|
||||
for (auto &textureBuffer : m_textureBuffers) {
|
||||
GLenum internalFormat;
|
||||
GLint internalFormat;
|
||||
GLenum dataFormat;
|
||||
|
||||
switch (m_numComponents) {
|
||||
@@ -71,8 +79,9 @@ void TextureCubeMap::initialize()
|
||||
break;
|
||||
}
|
||||
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, m_textureWidth, m_textureHeight, 0,
|
||||
dataFormat, GL_UNSIGNED_BYTE, textureBuffer);
|
||||
glTexImage2D(static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, internalFormat,
|
||||
static_cast<GLsizei>(m_textureWidth), static_cast<GLsizei>(m_textureHeight), 0, dataFormat,
|
||||
GL_UNSIGNED_BYTE, textureBuffer);
|
||||
|
||||
stbi_image_free(textureBuffer);
|
||||
i++;
|
||||
@@ -111,8 +120,8 @@ InternalCubeMap::InternalCubeMap(unsigned int resolution) : AbstractCubeMap("int
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
for (unsigned int i = 0; i < static_cast<int>(CubeMapFace::CUBEMAP_FACES_NUM_ITEMS); i++) {
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT24, resolution, resolution, 0,
|
||||
GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT24, static_cast<GLsizei>(resolution),
|
||||
static_cast<GLsizei>(resolution), 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Model.h"
|
||||
#include "../util/Log.h"
|
||||
#include "ResourceHandler.h"
|
||||
#include "Texture.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
@@ -63,12 +64,12 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
std::vector<std::string> textureSources;
|
||||
for (unsigned int i = 0; i < numTextures; i++) {
|
||||
std::string currentTextureSource;
|
||||
for (unsigned int i = 0; i < 128; i++) {
|
||||
for (unsigned int k = 0; k < 128; k++) {
|
||||
uint8_t currentChar;
|
||||
input.read((char *)¤tChar, sizeof(uint8_t));
|
||||
|
||||
if (currentChar) {
|
||||
currentTextureSource.push_back(currentChar);
|
||||
currentTextureSource.push_back(static_cast<char>(currentChar));
|
||||
}
|
||||
}
|
||||
textureSources.push_back(currentTextureSource);
|
||||
@@ -82,7 +83,7 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
for (unsigned int i = 0; i < numTextures; i++) {
|
||||
std::string texturePath = m_workingPath + '/' + textureSources[i].c_str();
|
||||
|
||||
auto loadModel = [=, &mutex]() {
|
||||
auto loadModel = [=, this, &mutex]() {
|
||||
ResourceId texture = ResourceHandler::instance().registerResource<Texture>(
|
||||
TextureDescriptor{texturePath, textureTypes[i]});
|
||||
|
||||
|
||||
@@ -3,38 +3,38 @@
|
||||
|
||||
ResourceId Resource::s_idCounter = 0;
|
||||
|
||||
Resource::Resource(const std::filesystem::path &path) : m_id(s_idCounter++), m_path(path)
|
||||
Resource::Resource(std::filesystem::path path) : m_id(s_idCounter++), m_path(std::move(path))
|
||||
{
|
||||
Log::logger().info("Resource \"{}\" with id {} created", m_path.string(), m_id);
|
||||
}
|
||||
|
||||
ResourceId Resource::id() const
|
||||
auto Resource::id() const -> ResourceId
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
bool Resource::isInitialized() const
|
||||
auto Resource::isInitialized() const -> bool
|
||||
{
|
||||
return m_initialized;
|
||||
}
|
||||
|
||||
const std::filesystem::path &Resource::resourcePath() const
|
||||
auto Resource::resourcePath() const -> const std::filesystem::path &
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
GLuint GlResource::glId() const
|
||||
auto GlResource::glId() const -> GLuint
|
||||
{
|
||||
return m_glId;
|
||||
}
|
||||
|
||||
NamedResource::NamedResource(const std::string &name) : m_name(name)
|
||||
{}
|
||||
NamedResource::NamedResource(std::string name) : m_name(std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
const std::string &NamedResource::name() const
|
||||
auto NamedResource::name() const -> const std::string &
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
NamedResource::~NamedResource()
|
||||
{}
|
||||
NamedResource::~NamedResource() = default;
|
||||
|
||||
@@ -10,17 +10,17 @@ using ResourceId = uint64_t;
|
||||
class Resource
|
||||
{
|
||||
public:
|
||||
Resource(const std::filesystem::path &path);
|
||||
Resource(std::filesystem::path path);
|
||||
Resource(const Resource &other) = delete;
|
||||
Resource(Resource &&other) = delete;
|
||||
Resource &operator=(const Resource &other) = delete;
|
||||
Resource &operator=(Resource &&other) = delete;
|
||||
auto operator=(const Resource &other) -> Resource & = delete;
|
||||
auto operator=(Resource &&other) -> Resource & = delete;
|
||||
|
||||
ResourceId id() const;
|
||||
const std::filesystem::path &resourcePath() const;
|
||||
[[nodiscard]] auto id() const -> ResourceId;
|
||||
[[nodiscard]] auto resourcePath() const -> const std::filesystem::path &;
|
||||
|
||||
protected:
|
||||
bool isInitialized() const;
|
||||
[[nodiscard]] auto isInitialized() const -> bool;
|
||||
virtual void initialize() = 0;
|
||||
|
||||
bool m_initialized = false;
|
||||
@@ -40,7 +40,7 @@ public:
|
||||
// virtual ~GlResource() = 0; TODO!!
|
||||
virtual void unbind() const = 0;
|
||||
|
||||
GLuint glId() const;
|
||||
auto glId() const -> GLuint;
|
||||
|
||||
protected:
|
||||
GLuint m_glId;
|
||||
@@ -49,11 +49,11 @@ protected:
|
||||
class NamedResource
|
||||
{
|
||||
public:
|
||||
NamedResource(const std::string &name);
|
||||
NamedResource(std::string name);
|
||||
virtual ~NamedResource() = 0;
|
||||
|
||||
const std::string &name() const;
|
||||
[[nodiscard]] auto name() const -> const std::string &;
|
||||
|
||||
protected:
|
||||
std::string m_name;
|
||||
private:
|
||||
const std::string m_name;
|
||||
};
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
|
||||
ResourceHandler ResourceHandler::s_instance;
|
||||
|
||||
ResourceHandler &ResourceHandler::instance()
|
||||
auto ResourceHandler::instance() -> ResourceHandler &
|
||||
{
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
template <typename T, typename... Param>
|
||||
ResourceId ResourceHandler::registerResource(Param const &...param)
|
||||
auto ResourceHandler::registerResource(Param const &...param) -> ResourceId
|
||||
{
|
||||
auto resource = std::make_shared<T>(param...);
|
||||
m_resources.emplace(resource->id(), resource);
|
||||
@@ -26,26 +26,27 @@ template ResourceId ResourceHandler::registerResource<TextureCubeMap>(TextureCub
|
||||
template ResourceId ResourceHandler::registerResource<InternalCubeMap>(int const &);
|
||||
template ResourceId ResourceHandler::registerResource<Model>(ModelDescriptor const &);
|
||||
|
||||
const std::shared_ptr<Resource> ResourceHandler::resource(const ResourceId id) const
|
||||
auto ResourceHandler::resource(const ResourceId resourceId) const -> std::shared_ptr<Resource>
|
||||
{
|
||||
auto it = m_resources.find(id);
|
||||
auto resourceIt = m_resources.find(resourceId);
|
||||
|
||||
if (it != m_resources.end()) {
|
||||
auto resource = it->second;
|
||||
if (resourceIt != m_resources.end()) {
|
||||
auto resource = resourceIt->second;
|
||||
|
||||
if (!resource->isInitialized())
|
||||
if (!resource->isInitialized()) {
|
||||
resource->initialize();
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
Log::logger().warn("Could not find resource with id {}", id);
|
||||
return std::shared_ptr<Resource>();
|
||||
Log::logger().warn("Could not find resource with id {}", resourceId);
|
||||
return {};
|
||||
}
|
||||
|
||||
const std::shared_ptr<Resource> ResourceHandler::resource(const std::string &name) const
|
||||
auto ResourceHandler::resource(const std::string &name) const -> std::shared_ptr<Resource>
|
||||
{
|
||||
auto it = std::find_if(m_resources.begin(), m_resources.end(), [&name](const auto &resource) {
|
||||
auto resourceIt = std::find_if(m_resources.begin(), m_resources.end(), [&name](const auto &resource) {
|
||||
if (auto namedResource = std::dynamic_pointer_cast<NamedResource>(resource.second)) {
|
||||
return namedResource->name() == name;
|
||||
}
|
||||
@@ -53,15 +54,16 @@ const std::shared_ptr<Resource> ResourceHandler::resource(const std::string &nam
|
||||
return false;
|
||||
});
|
||||
|
||||
if (it != m_resources.end()) {
|
||||
auto resource = it->second;
|
||||
if (resourceIt != m_resources.end()) {
|
||||
auto resource = resourceIt->second;
|
||||
|
||||
if (!resource->isInitialized())
|
||||
if (!resource->isInitialized()) {
|
||||
resource->initialize();
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
Log::logger().warn("Could not find resource with unique name \"{}\"", name);
|
||||
return std::shared_ptr<Resource>();
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
class ResourceHandler
|
||||
{
|
||||
public:
|
||||
static ResourceHandler &instance();
|
||||
static auto instance() -> ResourceHandler &;
|
||||
|
||||
template <typename T, typename... Param>
|
||||
ResourceId registerResource(Param const &...param);
|
||||
auto registerResource(Param const &...param) -> ResourceId;
|
||||
|
||||
const std::shared_ptr<Resource> resource(ResourceId id) const;
|
||||
const std::shared_ptr<Resource> resource(const std::string &name) const;
|
||||
[[nodiscard]] auto resource(ResourceId resourceId) const -> std::shared_ptr<Resource>;
|
||||
[[nodiscard]] auto resource(const std::string &name) const -> std::shared_ptr<Resource>;
|
||||
|
||||
private:
|
||||
ResourceHandler() = default;
|
||||
|
||||
@@ -9,7 +9,16 @@ Texture::Texture(const TextureDescriptor &descriptor)
|
||||
: AbstractTexture(descriptor.path), m_textureType(descriptor.textureType)
|
||||
{
|
||||
stbi_set_flip_vertically_on_load(1);
|
||||
m_textureBuffer = stbi_load(resourcePath().c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0);
|
||||
|
||||
int textureWidth{};
|
||||
int textureHeight{};
|
||||
int numComponents{};
|
||||
|
||||
m_textureBuffer = stbi_load(resourcePath().c_str(), &textureWidth, &textureHeight, &numComponents, 0);
|
||||
|
||||
m_textureWidth = static_cast<unsigned>(textureWidth);
|
||||
m_textureHeight = static_cast<unsigned>(textureHeight);
|
||||
m_numComponents = static_cast<unsigned>(numComponents);
|
||||
|
||||
if (!m_textureBuffer)
|
||||
Log::logger().warn("Texture {} could not be loaded", resourcePath().string());
|
||||
@@ -48,8 +57,8 @@ void Texture::initialize()
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_textureWidth, m_textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE,
|
||||
m_textureBuffer);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(internalFormat), static_cast<GLsizei>(m_textureWidth),
|
||||
static_cast<GLsizei>(m_textureHeight), 0, dataFormat, GL_UNSIGNED_BYTE, m_textureBuffer);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "AbstractTexture.h"
|
||||
#include "Resource.h"
|
||||
#include "TextureTypes.h"
|
||||
#include "TextureType.h"
|
||||
|
||||
#include <string>
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
project(obj-converter)
|
||||
|
||||
add_executable(obj-converter main.cpp)
|
||||
|
||||
target_link_libraries(obj-converter assimp glm)
|
||||
|
||||
Reference in New Issue
Block a user