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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:54:51+00:00 2026-05-20T21:54:51+00:00

I’m tweaking the shader examples in OpenGL Superbible 2010 to learn about shader usage.

  • 0

I’m tweaking the shader examples in OpenGL Superbible 2010 to learn about shader usage.

I wanna change the red component of a directional light that’s passed as a uniform to a diffuse light shader. The vector

GLfloat vDiffuseColor [] = {redDirectionalLight, greenDirectionalLight, blueDirectionalLight, 1.0f};

is defined globally.

I can tweak the light color if I specify the variables before compiling but not when the code is running. The glut keyboard function that I’m using does increment the color values at runtime, but the modified uniform isn’t being reloaded. In the SetupRC function I locate the position of the diffuseColor uniform

locColor = glGetUniformLocation(DirectionalLightSimpleDiffuseShader, "diffuseColor");

What I’m missing here?

vertex program:
#version 130

// Incoming per vertex... position and normal
in vec4 vVertex;
in vec3 vNormal;

// Set per batch
uniform vec4    diffuseColor;   
uniform vec3    vLightPosition;
uniform mat4    mvpMatrix;
uniform mat4    mvMatrix;
uniform mat3    normalMatrix;

// Color to fragment program
smooth out vec4 vVaryingColor;

void main(void) 
    { 
    // Get surface normal in eye coordinates
    vec3 vEyeNormal = normalMatrix * vNormal;

    // Get vertex position in eye coordinates
    vec4 vPosition4 = mvMatrix * vVertex;
    vec3 vPosition3 = vPosition4.xyz / vPosition4.w;

    // Get vector to light source
    vec3 vLightDir = normalize(vLightPosition - vPosition3);

    // Dot product gives us diffuse intensity
    float diff = max(0.0, dot(vEyeNormal, vLightDir));

    // Multiply intensity by diffuse color
    vVaryingColor.rgb = diff * diffuseColor.rgb;
    vVaryingColor.a = diffuseColor.a;

    // Let's not forget to transform the geometry
    gl_Position = mvpMatrix * vVertex;
    }

fragment program:

#version 130

out vec4 vFragColor;
smooth in vec4 vVaryingColor;

void main(void)
   { 
   vFragColor = vVaryingColor;
   }

Here’s the RenderScene code, which is called via glutDisplayFunc:

void RenderScene (void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



    modelViewMatrix.PushMatrix(); 

    M3DMatrix44f mCamera; // CAMERA MATRIX

       cameraFrame.GetCameraMatrix(mCamera);
    modelViewMatrix.PushMatrix(mCamera);  


    M3DMatrix44f vLightPos= {0.0f, 10.0f, 5.0f, 1.0f};
    M3DVector4f  vLightEyePos;
    m3dTransformVector4 (vLightEyePos, vLightPos, mCamera);


    shaderManager.UseStockShader (GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),
                                  vFloorColor);


    floorBatch.Draw();

    for (int i=0; i<NUM_SPHERES; i++){

         modelViewMatrix.PushMatrix(); 
         modelViewMatrix.MultMatrix(spheres[i]); 


            glUseProgram (DirectionalLightSimpleDiffuseShader);
            glUniform4fv (locColor, 1, vDiffuseColor); 
            glUniform3fv (locLight, 1, vEyeLight);
            glUniformMatrix4fv (locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix());



            glUniformMatrix4fv (locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix());
            glUniformMatrix3fv (locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix());


            sphereBatch.Draw();
        //  glUseProgram(0);
            modelViewMatrix.PopMatrix(); 
    }
  • 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-20T21:54:52+00:00Added an answer on May 20, 2026 at 9:54 pm

    Please tell me you’re changing vDiffuseColor and not redDirectionalLight, greenDirectionalLight, blueDirectionalLight.

    GLfloat vDiffuseColor [] = {redDirectionalLight, greenDirectionalLight, blueDirectionalLight, 1.0f};
    

    makes an array by copying the initializer, there is no connection to the original three variables.

    If you want to address the three component by name, instead use (assuming C++):

    GLfloat vDiffuseColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
    GLfloat&   redDirectionalLight = vDiffuseColor[0];
    GLfloat& greenDirectionalLight = vDiffuseColor[1];
    GLfloat&  blueDirectionalLight = vDiffuseColor[2];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.