0
\$\begingroup\$

I have a scene that imports .obj (Wavefront) files and I have written the shaders for rendering them in a scene with multiple light sources.

Here are my Shaders: Vertex Shader:

#version 330 core

layout (location = 0) in vec3 pos;          
layout (location = 1) in vec3 normal;   
layout (location = 2) in vec2 texCoord;

uniform mat4 model;         // model matrix
uniform mat4 view;          // view matrix
uniform mat4 projection;    // projection matrix

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;

void main()
{
    FragPos = vec3(model * vec4(pos, 1.0f));            // vertex position in world space
    Normal = mat3(transpose(inverse(model))) * normal;  // normal direction in world space

    TexCoord = texCoord;

    gl_Position = projection * view *  model * vec4(pos, 1.0f);
}

Fragment Shader:

#version 330 core

struct Material 
{
    vec3 ambient;
    sampler2D diffuseMap;
    vec3 specular;
    float shininess;
};

struct DirectionalLight
{
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

struct PointLight
{
    vec3 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float exponent;
};

struct SpotLight
{
    vec3 position;
    vec3 direction;
    float cosInnerCone;
    float cosOuterCone;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    int on;

    float constant;
    float linear;
    float exponent;
};


in vec2 TexCoord;
in vec3 FragPos;
in vec3 Normal;

#define MAX_POINT_LIGHTS 3

uniform DirectionalLight sunLight;
uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
uniform vec3 viewPos;

out vec4 frag_color;

vec3 calcDirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir);
vec3 calcPointLightColor(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 calcSpotLightColor(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

//-----------------------------------------------------------------------------------------------
// Main Shader Entry
//-----------------------------------------------------------------------------------------------
void main()
{ 
    vec3 normal = normalize(Normal);  
    vec3 viewDir = normalize(viewPos - FragPos);

    // Ambient ----------------------------------------------------------------------------------
    vec3 ambient = spotLight.ambient * material.ambient * vec3(texture(material.diffuseMap, TexCoord));
    vec3 outColor = vec3(0.0f); 

    outColor += calcDirectionalLightColor(sunLight, normal, viewDir);

   for(int i = 0; i < MAX_POINT_LIGHTS; i++)
        outColor += calcPointLightColor(pointLights[i], normal, FragPos, viewDir);  

    // If the light isn't on then just return 0 for diffuse and specular colors
    if (spotLight.on == 1)
        outColor += calcSpotLightColor(spotLight, normal, FragPos, viewDir);

    frag_color = vec4(ambient + outColor, 1.0f);
}

//-----------------------------------------------------------------------------------------------
// Calculate the direction light effect and return the resulting 
// diffuse and specular color summation
//-----------------------------------------------------------------------------------------------
vec3 calcDirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir)
{
    vec3 lightDir = normalize(-light.direction);  // negate => Must be a direction from fragment towards the light

    // Diffuse ------------------------------------------------------------------------- --------
    float NdotL = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));

     // Specular - Blinn-Phong ------------------------------------------------------------------
    vec3 halfDir = normalize(lightDir + viewDir);
    float NDotH = max(dot(normal, halfDir), 0.0f);
    vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);

    return (diffuse + specular);
}

//-----------------------------------------------------------------------------------------------
// Calculate the point light effect and return the resulting diffuse and specular color summation
//-----------------------------------------------------------------------------------------------
vec3 calcPointLightColor(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // Diffuse ----------------------------------------------------------------------------------
    float NdotL = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));

     // Specular - Blinn-Phong ------------------------------------------------------------------
    vec3 halfDir = normalize(lightDir + viewDir);
    float NDotH = max(dot(normal, halfDir), 0.0f);
    vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);

    // Attenuation using Kc, Kl, Kq -------------------------------------------------------------
    float d = length(light.position - FragPos);
    float attenuation = 1.0f / (light.constant + light.linear * d + light.exponent * (d * d));

    diffuse *= attenuation;
    specular *= attenuation;

    return (diffuse + specular);
}

//------------------------------------------------------------------------------------------------
// Calculate the spotlight effect and return the resulting // diffuse and specular color summation
//------------------------------------------------------------------------------------------------
vec3 calcSpotLightColor(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);
    vec3 spotDir  = normalize(light.direction);

    float cosDir = dot(-lightDir, spotDir);  // angle between the lights direction vector and spotlights direction vector
    float spotIntensity = smoothstep(light.cosOuterCone, light.cosInnerCone, cosDir);

    // Diffuse ----------------------------------------------------------------------------------
    float NdotL = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = spotLight.diffuse * NdotL * vec3(texture(material.diffuseMap, TexCoord));

     // Specular - Blinn-Phong ------------------------------------------------------------------
    vec3 halfDir = normalize(lightDir + viewDir);
    float NDotH = max(dot(normal, halfDir), 0.0f);
    vec3 specular = light.specular * material.specular * pow(NDotH, material.shininess);

    // Attenuation using Kc, Kl, Kq -------------------------------------------------------------
    float d = length(light.position - FragPos);
    float attenuation = 1.0f / (light.constant + light.linear * d + light.exponent * (d * d));

    diffuse *= attenuation * spotIntensity;
    specular *= attenuation * spotIntensity;

    return (diffuse + specular);
}

Now I want a particle system that should be rendered in the scene. I have read that a particle is basically a small quad always facing the camera. How would I proceed writing my shaders for the particle system?

Particle systems are 2D but the scene is 3D how would I manipulate them into my 3D scene.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.