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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:14:47+00:00 2026-06-05T07:14:47+00:00

I have a diffuse lighting shader which seems to work when the object is

  • 0

I have a diffuse lighting shader which seems to work when the object is not rotating. However, when I apply rotation transform, the light also seems to rotate along with the object. It’s like the object and the light stay still but the camera is the one that moves around the object.

Here’s my vertex shader code:

#version 110

uniform mat4 projectionMatrix;
uniform mat4 modelviewMatrix;
uniform vec3 lightSource;

attribute vec3 vertex;
attribute vec3 normal;

varying vec2 texcoord;

void main() {
    gl_Position = projectionMatrix * modelviewMatrix * vec4( vertex, 1.0 );

    vec3 N = gl_NormalMatrix * normalize( normal );
    vec4 V = modelviewMatrix * vec4( vertex, 1.0 );
    vec3 L = normalize( lightSource - V.xyz );

    float NdotL = max( 0.0, dot( N, L ) );

    gl_FrontColor = vec4( gl_Color.xyz * NdotL, 1.0 );

    gl_TexCoord[0] = gl_MultiTexCoord0;
}

and here’s the code that does the rotation:

scene.LoadIdentity();
scene.Translate( 0.0f, -5.0f, -20.0f );
scene.Rotate( angle, 0.0f, 1.0f, 0.0f );
object->Draw();

I sent the eye-space light position through a glUniform3f, inside the object->Draw() function. The light position is static, and defined as:

glm::vec4 lightPos( light.x, light.y, light.z, 1.0 );
glm::vec4 lightEyePos = modelviewMatrix * lightPos;
glUniform3f( uniforms.lightSource, lightEyePos.x, lightEyePos.y, lightEyePos.z );

What’s wrong with this approach?

Edit: The glm::lookAt code

Scene scene;
scene.LoadMatrix( projection );
scene.SetMatrixMode( Scene::Modelview );
scene.LoadIdentity();
scene.SetViewMatrix( glm::lookAt( glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, -5.0f, -20.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ) ) );

The SetViewMatrix code:

void Scene::SetViewMatrix( const glm::mat4 &matrix ) {
    viewMatrix = matrix;
    TransformMatrix( matrix );
}

Then I just changed the modelviewMatrix I used to the viewMatrix:

glm::vec4 lightPos( light.x, light.y, light.z, 1.0 );
glm::vec4 lightEyePos = viewMatrix * lightPos;
glUniform3f( uniforms.lightSource, lightEyePos.x, lightEyePos.y, lightEyePos.z );
  • 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-05T07:14:48+00:00Added an answer on June 5, 2026 at 7:14 am

    Statement 1: The light position is static.

    Statement 2: lightEyePos = modelviewMatrix * lightPos;

    These two claims are inconsistent. If your light position is supposed to be static, you shouldn’t be applying a rotated model matrix to it.

    If your lightPos is defined in world coordinates, then you should multiply it with the viewMatrix not the modelviewMatrix. The modelviewMatrix contains the model matrix, which contains the model’s rotation (you don’t want to apply this to a fixed light source).

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

Sidebar

Related Questions

I have browsed many online articles and adding lighting seems very straight forward but
Have deployed numerous report parts which reference the same view however one of them
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
have a help file which i have created in Notepad++ with the following syntax
I'm working on implementing deferred shading to my game. I have rendered the diffuse
So, it helps to transform everything to eye space before doing lighting calculations? I'm
I have a glutSolidTeapot (which has its surface normals generated automatically according to opengl.org)
I have one texture that has some portions which are transparent transparent I want
The tutorial has an example called the Flatten Shader, which displays the GLUT teapot
Have a problem that seems easy on paper but i'm having a big problem

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.