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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:06:33+00:00 2026-05-19T17:06:33+00:00

I’m trying to accomplish a spotlight effect with Cg. I’ve already managed to do

  • 0

I’m trying to accomplish a spotlight effect with Cg.
I’ve already managed to do normal ambient and diffuse lighting.

I understand the basic functionality of a spotlight (position, direction, cutoff angle), yet handling these in Cg still eludes me.

This is how I compute the spotlight parameters:

float4 dir_aux = mul(ModelViewProj, direction);
float4 lightP_aux = mul(ModelViewProj, lightPosition);

float3 lightP = lightP_aux.xyz;
float3 dir = normalize(dir_aux.xyz);

float3 P = IN.position;
float3 V = normalize(lightP - P);
dir = normalize(lightPosition - dir);

float angle = dot(V, dir);

direction is the pixel the spotlight is pointed to ( ex: (0, 0, 0) )

lightPosition is the position of the light

P is the point i am trying to light. IN.position comes from the vertex shader, and it’s already multiplied with modelViewProj.

angle is the cosine of the angle between the direction of the light and the direction from the light, to the point I’m trying to light.

The problem is that changing the direction of the light does not affect the spotlight whatsoever.It will always be centered in 0,0,0.
If I change lightPosition, the spotlight changes, but it still starts from 0,0,0 and expands oppsite the position of the light

Another thing is that when I compute the direction vector, I use lightPosition, instead of lightP.If I use lightP, the spotlight won’t work at all.

Also the spotlight only lights on half of the scene.

My main reference for this was Chapter 5(Lighting), from The Cg Tutorial.

  • 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-19T17:06:34+00:00Added an answer on May 19, 2026 at 5:06 pm

    Here’s my take on this, I think your vectors are pointing in the wrong directions (when you do the subtractions):

    // direction is actually the location of the target
    float4 target = direction; // renamed for clarity   
    
    float4 target_aux = mul(ModelViewProj, target);
    float4 lightP_aux = mul(ModelViewProj, lightPosition);
    
    float3 lightP = lightP_aux.xyz;
    float3 targetXYZ = target.xyz;
    
    // don't normalise this it's a location at this point, NOT a direction vector
    //float3 dir = normalize(dir_aux.xyz);
    
    float3 P = IN.position;
    // reversed to give vector from light source to IN.Position
    float3 V = normalize(P - lightP);
    
    // reversed to give vector from light source to target
    float3 dir = normalize(targetXYZ - lightP);
    
    float angle = dot(V, dir);
    

    A bit late, but I hope that helps 🙂

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.