(from Your Spectrum 16, July 1985) 3D DAZE It's here - the ultimate in 3D graphics. Mr MegaBasic, Mike Leaman shows another side to his talents with a program that'll transport you into the third dimension. How often have you wished you could create the smooth three dimensional graphics like the ones Ultimate has made famous in Knight Lore and Alien 8? Well, now you can - almost. Of course, the Ultimate team doesn't actually program on the Spectrum but on a much larger machine, then squirts it down. But you can get pretty close with these machine code routines that'll enable you to move objects in three dimensions. The tricky bit about 3D graphics is making sure that the objects meant to be at the front of the display look as though that's where they are. So, how's it done? Well, briefly, you have to draw the objects at the back first and then work your way forward. I've tackled it by keeping a list of all the objects that appear on the screen and this list contracts and expands according to how many objects there are. The list starts at 64700 and each entry consists of four bytes. Understanding these is the tricky bit. The first byte is the 'X' co-ordinate, the second the 'Y'-co-ordinate, the third the 'Z' co- ordinate and the last byte is the objects' code number. The easiest way to understand these co-ordinates is to think in terms of a matchbox - hold it up in front of you. 'Y' runs along the bottom from left to right, 'X' runs up the edge that goes from the top to bottom and 'Z' is the co-ordinate that travels along the edge that's going away from you. If you're still in trouble or you want to go into further depth, take a look at the articles on 3D plotting in YS issues 2 and 3. Now back to that list. Those objects that are to be placed at the back come at the start of the list. Then each time an object is displayed or erased, the list is scanned and the appropriate action taken. The shape of each object is stored in 64 bytes of RAM and I've included three objects with the code. The shape list starts at 65047, so that's where you'll find the first object, then the second object is at 65047+64 and so on. The 64 bytes of character info is split into two halves - the first 32 bytes represent the actual shape of the object, similar to the way you would define a UDG but with much more information because of the third dimension. This is followed by the second 32 byte block that describes the shape of the mask needed to erase the object. Now take a look at the code. It's in two parts - the first part that does all the work starts at 64000 and the second part contains shape info for the three example objects and starts at 65047. Here's how you enter the code. First type in the Hex loader and save it - you may need it later. Now RUN it and it'll automatically lower RAMtop so that there's space for the code. You'll then be asked for a start address - for the main block, enter 64000 and for the shape info enter 65047. Once you've put those in, you can press on with entering the actual code. For starters, you'l1 be prompted with an address - look at the main listing - the first prompt'll be 64000 so you should enter F3. The second prompt is 64001, so you'll enter CD93FB, remembering, of course, not to enter any spaces. When you've reached the end of a block, enter 'S' - the loader will then ask you for a Checksum. For the main block enter 79933, then for the shape data enter 17774. Now the loader checks the code for errors. If it finds one you'll just have to go back and re-check. As soon as you've entered the main block, carry on and enter the shape data and then save the code using: SAVE "code" CODE 64000,1368 Now enter NEW and type in the test program, again remembering to save it just in case the code crashes. At last, you're there! If all's well, you should be greeted by a number of bubbles moving across the screen in full and glorious 3D. OK, I can already hear you asking how you can use the routines yourself. As I've said, it takes four parameters to describe an object - the 'X', `Y' and 'Z' co-ordinates plus the shape code that defines what shape the object is to take. These parameters are passed to the machine code in four locations: 64637 'Y' co-ordinate 64638 'X' co-ordinate 64639 shape code 64640 'Z' co-ordinate Well, there are three routines that you can usefully adapt. The first is the one that covers the background. To understand this, you must know that the middle third of the screen is printed first, so you can use this part as a back drop. So, design your back drop and save it into memory which you can do as follows: first, reserve an area of memory using the CLEAR command, then POKE the address of this area into locations 64635 and 64636. Once you've drawn your backdrop, call the routine at 64423 and this'll save the middle third of the screen into memory. Now to print an object POKE its co-ordinates into the correct memory locations and call the routine at 64489. Then to erase an object POKE its co-ordinates into the correct locations and call the routine at 64556. You'll also have to remember to execute POKE 64703,0 before you start printing - that way you'll clear the display list. And finally a word of warning - so that the routine's as fast as possible I've left out all the error checking. You'll have to make sure that none of your 3D objects goes wandering off the edge of the screen or you may find yourself with a system crash. And now onto next month. Watch out for a 3D graphic designer that'll let you create your own clockwork mice or mutant daleks. Plus, for all the clever people who've mastered YS MegaBasic, I'll be showing you how to convert this month's machine code into MegaBasic. All you'll have to use then are a number of new Basic commands. Of course, all you poor socially deprived Speccy owners still relying on Sinclair Basic will have to resort to the infamous commands POKE and USR. It's a tough life!