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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:22:43+00:00 2026-05-26T23:22:43+00:00

I have a sphere centered at C(0,0,0). For now I calculate the normal inside

  • 0

I have a sphere centered at C(0,0,0). For now I calculate the normal inside the vertex shader. I don’t pass it to it.

#version 330

layout(location = 0) in vec3 in_Position; //declare position
layout(location = 1) in vec3 in_Color;

// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 MVP_matrix;

vec3 constant_col;
vec3 normal_pos,normal_light_pos;
vec3 Light_Pos = vec3(3.0f, 2.0f, 4.0f); //Light Source
float light_magn,norm_magn;
float dot_prod;
float angle;
float Light_in;

out vec3 ex_Color;

void main(void) {

// Multiply the MVP_ matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
gl_Position = MVP_matrix * vec4(in_Position, 1.0);

//normalizing vectors 
normal_pos = normalize(in_Position);
normal_light_pos = normalize(Light_Pos);

//calculating the vector's magnitude
light_magn = sqrt(pow(normal_light_pos.x,2) + pow(normal_light_pos.y,2) + pow(normal_light_pos.z,2));
norm_magn = sqrt(pow(normal_pos.x,2) + pow(normal_pos.y,2) + pow(normal_pos.z,2));

dot_prod = dot(normal_light_pos, normal_pos); //getting the dot product
angle = dot_prod/ (light_magn*norm_magn); //getting angle in radians

angle = clamp(angle, 0, 1); 

Light_in = cos(angle); // here I calculate the light intensity

constant_col = vec3(1.0f,0.0f,0.0f); //set the sphere's color to red

ex_Color = constant_col * Light_in ; //for sphere

}

my code is based on Lambertian cosine rule, basically from here:
http://en.wikipedia.org/wiki/Lambertian_reflectance

What I get is this:
enter image description here

  • 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-26T23:22:43+00:00Added an answer on May 26, 2026 at 11:22 pm

    The scalar (=dot) product of two vectors already gives you the cosine of the angle of those vectors. So your cos(angle) is totally superfluous.

    Also one normally calculates the dot product between the surface normal, and the normalized vector from light source to point on surface. You however do a dot product between light position and normal vector, which is not correct. You want something like

    dot_prod = dot( normalize(normal_light_pos - MV * gl_Vertex, normal_pos);
    

    note that the vertex is multiplied with modelview only (not modelviewprojection).

    Seriously, you should work through some decent tutorial, there’s just to much wrong with your attempts.

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

Sidebar

Related Questions

I have a unit sphere that Is centered at the origin and my camera
I have a sphere centered on the origin of the axes. I use the
I've built this piece of code...(javascript) Now we have a red sphere on the
I have created a solid sphere with texture and without animation in Blender.I have
I have web service deployed in Web Sphere and this service is SSL enabled.
I have below a code that will plot a sphere, it's proportions are defined
Sphere() : theRadius(1.0) { } Why is it preferable to have a constructor written
Let's say I have a upright capsule shape (swept sphere) that I would like
I have such hierarchy: class Sphere; class Cube; class SpherePair; class Entity {}; class
I have a number of spherical longitude/latitude coordinates for points on a sphere that

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.