Raycasting Formula?

Started by
10 comments, last by Andos 22 years, 1 month ago
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)
Advertisement
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!

This topic is closed to new replies.

Advertisement