Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:15:08+00:00 2026-06-07T09:15:08+00:00

I want to have two light sources: a directional one and a spotlight. I

  • 0

I want to have two light sources: a directional one and a spotlight. I cannot seem to get what I am doing wrong — probably not understanding how shaders work! I get the first light fine but no sign of the effects of the second one (aka spotlight). Here is the fragement shader that I came up with:

varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;


void main()
{
    vec3 n, halfV, viewV, ldir;
    float NdotL, NdotHV;
    vec4 color = ambientGlobal;
    float att, spotEffect;
    n = normalize(normal);

    NdotL = max(dot(n,normalize(lightDir)),0.0);

    if (NdotL > 0.0) {

        att = 1.0 / (gl_LightSource[0].constantAttenuation +
                gl_LightSource[0].linearAttenuation * dist +
                gl_LightSource[0].quadraticAttenuation * dist * dist);
        color += att * (diffuse * NdotL + ambient);

        halfV = normalize(halfVector);
        NdotHV = max(dot(n,halfV),0.0);
        color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);

        spotEffect = dot(normalize(gl_LightSource[1].spotDirection), normalize(-lightDir));
        if (spotEffect > gl_LightSource[1].spotCosCutoff) {
            spotEffect = pow(spotEffect, gl_LightSource[1].spotExponent);
            att = spotEffect / (gl_LightSource[1].constantAttenuation +
                    gl_LightSource[1].linearAttenuation * dist +
                    gl_LightSource[1].quadraticAttenuation * dist * dist);

            color += att * (diffuse * NdotL + ambient);


            halfV = normalize(halfVector);
            NdotHV = max(dot(n,halfV),0.0);
            color += att * gl_FrontMaterial.specular * gl_LightSource[1].specular * pow(NdotHV,gl_FrontMaterial.shininess);
        }

    }

    gl_FragColor = color;
}

PS: Surely this is a problem that has been solved…. Anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:15:10+00:00Added an answer on June 7, 2026 at 9:15 am

    Here is what I came up with:

    The vertex shader:

    varying vec3 N;
    varying vec3 v;
    void main(void)  
    {     
       v = vec3(gl_ModelViewMatrix * gl_Vertex);       
       N = normalize(gl_NormalMatrix * gl_Normal);
       gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
    }
    

    And the fragment shader:

    varying vec3 N;
    varying vec3 v;
    #define MAX_LIGHTS 2 
    void main (void)  
    {  
       vec4 finalColour;
    
       for (int i=0; i<MAX_LIGHTS; i++)
       {
           vec3 L = normalize(gl_LightSource[i].position.xyz - v);   
           vec3 E = normalize(-v);
           vec3 R = normalize(-reflect(L,N));  
           vec4 Iamb = gl_FrontLightProduct[i].ambient;    
           vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);
           Idiff = clamp(Idiff, 0.0, 1.0);     
           vec4 Ispec = gl_FrontLightProduct[i].specular 
                        * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
           Ispec = clamp(Ispec, 0.0, 1.0);
           finalColour += Iamb + Idiff + Ispec;
       }
       gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColour;
    }
    

    Which give this image:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two forms on one page and want to have the input boxes
I want to have two button in the same size one in the left
I want to have two input with one button for user to enter their
I want to have two Emacs windows on the screen: one for Dired and
I have two sites with different SITE_IDs, but I want to have only one
I have a UITextView and I want have two buttons. When the user taps
I want to have two columns on the bottom of this webpage I'm making,
I want to have two arrays: supplierList - that contains the int ID of
I want to have two EntityManagerFactories ( org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean in this case), with each of
In short: I want to have two fullscreen views, where I can switch between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.