// // Created by karl on 07.01.20. // #ifndef ECSGAME_RENDERSYSTEM_H #define ECSGAME_RENDERSYSTEM_H #include "../ECS.h" #include "../Components/Transform.h" #include "../Components/Mesh.h" #include "../Components/Camera.h" #include "../../Rendering/Shader.h" #include "../Components/ObjMesh.h" #include "../Components/Texture.h" #include "../Components/LODObjMesh.h" #include "../Components/DirectionalLight.h" #include "../../Rendering/Material.h" using namespace ECS; class RenderSystem : public EntitySystem { public: struct RenderObject { RenderObject(const glm::mat4 &matrix, const glm::vec3 &origin, unsigned int textureId, const Mesh &mesh, float distance, const Material &material) : matrix(matrix), origin(origin), texture_id(textureId), mesh(mesh), distance(distance), material(material) {} void render(Shader shader) const { glm::mat4 model_matrix = matrix; model_matrix[3] = glm::vec4(origin, 1.0); shader.setMat4("model", model_matrix); // 0 can't be a valid texture name, so we use it for meshes without textures here if (texture_id != 0) { glBindTexture(GL_TEXTURE_2D, texture_id); } shader.setFloat("diffuseStrength", material.diffuse); shader.setFloat("specularStrength", material.specular); mesh.render(); } glm::mat4 matrix; glm::vec3 origin; unsigned int texture_id; Mesh mesh; float distance; Material material; }; // Fill the passed vector with render objects of the given world std::vector> getRenderObjects(World *world, glm::vec3 cameraPos) { std::vector> renderObjects(2); // First normal, then transparent /*pWorld->each([&](Entity *ent, ComponentHandle mesh, ComponentHandle transform) { renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), 0)); });*/ /*// TODO: Is it possible to do get ObjMeshes in the Mesh loop above implicitly via polymorphism? * // TODO: Commented out because of double rendering - we only want to get objects that explicitly DON'T have a texture here! pWorld->each([&](Entity *ent, ComponentHandle mesh, ComponentHandle transform) { // Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh float distance = glm::distance(cameraPos, transform->getPosition()); if (distance > mesh->minDistance && distance < mesh->maxDistance) { renderObjects.emplace_back(RenderObject(transform->matrix, 0, mesh.get(), distance)); } });*/ // ObjMesh with textures world->each([&](Entity *ent, ComponentHandle mesh, ComponentHandle transform, ComponentHandle texture) { // Add the object to the renderObjects to draw if the distance is within the min and max distance of the mesh float distance = glm::distance(cameraPos, transform->get_origin()); if (distance > mesh->minDistance && distance < mesh->maxDistance) { // Get optional components ComponentHandle textureComponent = ent->get(); ComponentHandle materialComponent = ent->get(); Material material = materialComponent.isValid() ? materialComponent.get() : Material(); unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0; // Put it into the list of transparent render objects if the texture wants to be rendered transparently if (textureComponent.isValid() && textureComponent->render_transparent) { renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, mesh.get(), distance, material)); } else { renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, mesh.get(), distance, material)); } } }); // LODObjMesh with Texture world->each([&](Entity *ent, ComponentHandle lodMesh, ComponentHandle transform) { float distance = glm::distance(cameraPos, transform->get_origin()); for (const auto &mesh : lodMesh->meshes) { if (distance > mesh.minDistance && distance < mesh.maxDistance) { // Get optional components ComponentHandle textureComponent = ent->get(); ComponentHandle materialComponent = ent->get(); Material material = materialComponent.isValid() ? materialComponent.get() : Material(); unsigned int textureID = textureComponent.isValid() ? textureComponent->id : 0; // Put it into the list of transparent render objects if the texture wants to be rendered transparently if (textureComponent.isValid() && textureComponent->render_transparent) { renderObjects[1].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, mesh, distance, material)); } else { renderObjects[0].emplace_back(RenderObject(transform->matrix, transform->get_origin(), textureID, mesh, distance, material)); } } } }); // Sort transparent objects std::sort(renderObjects[1].begin(), renderObjects[1].end(), [](const RenderObject &first, const RenderObject &second) -> bool { return first.distance > second.distance; }); return renderObjects; } void render(World *pWorld, Shader shader) { pWorld->each([&](Entity *ent, ComponentHandle camera, ComponentHandle cameraTransform) { glClearColor(0.6f, 0.9f, 0.9f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.use(); // Lighting // TODO: Currently only the last light is used! pWorld->each([&](Entity *ent, ComponentHandle light) { shader.setVec3("lightDirection", light->direction); }); glm::vec3 cameraPos = cameraTransform->get_origin(); glm::mat4 view = cameraTransform->matrix; view[3] = glm::vec4(cameraPos, 1.0); shader.setMat4("projection", camera->projection); shader.setMat4("view", glm::inverse(view)); shader.setVec3("cameraPosition", cameraTransform->get_origin()); std::vector> allRenderObjects = getRenderObjects(pWorld, cameraPos); std::vector renderObjects = allRenderObjects[0]; std::vector transparentRenderObjects = allRenderObjects[1]; for (const RenderObject &obj : renderObjects) { obj.render(shader); } for (const RenderObject &obj : transparentRenderObjects) { obj.render(shader); } }); } }; #endif //ECSGAME_RENDERSYSTEM_H