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 250959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:33:16+00:00 2026-05-11T21:33:16+00:00

I try to implement point lights in OpenGL with GLSL. I send all the

  • 0

I try to implement point lights in OpenGL with GLSL. I send all the required data to the shaders. For simplicity I only use the diffuse light here.

My example shows a huge triangle which I want to illuminate with a single light source. The light source is shown as a small blue triangle.

For diffuse light I need to know the angle between the light and the surface. Therefore, I compute the normals and the light’s direction. The dot-product will then give me a number between -1 and 1 whereas everything between 0 and 1 is illuminated.

However, here I am stuck because this angle is computed incorrectly in my example. Since it is hard to debug shaders, I used several outputs as the color of my surface. I used the normal as the color for the triangle and wherever the light was, the surface was always green. That is, the normals point up the y-axis and therefore they are computed right. That is, the light’s direction must be wrong. However, I can’t find the reason why it is wrong. The light’s position and the vertex’ position are passed in world space to the vertex shader, which then transforms them to eye space, compute the light’s direction and pass this to the fragment shader:

vertex shader:

vec4 pos = modelview_matrix * <input vertex>;
vec4 lightPos = modelview_matrix * <light position>;

vec3 lightDir = normalize(lightPos.xyz - pos.xyz);

fragment shader:

float NdotL = max(dot(normalize(<correct normal>), normalize(lightDir)), 0.0);
gl_FragColor = vec4(1.0, NdotL, 0.0, 1.0);

I’ve appended some pictures of this example which definitely show, that the interpolation is really weird. I know want to know whether the code I pasted is correct or whether you need to know more of the code. FUrthermore, is this interpolation behaviour normal or is it a bug in my code?

http://img41.imageshack.us/img41/3566/interpolation.png
http://img189.imageshack.us/img189/3566/interpolation.png

Especially the first picture shows that the center spot of the light is NOT directly under the light but slightly moved to the center of the triangle.

If I pass “pos” to the fragment shader (that is, it is interpolated) and then compute “lightDir” in the fragment shader, everything works fine.

  • 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-05-11T21:33:17+00:00Added an answer on May 11, 2026 at 9:33 pm

    I think that normalizing your lightDir in the vertex shader affects the result you get when interpolating the vectors because it takes the length out of the equation and thus affects the “speed” of the interpolation.

    If you want a (visual) explanation, try to draw it in 2D on paper. First draw a line (your triangle) and a point (light source). Add two lines (lightDir) for both ends of the line to the light source and then shrink (normalize) those lightDir lines so that they have the same length and both meet at the light source. If you connect both ends of the normalized lightDir lines you will get a non-parallel line (if the light source isn’t exactly in the middle of your first line).
    Now if you draw a line that goes exactly through the middle of one of the lines you will see that it doesn’t go through the middle of the other line but will hit it a bit to the left or right.

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

Sidebar

Related Questions

No related questions found

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.