Initial commit - restart from existing code

This commit is contained in:
Tom Ray
2026-02-13 19:15:05 +01:00
parent 11df621bb2
commit 7c352bc280
1906 changed files with 491226 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
#include "CAbstractComponent.hpp"
namespace CosmicCore {
constexpr std::string typeToString(EComponentType type)
{
switch (type) {
case EComponentType::COMPONENT_SCRIPT:
return "Script";
case EComponentType::COMPONENT_CAMERA:
return "Camera";
case EComponentType::COMPONENT_LIGHT:
return "Light";
case EComponentType::COMPONENT_RIGIDBODY:
return "Rigidbody";
case EComponentType::COMPONENT_COLLIDER:
return "Collider";
case EComponentType::COMPONENT_CUSTOM:
return "Custom";
case EComponentType::COMPONENT_META:
return "Meta";
default:
return "UNKNOWN_COMPONENT";
}
}
}

View File

@@ -0,0 +1,43 @@
#ifndef CABSTRACT_COMPONENT_HPP
#define CABSTRACT_COMPONENT_HPP
#include "../Utils/CSerializable.hpp"
#include "../Entity/CEntity.hpp"
namespace CosmicCore {
/**
* @brief Enum representing every type of component that can be created.
*/
enum class EComponentType : unsigned char {
COMPONENT_SCRIPT,
COMPONENT_CAMERA,
COMPONENT_LIGHT,
COMPONENT_RIGIDBODY,
COMPONENT_COLLIDER,
COMPONENT_CUSTOM,
COMPONENT_RENDERER,
COMPONENT_META,
COMPONENT_COUNT
};
constexpr std::string typeToString(EComponentType type);
class CAbstractComponent : public CSerializable{
public:
CAbstractComponent(CEntity& entity): m_owningEntity(entity){};
virtual ~CAbstractComponent() = default;
CEntity& getEntity() {
return m_owningEntity;
}
virtual nlohmann::json to_json() = 0;
private:
/**
* @brief The entity to which the component is linked.
*/
CEntity m_owningEntity;
};
}
#endif

View File

@@ -0,0 +1,10 @@
#ifndef CABSTRACTCAMERA_HPP
#define CABSTRACTCAMERA_HPP
#include "../CAbstractComponent.hpp"
namespace CosmicCore {
class CAbstractCamera: CAbstractComponent
{
};
}
#endif

View File

@@ -0,0 +1,10 @@
#ifndef CABSTRACTCOLLIDER_HPP
#define CABSTRACTCOLLIDER_HPP
#include "../CAbstractComponent.hpp"
namespace CosmicCore {
class CAbstractCollider: public CAbstractComponent
{
};
}
#endif

View File

@@ -0,0 +1,34 @@
#include "CAbstractCustomComponent.hpp"
namespace CosmicCore {
CAbstractCustomComponent::CAbstractCustomComponent(CEntity& entity): CAbstractComponent(entity)
{
}
void CAbstractCustomComponent::addProperty(std::string name, float value)
{
m_properties[name] = value;
}
float CAbstractCustomComponent::getProperty(std::string name)
{
return m_properties[name];
}
void CAbstractCustomComponent::setProperty(std::string name, float value)
{
if (m_properties.count(name) != 0)
{
m_properties[name] = value;
}
}
nlohmann::json CAbstractCustomComponent::to_json()
{
nlohmann::json j;
j["ComponentType"] = EComponentType::COMPONENT_CUSTOM;
return j;
}
}

View File

@@ -0,0 +1,40 @@
#ifndef CABSTRACTCUSTOMCOMPONENT_HPP
#define CABSTRACTCUSTOMCOMPONENT_HPP
#include "../CAbstractComponent.hpp"
#include <unordered_map>
namespace CosmicCore {
/**
* @brief Class representing a Data Dictonnary. Is a component.
*/
class CAbstractCustomComponent : public CAbstractComponent{
private:
std::unordered_map<std::string, float> m_properties;
public:
/**
* @brief CData's default constructor, disabled.
*/
CAbstractCustomComponent() = delete;
virtual ~CAbstractCustomComponent() = default;
/**
* @brief CData's constructor.
* @param[in, out] entity The entity to which we want to attach specific informations.
*/
CAbstractCustomComponent(CEntity& entity);
void addProperty(std::string name, float value);
float getProperty(std::string name);
void setProperty(std::string name, float value);
nlohmann::json to_json();
};
}
#endif

View File

@@ -0,0 +1,180 @@
#include "CTransform.hpp"
namespace CosmicCore
{
void CTransform::calcTranslation(void) {
m_translation = glm::translate(glm::mat4(1.0f), m_center);
}
void CTransform::calcRotation(void) {
m_rotation = glm::toMat4(m_orientation);
}
void CTransform::calcHomothety(void) {
m_homothety = glm::scale(glm::mat4(1.0f), m_scale);
}
void CTransform::calcTranformation(void) {
m_transformation = m_translation * m_rotation * m_homothety;
}
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_observers() {
calcTranslation();
calcRotation();
calcHomothety();
calcTranformation();
}
CTransform::CTransform(CEntity& entity, glm::vec3 center, glm::vec3 scale, glm::vec3 eulerAngle) : CAbstractComponent(entity),
m_center(center),
m_scale(scale),
m_orientation(eulerAngle),
m_observers() {
calcTranslation();
calcRotation();
calcHomothety();
calcTranformation();
}
glm::mat4 CTransform::getTranslation(void) const {
return m_translation;
}
glm::mat4 CTransform::getRotation(void) const {
return m_rotation;
}
glm::mat4 CTransform::getHomothety(void) const {
return m_homothety;
}
glm::mat4 CTransform::getTransormation(void) const {
return m_transformation;
}
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;
}
}*/
}
glm::vec3 CTransform::getCenter(void) const {
return m_center;
}
glm::vec3* CTransform::getCenterReference(void)
{
return &m_center;
}
glm::vec3 CTransform::getScale(void) const {
return m_scale;
}
glm::vec3& CTransform::getScalePtr(void)
{
return m_scale;
}
glm::vec3 CTransform::getEulerAngle(void) const {
return glm::eulerAngles(m_orientation);
}
glm::quat CTransform::getOrientation(void) const
{
return m_orientation;
}
void CTransform::setCenter(glm::vec3 newCenter) {
m_center = newCenter;
calcTranslation();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onCenterChange();
}
}
void CTransform::setUniformScale(float newScale) {
m_scale = glm::vec3(newScale, newScale, newScale);
calcHomothety();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onScaleChange();
}
}
void CTransform::setScale(glm::vec3 newScale) {
m_scale = newScale;
calcHomothety();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onScaleChange();
}
}
void CTransform::rotate(glm::vec3 axis, float angle) {
m_orientation = glm::rotate(m_orientation, angle, axis);
calcRotation();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onRotationChange();
}
}
void CTransform::setEulerAngle(glm::vec3 newEulerAngle) {
m_orientation = glm::quat(newEulerAngle);
calcRotation();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onRotationChange();
}
}
void CTransform::setQuaternion(glm::quat newQuaternion) {
m_orientation = newQuaternion;
calcRotation();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onRotationChange();
}
}
void CTransform::forward(glm::vec3 value) {
glm::vec3 relValue = m_orientation * value;
m_center += relValue;
calcTranslation();
calcTranformation();
for (auto const& obs : m_observers) {
if(auto ptrObs = obs.lock())
ptrObs->onCenterChange();
}
}
void CTransform::addObserver(std::weak_ptr<CTransformObserver> newObserver) {
m_observers.emplace_back(newObserver);
}
void CTransform::removeObserver(std::weak_ptr<CTransformObserver> obs)
{
//TODO
}
}

View File

@@ -0,0 +1,245 @@
#ifndef CTRANSFORM_HPP
#define CTRANSFORM_HPP
#include "../../Observer/CTransformObserver.hpp"
#include "../CAbstractComponent.hpp"
#include <memory>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
namespace CosmicCore
{
/**
* @brief Class representing the transformations of the object in the space.
*/
class CTransform : public CAbstractComponent {
private:
/**
* @brief A 3D vector representing the position of the entity .
*/
glm::vec3 m_center;
/**
* @brief A 3D vector representing the scale of the entity on each axis.
*/
glm::vec3 m_scale;
/**
* @brief A quaternion which represent the rotation.
*/
glm::quat m_orientation;
/**
* @brief A mat4 matrix representing the translation transformation.
*/
glm::mat4 m_translation;
/**
* @brief A mat4 matrix representing the rotation transformation.
*/
glm::mat4 m_rotation;
/**
* @brief A mat4 representing the scale of the object on each axis.
*/
glm::mat4 m_homothety;
/**
* @brief Full transformation of the object.
* result of m_translation * m_rotation * m_homothety.
*/
glm::mat4 m_transformation;
/**
* @brief a list of observer.
*/
std::vector<std::weak_ptr<CTransformObserver>> m_observers;
/**
* @brief Compute the translation matrix.
*/
void calcTranslation(void);
/**
* @brief Compute the rotation matrix.
*/
void calcRotation(void);
/**
* @brief Compute the homothety matrix.
*/
void calcHomothety(void);
/**
* @brief Compute the full transformation of the object.
*/
void calcTranformation(void);
public:
/**
* @brief CTransform's constructor. Assign the center to (0,0,0) and the scale to (1,1,1).
* @param[in, out] m_entity The entity to which the transform belongs.
*/
CTransform(CEntity& m_entity);
/**
* @brief CTransform's constructor.
* @param[in, out] m_entity The entity to which the transform belongs.
* @param[in] center A 3D vector which represent the initial position of the entity.
* @param[in] scale A 3D vector which represent the scale of the entity on each axis.
* @param[in] eulerAngle A 3D vector wich represent the initial rotation of the entity.
*/
CTransform(CEntity& m_entity, glm::vec3 center, glm::vec3 scale, glm::vec3 eulerAngle);
/**
* @brief Getter to the translation matrix.
* @return A mat4 equal to m_translation.
*/
glm::mat4 getTranslation(void) const;
/**
* @brief Getter to the rotation matrix.
* @return A mat4 equal to m_rotation.
*/
glm::mat4 getRotation(void) const;
/**
* @brief Getter to the homothety.
* @return A mat4 equal to m_homothety .
*/
glm::mat4 getHomothety(void) const;
/**
* @brief Getter to the full transformation.
* @return A mat4 equal to m_transformation.
*/
glm::mat4 getTransormation(void) const;
/**
* @brief Getter to the full inherited transformation.
* Return the current entity's transformation if there is no parent or the transformation of each parent recursively times the current transformation.
* @return A mat4 representing the actual transformation of the entity (in the global scale).
*/
glm::mat4 getInheritedTransformation(void) const;
/**
* @brief Getter to the entity's center.
* @return The 3D vector of the position.
*/
glm::vec3 getCenter(void) const;
/**
* @brief Getter to the entity's center by its pointer.
* @return The 3D vector of the position.
*/
glm::vec3* getCenterReference(void);
/**
* @brief Getter to the scale.
* @return The 3D vector of the scale.
*/
glm::vec3 getScale(void) const;
glm::vec3& getScalePtr(void);
/**
* @brief Getter to the angles of the rotation on each axis.
* @return The 3D vector of the rotation angles, determining the orientation of the object.
*/
glm::vec3 getEulerAngle(void) const;
/**
* @brief Getter to the quaternion.
* @return A quaternion representing the orientation of the object.
*/
glm::quat getOrientation(void) const;
/**
* @brief Setter of the position.
* @param[in] newCenter The new position of the object.
*/
void setCenter(glm::vec3 newCenter);
/**
* @brief Setter of the uniform scale (same scale on the 3-axis).
* @param[in] newScale The new uniform scale.
*/
void setUniformScale(float newScale);
/**
* @brief Setter of the scale.
* @param[in] newScale The new scale on each axis (3D vector).
*/
void setScale(glm::vec3 newScale);
/**
* @brief Rotate the object on a given axis with the given angle.
* @param[in] axis The axis on which the rotation will occur.
* @param[in] angle The angle the rotation must reach.
*/
void rotate(glm::vec3 axis, float angle);
/**
* @brief Translate the object on the given axis with the given value on each axis.
* @param[in] value The 3D vector of the translation.
*/
void forward(glm::vec3 value);
/**
* @brief Setter of the Angles.
* @param[in] newEulerAngle The new orientation of the object.
*/
void setEulerAngle(glm::vec3 newEulerAngle);
/**
* @brief Setter of the orientation quaternion.
* @param[in] newQuaternion The new orientation of the object.
*/
void setQuaternion(glm::quat newQuaternion);
/**
* @brief Add an observer on the transform. Can be from any class if the said class is child class of CTransformObserver.
* @param[in, out] newObserver The observer to add.
*/
void addObserver(std::weak_ptr<CTransformObserver> newObserver);
/**
* @brief Remove an observer of the transform. Does nothing if the observer doesn't exist.
* @param[in, out] obs The observer to remove.
*/
void removeObserver(std::weak_ptr<CTransformObserver> obs);
nlohmann::json to_json() {
nlohmann::json j;
j["Position"]["x"] = m_center.x;
j["Position"]["y"] = m_center.y;
j["Position"]["z"] = m_center.z;
j["Rotation"]["x"] = getEulerAngle().x;
j["Rotation"]["y"] = getEulerAngle().y;
j["Rotation"]["z"] = getEulerAngle().z;
j["Scale"]["x"] = getScale().x;
j["Scale"]["y"] = getScale().y;
j["Scale"]["z"] = getScale().z;
return j;
};
static CTransform from_json(CEntity entity,nlohmann::json& j)
{
CTransform t(entity);
t.setCenter(glm::vec3(j["Position"]["x"], j["Position"]["y"], j["Position"]["z"]));
t.setEulerAngle(glm::vec3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]));
t.setScale(glm::vec3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]));
return t;
};
};
}
#endif

View File

@@ -0,0 +1,12 @@
#ifndef CABSTRACTRENDERER_HPP
#define CABSTRACTRENDERER_HPP
#include "../CAbstractComponent.hpp"
namespace CosmicCore {
class CAbstractRenderer : public CAbstractComponent
{
public:
CAbstractRenderer(CEntity& e): CAbstractComponent(e){};
virtual void render() = 0;
};
}
#endif

View File

@@ -0,0 +1,44 @@
#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;
}

View File

@@ -0,0 +1,78 @@
#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

View File

@@ -0,0 +1,10 @@
#include "CTestRenderer.hpp"
#include <iostream>
namespace CosmicCore {
void CTestRenderer::render()
{
int a = 2*25;
int b = a +2;
std::cout << b << std::endl;
};
}

View File

@@ -0,0 +1,17 @@
#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

View File

@@ -0,0 +1,39 @@
#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

View File

@@ -0,0 +1,39 @@
#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

View File

@@ -0,0 +1,216 @@
#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);
}

View File

@@ -0,0 +1,132 @@
#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

View File

@@ -0,0 +1,48 @@
#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

View File

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

View File

@@ -0,0 +1,159 @@
#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

View File

@@ -0,0 +1,18 @@
#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;
}

View File

@@ -0,0 +1,72 @@
#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

View File

@@ -0,0 +1,74 @@
#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);
}

View File

@@ -0,0 +1,31 @@
#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

View File

@@ -0,0 +1,111 @@
#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);
}

View File

@@ -0,0 +1,47 @@
#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

View File

@@ -0,0 +1,10 @@
#ifndef CABSTRACTLIGHT_HPP
#define CABSTRACTLIGHT_HPP
#include "../CAbstractComponent.hpp"
namespace CosmicCore {
class CAbstractLight: public CAbstractComponent
{
};
}
#endif

View File

@@ -0,0 +1,23 @@
#ifndef CMETADATA_HPP
#define CMETADATA_HPP
#include "../CAbstractComponent.hpp"
#include "nlohmann/json_fwd.hpp"
#include <string>
namespace CosmicCore {
class CMetaData : public CAbstractComponent{
private:
std::string m_name;
std::string m_description;
public:
CMetaData(CEntity& entity): CAbstractComponent(entity){}
CMetaData(CEntity& entity, std::string na, std::string desc): CAbstractComponent(entity), m_name(std::move(na)), m_description(std::move(desc)){}
nlohmann::json to_json(){return nlohmann::json();}
void setName(std::string name){m_name = std::move(name);}
void setDescription(std::string desc){m_description = std::move(desc);}
};
}
#endif

View File

@@ -0,0 +1,6 @@
#include "CAbstractRigidbody.hpp"
namespace CosmicCore {
CAbstractRigidBody::CAbstractRigidBody(CEntity& entity): CAbstractComponent(entity)
{
}
}

View File

@@ -0,0 +1,17 @@
#ifndef CABSTRACTRIGIDBODY_HPP
#define CABSTRACTRIGIDBODY_HPP
#include "../CAbstractComponent.hpp"
#include "nlohmann/json_fwd.hpp"
namespace CosmicCore {
class CAbstractRigidBody: public CAbstractComponent
{
public:
/**
* @brief CData's constructor.
* @param[in, out] entity The entity to which we want to attach specific informations.
*/
CAbstractRigidBody(CEntity& entity);
nlohmann::json to_json(){return nlohmann::json();};
};
}
#endif

View File

@@ -0,0 +1,32 @@
#include "CAbstractScript.hpp"
//#include "../../../Controller/CKernel.hpp"
namespace CosmicCore {
//CPlugin<CAbstractScript>* CAbstractScript::m_scriptPLugin = nullptr;
CAbstractScript::CAbstractScript(CEntity& entity) : CAbstractComponent(entity)
{
//m_kernelPTR = CKernel::m_kernel;
}
nlohmann::json CAbstractScript::to_json()
{
nlohmann::json absScript;
absScript["scriptName"] = m_scriptName;
absScript["ComponentType"] = EComponentType::COMPONENT_SCRIPT;
return absScript;
}
CAbstractScript* CAbstractScript::from_json(CEntity& entity, nlohmann::json& j)
{
/*std::string name = j["scriptName"];
CAbstractScript* script = CAbstractScript::m_scriptPLugin->useCreator("instantiate"+name);
script->setEntity(entity);
script->setKernel(CKernel::m_kernel);
script->start();
return script;*/
return nullptr;
}
}

View File

@@ -0,0 +1,33 @@
#ifndef CABSTRACTSCRIPT_HPP
#define CABSTRACTSCRIPT_HPP
#include "../CAbstractComponent.hpp"
#include "../../Entity/CEntity.hpp"
#include <memory>
//#include "../../../Modules/CPlugin.hpp"
//#include "../../../Controller/CKernel.fwd.hpp"
namespace CosmicCore {
//TODO
class CAbstractScript : public CAbstractComponent
{
private:
std::string m_scriptName;
//CKernel* m_kernelPTR;
public:
//static CPlugin<CAbstractScript>* m_scriptPLugin;
CAbstractScript(CEntity& entity);
virtual void start() = 0;
virtual void update() = 0;
nlohmann::json to_json();
static CAbstractScript* from_json(CEntity& entity, nlohmann::json& j);
virtual ~CAbstractScript(){};
std::string getName() { return m_scriptName; };
//CKernel* getKernel() { return m_kernelPTR; };
//void setKernel(CKernel* kernel) { m_kernelPTR = kernel; };
void setName(std::string name) { m_scriptName = name; };
};
}
#endif