RUSH #01
29 мая 1999 |
|
Spectrum programming - Fast 3D-calculations: an advanced algorithm.
Fast 3D-graphics for SPECCY, it really viable? Perhaps the following article will reveal to you previously unknown possibilities SPECCY and will be useful when working with three-dimensional objects. The article is designed to more or less trained readers, but may be useful even novice users ... Despite some chaotic and nedoskaznnost flow of information here described to some extent an outstanding way to work with 3D objects in real time. The procedure is remarkable that works very quickly. The main essence of this method consists in the fact that rotates not each point separately, and immediately the whole plane. This method is useful not only nozhet to create cool vector effects, but and for writing new toys. And how rulez'no will look old vector game, reworked in the light of recent koderskih achievements! ... Imagine, For example, ELITE, running 2-3 times faster or more smoothly! Let us, indeed, to the material, afforded RUFF'om, even before the He felt the taste of writing on toys Amiga and became successful, "kodit" there ... A good example illustrating algorithm described below is the effect of Download "Hallutinations in Opera" - Rotating 3-D people, consisting of some vectors. Ruff> I once read an article in the "ECHO 1 about the withdrawal of 3-D objects. There is described the traditional method, which all (who znaet) use. This, of course, very interesting turning point around the axes X, Y and Z on special formulas. To rotate around the axis X: Y '= COS (N) * Y-SIN (N) * Z Z '= SIN (N) * Y + COS (N) * Z around the axis Y: X '= SIN (N) * Z + COS (N) * X Z '= COS (N) * Z-SIN (N) * X around the axis Z: X '= COS (N) * X-SIN (N) * Y Y '= SIN (N) * X + COS (N) * T (N - Angle) This method has drawbacks. And if more precisely, one very big drawback - takes a long time! At operation multiplication of (what had they not been fast) and calculating the COS and SIN (even on the table) takes a long time. If we consider the problem with the signs and then which is usually the object rotates once around three axes (some even use BASIC calculator), then there's nothing choice but to first count coordinates, and then withdraw graphix. Some even loads separately It is nice BASIC'e coordinates. The same lamer'stvo! (In some cases). However, sometimes vstrechayutsya programmki with REAL-TIME 3D CALCULATING'om. But how do they retard! ... This was the entry. Now closer to case. I invented a new way to display 3D objects. Here are his virtues: - It is much faster (!!!) vysheopisanogo fashion. to calculate the coordinates of one fixed point, to about 200 so- elements (if desired can be done faster). - 3D object can not only rotate, but also to deform (stretch and compress and so on.) I can not fully bring here listing of this effect, so bring all in pieces, which you can build and supplement who needs what. Here's the first piece - the procedure itself Print 3D objects: PRINT: LD (SAV +1), SP; save SP LD SP, ... ; ... - This is the start address of the origin ; Points of a 3D object. At one point 4 bytes And (3 bytes - the coordinates X, Y, Z; 1 byte, high byte Spreadsheets axes (about her ; A bit later) who, incidentally, further , Denoted label TABAX). EXX LD HL, BUFER; Special buffer ; Clear the screen , (See below) LD B, 175; number of points vectors LP1 ; Next comes how to print a single point: EXX POP BC; in BC - Y, X POP HL; in HL - or older. B table ; Axes, Z LD A, (HL) LD E, L LD L, B INC H ADD A, (HL) INC H LD L, C ADD A, (HL) ADD A, HX LD C, A INC H LD A, (HL) LD L, E INC H ADD A, (HL) LD L, B INC H ADD A, (HL) ADD A, LX LD L, A ; Now the coordinates of the points (X and Y screen) ; Computed pomeshrny registers C and L. ; Next comes how to print the point of ; Coordinates in these registers (69 bar).. ; For it needs a special table, so, Who, who does not know of such a procedure, , Can use his. PLOT: LD H, TABLE_P LD D, (HL) INC H LD A, (HL) INC H LD L, C ADD A, (HL) INC H LD E, A LD A, (DE) OR (HL) LD (DE), A ; Well, then. Point printed, we can now ; (If desired) in a special buffer ; Put the address on the screen this point , (In order then to quickly clean ; Screen using POP HL: LD (HL), 0: ; POP HL: LD (HL), 0: POP HL ...). LD A, D EXX DEC HL; address buffer increases ; Up-down (for convenience). LD (HL), A EXX LD A, E EXX DEC HL LD (HL), A DJNZ LP1; next point. SAV LD SP, 0 RET One unusual look team LD SP, 0: RET, but saves memory. I hope you guessed that the procedure itself, in itself will not work. MORE need to write a routine cleaning screen and how to create tables axes. Here I they do not cite due to the large volume (Length), which they occupy. Clear the screen of course, I could write only haschet that each has its own principles (who b wrote the installer, who would put everything in DUP'y, and so on. , etc).. In short, write themselves. And as for creating tables with coordinates axes, the better I will first explain the principle. Y ===== .... ________________ === ==. . / "^! ==. . / "*! ===. . /> 48! ===. . / "*! ===. . ! !. * A! ==. . ! ! *! === ==. . ! ! *! ===== .... ! ! *! ! ! *! Figure 1 ! ! *! ! "* 32! ! _______^________! ! 30 /****************>/ X ! ^ / * / ! / * / !/<________________/ Z See Figure 1. You guessed it - a cube. Imagine that inside It is W-D object. Each point This object has three coordinate axes of Y, X and Z. Here the axis is not endless. For example, they may consist of 64 points (coordinates). That is, the point can be defined by three numbers from 0 to 63. And now the fun ... Take, for example, a point with coordinates Y = 48, X = 32, Z = 30. How to find the coordinates in the screen, when you consider that the Z axis is rotated as in the picture? On each axis point marked with sootvetstvuyuschty coordinate. Glaenoe - need learn the Y and X of these points and the center (point of intersection of axes) on the screen. Suppose we know them: Koordinaty from the upper left corner of Y, X: On the axis of Y-70, 1946 On the axis X-172, 140 On the axis of Z-180, 1940 Centre -172.46 To calculate the coordinates of the screen point "A" apply two simple formulas: Oboznachim coordinates in the screen (that is, that above, only the letters instead of numbers): On the axis of Y-Yy, Xy On the axis X-Yx, Xx On the axis of Z-Yz, Xz Center-Yc, Xc Thus, the formula: Y = (Yy-Yc) + (Yx-Yc) + (Yz-Yc) + Yc X = (Xy-Xc) + (Xx-Xc) + (Xz-Xc) + Xc Substitute values: Y = (70-172) + (172-172) + (180-172) +172 = = 78 X = (46-46) + (140-46) + (40-46) +46 = 134 Koordinaty point "A" screen 78.134 The above programm printing 3D Object does exactly that - calculated the sum (though a bit differently), printed dot and remembers the address on the screen. And what should make the program rasschityvaniya coordinate axes? - Of course calculate the coordinates of the axes ... But let's first understand with formulas ... You probably have long poraskryvali brackets posokraschali extra coordinates of the center and believe that I have not noticed? So, in general, can only be done in This makes little sense. Here's the thing: here the coordinates of each point of the axis coordinate of the center is taken away. We return to the coordinates of the points axis. You guessed it, will have to rely coordinates of all 64 points of axes. And these coordinates, we will not count as They have a screen, and the center coordinates in screen 0.0. And then will only add the coordinates of the center. The procedure itself calculate the coordinates of the axes does not take much time. I, for example, (if the procedure set in the early interruption) Frame Scan will not have time reached even before the first lines of the screen. But for the coordinate axes needed buffer. Calculate its length: One axis, 64 points, that is 128 coordinates. Total number of axles 3, then all coordinates 128 * 3 = 384 coordinates. One coordinate is one bytes. But in order to accelerate the withdrawal 3D sprite better coordinates of one axis to keep the 512 bytes (the coordinates of Y-256 bytes, the coordinates X-256 bytes. Of the 256 bytes used only 64). Total buffer will 512 * 3 = 1536 bytes. Is not very much. Well, now the program itself. Principle Work will be explained by assuming that as we go. As already stated above, we expect coordinate axes so that if the center had coordinates 0, 0. And then, in the derivation, add the coordinates of the center. This will save time. By the way, the coordinates of the center of my will be stored in the files of the register pair IX, so that its contents do not change, or save, and then restores (my program rasschityvaniya coordinates axes associated with the program print 3D sprite pair IX, and the contents of the buffer coordinates axes). AX ; Procedure rasschityvaniya coordinates ; Axes. Coordinates (in screen) ; Extreme points of the axes , Are given in the registers of HL ', BC, HL. ; Coordinates of the center in DE. ; By the way, for those who do not want , So that 3D objects are compressed and tensile ; Valsya, you can turn to these ; 4 points using the formula 3-D rotation (look up). TABAX EQU 250 (64000 = 250 * 256) ; High byte buffer in which ; Calculated coordinate axes. ; Address buffer is a multiple of 256 (!!!) LD A, E SUB 32 LD E, A LD A, L SUB 32 LD L, A LD A, C SUB 32 LD C, A EXX LD A, L SUB 32 LD L, A EXX LD HX, D; refers st.bayt IX, LD LX, E; low byte of IX. ; (Undocumented commands Processor Z-80. 6 cycles) LD A, L PUSH AF LD A, C PUSH AF EXX LD A, L PUSH AF EXX LD A, H PUSH AF LD A, B PUSH AF EXX LD A, TABAX LD (VAR), A LD A, H LD B, HX CALL K64 POP AF LD B, HX CALL K64 POP AF LD B, HX CALL K64 POP AF LD B, LX CALL K64 POP AF LD B, LX CALL K64 POP AF LD B, LX CALL K64 RET K64 ; K64-most important procedure. Called ; The program as much as 6 times. Calculates , 64 intermediate coordinates. Coordinate, the purpose in the register A and the source of B. ; Source we have the center, then to B is always ; Coordinate of the center (Y or X). A general ; Zdea she only needed to obtain And the difference (A-B or B-A) and to determine ; Direction (positive or negative, with respect to). So, as the coordinates of the center of ; Here, 0, 0, - the address table 64 bytes (where , Put all the numbers) times 256. His ; Byte given at VAR. ; This byte, by the way, the files of the end of self, small increases to 1. ; Tak I figured at first the coordinates of Y, ; Calling this procedure, and then X-s, ; Calling her again. we have three axes, , So the procedure is called K64 6 times. CP B JP C, LOOP1 SUB B LD L, A LD H, 0 ADD HL, HL ADD HL, HL EX DE, HL LD HL, 0 LOOP2 EXX LD A, (VAR) LD H, A INC A LD (VAR), A LD L, 0 EXX DUP 64, or any loop-type ; LD B, 64 :...: DJNZ ... LD A, H ADD HL, DE EXX LD (HL), A INC L EXX EDUP RET LOOP1 LD C, A LD A, B SUB C LD L, A LD H, 0 ADD HL, HL ADD HL, HL EX DE, HL LD HL, 0 LOOP3 EXX LD A, (VAR) LD H, A INC A LD (VAR), A LD L, 0 EXX DUP 64, or any loop-type ; LD B, 64 :...: DJNZ ... SBC HL, DE LD A, H EXX LD (HL), A INC L EXX EDUP RET CP B JP C, LOOP4 SUB B LD L, A LD H, 0 ADD HL, HL ADD HL, HL EX DE, HL LD H, B LD L, 0 JP LOOP2 LOOP4 LD C, A LD A, B SUB C LD L, A LD H, 0 ADD HL, HL ADD HL, HL EX DE, HL ADD A, C LD H, A LD L, 0 JP LOOP3 VAR NOP NOP Well, that's all ... For those who are without going into all the nuances, or does not want to further change, to improve this method, and wants to write all its own way, some suggestions: 1. Do not forget about the format of storing the coordinates Nat 3D object. Do not forget the buffers (coordinate axes, delete buffer). 2. Coordinates of extreme points of the axes (Kz, Kx, Ky, and center), consider the formulas rotation or by a sine plate. 3. Algorithm of the program should be vyvyda where something like this: EI HALT DI CALL PRINT Record in the HL ', BC, HL, DE following values out of sin.tablitsi (must also take into account that they were not too large, otherwise sprite can get out of the screen and climb On the other hand. CALL AX Clear the screen using DELETE bufer'a. It is desirable to adjust everything to work with two screens. JP BEGIN
Other articles:
Similar articles:
В этот день... 21 November