~leinnan/raylibGame unlisted

2ca751b4655523f56c3405a7a0b251c8048795c2 — Piotr Siuszko 4 months ago 4118e1c
WIP lights
A data/shaders/base_lighting.vs => data/shaders/base_lighting.vs +33 -0
@@ 0,0 1,33 @@
#version 330

// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;

// Input uniform values
uniform mat4 mvp;
uniform mat4 matModel;

// Output vertex attributes (to fragment shader)
out vec3 fragPosition;
out vec2 fragTexCoord;
out vec4 fragColor;
out vec3 fragNormal;

// NOTE: Add here your custom variables

void main()
{
    // Send vertex attributes to fragment shader
    fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
    fragTexCoord = vertexTexCoord;
    fragColor = vertexColor;
    
    mat3 normalMatrix = transpose(inverse(mat3(matModel)));
    fragNormal = normalize(normalMatrix*vertexNormal);

    // Calculate final vertex position
    gl_Position = mvp*vec4(vertexPosition, 1.0);
}

A data/shaders/lighting.fs => data/shaders/lighting.fs +89 -0
@@ 0,0 1,89 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec3 fragPosition;
in vec2 fragTexCoord;
in vec4 fragColor;
in vec3 fragNormal;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

// Output fragment color
out vec4 finalColor;

// NOTE: Add here your custom variables

#define     MAX_LIGHTS              4
#define     LIGHT_DIRECTIONAL       0
#define     LIGHT_POINT             1

struct MaterialProperty {
    vec3 color;
    int useSampler;
    sampler2D sampler;
};

struct Light {
    int enabled;
    int type;
    vec3 position;
    vec3 target;
    vec4 color;
};

// Input lighting values
uniform Light lights[MAX_LIGHTS];
uniform vec4 ambient;
uniform vec3 viewPos;

void main()
{
    // Texel color fetching from texture sampler
    vec4 texelColor = texture(texture0, fragTexCoord);
    vec3 lightDot = vec3(0.0);
    vec3 normal = normalize(fragNormal);
    vec3 viewD = normalize(viewPos - fragPosition);
    vec3 specular = vec3(0.0);

    // NOTE: Implement here your fragment shader code

    for (int i = 0; i < MAX_LIGHTS; i++)
    {
        if (lights[i].enabled == 1)
        {
            vec3 light = vec3(0.0);
            
            if (lights[i].type == LIGHT_DIRECTIONAL) 
            {
                light = -normalize(lights[i].target - lights[i].position);
            }
            
            if (lights[i].type == LIGHT_POINT) 
            {
                light = normalize(lights[i].position - fragPosition);
            }
            
            float NdotL = max(dot(normal, light), 0.0);
            lightDot += lights[i].color.rgb*NdotL;

            float specCo = 0.0;
            if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine
            specular += specCo;
        }
    }

    finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
    finalColor += texelColor*(ambient/10.0);
    
    // Gamma correction
    finalColor = pow(finalColor, vec4(1.0/2.2));

    float dist = length(viewPos - fragPosition);
    const vec4 fogColor = vec4(0.5, 0.5, 0.5, 1.0);
    const float fogDensity = 0.022;
    float fogFactor = 1.0/exp((dist*fogDensity)*(dist*fogDensity));
    fogFactor = clamp(fogFactor, 0.0, 1.0);
    finalColor = mix(fogColor, finalColor, fogFactor);;
}

M src/components/Actor.hpp => src/components/Actor.hpp +0 -1
@@ 3,7 3,6 @@
#include <string>

namespace Components {
    struct GameRestart{};

enum class Activity { Idle, Walking, AttackAnticipation, AttackContact, AttackRecovery };


A src/components/GameRestart.hpp => src/components/GameRestart.hpp +4 -0
@@ 0,0 1,4 @@
#pragma once
namespace Components {
    struct GameRestart{};
}
\ No newline at end of file

A src/components/ModelLightsShader.hpp => src/components/ModelLightsShader.hpp +8 -0
@@ 0,0 1,8 @@
#pragma once
#include "raylib.h"

namespace Components {
    struct ModelLightsShader {
        Shader shader;
    };
}
\ No newline at end of file

M src/main.cpp => src/main.cpp +1 -1
@@ 1,7 1,7 @@
#include "raylib.h"

#include "INIReader.hpp"
#include "components/Actor.hpp"
#include "components/GameRestart.hpp"
#include "factories/UnitFactory.hpp"
#include "systems/AiSystem.hpp"
#include "systems/CollisionSystem.hpp"

M src/systems/PlayerInputSystem.cpp => src/systems/PlayerInputSystem.cpp +1 -0
@@ 7,6 7,7 @@
#include "components/Transform.hpp"
#include "components/Velocity.hpp"
#include "components/Actor.hpp"
#include "components/GameRestart.hpp"

#include <cmath>
#include <iostream>

A src/systems/RenderLightingSystem.cpp => src/systems/RenderLightingSystem.cpp +26 -0
@@ 0,0 1,26 @@
//
// Created by 48785 on 04.06.2020.
//
#include "RenderLightingSystem.hpp"
#include "components/ModelLightsShader.hpp"

void RenderLightingSystem::Init(entt::registry<> &registry) {
    registry.construction<Components::ModelLightsShader>().connect<&RenderLightingSystem::LoadLightingShader>(this);
    registry.destruction<Components::ModelLightsShader>().connect<&RenderLightingSystem::UnloadLightingShader>(this);
}

void RenderLightingSystem::LoadLightingShader(entt::registry<> & registry, std::uint32_t entity){
    auto object = registry.get<Components::ModelLightsShader>(entity);
    object.shader = LoadShader("data/shaders/base_lighting.vs", "data/shaders/lighting.fs");
    object.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(object.shader, "matModel");
    object.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(object.shader, "viewPos");
    const int ambientLoc = GetShaderLocation(object.shader, "ambient");
    const float values[4]{ 0.2f, 0.2f, 0.2f, 1.0f };

    SetShaderValue(object.shader, ambientLoc, values, UNIFORM_VEC4);
}

void RenderLightingSystem::UnloadLightingShader(entt::registry<> & registry, std::uint32_t entity){
    auto object = registry.get<Components::ModelLightsShader>(entity);
    UnloadShader(object.shader);
}

A src/systems/RenderLightingSystem.hpp => src/systems/RenderLightingSystem.hpp +20 -0
@@ 0,0 1,20 @@
//
// Created by 48785 on 04.06.2020.
//

#ifndef RAYLIBECS_RENDERLIGHTINGSYSTEM_HPP
#define RAYLIBECS_RENDERLIGHTINGSYSTEM_HPP
#include <entt/entity/registry.hpp>


class RenderLightingSystem {
    void Init(entt::registry<> &registry);
    void LoadLightingShader(entt::registry<> &registry, std::uint32_t entity);
    void UnloadLightingShader(entt::registry<> &registry, std::uint32_t entity);
private:

};



#endif //RAYLIBECS_RENDERLIGHTINGSYSTEM_HPP

M src/systems/RenderSystem.cpp => src/systems/RenderSystem.cpp +36 -6
@@ 12,9 12,23 @@
#include "components/WorldScreenText.hpp"
#include <cmath>
#include <iostream>
#include "raymath.h"

namespace Systems {
void RenderSystem::Init(entt::registry<> &registry) {
    modelLightsShader = LoadShader("data/shaders/base_lighting.vs", "data/shaders/lighting.fs");
    modelLightsShader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(modelLightsShader, "matModel");
    modelLightsShader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(modelLightsShader, "viewPos");
    int ambientLoc = GetShaderLocation(modelLightsShader, "ambient");
    const float values[4]{ 0.2f, 0.2f, 0.2f, 1.0f };
    SetShaderValue(modelLightsShader, ambientLoc, values, UNIFORM_VEC4);


    lights[0] = light::CreateLight(light::LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), WHITE, modelLightsShader);
    lights[1] = light::CreateLight(light::LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), RED, modelLightsShader);
    lights[2] = light::CreateLight(light::LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), GREEN, modelLightsShader);
    lights[3] = light::CreateLight(light::LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), BLUE, modelLightsShader);

    registry.construction<Components::BillboardRenderer>().connect<&RenderSystem::LoadEntityTexture>(this);
    registry.destruction<Components::BillboardRenderer>().connect<&RenderSystem::UnloadEntityTexture>(this);
    registry.construction<Components::MeshRenderer>().connect<&RenderSystem::LoadEntityTexture>(this);


@@ 24,6 38,15 @@ void RenderSystem::Init(entt::registry<> &registry) {
void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
    const float w = GetScreenWidth();
    const float h = GetScreenHeight();
    const float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };

    UpdateLightValues(modelLightsShader, lights[0]);
    UpdateLightValues(modelLightsShader, lights[1]);
    UpdateLightValues(modelLightsShader, lights[2]);
    UpdateLightValues(modelLightsShader, lights[3]);

    SetShaderValue(modelLightsShader, modelLightsShader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);

    auto target = LoadRenderTexture(w,h);
    auto fxShader = LoadShader(0, "data/shaders/pixelizer.fs");
    UpdateCamera(registry,camera);


@@ 41,11 64,15 @@ void RenderSystem::Update(entt::registry<> &registry, Camera &camera) {
        [&](const auto, const auto &billboard, const auto &transform) {
            DrawBillboard(camera, billboard.texture, transform.pos, billboard.size, billboard.color);
        });
    registry.view<Components::MeshRenderer, Components::Transform>().each(
        [&](const auto, const auto &mesh, const auto &transform) {
            DrawModelEx(mesh.model, transform.pos, {0.f, -1.f, 0.f}, transform.angle,
                        {mesh.scale, mesh.scale, mesh.scale}, WHITE);
        });
    BeginShaderMode(modelLightsShader);
    {
        registry.view<Components::MeshRenderer, Components::Transform>().each(
            [&](const auto, const auto &mesh, const auto &transform) {
                DrawModelEx(mesh.model, transform.pos, {0.f, -1.f, 0.f}, transform.angle,
                            {mesh.scale, mesh.scale, mesh.scale}, WHITE);
            });
    }
    EndShaderMode();

    registry.view<Components::Collisions, Components::Transform>().each(
        [&](const auto, const auto &collisions, const auto &transform) {


@@ 109,10 136,13 @@ void RenderSystem::LoadEntityTexture(entt::registry<> &registry, std::uint32_t e
        Components::MeshRenderer &mesh = registry.get<Components::MeshRenderer>(entity);

        mesh.model = LoadModel(mesh.modelPath.c_str());

        if(!mesh.diffusePath.empty()) {
            mesh.diffuse = LoadTexture(mesh.diffusePath.c_str());
            if(mesh.model.materialCount > 0)
            if(mesh.model.materialCount > 0){
                mesh.model.materials[0].maps[MAP_DIFFUSE].texture = mesh.diffuse;
                mesh.model.materials[0].shader = modelLightsShader;
            }
        }
    }
}

M src/systems/RenderSystem.hpp => src/systems/RenderSystem.hpp +3 -0
@@ 1,6 1,7 @@
#ifndef RENDER_SYSTEM_HPP
#define RENDER_SYSTEM_HPP
#include "raylib.h"
#include "utils/lightArkona.h"
#include <entt/entity/registry.hpp>

namespace Systems {


@@ 16,6 17,8 @@ class RenderSystem {

  private:
    void UpdateCamera(entt::registry<> &registry, Camera &camera);
    Shader modelLightsShader;
    light::Light lights[light::MAX_LIGHTS] = { 0 };
};
} // namespace Systems
  // Systems

A src/utils/lightArkona.h => src/utils/lightArkona.h +94 -0
@@ 0,0 1,94 @@
#pragma once
#include "raylib.h"

namespace light {
    const int MAX_LIGHTS = 4; // Max dynamic lights supported by shader
    // Light data
    typedef struct {
        int type;
        Vector3 position;
        Vector3 target;
        Color color;
        bool enabled;

        // Shader locations
        int enabledLoc;
        int typeLoc;
        int posLoc;
        int targetLoc;
        int colorLoc;
    } Light;

    // Light type
    typedef enum {
        LIGHT_DIRECTIONAL,
        LIGHT_POINT
    } LightType;

    static int lightsCount = 0;    // Current amount of created lights

    // Send light properties to shader
    // NOTE: Light shader locations should be available
    static void UpdateLightValues(Shader shader, Light light)
    {
        // Send to shader light enabled state and type
        SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT);
        SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT);

        // Send to shader light position values
        const float position[3] = { light.position.x, light.position.y, light.position.z };
        SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3);

        // Send to shader light target position values
        const float target[3] = { light.target.x, light.target.y, light.target.z };
        SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3);

        // Send to shader light color values
        const float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
                           (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
        SetShaderValue(shader, light.colorLoc, color, UNIFORM_VEC4);
    }

    // Create a light and get shader locations
    static Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
    {
        Light light = { 0 };

        if (lightsCount < MAX_LIGHTS)
        {
            light.enabled = true;
            light.type = type;
            light.position = position;
            light.target = target;
            light.color = color;

            // TODO: Below code doesn't look good to me,
            // it assumes a specific shader naming and structure
            // Probably this implementation could be improved
            char enabledName[32] = "lights[x].enabled\0";
            char typeName[32] = "lights[x].type\0";
            char posName[32] = "lights[x].position\0";
            char targetName[32] = "lights[x].target\0";
            char colorName[32] = "lights[x].color\0";

            // Set location name [x] depending on lights count
            enabledName[7] = '0' + lightsCount;
            typeName[7] = '0' + lightsCount;
            posName[7] = '0' + lightsCount;
            targetName[7] = '0' + lightsCount;
            colorName[7] = '0' + lightsCount;

            light.enabledLoc = GetShaderLocation(shader, enabledName);
            light.typeLoc = GetShaderLocation(shader, typeName);
            light.posLoc = GetShaderLocation(shader, posName);
            light.targetLoc = GetShaderLocation(shader, targetName);
            light.colorLoc = GetShaderLocation(shader, colorName);

            UpdateLightValues(shader, light);

            lightsCount++;
        }

        return light;
    }
}
\ No newline at end of file