Академический Документы
Профессиональный Документы
Культура Документы
������������������������������������������������������������������������������
��������������������������������������������������������������������
����������������������������������������������������������
Three Dimensional Shading In Computer Graphics
����������������������������������������������������������
��������������������������������������������������������������������
������������������������������������������������������������������������������
By Lithium /VLA
Hopefully you have read the companion document 3DROTATE.DOC, as this one
will build apon the concepts presented in my attempt to teach some of the
math need to make 3D graphics a reality. This file will cover such important
topics as the Dot Product and how routines are best constructed for real-time
3D rotations and planar shading.
The Dot Product is a neat relation that will allow you to quickly find
the angle between any two vectors. It's easiest to explain graphicly, so
I will exercise my extended-ASCII keys.
Where Xa, and the others coorispond to some value on their respective Axis's
�A
/
/
/
/
\ � <-- Angle Theta between vector A and B
\
\
\
�B
Cos(�) = Xa * Xb + Ya * Yb + Za * Zb
����������������������������
�A�*�B�
Example:
Cos(�) = 1 * 4 + 2 * 5 + 3 * 6 = 4 + 10 + 18 = 32 = 0.9746
��������������������� ��������������� �����
(3.7417)*(8.7750) 32.8334 32.8334
So, your wondering how this revolutionizes you code, huh? Well, remember
our other friend, the Normal vector? You use Normal vectors that define
the directions of everything in our 3D world. Let's say that vector A was
the Normal vector from my plane, and B is a vector that shows the direction
that the light in my scene is pointing. If I do the Dot Product of them,
you will get the angle between them, if that angle is >= 90� and <= 270�
then no light falls on the visible surface and it doesn't need to be
displayed.
Also notice, the way the values of the Cosine orient themselves
270�
STRUC XYZs
Xpos dd ?
Ypos dd ?
Zpos dd ?
Dist dd ?
ENDS XYZs ;size is 16 bytes
The X,Y,Zpos define a point in 3D space, Dist is the distance from the origin
Dist = �( X� + Y� + Z� )
Precalculate these values and have them handy in your data area
STRUC PlaneSt
NumPts db ? ;3 or 4
NormIndex dw ?
PtsIndex dw ?
dw ?
dw ?
dw ?
ENDS PlaneSt
The number of points that in the plane depends on the number your fill
routines can handle you must have at least 3 and more than 6 is not suggested
MaxPoints = 100
MaxPlanes = 100
You'll notice that I defined the first Normal and then created space for
the rest of the possible normals. I'll call this first normal, the
Zero Normal. It will have special properties for planes that don't shade
and are never hidden.
Well, before I start telling all the tricks to the writting code, let me
make sure a couple of points are clear.
- In the 3DROTATE.DOC I said that you could set your view point on the
Z-Axis and then figure out if planes were visible by the post-rotation
Normal vectors, if their Z was > 0 then display, if not, don't
That is an easy way to set up the data, and I didn't feel like going
into the Dot Product at the time, so I generalized. So, what if you
don't view your plane from the Z-Axis, the answer is you use the...
Dot Product!
that's right. The angle will be used now to figure wheither or not to
display the plane.
- I have been mentioning lights and view points as vectors that I can
use with the Normal vector from my plane. To work correctly, these
vectors for the lights and view should point in the direction that you
are looking or the direction that the light is pointing, *NOT* a vector
drawn from the origin to the viewer position or light position.
- True Normal vectors only state a direction, and should therefore have
a unit distance of 1. This will have the advantage of simplifying the
math involved to figure you values. Also, for God's sake, pre-compute
your normal, don't do this everytime. Just rotate them when you do your
points and that will update their direction.
So:
Cos(�) = Xa * Xb + Ya * Yb + Za * Zb
����������������������������
�A�*�B�
Is Reduced To:
Cos(�) = Xa * Xb + Ya * Yb + Za * Zb
- You ASM users might be wondering why I defined my Zero Normal as:
<0,0,0,10000h> How does 10000h = a length of 1 ?
Well, this is a trick you can do in ASM, instead of using floating point
values that will be slow on computers without math co-processors, we can
use a double word to hold our value. The high word holds the integer
value, and the low word is our decimal. You do all of your computations
with the whole register, but only pull the high word when you go to
display the point. So, with that under consideration, 10000h = 1.00000
Not bad for integers.
- How does the Zero Normal work? Since the X,Y,and Z are all 0, the
Cos(�) = 0, so if you always display when Cos(�) = 0, then that plane
will always be seen.
Change Angles
Goto Rotation
A quick way to figure out which color to shade your plane if you are
using the double word values like I described before is to take the
Dot Product result, it will lie between 10000h - 0h if you would like
say 16 shades over the angles, then take that value and shr ,12 that will
give you a value from 0h - 10h (0-16, or 17 colors) if you make 10h into
0fh, add that offset to a gradient in your palette, then you will have
the color to fill your polygon with.
Note also that the Cosine function is weighted toward the extremes.
If you want a smooth palette change as the angles change, your palette
should weight the gradient accordingly.
Well, that's all that I feel like doing tonight, and besides, Star Trek is on!
So, see VLA.NFO for information about contacting myself or any of the other
members of VLA.
Happy Coding!