PDA

View Full Version : Shadows and also a light source question



Andrew Davey
05-03-2004, 11:07 PM
Is there an easy way to implement shadows, i am only using lines to draw my objects, i.e. a collection of lines to implement a tree.

Also in my "world" i have a sun, i was wondring if i could use this sun as my light source, and if the sun moved the light source would also be different, this light source will hopefully be used to work out and draw the shadows.

This is what i guess the prog will have to know in order to draw the shadow.

1: The plane to draw on, i.e. y axis is 0
2: The angle of the sun
3: The position of the lines

The prog i guess will then work out the new length of the shadow depending on the suns angle in relation to the current line.

Am i thinking in the right way?? if so what do i have to do to implement this?

Remeber i am using nothing complex as my object just plain simple 3d lines.

plasmonster
05-03-2004, 11:30 PM
Andrew,

you are on the right track. Here's a simple solution:

P' = P - (dot(P,N)/dot(S,N)) * S.

where
P'= new vertex position on the shadow plane
P = original vertex position
S = direction of sun light (unit vector)
N = normal of shadow plane (distance is assumed to be 0)

So, you could project each line vertex using this equation, and then blend the projected line in black, for example, to simulate a shadow.

edit:

If your shadow plane is the plane y=0, then you could simplify all this to:

P' = P - (P.y / S.y) * S.

Andrew Davey
05-03-2004, 11:37 PM
And the dot, i gues this a maths thing that i can see on ultimategameprogramming as i remeber seeing it somewhere in a "tutorial", its something to do with the normal is it not?

plasmonster
05-03-2004, 11:44 PM
Oops! Yeah, it's a math thing. An easy one though.

dot(A,B) = A.x*B.x + A.y*B.y + A.z*B.z

A normal vector N is any vector divided by its length:

N = V / length(V).

The length of a vector is: length(V) = sqrt( dot(V,V) ).

Andrew Davey
05-03-2004, 11:52 PM
so just quickly if my vector, i.e. branch has these coords:

start.x = 0
start.y = 0
start.z = 0

direction.x = 0
direction.y = 1
direction.z = 0

(This is a straight line going up)

and a length of 4

what would the vector be? i guess it will be ( start.x + (direction.x * length) )+ ( start.y + (direction.y * length) ) + ( start.z + (direction.z * length) ) / length;

???? As start.x * direction.x * length is where the line ends.

Andrew Davey
05-03-2004, 11:56 PM
sorry i think i understood your message wrong, i have no re read it and i understand whats happoening, funnily enough i actually use the same thing somewhere in my program.

D'oh!!!

plasmonster
05-04-2004, 12:01 AM
If your shadow plane is the plane y=0, then you could simplify all this to:

P' = P - (P.y / S.y) * S

or without the vector notation

P.x' = P.x - (P.y / S.y) * S.x
P.y' = P.y - (P.y / S.y) * S.y
P.z' = P.z - (P.y / S.y) * S.z

This will work for any point P in your world.

Andrew Davey
05-04-2004, 12:15 AM
gotcha cheers will try that and let you know.