Algorithms
_________
Prepared based on materials collected from
corners of the world's software by criminal organizations, I
mean Hackers Group. Otfikseno under standard Odyssey by Phantom
Lord.
3d Basic - Just about the 3D
______________________
For a start, lets look at some of the 3d
basics. At first, what is 3d? Well, 3d is
short for three dimensional, a dimension
we often say we as people live in. Now,
lets look at some of the other dimension,
maybe this would help us understanding 3d.
One dimensional are just a line, and our
position on this line are represented by
the letter x.Two dimensional are represented by the letters x
and y. This is what we know as a coordinatesystem, x goes from
left to right and y from lower to upper.
This is actually also our screen. Now, the
third dimension are represented by x, y and
z. This new axis, z, moves in and out of
our coordinate system, it gives us a depth.
This is what we see in reality too.Take an
example. Close one of your eyes (since two
eyes confuses the 3d view) and take out a
pen. Hold this pen as far away as possible
and think of this as z value 100. Now by,
moving the pen closer to your eye, you decrease the z value,
and when you cant get further, you have a z value of one, Now,
with a depth value of one, we have the actual size of the pen.
The pen is now much larger than it was before, often larger
than the sun itself. Although we know the
sun is actually thousands, maybe even millions of kilometers in
radius, we only see is as a ball no larger than 2-3 centimeter
wide. This is because of the distance between you and the sun,
which are eight light minuttes. Now, if you stod 1 centimeter
from the sun, you would see its actual size, but
moving a centimeter away, it would be half
the size in your eyes. Now, although this
is not completely true, the princip are
correct, very correct indeed. So, we can
find the x and y values that we see apoint
as, by knowing its x, y and z coordinates.
The formul ars are:
For starters, let's look at some
foundations 3D. First, what is 3D? Well,
3D reduction from three-dimensionality.
Now, let's look at some other dimension, maybe it will help
We understand 3D. One dimensional - only a line, and our
position on this line is represented by the letter x.
Two-dimensional - is represented by the letters x and y. This -
what we call a coordinate system, x is left right and y
upwards. This is - in fact, as our screen. Now,
three-dimensional represented by x, y and z. This new axis, z,
moving away from us into the depths of our system
coordinates, it gives us depth. It's - what
What we see is actually takzhe.Vozmem example. Close one of
your eyes (as two eyes confused 3D view) and take
pen. Take this pen so far
as possible, and think that z is
100 for this. Now, move the pen closer to your eye while you
reduce value z, and when you can no longer
move, you have a value of z alone.
Now, with the value of depth one, we have the actual size of
the pen. Perot now much larger than it was before, often
more than solntse.Hotya we know that the sun - in fact,
thousands, perhaps even millions of kilometers in radius, we
just see it as the ball is not larger in diameter than 2.3
santimetra.Eto - because of the distance between you and the
sun, which is 8 light-minutes. Now, if you ctoyali 1
centimeter from the sun, you would see its actual size, but
moving an inch away, it will be half the size of your eyes.
Now, although this is not entirely true, the principle is
correct, it is very indeed correct. So, we can find x and the
value y, where we see the point Knowing x, y and z coordinates.
Claims:
x '= x / z
y '= y / z
Note, that since we use a computer, we need to add half the
screen coordinates to the result. This is because the point 0,0,
0 would be found at the upperleft corner
of our screen. Therefore, if we add 128
and 96 to our value, we can see both negative and positive x /
y values. The formulars we should use would then
be:
Note that, since we use a computer, we must add half the
screen coordinates to the result. That - that, because
otherwise the point would be found 0,0,0 in the upper left
corner of the screen. Therefore, if we add 128 and 96 to our
value, we can see both negative and positive values of x / y.
Formulas that we need to use would then be:
x '= x / z +128
y '= y / z +96
Now, the basics about what 3d are should
be in place.
This is basic information about the 3D.
Normals - Normals
_________________
If we want to do several new 3d routines,
like backface removal, environment mapping
and lightsources, we have to know of normals. In backface
removal we find out if they are facing away from us, in
enirmonment mapping they give us our u and v coordinates, and
in light sources we use them to find the light intensity.
Actually normals are not only used in 3d, they are
also used in 2d, and thats the place where
we should start learning about them. Normals are a direction,
not a point in space. The important thing about normals, and the
handy thing, is that they are point 90 degress out in space
from their orign.
If we want to do several new 3-D
procedures, like removal of hidden surfaces, O environment and
sources light, we need to know about the normals. When you
delete an invisible surface, they (the normal) indicate to us
whether the surface aimed at deriving the environment they
provide us our coordinates u and v, and the sources
lighting, we use them to find
light intensity. In fact, the normal
used not only in 3D, they are also used in 2D, and a place
where we must begin to learn about them.
Normal - the direction, rather than a point in
prostranstve.Vazhnaya thing with respect to the normals, and
convenient thing is that they are always directed at an angle
of 90 degrees to the outside in space with respect to their
start on the surface.
The trouble, as one can see, is that there
are two normals per line. They point in
each their direction, each one the opposit
of the other. This is actually a great
problem in 3d normals, since this could
course objects to be invisible, due to back
face removal, or it could mess up the environment mapper, or
angle to the light source. Actually, there is no way to solve
this.
Now, in 3d we have both faces and vertices. A face have a
normal and so do vertices. Before we can calculate the normal
for the three vertices in a face, we need to
calculate the face normal first.We do this
by taking the dotproduct of the delta x, y
and z values. This would look like this in
code:
Difficulty, as can be seen, there is
where the two normals to the line. They point each in its own
direction, each opozitsionno another. It's actually a big
problem in 3D is normal, since it could be the direction of the
object to be invisible, because of the removal of the back
surface, or it might make a mess in the output environment, or
the angle at the source of lighting. In fact, there is no way
to solve it. Now, in 3D we have and the plane and
vershiny.Ploskost has normal and just as normal at the
vertices. First, we can calculate the normal for the three
vertices on the plane, we must calculate
face normal at first. We do this
by pointwise multiplication of differences
values of x, y and z. It would be like this
in the code:
nx = ((y1-y2) * (z1-z3 ))-(( z1-z2) * (y1-y3));
ny = ((z1-z2) * (x1-x3 ))-(( x1-x2) * (z1-z3));
nz = ((x1-x2) * (y1-y3 ))-(( y1-y2) * (x1-x3));
This returns a point in 3d space which is
a direction point. This is the direction
from the orign (0,0,0) to the normal point.
This line which could be made from these
points, are our direction. This normal value which we now have
found, could be used in our code, but this isnt very
handy.Since a normal is only a direction, it doesnt matter
where in 3d space the normal point are placed, as long the
direction from (0, 0,0) are the same.
Example could be the normal point (1000,
0,0). This point returns the same direction as the normal point
(1,0,0). Actually, keeping our normal length equal to one
turns out quite handy later on, when we
wants to calculate angles between normals.
But how do we get our vector length to equal one? Simple, we
divide by its length. Now, we find our length and divide the
values by saying:
It returns a point in 3D space
which expresses the point of direction. This
direction from the beginning (0,0,0) to point to the normal.
This line, which could be made of these points is our focus.
This value is normal, we now found, you can use in our
code, but it's not very convenient. Since the normal - this is
only the direction is not important where in the 3D space of
the normal point is placed, and how far it is from (0,0,0).
Example - Point of the normal (1000,0,0). This point has the
same direction as the point normal (1,0,0). In fact, during
storage Our normal lengths equal to one more convenient when we
want to calculate the angles between normals. But how do we get
our vector length to make it equal to unity? Simply, we divide
it by the length. Now, we find the length and divide our values:
length = sqrt ((nx * nx) + (ny * ny) + (nz * nz));
nx = nx / length;
ny = ny / length;
nz = nz / length;
That much for face normals, but how about
vertex normals, we still haven't looked into this yet. Well,
what we basicly have got now, is a face with a normalvector
pointing 90 degress out of it. So, eventually, the vertex
normals must follow the same rule. But how do we determine
where in 1990 degrees from a point is, it would actually
be any direction at all.What we need here,
is an object which facenormals are all cal
culated. Then for every vertex we run trough every face, and
finds out if this face "Uses" the current vertex. If it does,
you add the facenormal to the vertexnormal, do
the same with the next, and so on. When
this is done, you divide the vertexnormal
by the number of "hits" you had before (how
many faces that used that vertex). This means, that the result
normal is a normal calculated out of normals which stand 90
degress flat, and therefore the result nor
mal would have the right angle. Actually,
you can only talk about a right angle, there is no obvius way
of telling wether or not the normal vector are pointing
cerrectly, since every direction is the right direction.
However, the facenormals can point two ways, which tends to get
wuite anoying sometimes.This only results in the
opposite normal, e.g (0,0, -1) is opposite
of (0,0,1) and so on.It does no great harm
and it doesn't happend quite often.
This is so for facial normals, but how about the normals of
vertices, we still do not studied this yet. Well, we just
we have now, the screen with the normal vector at
90 degrees. So, ultimately, the vertex normal must inherit it
the same rule. But how do we define where
90 degrees from the point, it's actually any
direction at all. What we need
here at the facility in which all normal
surfaces are calculated. Then, for each
vertices we're looping through all the surface
and find out, use "whether this surface is the current vertex.
If so, you add on surface normal to normal
top, do the same with the next,
and so on. When done, you divide the normal peak to the number
of "hits" that you had before (how many planes have used this
vertex). This means that as a result of the normal - normal,
raschitanaya of the normals, which are on 90 degrees to the
plane, and hence normal as a result would be a right angle.
In fact, you can only talk about
right angle, there is no way
report there or not, the normal vector
correctly points out, since every direction - forward
direction. However, the surface normal may indicate two in ways
that are opposite. This only leads to the opposite of the
normal, ie (0,0, -1) - in front of (0,0,1) and so
dalee.Eto does no great harm, and it does not make much
happiness.