Graphics Libary programming
Before i'll start, I want to tell you first, what this tutorial will cover:
- How to calculate the Screen-Coordinates of a 3D Point
- How to Draw filled triangles
- Sorting of triangles
- Short Description how Screen-To-World Rasterisation works
Other topics will be included in upcoming tutorials ;)
Well, anyway, let's start then, shall we? Yes we shall.
World-To-Screen Rasterisation
Firstly, some might want to know why one would rasterize a triangle or any
other 3D-object. Without rasterising (rasterizing??? i forgot ;))) a object would
look very very strange... Imagine you living in a world where you can't keep the
near and the far apart, and you think that everything has the same distance.
Trust me, you don't want to live in such a world. That's why we want to
rasterize ;). Here we go:
Since the Person sitting infront of the screen is mostly the one viewing the
stuff thats on it, i will refer to that person as the Viewer. And the distance from
the viewer to the screen will be called the focus, some constant. (Like in cameras)
And the distance from the viewer to the Object will be called Z, or depth.
This object mostly has a height and width, wich we will call X (Width) and Y(height)
So now all we need is the Width and Height of the object for the screen.
What we know is this, that the screen X and Y - Coordinates are on the same line
from the viewer as the Real - X and Y - Coordinates. This means:
I Hope you can recognize what the image is trying to show ;). Well... the upper
Diagonal line is the one i mentioned before, wich passes the Real X or Y Coord
and the X or Y Screen Coord.
In school the teacher must have told you about rays and their proportions. ;)
well, anyway. From now on i will call the X-Screen-Coord = i and the Y-Screen
-Coord = j. The object Y-Coord and so on will keep their names ;). From the
Rays we know 1 thing, that i/focus = x/z OR j/focus = y/z. from this we know that
i = focus * x/z AND j = focus * y/z. tada. now writing a program that displays
a wireframe would be easy, just rasterise all points then connect them ;).
But nobody realy likes wireframes all to much altough there are exceptions ;)
That's why we want to draw filled triangles, altough textured once are nicer
but filled polygons are more "important", and are needed for texturing ;).
Filled Polygons
Drawing filled Polygons isn't all too hard, and can be done in a few simple
steps. First we Draw a wireframe to a Temporary buffer, than we start at the
top of the Buffer and on every level of the Buffer we go from Left to Right.
And once we intersect with the wireframe, we know we are in the Triangle. If
we intersect again, we know we are out of the triangle ;). For more Information
Download the sample file ;).
Screen-To-World (Ray-tracing)
Well, Screen-To-World is very simple actualy, the only problem is, it is very
slow, and can hardly be done in Realtime. It is also called Ray-Tracing. The
Advantage of it is, it is very accurate and pretty, and you can do very nive
Lighting effects, like shadows and reflections. The difference to World-To-Screen
is that instead of calculating what Screen Coordinates a Object has, You calculate,
What Object belongs to a certain screen Pixel. :)) Ray-Tracing Spheres is very easy,
and almost anybody can do it.you start from One Point, wich is near to the Screen,
and Check if it intersects with any objects. The Ray should start at:
i/focus | j/focus | 1 and it should end at:
MaxRange*i/focus | MaxRange*j/focus | MaxRange. The Points are in this format:
X|Y|Z. If you want to know how cool Ray-tracing is, just watch any 3D movie.
Best example so far is Final Fantasy i think ;). By the way. to check if a point
is in a Sphere, you just calculate the Distance between the Point and the Middle of
the sphere, and if the distance is smaller than the radius, than the point is in
the sphere. to calculate the Distance you just use following formula:
SQR((X2-X1)^2+(Y2-Y1)^2+(Z2-Z1)^2). this calculates the distance
between any 2 Points in 3D, and also 2D, just remove the (Z2-Z1)^2 part ;).