Rework API graphique Vulkan - EnTT pour ECS + Chargement modèle 3D assimp + SDL3 pour events input et fenetre + mesh texture camera transform ok + attention tous les assets nouveaus ne sont pas commités et il y a du code test en dur dans scene addentity + restructuration globale
This commit is contained in:
@@ -27,7 +27,7 @@ namespace CosmicCore {
|
||||
CAbstractComponent(CEntity& entity): m_owningEntity(entity){};
|
||||
virtual ~CAbstractComponent() = default;
|
||||
|
||||
CEntity& getEntity() {
|
||||
CEntity getEntity() const {
|
||||
return m_owningEntity;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#ifndef CABSTRACTCAMERA_HPP
|
||||
#define CABSTRACTCAMERA_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractCamera: CAbstractComponent
|
||||
{
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
153
src/Engine/Core/Component/Camera/CCamera.hpp
Normal file
153
src/Engine/Core/Component/Camera/CCamera.hpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#ifndef CCAMERA_HPP
|
||||
#define CCAMERA_HPP
|
||||
|
||||
#include "../CAbstractComponent.hpp"
|
||||
//pour vulkan depth 0-1 (opengl [-1,1])
|
||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include "../../Graphics/API/VulkanImpl.hpp"
|
||||
#include "../Geometry/CTransform.hpp"
|
||||
namespace CosmicCore {
|
||||
|
||||
class CCamera : public CAbstractComponent {
|
||||
private:
|
||||
// paramètres de la caméra
|
||||
float m_fov = 45.0f;
|
||||
float m_near = 0.1f;
|
||||
float m_far = 1000.0f;
|
||||
float m_aspect = 16.0f / 9.0f;
|
||||
|
||||
// matrices calculées
|
||||
glm::mat4 m_view = glm::mat4(1.0f);
|
||||
glm::mat4 m_projection = glm::mat4(1.0f);
|
||||
|
||||
// GPU
|
||||
struct CameraUBOData {
|
||||
glm::mat4 view;
|
||||
glm::mat4 projection;
|
||||
};
|
||||
|
||||
std::array<VMABuffer, MAX_FRAMES_IN_FLIGHT> m_uniformBuffers;
|
||||
std::array<void*, MAX_FRAMES_IN_FLIGHT> m_mappedData{};
|
||||
std::array<ManagedDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_descriptorSets;
|
||||
|
||||
void recalcProjection() {
|
||||
m_projection = glm::perspective(glm::radians(m_fov), m_aspect, m_near, m_far);
|
||||
// Vulkan a l'axe Y inversé par rapport à OpenGL
|
||||
m_projection[1][1] *= -1;
|
||||
}
|
||||
|
||||
public:
|
||||
CCamera(CEntity& entity) : CAbstractComponent(entity) {
|
||||
recalcProjection();
|
||||
}
|
||||
|
||||
~CCamera(){
|
||||
destroy();
|
||||
}
|
||||
|
||||
void setFov(float fov) {
|
||||
m_fov = fov;
|
||||
recalcProjection();
|
||||
}
|
||||
|
||||
void setAspect(float aspect) {
|
||||
m_aspect = aspect;
|
||||
recalcProjection();
|
||||
}
|
||||
|
||||
void setNearFar(float near, float far) {
|
||||
m_near = near;
|
||||
m_far = far;
|
||||
recalcProjection();
|
||||
}
|
||||
|
||||
// getters
|
||||
glm::mat4 getView(const CTransform& transform) const {
|
||||
glm::quat q = transform.getOrientation();
|
||||
glm::vec3 position = transform.getCenter();
|
||||
|
||||
// rotation inverse (conjugué du quaternion) + translation inverse
|
||||
glm::mat4 rotInverse = glm::toMat4(glm::conjugate(q));
|
||||
glm::mat4 transInverse = glm::translate(glm::mat4(1.0f), -position);
|
||||
|
||||
return rotInverse * transInverse;
|
||||
}
|
||||
glm::mat4 getProjection() const { return m_projection; }
|
||||
|
||||
// GPU
|
||||
void initUniformBuffer(VmaAllocator allocator) {
|
||||
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
|
||||
|
||||
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
VkBufferCreateInfo bufferInfo{};
|
||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
bufferInfo.size = sizeof(CameraUBOData);
|
||||
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo{};
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
||||
allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
||||
|
||||
VmaAllocationInfo info{};
|
||||
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo,
|
||||
&m_uniformBuffers[i].buffer,
|
||||
&m_uniformBuffers[i].allocation,
|
||||
&info);
|
||||
m_mappedData[i] = info.pMappedData;
|
||||
|
||||
m_descriptorSets[i] = api->getDescriptorPoolManager().allocate(
|
||||
*api->getCameraLayout()
|
||||
);
|
||||
|
||||
vk::DescriptorBufferInfo descriptorBufferInfo{
|
||||
vk::Buffer(m_uniformBuffers[i].buffer),
|
||||
0,
|
||||
sizeof(CameraUBOData)
|
||||
};
|
||||
vk::WriteDescriptorSet descriptorWrite{
|
||||
*m_descriptorSets[i].set,
|
||||
0, 0, 1,
|
||||
vk::DescriptorType::eUniformBuffer,
|
||||
{}, &descriptorBufferInfo
|
||||
};
|
||||
static_cast<vk::raii::Device*>(api->getDevice())->updateDescriptorSets(descriptorWrite, {});
|
||||
}
|
||||
}
|
||||
|
||||
void destroyUniformBuffer(VmaAllocator allocator) {
|
||||
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
|
||||
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
api->getDescriptorPoolManager().free(m_descriptorSets[i]);
|
||||
vmaDestroyBuffer(allocator, m_uniformBuffers[i].buffer,
|
||||
m_uniformBuffers[i].allocation);
|
||||
}
|
||||
}
|
||||
|
||||
void updateUniformBuffer(uint32_t frameIndex, const CTransform& transform) {
|
||||
CameraUBOData data{ getView(transform), m_projection };
|
||||
memcpy(m_mappedData[frameIndex], &data, sizeof(data));
|
||||
}
|
||||
|
||||
vk::raii::DescriptorSet& getDescriptorSet(uint32_t frameIndex) {
|
||||
return m_descriptorSets[frameIndex].set;
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
|
||||
|
||||
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
|
||||
api->destroyBuffer(m_uniformBuffers[i], api->getCurrentFrameIndex());
|
||||
api->destroyDescriptorSet(m_descriptorSets[i], api->getCurrentFrameIndex());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
nlohmann::json to_json(){};
|
||||
};
|
||||
|
||||
} // namespace CosmicCore
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "CTransform.hpp"
|
||||
|
||||
#include "../../Graphics/API/VulkanImpl.hpp"
|
||||
#include "../Relationships/CRelationship.hpp"
|
||||
namespace CosmicCore
|
||||
{
|
||||
|
||||
@@ -22,7 +23,7 @@ namespace CosmicCore
|
||||
CTransform::CTransform(CEntity& entity) : CAbstractComponent(entity),
|
||||
m_center(0.0f, 0.0f, 0.0f),
|
||||
m_scale(1.0f, 1.0f, 1.0f),
|
||||
m_orientation(),
|
||||
m_orientation(glm::identity<glm::quat>()),
|
||||
m_observers() {
|
||||
calcTranslation();
|
||||
calcRotation();
|
||||
@@ -41,6 +42,11 @@ namespace CosmicCore
|
||||
calcTranformation();
|
||||
}
|
||||
|
||||
CTransform::~CTransform()
|
||||
{
|
||||
destroyUniformBuffer();
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getTranslation(void) const {
|
||||
return m_translation;
|
||||
}
|
||||
@@ -58,17 +64,18 @@ namespace CosmicCore
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getInheritedTransformation(void) const {
|
||||
/*const auto& entity = getEntity();
|
||||
if(entity)
|
||||
{
|
||||
if (entity->getParent().expired()) {
|
||||
return m_transformation;
|
||||
}
|
||||
else {
|
||||
auto p = entity->getParent().lock();
|
||||
return p->getTransform().getInheritedTransformation() * m_transformation;
|
||||
}
|
||||
}*/
|
||||
auto& registry = getEntity().getRegistry();
|
||||
|
||||
if (!registry().all_of<CRelationship>(*getEntity()))
|
||||
return m_transformation;
|
||||
|
||||
auto& rel = registry().get<CRelationship>(*getEntity());
|
||||
|
||||
if (rel.getParent() == entt::null)
|
||||
return m_transformation;
|
||||
|
||||
auto& parentTransform = registry().get<CTransform>(rel.getParent());
|
||||
return parentTransform.getInheritedTransformation() * m_transformation;
|
||||
}
|
||||
|
||||
glm::vec3 CTransform::getCenter(void) const {
|
||||
@@ -158,7 +165,7 @@ namespace CosmicCore
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::forward(glm::vec3 value) {
|
||||
void CTransform::forward(glm::vec3 value) {
|
||||
glm::vec3 relValue = m_orientation * value;
|
||||
m_center += relValue;
|
||||
calcTranslation();
|
||||
@@ -177,4 +184,47 @@ namespace CosmicCore
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
void CTransform::updateUniformBuffer(uint32_t frameIndex) {
|
||||
auto data{ getInheritedTransformation() }; // utilise la transformation héritée
|
||||
memcpy(m_mappedData[frameIndex], &data, sizeof(data));
|
||||
}
|
||||
|
||||
void CTransform::initUniformBuffer(VmaAllocator allocator) {
|
||||
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
|
||||
|
||||
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
VkBufferCreateInfo bufferInfo{};
|
||||
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
bufferInfo.size = sizeof(glm::mat4);
|
||||
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo{};
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
||||
allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
||||
|
||||
VmaAllocationInfo info{};
|
||||
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo,
|
||||
&m_uniformBuffers[i].buffer,
|
||||
&m_uniformBuffers[i].allocation,
|
||||
&info);
|
||||
m_mappedData[i] = info.pMappedData;
|
||||
|
||||
m_descriptorSets[i] = api->getDescriptorPoolManager().allocate(api->getTransformLayout());
|
||||
|
||||
vk::DescriptorBufferInfo descriptorBufferInfo{vk::Buffer(m_uniformBuffers[i].buffer), 0, sizeof(glm::mat4)};
|
||||
vk::WriteDescriptorSet descriptorWrite{m_descriptorSets[i].set, 0, 0,1, vk::DescriptorType::eUniformBuffer, {}, &descriptorBufferInfo};
|
||||
static_cast<vk::raii::Device*>(api->getDevice())->updateDescriptorSets(descriptorWrite, {});
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::destroyUniformBuffer() {
|
||||
VulkanImpl* api = dynamic_cast<VulkanImpl*>(GraphicsAPI::getAPI().get());
|
||||
|
||||
for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
|
||||
api->destroyBuffer(m_uniformBuffers[i], api->getCurrentFrameIndex());
|
||||
api->destroyDescriptorSet(m_descriptorSets[i], api->getCurrentFrameIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,9 @@
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
#include "../../Graphics/API/VMABuffer.hpp"
|
||||
#include "../../Graphics/API/ManagedDescriptorSet.hpp"
|
||||
|
||||
namespace CosmicCore
|
||||
{
|
||||
/**
|
||||
@@ -79,6 +82,10 @@ namespace CosmicCore
|
||||
* @brief Compute the full transformation of the object.
|
||||
*/
|
||||
void calcTranformation(void);
|
||||
|
||||
std::array<VMABuffer, MAX_FRAMES_IN_FLIGHT> m_uniformBuffers;
|
||||
std::array<void*, MAX_FRAMES_IN_FLIGHT> m_mappedData{};
|
||||
std::array<ManagedDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_descriptorSets;
|
||||
public:
|
||||
|
||||
/**
|
||||
@@ -87,6 +94,13 @@ namespace CosmicCore
|
||||
*/
|
||||
CTransform(CEntity& m_entity);
|
||||
|
||||
~CTransform();
|
||||
|
||||
CTransform(CTransform&&) = default;
|
||||
CTransform& operator=(CTransform&&) = default;
|
||||
|
||||
CTransform(const CTransform&) = delete;
|
||||
CTransform& operator=(const CTransform&) = delete;
|
||||
/**
|
||||
* @brief CTransform's constructor.
|
||||
* @param[in, out] m_entity The entity to which the transform belongs.
|
||||
@@ -240,6 +254,17 @@ namespace CosmicCore
|
||||
t.setScale(glm::vec3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]));
|
||||
return t;
|
||||
};
|
||||
|
||||
void initUniformBuffer(VmaAllocator allocator);
|
||||
void destroyUniformBuffer();
|
||||
void updateUniformBuffer(uint32_t frameIndex);
|
||||
|
||||
VMABuffer& getUniformBuffer(uint32_t frameIndex) {
|
||||
return m_uniformBuffers[frameIndex];
|
||||
};
|
||||
vk::raii::DescriptorSet& getDescriptorSet(uint32_t frameIndex) {
|
||||
return m_descriptorSets[frameIndex].set;
|
||||
};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -6,7 +6,6 @@ namespace CosmicCore {
|
||||
{
|
||||
public:
|
||||
CAbstractRenderer(CEntity& e): CAbstractComponent(e){};
|
||||
virtual void render() = 0;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -1,44 +0,0 @@
|
||||
#include "CModel.hpp"
|
||||
|
||||
CModel::CModel(std::vector<CMesh*> meshes) :
|
||||
m_meshes(meshes),
|
||||
m_loaded(false) {
|
||||
}
|
||||
|
||||
CModel::~CModel(void) {
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++)
|
||||
{
|
||||
delete m_meshes[i];
|
||||
}
|
||||
}
|
||||
|
||||
void CModel::load(void) {
|
||||
// For each meshes of the model.
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++) {
|
||||
m_meshes[i]->load();
|
||||
}
|
||||
|
||||
m_loaded = true;
|
||||
}
|
||||
|
||||
void CModel::draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
// For each meshes of the model.
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++) {
|
||||
m_meshes[i]->draw(model, view, projection, lightPos, intensity);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<CMesh*>& CModel::getMeshes()
|
||||
{
|
||||
return m_meshes;
|
||||
}
|
||||
|
||||
void CModel::setMeshes(std::vector<CMesh*> meshes)
|
||||
{
|
||||
m_meshes = meshes;
|
||||
}
|
||||
|
||||
bool CModel::isLoaded()
|
||||
{
|
||||
return m_loaded;
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
#ifndef CMODEL_HPP
|
||||
#define CMODEL_HPP
|
||||
|
||||
#include "Material/SMaterial.hpp"
|
||||
#include "Mesh/CMesh.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @brief Class representing a 3D model.
|
||||
*/
|
||||
class CModel {
|
||||
private:
|
||||
/**
|
||||
* @brief The meshes, a vector which contains all the meshes of the object.
|
||||
*/
|
||||
std::vector<CMesh*> m_meshes;
|
||||
|
||||
/**
|
||||
* @brief A boolean representing the loading state of the model.
|
||||
*/
|
||||
bool m_loaded;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CModel's default constructor, disabled.
|
||||
*/
|
||||
CModel(void) = delete;
|
||||
|
||||
/**
|
||||
* @brief CModel constructor.
|
||||
* @param meshes : The list of the meshes of the object.
|
||||
*/
|
||||
CModel(std::vector<CMesh*> meshes);
|
||||
|
||||
/**
|
||||
* @brief CModel's destructor.
|
||||
*/
|
||||
~CModel(void);
|
||||
|
||||
/**
|
||||
* @brief Load the model in the GPU.
|
||||
*/
|
||||
void load(void);
|
||||
|
||||
/**
|
||||
* @brief Render the model on the screen.
|
||||
* @param[in] model The transform of the object.
|
||||
* @param[in] view The view of the object.
|
||||
* @param[in] projection The projection of the object.
|
||||
* @param[in] lightPos The light's position.
|
||||
* @param[in] intensity The light's intensity.
|
||||
*/
|
||||
void draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
|
||||
/**
|
||||
* @brief Getter to the meshes of the model.
|
||||
* @return A vector which represent the list of meshes of the model, it's a direct reference to the member m_meshes.
|
||||
*/
|
||||
std::vector<CMesh*>& getMeshes(void);
|
||||
|
||||
/**
|
||||
* @brief Setter to the meshes.
|
||||
* @param[in] meshes The list of meshes to set to the model.
|
||||
*/
|
||||
void setMeshes(std::vector<CMesh*> meshes);
|
||||
|
||||
/**
|
||||
* @brief Getter to m_loaded member, say if the model is loaded or not.
|
||||
* @return A boolean representing the state of the model, loaded or not.
|
||||
*/
|
||||
bool isLoaded(void);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
3
src/Engine/Core/Component/Graphics/CRenderer.cpp
Normal file
3
src/Engine/Core/Component/Graphics/CRenderer.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "CRenderer.hpp"
|
||||
namespace CosmicCore {
|
||||
}
|
||||
18
src/Engine/Core/Component/Graphics/CRenderer.hpp
Normal file
18
src/Engine/Core/Component/Graphics/CRenderer.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef CTESTRENDERER_HPP
|
||||
#define CTESTRENDERER_HPP
|
||||
#include "CAbstractRenderer.hpp"
|
||||
#include "../../Graphics/Data/CModel.hpp"
|
||||
namespace CosmicCore {
|
||||
class CRenderer: public CAbstractRenderer
|
||||
{
|
||||
CModel* m_model = nullptr;
|
||||
public:
|
||||
CRenderer() = delete;
|
||||
CRenderer(CEntity& e): CAbstractRenderer(e){};
|
||||
CRenderer(CEntity& e, CModel* mod): CAbstractRenderer(e), m_model(mod){};
|
||||
nlohmann::json to_json() override{return nlohmann::json();};
|
||||
CModel* getModel(){return m_model;};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#include "CTestRenderer.hpp"
|
||||
#include <iostream>
|
||||
namespace CosmicCore {
|
||||
void CTestRenderer::render()
|
||||
{
|
||||
int a = 2*25;
|
||||
int b = a +2;
|
||||
std::cout << b << std::endl;
|
||||
};
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#ifndef CTESTRENDERER_HPP
|
||||
#define CTESTRENDERER_HPP
|
||||
#include "CAbstractRenderer.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
class CTestRenderer: public CAbstractRenderer
|
||||
{
|
||||
public:
|
||||
CTestRenderer() = delete;
|
||||
CTestRenderer(CEntity& e): CAbstractRenderer(e){};
|
||||
void render() override;
|
||||
nlohmann::json to_json(){return nlohmann::json();};
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef SCOLOR_HPP
|
||||
#define SCOLOR_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file SColor.hpp
|
||||
* @brief File for the color struct of a material.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief the number of type in the enum EColorType.
|
||||
*/
|
||||
#define COLOR_TYPE_NUMBER 2
|
||||
|
||||
/**
|
||||
* @brief The enum for color type in the material.
|
||||
*/
|
||||
enum EColorType {
|
||||
COLOR_DIFFUSE,
|
||||
COLOR_SPECULAR,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The struct color of the material.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief 4D Vector representing the RGBA color.
|
||||
*/
|
||||
glm::vec4 m_vector;
|
||||
|
||||
/**
|
||||
* @brief The color's type.
|
||||
*/
|
||||
EColorType m_type;
|
||||
} SColor;
|
||||
|
||||
#endif
|
||||
@@ -1,39 +0,0 @@
|
||||
#ifndef SMATERIAL_HPP
|
||||
#define SMATERIAL_HPP
|
||||
|
||||
#include "../Shader/CShader.hpp"
|
||||
#include "../Texture/CAbstractTexture.hpp"
|
||||
#include "SColor.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @file SMaterial.hpp
|
||||
* @brief File for the material struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief The struct material.
|
||||
*/
|
||||
typedef struct SMaterial {
|
||||
/**
|
||||
* @brief List of textures for the material.
|
||||
*/
|
||||
std::vector<CAbstractTexture*> m_textures;
|
||||
|
||||
/**
|
||||
* @brief List of colors for the material.
|
||||
*/
|
||||
std::vector<SColor> m_colors;
|
||||
|
||||
/**
|
||||
* @brief The shader used to render.
|
||||
*/
|
||||
CShader* m_shader = nullptr;
|
||||
|
||||
~SMaterial() { for (unsigned int i = 0; i < m_textures.size(); ++i) {
|
||||
delete m_textures[i];
|
||||
} };
|
||||
} SMaterial;
|
||||
|
||||
#endif
|
||||
@@ -1,216 +0,0 @@
|
||||
#include "CMesh.hpp"
|
||||
|
||||
void CMesh::bindMaterial(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
// Test the material.
|
||||
if (m_material == nullptr) {
|
||||
throw CLogicException("This mesh is not linked to a material.");
|
||||
}
|
||||
|
||||
// Active the shader.
|
||||
m_material->m_shader->use();
|
||||
|
||||
// Give the matrix to the shader.
|
||||
m_material->m_shader->setMat4("model", model);
|
||||
m_material->m_shader->setMat4("view", view);
|
||||
m_material->m_shader->setMat4("projection", projection);
|
||||
m_material->m_shader->setVec3("lightPos", lightPos);
|
||||
m_material->m_shader->setFloat("intensity", intensity);
|
||||
|
||||
// Counter for each Texture Type :
|
||||
unsigned int countTex[TEXTURE_TYPE_NUMBER];
|
||||
for (unsigned int i = 0; i < TEXTURE_TYPE_NUMBER; i++) {
|
||||
countTex[i] = 0;
|
||||
}
|
||||
|
||||
// For each texture in the material :
|
||||
for (unsigned int i = 0; i < m_material->m_textures.size(); i++) {
|
||||
|
||||
// Active the shader unit.
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
|
||||
// Retrieve texture name.
|
||||
std::string name;
|
||||
|
||||
switch (m_material->m_textures[i]->getType()) {
|
||||
case TEXTURE_DIFFUSE:
|
||||
name = "textureDiffuse" + std::to_string(countTex[0]);
|
||||
countTex[0]++;
|
||||
break;
|
||||
case TEXTURE_SPECULAR:
|
||||
name = "textureSpecular" + std::to_string(countTex[1]);
|
||||
countTex[1]++;
|
||||
break;
|
||||
case TEXTURE_NORMAL:
|
||||
name = "textureNormal" + std::to_string(countTex[2]);
|
||||
countTex[2]++;
|
||||
break;
|
||||
case TEXTURE_HEIGHT:
|
||||
name = "textureHeight" + std::to_string(countTex[3]);
|
||||
countTex[3]++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Get the uniform storage space of the shader.
|
||||
glUniform1i(glGetUniformLocation(m_material->m_shader->getId(), name.c_str()), i);
|
||||
|
||||
// And finally bind the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, m_material->m_textures[i]->getId());
|
||||
}
|
||||
|
||||
// Always good practice to set everything back to defaults once configured.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
|
||||
|
||||
// Counter for each color Type :
|
||||
unsigned int countCol[COLOR_TYPE_NUMBER];
|
||||
for (unsigned int i = 0; i < COLOR_TYPE_NUMBER; i++) {
|
||||
countCol[i] = 0;
|
||||
}
|
||||
|
||||
// For each color in the material :
|
||||
for (unsigned int i = 0; i < m_material->m_colors.size(); i++) {
|
||||
|
||||
// Retrieve texture name.
|
||||
std::string name;
|
||||
|
||||
switch (m_material->m_colors[i].m_type) {
|
||||
case COLOR_DIFFUSE:
|
||||
name = "colorDiffuse" + std::to_string(countCol[0]);
|
||||
countCol[0]++;
|
||||
break;
|
||||
case COLOR_SPECULAR:
|
||||
name = "colorSpecular" + std::to_string(countCol[1]);
|
||||
countCol[1]++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Give the color as a vector 4.
|
||||
m_material->m_shader->setVec4(name, m_material->m_colors[i].m_vector);
|
||||
}
|
||||
|
||||
// Active the shader.
|
||||
//m_material->m_shader->use();
|
||||
}
|
||||
|
||||
CMesh::CMesh(void) :
|
||||
m_vertexes(),
|
||||
m_indexes(),
|
||||
m_material(nullptr),
|
||||
m_vertexBufferObject(0),
|
||||
m_elementBufferObject(0),
|
||||
m_vertexArrayObject(0) {
|
||||
}
|
||||
|
||||
CMesh::~CMesh(void) {
|
||||
// Delete the VAO.
|
||||
if (m_vertexArrayObject != 0) {
|
||||
glDeleteVertexArrays(1, &m_vertexArrayObject);
|
||||
}
|
||||
|
||||
if (m_material != nullptr)
|
||||
{
|
||||
delete m_material;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<SVertex>& CMesh::getVertexes(void) {
|
||||
return m_vertexes;
|
||||
}
|
||||
|
||||
std::vector<unsigned int>& CMesh::getIndexes(void) {
|
||||
return m_indexes;
|
||||
}
|
||||
|
||||
SMaterial* CMesh::getMaterial(void) {
|
||||
return m_material;
|
||||
}
|
||||
|
||||
void CMesh::setMaterial(SMaterial* material) {
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void CMesh::setVertices(std::vector<SVertex> vertices)
|
||||
{
|
||||
m_vertexes = vertices;
|
||||
}
|
||||
|
||||
void CMesh::setIndexes(std::vector<unsigned int> indexes)
|
||||
{
|
||||
m_indexes = indexes;
|
||||
}
|
||||
|
||||
void CMesh::load(void) {
|
||||
if (m_vertexes.empty() || m_indexes.empty()) {
|
||||
throw CLogicException("This mesh do no contain vertex data.");
|
||||
}
|
||||
|
||||
// Create the VAO.
|
||||
glGenVertexArrays(1, &m_vertexArrayObject);
|
||||
|
||||
// Create the VBO.
|
||||
glGenBuffers(1, &m_vertexBufferObject);
|
||||
|
||||
// Create the EBO.
|
||||
glGenBuffers(1, &m_elementBufferObject);
|
||||
|
||||
|
||||
// Open the VAO.
|
||||
glBindVertexArray(m_vertexArrayObject);
|
||||
|
||||
// Open the VBO, and link it to the VAO.
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
|
||||
|
||||
// Give the block of memory of vertexes vector (so, it works like a c list), and the struct is transparent du to their conception).
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexes.size() * sizeof(SVertex), &m_vertexes[0], GL_STATIC_DRAW);
|
||||
|
||||
// Open the EBO, and link it to the VAO.
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBufferObject);
|
||||
|
||||
// Give the block of memory of indexes vector (so, it works like a c list), and the struct is transparent du to their conception).
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexes.size() * sizeof(unsigned int), &m_indexes[0], GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// Set the vertex attribute pointers.
|
||||
// Vertex Positions.
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)0);
|
||||
// Vertex normals.
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_normal));
|
||||
// Vertex texture coords.
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_texCoords));
|
||||
// Vertex tangent.
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_tangent));
|
||||
// Vertex bitangent.
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_bitangent));
|
||||
|
||||
|
||||
// Unbind the VAO.
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void CMesh::draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
if (m_vertexArrayObject == 0) {
|
||||
throw CLogicException("This mesh is not loaded in GPU, you can not draw it.");
|
||||
}
|
||||
|
||||
// Bind the material.
|
||||
bindMaterial(model, view, projection, lightPos, intensity);
|
||||
|
||||
// Enable the vertex array object, so the shader use the vertex attribute in this vao.
|
||||
glBindVertexArray(m_vertexArrayObject);
|
||||
|
||||
// Render the triangles in the vertex array object.
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)m_indexes.size(), GL_UNSIGNED_INT, 0);
|
||||
|
||||
// Disable the vertex array object.
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
#ifndef CMESH_HPP
|
||||
#define CMESH_HPP
|
||||
|
||||
#include "../Material/SMaterial.hpp"
|
||||
#include "../Texture/CAbstractTexture.hpp"
|
||||
#include "SVertex.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
#include <vector>
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file CMesh.hpp
|
||||
* @brief File for the mesh struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class CMesh, used to represent a mesh, a set of vertex (which can be indexed), ready to be drawn.
|
||||
*/
|
||||
class CMesh {
|
||||
private:
|
||||
/**
|
||||
* @brief List of the vertices of the mesh.
|
||||
*/
|
||||
std::vector<SVertex> m_vertexes;
|
||||
|
||||
/**
|
||||
* @brief List of the indices of the mesh.
|
||||
*/
|
||||
std::vector<unsigned int> m_indexes;
|
||||
|
||||
/**
|
||||
* @brief A pointer to the material containing textures, colors and the shader used for the mesh.
|
||||
*/
|
||||
SMaterial* m_material;
|
||||
|
||||
/**
|
||||
* @brief The vertex buffer.
|
||||
*/
|
||||
unsigned int m_vertexBufferObject;
|
||||
|
||||
/**
|
||||
* @brief The index buffer.
|
||||
*/
|
||||
unsigned int m_elementBufferObject;
|
||||
|
||||
/**
|
||||
* @brief Vertex array object, all the data.
|
||||
*/
|
||||
unsigned int m_vertexArrayObject;
|
||||
|
||||
/**
|
||||
* @brief Apply material to use for drawing.
|
||||
* @param[in] model the model matrix.
|
||||
* @param[in] view the view matrix.
|
||||
* @param[in] projection the projection matrix.
|
||||
* @param[in] lightPos the position of the light source.
|
||||
* @param[in] intensity the intensity of the light source.
|
||||
*/
|
||||
void bindMaterial(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief the constructor.
|
||||
*/
|
||||
CMesh(void);
|
||||
|
||||
/**
|
||||
* @brief the destructor.
|
||||
*/
|
||||
~CMesh(void);
|
||||
|
||||
/**
|
||||
* @brief The vertex array getter.
|
||||
* @return std::vector<SVertex>&, A reference to the list of vertex.
|
||||
*/
|
||||
std::vector<SVertex>& getVertexes(void);
|
||||
|
||||
/**
|
||||
* @brief The index array getter.
|
||||
* @return std::vector<unsigned int>&, A reference to the list of index.
|
||||
*/
|
||||
std::vector<unsigned int>& getIndexes(void);
|
||||
|
||||
/**
|
||||
* @brief The material getter.
|
||||
* @return SMaterial*, the material.
|
||||
*/
|
||||
SMaterial* getMaterial(void);
|
||||
|
||||
/**
|
||||
* @brief The material setter.
|
||||
* @param[in] material the wanted material.
|
||||
*/
|
||||
void setMaterial(SMaterial* material);
|
||||
|
||||
/**
|
||||
* @brief The vertex setter.
|
||||
* @param[in] vertices list of the vertices to give to the GPU.
|
||||
*/
|
||||
void setVertices(std::vector<SVertex> vertices);
|
||||
|
||||
/**
|
||||
* @brief The indexes setter.
|
||||
* @param[in] indexes list of the indexes for the index buffer
|
||||
*/
|
||||
void setIndexes(std::vector<unsigned int> indexes);
|
||||
|
||||
/**
|
||||
* @brief Load the mesh in GPU.
|
||||
*/
|
||||
void load(void);
|
||||
|
||||
/**
|
||||
* @brief draw the mesh.
|
||||
* @param[in] model the model matrix.
|
||||
* @param[in] view the view matrix.
|
||||
* @param[in] projection the projection matrix.
|
||||
* @param[in] lightPos the position of the light source.
|
||||
* @param[in] intensity the intensity of the light source.
|
||||
*/
|
||||
void draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,48 +0,0 @@
|
||||
#ifndef SVERTEX_HPP
|
||||
#define SVERTEX_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file SVertex.hpp
|
||||
* @brief File for the vertex struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief the struct Vertex, use to represent vertex and vertex attribute.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief Position attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_position;
|
||||
|
||||
/**
|
||||
* @brief Normal attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_normal;
|
||||
|
||||
/**
|
||||
* @brief Texture coordinates attribute.
|
||||
* 2D vector.
|
||||
*/
|
||||
glm::vec2 m_texCoords;
|
||||
|
||||
/**
|
||||
* @brief Tangent attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_tangent;
|
||||
|
||||
/**
|
||||
* @brief Bitangent attribute.
|
||||
* 3D vector (perpendicular to m_tangent vector).
|
||||
*/
|
||||
glm::vec3 m_bitangent;
|
||||
|
||||
// m_normal, m_tangent, m_bitangent are supposed to create a vector base to compute normal map.
|
||||
} SVertex;
|
||||
|
||||
#endif
|
||||
@@ -1,6 +0,0 @@
|
||||
#include "CShader.hpp"
|
||||
|
||||
CShader::~CShader()
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,159 +0,0 @@
|
||||
#ifndef CSHADER_HPP
|
||||
#define CSHADER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file CShader.hpp
|
||||
* @brief File of CShader, a class to manage complete shader program, composed of a vertex shader,
|
||||
* a fragment shader and sometime a geometry shader.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class to manage complete shader program, composed of a vertex shader,
|
||||
* a fragment shader and sometime a geometry shader.
|
||||
*/
|
||||
class CShader {
|
||||
public:
|
||||
virtual ~CShader();
|
||||
/**
|
||||
* @brief Get the id of the shader.
|
||||
* @return unsigned int, the id.
|
||||
*/
|
||||
virtual unsigned int getId(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the name of the shader.
|
||||
* @return std::string, the name.
|
||||
*/
|
||||
virtual std::string getName(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the vertex of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getVertexPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the geometry of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getGeomPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the fragment of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getFragmentPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief set the name of the shader.
|
||||
* @param[in] name the name.
|
||||
*/
|
||||
virtual void setName(std::string name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the vertex source path of the shader.
|
||||
* @param[in] vp the path.
|
||||
*/
|
||||
virtual void setVertexPath(std::string vp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the geometry source path of the shader.
|
||||
* @param[in] gp the path.
|
||||
*/
|
||||
virtual void setGeomPath(std::string gp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the fragment source path of the shader.
|
||||
* @param[in] fp the path.
|
||||
*/
|
||||
virtual void setFragmentPath(std::string fp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Return true if the shader is ready to use (compiled and linked).
|
||||
* @return bool.
|
||||
*/
|
||||
virtual bool isReady(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Return true if the shader program has a geometry shader.
|
||||
* @return bool.
|
||||
*/
|
||||
virtual bool hasGeom(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Set this shader as active, so it will be used for the next draw.
|
||||
*/
|
||||
virtual void use(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief Init the shader to make it ready to use (read the fiel, compile, link...).
|
||||
*/
|
||||
virtual void init(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setBool(const std::string& name, bool value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setInt(const std::string& name, int value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setFloat(const std::string& name, float value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat2(const std::string& name, glm::mat2 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat3(const std::string& name, glm::mat3 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat4(const std::string& name, glm::mat4 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec2(const std::string& name, glm::vec2 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec3(const std::string& name, glm::vec3 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec4(const std::string& name, glm::vec4 value) const = 0;
|
||||
};
|
||||
#endif
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
void CAbstractTexture::setId(unsigned int newId) {
|
||||
m_glId = newId;
|
||||
}
|
||||
|
||||
CAbstractTexture::CAbstractTexture(ETextureType type) :
|
||||
m_glId(0),
|
||||
m_type(type) {
|
||||
}
|
||||
|
||||
unsigned int CAbstractTexture::getId(void) {
|
||||
return m_glId;
|
||||
}
|
||||
|
||||
ETextureType CAbstractTexture::getType(void) {
|
||||
return m_type;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
#ifndef CABSTRACTTEXTURE_HPP
|
||||
#define CABSTRACTTEXTURE_HPP
|
||||
|
||||
#define TEXTURE_TYPE_NUMBER 4
|
||||
|
||||
/**
|
||||
* @brief enum representing every type of texture available.
|
||||
*/
|
||||
enum ETextureType {
|
||||
TEXTURE_DIFFUSE,
|
||||
TEXTURE_SPECULAR,
|
||||
TEXTURE_NORMAL,
|
||||
TEXTURE_HEIGHT,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Class representing a texture.
|
||||
*/
|
||||
class CAbstractTexture {
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief OpenGL ID.
|
||||
*/
|
||||
unsigned int m_glId;
|
||||
|
||||
/**
|
||||
* @brief The texture's type.
|
||||
*/
|
||||
ETextureType m_type;
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* @brief Setter to the m_glId member.
|
||||
* @param newId : OpenGL id to set.
|
||||
*/
|
||||
void setId(unsigned int newId);
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CAbstractTexture's default constructor, disabled.
|
||||
*/
|
||||
CAbstractTexture(void) = delete;
|
||||
|
||||
/**
|
||||
* @brief CAbstractTexture's constructor.
|
||||
* @param[in] type The texture's type.
|
||||
*/
|
||||
CAbstractTexture(ETextureType type);
|
||||
|
||||
virtual ~CAbstractTexture() {};
|
||||
|
||||
/**
|
||||
* @brief Getter to the m_glId member.
|
||||
* @return The m_glId member.
|
||||
*/
|
||||
unsigned int getId(void);
|
||||
|
||||
/**
|
||||
* @brief Getter to the texture's type.
|
||||
* @return The texture's type.
|
||||
*/
|
||||
ETextureType getType(void);
|
||||
|
||||
/**
|
||||
* @brief Load the texture in the GPU (uses only redefined function in the child classes).
|
||||
*/
|
||||
virtual void init(void) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,74 +0,0 @@
|
||||
#include "CImageTexture.hpp"
|
||||
|
||||
CImageTexture::CImageTexture(ETextureType type, std::string filePath) :
|
||||
CAbstractTexture(type),
|
||||
m_filePath(filePath) {
|
||||
}
|
||||
|
||||
std::string CImageTexture::getFilePath(void) {
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
void CImageTexture::setFilePath(std::string filePath) {
|
||||
m_filePath = filePath;
|
||||
}
|
||||
|
||||
void CImageTexture::init(void) {
|
||||
// Read the image file.
|
||||
SDL_Surface* sdlSurface = IMG_Load(m_filePath.c_str());
|
||||
|
||||
if (sdlSurface == NULL) {
|
||||
throw CLibException(std::string("Image file \"") + m_filePath + std::string("\" can not be opened : ") + std::string(SDL_GetError()));
|
||||
}
|
||||
|
||||
// Create the id.
|
||||
unsigned int id;
|
||||
glGenTextures(1, &id);
|
||||
setId(id);
|
||||
|
||||
// Image format.
|
||||
GLenum internalFormat(0);
|
||||
GLenum externalFormat(0);
|
||||
|
||||
if (sdlSurface->format->BytesPerPixel == 3) {
|
||||
// We use RGB as internal format.
|
||||
internalFormat = GL_RGB;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGB;
|
||||
else
|
||||
externalFormat = GL_BGR;
|
||||
}
|
||||
else if (sdlSurface->format->BytesPerPixel == 4) {
|
||||
// We use RGBA as internal format.
|
||||
internalFormat = GL_RGBA;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGBA;
|
||||
else
|
||||
externalFormat = GL_BGRA;
|
||||
}
|
||||
else {
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
throw CRuntimeException("Unknow image internal format.");
|
||||
}
|
||||
|
||||
// Lock the texture to use it.
|
||||
glBindTexture(GL_TEXTURE_2D, getId());
|
||||
|
||||
// Fill the GL texture.
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, sdlSurface->w, sdlSurface->h, 0, externalFormat, GL_UNSIGNED_BYTE, sdlSurface->pixels);
|
||||
|
||||
// Set filters : the near texture have a linear filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
// The far texture have a nearest filter, meaning no filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Unlock the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
// Free the surface.
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef CIMAGETEXTURE_HPP
|
||||
#define CIMAGETEXTURE_HPP
|
||||
|
||||
#include "../../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../../Controller/Exception/CRuntimeException.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
class CImageTexture : public CAbstractTexture {
|
||||
private:
|
||||
// Path of the file.
|
||||
std::string m_filePath;
|
||||
public:
|
||||
CImageTexture(void) = delete;
|
||||
~CImageTexture(void) {};
|
||||
CImageTexture(ETextureType type, std::string filePath);
|
||||
std::string getFilePath(void);
|
||||
void setFilePath(std::string filePath);
|
||||
virtual void init(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,111 +0,0 @@
|
||||
#include "CTextTexture.hpp"
|
||||
|
||||
CTextTexture::CTextTexture(ETextureType type, std::string filePath, std::string text, unsigned int fontSize, glm::vec3 fontColor, glm::bvec4 fontStyle) :
|
||||
CAbstractTexture(type),
|
||||
m_fontFilePath(filePath),
|
||||
m_renderText(text),
|
||||
m_fontSize(fontSize),
|
||||
m_fontColor(fontColor),
|
||||
m_fontStyle(fontStyle) {
|
||||
}
|
||||
|
||||
std::string CTextTexture::getFontFilePath(void) {
|
||||
return m_fontFilePath;
|
||||
}
|
||||
|
||||
void CTextTexture::setFontFilePath(std::string filePath) {
|
||||
m_fontFilePath = filePath;
|
||||
}
|
||||
|
||||
std::string CTextTexture::getText(void) {
|
||||
return m_renderText;
|
||||
}
|
||||
|
||||
void CTextTexture::setText(std::string text) {
|
||||
m_renderText = text;
|
||||
}
|
||||
|
||||
void CTextTexture::init(void) {
|
||||
// Read the font file.
|
||||
TTF_Font* font = TTF_OpenFont(m_fontFilePath.c_str(), m_fontSize);
|
||||
|
||||
SDL_Color color;
|
||||
color.r = m_fontColor.x / 255u;
|
||||
color.g = m_fontColor.y / 255u;
|
||||
color.b = m_fontColor.z / 255u;
|
||||
|
||||
int style = TTF_STYLE_NORMAL;
|
||||
if (m_fontStyle.x) {
|
||||
style |= TTF_STYLE_BOLD;
|
||||
}
|
||||
if (m_fontStyle.y) {
|
||||
style |= TTF_STYLE_ITALIC;
|
||||
}
|
||||
if (m_fontStyle.z) {
|
||||
style |= TTF_STYLE_UNDERLINE;
|
||||
}
|
||||
if (m_fontStyle.z) {
|
||||
style |= TTF_STYLE_STRIKETHROUGH;
|
||||
}
|
||||
TTF_SetFontStyle(font, style);
|
||||
|
||||
SDL_Surface* sdlSurface = TTF_RenderText_Blended(font, m_renderText.c_str(), color);
|
||||
|
||||
if (sdlSurface == NULL) {
|
||||
throw CLibException(std::string("Can not render the text : ") + std::string(SDL_GetError()));
|
||||
}
|
||||
|
||||
// Create the id.
|
||||
unsigned int id;
|
||||
glGenTextures(1, &id);
|
||||
setId(id);
|
||||
|
||||
// Image format.
|
||||
GLenum internalFormat(0);
|
||||
GLenum externalFormat(0);
|
||||
|
||||
if (sdlSurface->format->BytesPerPixel == 3) {
|
||||
// We use RGB as internal format.
|
||||
internalFormat = GL_RGB;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGB;
|
||||
else
|
||||
externalFormat = GL_BGR;
|
||||
}
|
||||
else if (sdlSurface->format->BytesPerPixel == 4) {
|
||||
// We use RGBA as internal format.
|
||||
internalFormat = GL_RGBA;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGBA;
|
||||
else
|
||||
externalFormat = GL_BGRA;
|
||||
}
|
||||
else {
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
throw CRuntimeException("Unknow image internal format.");
|
||||
}
|
||||
|
||||
// Lock the texture to use it.
|
||||
glBindTexture(GL_TEXTURE_2D, getId());
|
||||
|
||||
// Fill the GL texture.
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, sdlSurface->w, sdlSurface->h, 0, externalFormat, GL_UNSIGNED_BYTE, sdlSurface->pixels);
|
||||
|
||||
// Set filters : the near texture have a linear filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
// The far texture have a nearest filter, meaning no filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Unlock the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
// Free the font.
|
||||
TTF_CloseFont(font);
|
||||
|
||||
// Free the surface.
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef CTEXTTEXTURE_HPP
|
||||
#define CTEXTTEXTURE_HPP
|
||||
|
||||
#include "../../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../../Controller/Exception/CRuntimeException.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <SDL_ttf.h>
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
class CTextTexture : CAbstractTexture {
|
||||
private:
|
||||
// Path of the file.
|
||||
std::string m_fontFilePath;
|
||||
|
||||
// The size of the font.
|
||||
unsigned int m_fontSize;
|
||||
|
||||
// The color of the font.
|
||||
glm::vec3 m_fontColor;
|
||||
|
||||
// The style of the font (x is bold, y is italic, z is underline, w is strikethrough).
|
||||
glm::bvec4 m_fontStyle;
|
||||
|
||||
// The text to render.
|
||||
std::string m_renderText;
|
||||
|
||||
public:
|
||||
CTextTexture(void) = delete;
|
||||
CTextTexture(ETextureType type, std::string filePath, std::string text, unsigned int fontSize, glm::vec3 fontColor, glm::bvec4 fontStyle);
|
||||
~CTextTexture() {};
|
||||
std::string getFontFilePath(void);
|
||||
void setFontFilePath(std::string filePath);
|
||||
std::string getText(void);
|
||||
void setText(std::string text);
|
||||
virtual void init(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
29
src/Engine/Core/Component/Relationships/CRelationship.hpp
Normal file
29
src/Engine/Core/Component/Relationships/CRelationship.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef CRELATIONSHIP_HPP
|
||||
#define CRELATIONSHIP_HPP
|
||||
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace CosmicCore {
|
||||
class CRelationship : public CAbstractComponent{
|
||||
private:
|
||||
EntityId parent = entt::null;
|
||||
std::vector<EntityId> children;
|
||||
public:
|
||||
CRelationship(CEntity& entity): CAbstractComponent(entity){}
|
||||
CRelationship(CEntity& entity, EntityId p): CAbstractComponent(entity), parent(p){}
|
||||
CRelationship(CEntity& entity, std::vector<EntityId> childs): CAbstractComponent(entity), children(childs){}
|
||||
CRelationship(CEntity& entity, EntityId p, std::vector<EntityId> childs): CAbstractComponent(entity), parent(p), children(childs){}
|
||||
|
||||
nlohmann::json to_json(){return nlohmann::json();}
|
||||
void setParent(EntityId par){parent = par;}
|
||||
void addChild(EntityId child){children.emplace_back(child);}
|
||||
EntityId getParent(){return parent;}
|
||||
std::vector<EntityId>& getChildren(){return children;}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user