[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
DenTut8-Pas
If you are already a 3-D guru, you may as well skip this text file, have a quick look at the sample program then go back to sleep, because I am going to explain in minute detail exactly how the routines work ;)
Now, let us move the value of BX directly into DI, thereby removing a costly push and pop. The MOV and the XOR of DX can be replaced by it's equivalent, SHL DX,8
As you can see, we have brought the clock ticks down from 153 ticks to 71 ticks ... quite an improvement. (The current ASPHYXIA putpixel takes 48 clock ticks) . As you can see, by going through your routines a few times, you can spot and remove unnecessary instructions, thereby greatly increasing the speed of your program.
In this trainer, we are using lines, so we define 2 X,Y and Z coordinates, one for each end of the line. A line from far away, in the upper left of the X and Y axes, to close up in the bottom right of the X and Y axes, would look like this :
Anyway, we have now rotated an object in two dimensions, AROUND THE Z AXIS. In matrix form, the equation looks like this :
In the sample program, we have a constant, never changing base object. This is rotated into a second variable, which is then drawn. I am sure many of you can thing of cool ways to change the base object, the effects of which will appear while the object is rotating. One idea is to "pulsate" a certain point of the object according to the beat of the music being played in the background. Be creative. If you feel up to it, you could make your own version of transformers ;)
) ... it uses 12 muls and 2 divs per point. (Asphyxia currently has
9 muls and 2 divs per point) Real math is used for all the calculations
in the sample program, which is slow, so fixed point math should be
implemented (I will cover fixed point math in a future trainer). The
line routine currently being used is very slow. Chain-4 could be used to
cut down on screen flipping times.
Color values per line should be added, base object morphing could be put in, polygons could be used instead of lines, handling of more then one object should be implemented, clipping should be added instead of not drawing something if any part of it is out of bounds.
In other words, you have a lot of work ahead of you ;)
I am delving into the murky world of texture mapping. If anyone out there has some routines on the subject and are interested in swapping, give me a buzz!
What to do in future trainers? Help me out on this one! Are there any effects/areas you would like a bit of info on? Leave me a message!
I unfortunately did not get any messages regarding BBS's that carry this series, so the list that follows is the same one from last time. Give me your names, sysops!
Aaaaargh!!! Try as I might, I can't think of a new quote. Next time, I promise!
Bye for now, - Denthor
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
DenTut8-Pas
ÕÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸
³ W E L C O M E ³
³ To the VGA Trainer Program ³ ³
³ By ³ ³
³ DENTHOR of ASPHYXIA ³ ³ ³
ÔÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ; ³ ³
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ³
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
--==[ PART 8 ]==--
- Introduction
If you are already a 3-D guru, you may as well skip this text file, have a quick look at the sample program then go back to sleep, because I am going to explain in minute detail exactly how the routines work ;)
- Optimisation
Procedure Putpixel (X,Y : Integer; Col : Byte; where:word);
BEGIN
Asm
push ds { 14 clock ticks }
push es { 14 }
mov ax,[where] { 8 }
mov es,ax { 2 }
mov bx,[X] { 8 }
mov dx,[Y] { 8 }
push bx { 15 }
mov bx, dx { 2 }
mov dh, dl { 2 }
xor dl, dl { 3 }
shl bx, 1 { 2 }
shl bx, 1 { 2 }
shl bx, 1 { 2 }
shl bx, 1 { 2 }
shl bx, 1 { 2 }
shl bx, 1 { 2 }
add dx, bx { 3 }
pop bx { 12 }
add bx, dx { 3 }
mov di, bx { 2 }
xor al,al { 3 }
mov ah, [Col] { 8 }
mov es:[di],ah { 10 }
pop es { 12 }
pop ds { 12 }
End;
END;
Total = 153 clock ticks
NOTE : Don't take my clock ticks as gospel, I probably got one or two
wrong.
Right, now for some optimising. Firstly, if you have 286 instructions
turned on, you may replace the 6 shl,1 with shl,6. Secondly, the Pascal
compiler automatically pushes and pops ES, so those two lines may be
removed. DS:[SI] is not altered in this procedure, so we may remove
those too. Also, instead of moving COL into ah, we move it into AL and
call stosb (es:[di]:=al; inc di). Let's have a look at the routine now :
Procedure Putpixel (X,Y : Integer; Col : Byte; where:word);
BEGIN
Asm
mov ax,[where] { 8 }
mov es,ax { 2 }
mov bx,[X] { 8 }
mov dx,[Y] { 8 }
push bx { 15 }
mov bx, dx { 2 }
mov dh, dl { 2 }
xor dl, dl { 3 }
shl bx, 6 { 8 }
add dx, bx { 3 }
pop bx { 12 }
add bx, dx { 3 }
mov di, bx { 2 }
mov al, [Col] { 8 }
stosb { 11 }
End;
END;
Total = 95 clock ticks
Now, let us move the value of BX directly into DI, thereby removing a costly push and pop. The MOV and the XOR of DX can be replaced by it's equivalent, SHL DX,8
Procedure Putpixel (X,Y : Integer; Col : Byte; where:word); assembler;
asm
mov ax,[where] { 8 }
mov es,ax { 2 }
mov bx,[X] { 8 }
mov dx,[Y] { 8 }
mov di,bx { 2 }
mov bx, dx { 2 }
shl dx, 8 { 8 }
shl bx, 6 { 8 }
add dx, bx { 3 }
add di, dx { 3 }
mov al, [Col] { 8 }
stosb { 11 }
end;
Total = 71 clock ticks
As you can see, we have brought the clock ticks down from 153 ticks to 71 ticks ... quite an improvement. (The current ASPHYXIA putpixel takes 48 clock ticks) . As you can see, by going through your routines a few times, you can spot and remove unnecessary instructions, thereby greatly increasing the speed of your program.
- Defining a 3-D object
Y Z
/|\ /
| /
X<-----|----->
|
\|/
X is the horisontal axis, from left to right. Y is the vertical axis,
from top to bottom. Z is the depth, going straight into the screen.In this trainer, we are using lines, so we define 2 X,Y and Z coordinates, one for each end of the line. A line from far away, in the upper left of the X and Y axes, to close up in the bottom right of the X and Y axes, would look like this :
{ x1 y1 z1 x2 y2 z2 }
( (-10,10,-10),(10,-10,10) )
- Rotating a point with matrixes
NOTE : I thought that more then one matix are matrisese (sp), but my
spellchecker insists it is matrixes, so I let it have it's way
Having a 3-D object is useless unless you can rotate it some way. For
demonstration purposes, I will begin by working in two dimensions, X and
Y.
Let us say you have a point, A,B, on a graph.
Y
| /O1 (Cos (a)*A-Sin (a)*B , Sin (a)*A+Cos (a)*B)
|/ (A,B)
X<-----|------O-->
|
|
Now, let us say we rotate this point by 45 degrees anti-clockwise. The
new A,B can be easily be calculated using sin and cos, by an adaption of
our circle algorithm, ie.
A2:=Cos (45)*A - Sin (45)*B
B2:=Sin (45)*A + Cos (45)*B
I recall that in standard 8 and 9, we went rather heavily into this in
maths. If you have troubles, fine a 8/9/10 maths book and have a look;
it will go through the proofs etc.Anyway, we have now rotated an object in two dimensions, AROUND THE Z AXIS. In matrix form, the equation looks like this :
[ Cos (a) -Sin (a) 0 0 ] [ x ] [ Sin (a) Cos (a) 0 0 ] . [ y ] [ 0 0 1 0 ] [ z ] [ 0 0 0 1 ] [ 1 ]I will not go to deeply into matrixes math at this stage, as there are many books on the subject (it is not part of matric maths, however). To multiply a matrix, to add the products of the row of the left matrix and the column of the right matrix, and repeat this for all the columns of the left matrix. I don't explain it as well as my first year maths lecturer, but have a look at how I derived A2 and B2 above. Here are the other matrixes :
Matrix for rotation around the Y axis : [ Cos (a) 0 -Sin (a) 0 ] [ x ] [ 0 1 0 0 ] . [ y ] [ Sin (a) 0 Cos (a) 0 ] [ z ] [ 0 0 0 1 ] [ 1 ] Matrix for rotation around the X axis : [ 1 0 0 ] [ x ] [ 0 Cos (a) -Sin (a) 0 ] . [ y ] [ 0 Sin (a) Cos (a) 0 ] [ z ] [ 0 0 0 1 ] [ 1 ]By putting all these matrixes together, we can translate out 3D points around the origin of 0,0,0. See the sample program for how we put them together.
In the sample program, we have a constant, never changing base object. This is rotated into a second variable, which is then drawn. I am sure many of you can thing of cool ways to change the base object, the effects of which will appear while the object is rotating. One idea is to "pulsate" a certain point of the object according to the beat of the music being played in the background. Be creative. If you feel up to it, you could make your own version of transformers ;)
- Drawing a 3D point to screen
| ________-------------
____|___------ o Object at X,Y,Z o1 Object at X,Y,Z2
Eye -> O)____|___
| ------________
| -------------- Field of vision
Screen
Let us pretend that the centre of the screen is the horizon of our
little 3D world. If we draw a three dimensional line from object "o" to
the centre of the eye, and place a pixel on the X and Y coordinates
where it passes through the screen, we will notice that when we do the
same with object o1, the pixel is closer to the horizon, even though
their 3D X and Y coords are identical, but "o1"'s Z is larger then
"o"'s. This means that the further away a point is, the closer to the
horizon it is, or the smaller the object will appear. That sounds
right, doesent it? But, I hear you cry, how do we translate this into a
formula? The answer is quite simple. Divide your X and your Y by your Z.
Think about it. The larger the number you divide by, the closer to zero,
or the horizon, is the result! This means, the bigger the Z, the
further away is the object! Here it is in equation form :
nx := 256*x div (z-Zoff)+Xoff
ny := 256*y div (z-Zoff)+Yoff
NOTE : Zoff is how far away the entire object is, Xoff is the objects X
value, and Yoff is the objects Y value. In the sample program,
Xoff start off at 160 and Yoff starts off at 100, so that the
object is in the middle of the screen.
The 256 that you times by is the perspective with which you are viewing.
Changing this value gives you a "fish eye" effect when viewing the
object. Anyway, there you have it! Draw a pixel at nx,ny, and viola! you
are now doing 3D! Easy, wasn't it?- Possible improvements
Color values per line should be added, base object morphing could be put in, polygons could be used instead of lines, handling of more then one object should be implemented, clipping should be added instead of not drawing something if any part of it is out of bounds.
In other words, you have a lot of work ahead of you ;)
- In closing
I am delving into the murky world of texture mapping. If anyone out there has some routines on the subject and are interested in swapping, give me a buzz!
What to do in future trainers? Help me out on this one! Are there any effects/areas you would like a bit of info on? Leave me a message!
I unfortunately did not get any messages regarding BBS's that carry this series, so the list that follows is the same one from last time. Give me your names, sysops!
Aaaaargh!!! Try as I might, I can't think of a new quote. Next time, I promise!
Bye for now, - Denthor
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
