Archived

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

Andos

Raycasting Formula?

Recommended Posts

Andos    124
I''m making a 3D engine and I want some lights. My prob here is that the light also appears on the other side of my 3D object. I have made a light formula and I only want the light to apear on the side nearest the light source. The Raycasting formula?

Share this post


Link to post
Share on other sites
coelurus    259
I assume you use the dot product to calculate light? In that case, if the result is negative (or positive), the triangle-face is facing away (just like hidden surface removal), and you should not lit the face.

Share this post


Link to post
Share on other sites
SwSh    145
Generally, if you don''t use the api light system itself, (like OpenGL or DX lighting) you should first find the normal of your plane (triangle, quad, whatever) and the vector from the middle of that plane to the light. (you can make it faster by chosing one of the vertices and not the middle)
The dot product of these two normalized vectors should give you the cosine of the lightray falling on the plane. If it''s negative, the light is behind so the poly is not lit. If it''s positive, the light shines somewhere in front of your poly and you should compute how much it affects the polygon color. (using the cosine ofcoz)
Now sth about the dot product:
suppose you have the normalized (length = 1) normal vector: N
and the vector from your polygon to the light: L
N = (nx, ny, nz) L = (lx, ly, lz)
The dot product: Dot(N, L) should be computed as:
DotProduct = nx*lx + ny*ly + nz*lz
Got it? ;-)

I advise you to search some good tutorials on lighting. There are many.

Greetings

Share this post


Link to post
Share on other sites
Andos    124
You see... I''m from denamrk and only 16 years old, so I''m having a little troubble understanding what you are meaning. You are right. I''m trying to make a non textured 3D engine with the way of lightning you explained. I''ve made a formula for this. I''t quite complicated but i''s used to get the percent og lightning of the colour brightness. 0%= black 100%=white 50%=(whatever)

Share this post


Link to post
Share on other sites
Andos    124
You see... I''m from Denmark and only 16 years old, so I''m having a little troubble understanding what you mean. You are right. I''m trying to make a non textured 3D engine with the way of lightning you explained. I''ve made a formula for this. I''t quite complicated but i''s used to get the percent og lightning of the colour brightness. 0%= black 100%=white 50%=(whatever)
My formula is:
(( Tan-1(( Ya – Yb)/(Xa – Xb))+90)/ Tan-1((((Xa+Xb)/2)-Xlight)/(((Ya+Yb)/2)-Ylight))*100/2)+(( Tan-1(( Za – Zb)/(Xa – Xb))+90)/ Tan-1((((Xa+Xb)/2)-Xlight)/(((Za+Zb)/2)-Zlight))*100/2)

This gives the percent of the x and z divided by 2 and added to the percent of x and z and divided by 2.
I hope youunderstand what I mean.
Can you explane what vectors is? Is it the angle between to spots? I don''t know.

Share this post


Link to post
Share on other sites
SwSh    145
Whoooow, complicated formula :D
Well a vector in the 3 dimentional space is (generally) defined by three coördinates; the x, y, and z.
Imagine a point in space with these coörds. A vector with these coördinates would be the direction to the point from the origin. (the origin is always at x = 0, y = 0, z = 0)

Unfortunately for you (God bless for me :-D), I''m not a teacher.
I think it should be a good idea to ask your mathematics-teacher more explanation about vectors and points ''cause I''m really bad in explaining things (especially in english, lol)
I advise you to look at http://www.gamedev.net/community/forums/topic.asp?topic_id=86063
they''re also talking about points and vectors.

Share this post


Link to post
Share on other sites
coelurus    259
Gee, you''ve worked on that one, didn''t you? Am as old as you (I live in Sweden too ), and I haven''t done something like that! (Mostly because I wouldn''t think it was worth the work with all those trigs, phew!).

I''ve learnt you''re using some sort of Click-programming, right? How are your geometry and lightsources stored? How do they work? Can you read vertices and manipulate them?

If you answer these questions, we might give u some more help.

Share this post


Link to post
Share on other sites
Andos    124
It took me 45 minutes to make that one...
I''m planning to make several lists. One for X one for Y and one for Z and one that sorts them in a ways i can use when i shall render the screen. (No textures only coloures) If I can understand the point in polygon thing i can render the screen.

I''m thinking of make a database (made automatickley when making the 3D objects) so that my engine can detect what the ID is for the vertices in the polygon.

Example on my light formula:


Polygon(side)
|
|
| (L)ight
|
|
Result= 100% light on the surface

Polygon(side)
\
\
\ (L)ight
\
\
Result= 75% light on hte surface
because the side was twisted.


---------- (L)ight

Result= 0% light on the surface


(This is in 2D but my formulais in 3D)

Share this post


Link to post
Share on other sites
Andos    124
It took me 45 minutes to make that one...
I''m planning to make several lists. One for X one for Y and one for Z and one that sorts them in a ways i can use when i shall render the screen. (No textures only coloures) If I can understand the point in polygon thing i can render the screen.

I''m thinking of make a database (made automatickley when making the 3D objects) so that my engine can detect what the ID is for the vertices in the polygon.

Example on my light formula:


Polygon(side)
|
|
| (L)ight
|
|
Result= 100% light on the surface

Polygon(side)
\
\
\ (L)ight
\
\
Result= 75% light on hte surface
because the side was twisted.


---------- (L)ight

Result= 0% light on the surface


(This is in 2D but my formulais in 3D)

Share this post


Link to post
Share on other sites
coelurus    259
Bulb light? Spreading light everywhere?

Here''s a little help on that:

First, calculate the normal of the polygon to draw (vector perpendicular to polygon):

nx = (y1 - y0) * (z2 - z0) - (y2 - y0) * (z1 - z0)
ny = (x1 - x0) * (z2 - z0) - (x2 - x0) * (z1 - z0)
nz = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)

(Plz correct if something''s wrong above)

Normalize normal (length = 1):
length = sqrt(nx^2 + ny^2 + nz^2)
nx = nx / length
ny = ny / length
nz = nz / length

Get vector from light to centroid (center of polygon):
lx = (x0 + x1 + ... xn) / num_points - lightX
ly = for y
lz = for z

Normalize vector:
The same as with the normal, just a few changes

Lighting:
color = original_color * (nx * lx + ny * ly + nz * lz)

Haven''t tried this, but it should work quite well. Note that hidden surface removal can be performed, by checking the sign on "nz". If it''s positive, it''s visible, or vice versa. Try it out, it will cut down your number of polygons to draw with about 50%

This was a brute force method, with perfect calculations (if something''s note wrong). Try optimize it!

Share this post


Link to post
Share on other sites