Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Diffuse and Specular Effect

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2018
    Posts
    28

    Diffuse and Specular Effect

    Hi,

    I am working on a 3D Graphics with light effect. I draw a plane with scaling(4,4,4). I set camera position at (1.0, 0.0, 10.0) and light at (0.0,0.0,5.0). Material diffuse color is (0.5,0.0,0.0,1.0) and specular color is (0.1,0.5,0.0,1.0). But there is no specular effect in result.

    This is my shader source code :

    Code.cpp

    Code glsl:
    #version 300 es
     
    in vec3 a_position;
    in vec3 a_normal;
    in vec2 texcoord;
     
    uniform vec4 mat_ambient;
    uniform vec4 mat_diffuse;
    uniform vec4 mat_emission;
    uniform vec4 mat_specular;
     
    uniform mat4 u_mvp;
    uniform mat4 u_world;
    uniform mat4 u_view;
     
    uniform vec3 u_lightPos;
    uniform vec3 u_eyePos;
     
    vec3 diffuse_effect, specular_effect;
     
    void main() 
    {
     
     
        gl_Position = u_mvp *  vec4(a_position,1.0);
        vec4 worldPos = u_world *  vec4(a_position,1.0);
        vec3 worldNormal = normalize(mat3(inverse(transpose(u_world))) * a_normal);
        vec3 viewDir = normalize(u_eyePos.xyz - worldPos.xyz); //also a unit vector
     
     
        lig[0].color = vec4(1.0,1.0,1.0,1.0);
        lig[0].position = vec3(0.0,5.0,10.0);
     
        lig[0].const_atten = 1.0f;
        lig[0].linear_atten = 0.0f;
        lig[0].quad_atten = 0.00111109f;
        lig[0].type = 1;
     
        float matShine = 50.0f;
     
     
        vec3 light_dir = (lig[0].position - worldPos.xyz);
     
        float diff_fac = max(0.0,dot(light_dir,worldNormal));
     
        float attn, spec_co_eff;
     
        diffuse_effect = lig[0].color.rgb * diff_fac * mat_diffuse.xyz;
     
        float dist = length(light_dir);
     
        light_dir = normalize(light_dir);
     
        attn = 1.0 / (lig[0].const_atten + lig[0].linear_atten * dist + lig[0].quad_atten  * dist * dist);
     
        if(dot(light_dir,worldNormal) < 0.0)
        {
            specular_effect = vec3(0.0,0.0,0.0);
        }
        else 
        {
     
            spec_co_eff = pow(max(0.0, dot(reflect(-light_dir, worldNormal), viewDir)), matShine);
            specular_effect = lig[0].color.rgb * attn * spec_co_eff * mat_specular.xyz;
        }
     
        linear_color = vec4((diffuse_effect + specular_effect ), 1.0);
    }


    Thanks in advance.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	spec.jpg 
Views:	35 
Size:	4.6 KB 
ID:	2770  
    Last edited by Dark Photon; 05-23-2018 at 04:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •