Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

elis-cool

Raytraced diffuse lighting...

This topic is 5672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, im working on a ray tracer... but im not sure how to correctly calculate the lighting... ive googled, but there isnt very much at all on the subject... this is what i Have so far:
    
// ...

if(pHitObject)
{
    CVECTOR3 vecNormal = pHitObject->vecCenter - vecHitLocation;
    // It can only do spheres at the moment :)

    vecNormal.x /= pHitObject->radius;
    vecNormal.y /= pHitObject->radius;
    vecNormal.z /= pHitObject->radius;

    // this is the part that doesnt work, ive read this way, and a way using the cross product

    CVECTOR3 vecLighting = vecHitLocation - vecPointLightPos;
    vecLighting.Normalize();
    float fAngle = vecLighting.Dot(vecNormal);

    dwPixelColor = // what goes here??

}
else
{
    dwPixelColor = 0x00000000;
}
    

MasterDepot [edited by - elis-cool on February 9, 2003 6:08:38 AM]

Share this post


Link to post
Share on other sites
Advertisement

float fAngleCosine = vecLighting.Dot(vecNormal);

The dotproduct doesn''t give you the angle, but the cosine of the angle. And that value happens to be directly the lighting factor you need:

RGB PixelColour = (fAngleCosine <= 0) ? RGB(0, 0, 0) : fAngleCosine * ColourOfHitObject * ColourOfLight * DistanceAttenuation ...etc... ;

dwPixelColor = ConvertToDWORD(PixelColour);

Share this post


Link to post
Share on other sites
Thanks Yann, I pretty much had that... but im not sure about the color values, like are they meant to be in the range 0.0 - 1.0? how do I convert a dword color to and from that range?


MasterDepot

Share this post


Link to post
Share on other sites
Best thing would be to keep all colours in the 0..1 range. Then, at the very last step (when drawing a pixel), convert them to the 0..255 range.

So your RGB PixelColour would have 3 floating point components (add alpha, if you like/need), each one ranging from 0 to 1.

To convert those into a DWORD, you need to know the layout of the colour components. That depends on your particular API. The conversion is just a range remap from 0..1 to 0..255, and then some bitshifting.

If your DWORD component layout was eg. ARGB, then the conversion would look something like this:

PixelColour *= 255.0f; // multiply all components with 255 (make sure that they are clamped to 0..1 first !
DWORD = ((int)PixelColour.a << 24) | ((int)PixelColour.r << 16) | ((int)PixelColour.g << 8) | ((int)PixelColour.b);


Share this post


Link to post
Share on other sites
Thanks yann, but is fAngleCosine meant to be going over 1.0? it seems to be in the range 0.0 - ~5.0, so when its multiplying the colors together it overflows the 1.0 boundry, and so the would be bright spot on the sphere ends up with other colors... but if I cap it to 1.0 then and there (instead of at the last convert to dword step) then the bright spot ends up as just one big flat shaded color in the middle then it shades off to black around the edges...

500x1


MasterDepot

Share this post


Link to post
Share on other sites
If vecNormal is a normalized vector, it should not be returning 5.. it should return a value between 0 and 1.

-1 0 0 dot -1 0 0 = 1
0 0 0 dot 0 0 0 = 0
0 1 0 dot 0 1 0 = 1
.5 .5 0 dot .5 .5 0 = .5
-.5 0 .5 dot -.5 0 .5 = .5
As you can see, there is no way for a normalized vector dot producted with itself to be outside the 0 -> 1 range.

Share this post


Link to post
Share on other sites
ok, so I normalized the normal vecter, I thought this was supposed to normalize it though, it cant have been...

CVECTOR3 vecNormal = pHitObject->vecCenter - vecHitLocation;
// It can only do spheres at the moment
vecNormal.x /= pHitObject->radius;
vecNormal.y /= pHitObject->radius;
vecNormal.z /= pHitObject->radius;

well anyway, its working now...


MasterDepot

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!