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

  • Home
  • SEARCH
  • 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 3405104
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:27:52+00:00 2026-05-18T05:27:52+00:00

I have been trying to create a simple point light in HLSL for a

  • 0

I have been trying to create a simple point light in HLSL for a couple of days now, I have been following this guide:
D3DBook:(Lighting) Direct Light Sources

Specifically the attenuation function and section on point lights. Here is my code:

    //------------------------------------------------------------------------------------------------------
// Global params provided by the app
//------------------------------------------------------------------------------------------------------
uniform extern float4x4 gWVP;
uniform extern float4x4 gWorldViewIT; //for normals
uniform extern float4x4 gWorldView;//work out view vector from all vertices
uniform extern float4x4 gLightMatrix;//light in view space
uniform extern float4x4 gWorld;

uniform extern float4 gDiffuseMtrl;
uniform extern float4 gAmbMtrl;
uniform extern float4 gSpecMtrl;

//lights
uniform extern float4 gLightCol = {1, 1, 1, 1}; //set to white
//uniform extern float4 gLightPos[4];
uniform extern float4 gLightPos[4] = {{0,0,3,0},{0,0,-1,0},{0,100,1,0},{0,-100,0,0}};
uniform extern float gLightPow[4] = {25,25,0.1,0.1};//range of light


uniform extern float roughness;//roughness per object
uniform extern float ref;//reflectance at normal//used in fresnel calculation

//pi
const shared float pi= 3.14159f; 

//attenuation constants
const float a_a = 0.0f;
const float a_b = 0.1f;
const float a_c = 1.0f;


//calculate light attenuation
float atten( float distance, float range, float a, float b, float c)
{
    float atten = 1.0f / ((a * distance * distance) + (b * distance) + c );

    //step clamps to 0 if out of range
    return step(distance, range) * saturate( atten );
}


//---------------------------------------------------------------------------------------------------------------------------
// Input channel (vertex shader)
//---------------------------------------------------------------------------------------------------------------------------
struct InputVS
{
    float3 posL : POSITION0;
    float3 Norm : NORMAL;

};  
//---------------------------------------------------------------------------------------------------------------------------
// Output channel (vertex shader)
//---------------------------------------------------------------------------------------------------------------------------
struct OutputVS
{
    float4 posH     : POSITION0;
    float4 col      : COLOR0;
};

//vertex shader//passthrough
OutputVS gourardVS(InputVS input)
{
    float n = 1/roughness;
    //Zero out our output
    OutputVS outVS = (OutputVS)0;

    //Transform to homogeneous clipspace
    outVS.posH = mul(float4(input.posL, 1.0f), gWVP);

    float4 col_amb = gAmbMtrl*gLightCol;
    float4 col_diff = gDiffuseMtrl*gLightCol;
    float4 col_spec = gSpecMtrl*gLightCol;

    //ambient term  
    float4 ambient={0.2, 0.2, 0.2, 1.00f};

    float4 finalColour=0;

    float diff = 0;

    float3 pWorld = mul( float4( input.posL, 1.0f ), gWorld).xyz;

    //normal
    float3 N =normalize(mul(float4(input.Norm, 1.0f),gWorldViewIT));

    //point lights
    float attenu = atten(distance(pWorld, gLightPos[1]), gLightPow[1], a_a, a_b, a_c);

    float3 l_dir= normalize(gLightPos[1] - pWorld);

    //n dot l
    float dotNL =  max(0, dot(N, l_dir));

    finalColour = float4( attenu * dotNL * col_diff);
    //}
    outVS.col = finalColour;// + (ambient * col_amb);
    outVS.col.a = 1;


    //return 
    return outVS;
}
//---------------------------------------------------------------------------------------------------------------------------
// Input channel pixel shader
//---------------------------------------------------------------------------------------------------------------------------
struct InputPS{
    float4 posH     : POSITION0;
    float4 col      : COLOR0;
};

float4 noPS(InputPS input): COLOR
{
return input.col;
}

technique Phong{
    pass P0
    { 
            Lighting       = TRUE;
            SpecularEnable = TRUE;
        vertexShader = compile vs_3_0 gourardVS();
        pixelShader = compile ps_3_0 noPS();
        //specify render device states associated with the pass
        //FillMode = WireFrame;
        //ShadeMode = Gouraud;
    }
}

I’m pretty sure the matrices being passed in are correct as I have repurposed this from a directional light example, so this only leaves the HLSL code as the source of the problem. This outputs the correct vertices but they are almost unlit no matter what values I use on the light position and Power arrays.

  • 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-18T05:27:53+00:00Added an answer on May 18, 2026 at 5:27 am

    I finally found the problem, the light direction is in world space while the normals are in View space, I had forgot to transform the light direction by the view matrix(gLightMatrix) here is the revised code:

    ...
    float3 l_dir= normalize(gLightPos[1] - pWorld);
    
    float3 L=normalize(mul(l_dir, gLightMatrix));
    
    //n dot l
    float dotNL =  max(0, dot(N, L));
    
    finalColour = float4( attenu * dotNL * col_diff);
    ...
    

    Thank you anyway, everyone who had a look.

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

Sidebar

Related Questions

I have been trying to get around this error for a day now and
I have been learning Prism and Silverlight and am now trying to create a
I am new to XML and have been trying some simple examples and they
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM
I have been trying to work my way through Project Euler, and have noticed
I have been trying to explain the difference between switch statements and pattern matching(F#)
I have been trying to read a picture saved in Access DB as a
I have been trying to learn multi-threaded programming in C# and I am confused

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.