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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:16:32+00:00 2026-06-12T05:16:32+00:00

I am implementing a diffuse per-vertex shader from the OpenGL 4.0 Shading Language Cookbook,

  • 0

I am implementing a diffuse per-vertex shader from the OpenGL 4.0 Shading Language Cookbook, but have modified slightly in order to fit my project.

This is my vertex shader code:

layout(location = 0) in vec4 vertexCoord;
layout(location = 1) in vec3 vertexNormal;

uniform vec4 position; // Light position, initalized to vec4(100, 100, 100, 1)
uniform vec3 diffuseReflectivity; // Initialized to vec3(0.8, 0.2, 0.7)
uniform vec3 sourceIntensity; // Initialized to vec3(0.9, 1, 0.3)

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

out vec3 LightIntensity;

void main(void) {
    // model should be the normalMatrix here in case that
    // non-uniform scaling has been applied.
    vec3 tnorm = normalize(vec3(model * vec4(vertexNormal, 0.0)));

    // Convert to eye/camera space
    vec4 eyeCoordsLightPos = view * model * position;
    vec4 eyeCoords = view * model * vertexCoord;
    vec3 s = normalize(vec3(eyeCoordsLightPos - eyeCoords));

    // Diffuse shading equation
    LightIntensity = sourceIntensity * diffuseReflectivity * max(dot(s,tnorm), 0.0);

    gl_Position = projection * view * model * vertexCoord;
}

And this is my fragment shader:

in vec3 LightIntensity;

layout(location = 0) out vec4 FragColor;

void main(){
    FragColor = vec4(LightIntensity, 1.0);
}

I am rendering a box, and I have values for all normals. However the box is just black.
Assuming my uniforms are set correctly on rendering, is there anything obviously wrong with my shader code?

What I have changed compared to the code from the book is I use the model matrix as a normal matrix and I have my light position passed in model space, but convert it to camera space in the shader.

Image of rendering, do not be confused by the shaded background which is a texture:
Image of rendering, do not be confused by the shaded background which is a texture.

Picture when rendering with FragColor = vec4(f_vertexNormal, 1.0); in fragment shader:
enter image description here

Updated code with tnorm in eye space suggestion:

void main(void) {
    // model should be the normalMatrix here in case that
    // non-uniform scaling has been applied.
    vec3 tnorm = normalize(vec3(model * vec4(vertexNormal, 0.0)));

    // Convert to eye/camera space
    vec3 eyeTnorm = vec3(view * vec4(tnorm, 0.0));
    vec4 eyeCoordsLightPos = view * position;
    vec4 eyeCoords = view * model * vertexCoord;
    vec3 s = normalize(vec3(eyeCoordsLightPos - eyeCoords));

    // Diffuse shading equation
    LightIntensity = sourceIntensity * diffuseReflectivity * max(dot(s,eyeTnorm), 0.0);

    gl_Position = projection * view * model * vertexCoord;
    f_vertexNormal = vertexNormal;
}
  • 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-12T05:16:34+00:00Added an answer on June 12, 2026 at 5:16 am

    In addition with my comment above, you’re dot-ing a world space vector tnorm with an eye space vector s. You should only dot vectors in the same space. So you might want to consider transforming tnorm to eye space as well.

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

Sidebar

Related Questions

I'm working on implementing deferred shading to my game. I have rendered the diffuse
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
Im implementing NFS and almoste done but the RFC section 3.3.8 says this in
Does implementing the Zend Framework require the existing MySQL database to be modified? If
Implementing slides.js on our site, I seem to have somehow caused the actual sliding
Implementing page not found in django and have looked at the documentation 404 I
I implementing OLE DB provider for my custom database. It will be used from
While implementing the code from this question on my project I realized there's 3
Implementing an app where the user can log in I have the following situation:

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.