Implementation of Phong shading on Speccy

ZXNet echo conference «code.zx»

From Aleksey Malov To All 22 April 2000

Greetings, All! Phong lighting (hereinafter PS) - quite common in demos on PC and Amiga type lighting. 3d objects with PS look much prettier, than with Gouraud Shading. PS can be found in several Spectrum demes from 1999-2000. - Napalm by Eternity Industry, Bobering by Syndrome, Voodoo by Scene. Speed PS implementations in all these demos are quite small (about 2-3 fps). Either The hands are crooked, or 3.5 MHz is having an effect. However, you have the option to do faster than these demo-making monsters. The easiest way is to speed up throwing chunks onto the screen (all these demos use 101 clock cycles) inner loop, but not the 70-82 clock one I suggested). However, this will allow forces to save 25-35 thousand cycles per frame. All that remains is to speed up the internal the lighting cycle itself. So let's begin. Phong lighting makes the following assumptions: 1. The light source is infinitely distant => illumination occurs in a parallel beam rays 2. The surface does not reflect light, but only scatters it. 3. When interpolating the normal from vertex to vertex, the length of the normal DOES NOT CHANGE (this does not have much effect if the polygons describe a fairly smooth surface body). Taking this into account, lighting can be reduced to the usual texture mapping, in which serves as a table calculated according to the following. formula (Texture has a size of 64*64):a(y,x)=amp*(sin (y*pi/64) * sin (x*pi/64))^4. amp=source brightness However, it is better to store the texture 256*64, i.e. in the remaining 3/4 of the texture width you can place textures for the background, for polygons, etc. It is not difficult to calculate this texture in assembler. Raise to the 4th power possible using a table. Because the polygons that make up an object describe some kind of curve surface, then the normals to the vertices of the polygons should be calculated as follows: 1. We perform a vector addition of normals to polygons that have a common top. 2. then we reduce the resulting vector to a length of 32 (since our texture is 64*64): n=number of polygons having a given vertex ─ ----- ─ N= Ni / ────────*32 ----- n i=1 This is necessary so that all the normals to the vertices are the same length. The coordinates of each polygon vertex in the texture are calculated using the formula: u=32+Nx v=32+Ny Well, all that remains is to output the texture-filled polygon to the chunk buffer. inner loop: ld a,h add hl,de exx ld b,a ld c,h add hl,de ld a,(bc) exx ld(bc),a inc c ;hl-v (8.8 fixed point) ;de-dv (8.8 fixed point) ;hl'-u (8.8 fixed point) ;de'-du (8.8 fixed point) Due to a strange misunderstanding, the copyright for this inner loop was assigned to myself Wolf/Scene. Although it is practically no different from inner, invented in 1995loop'a BUSY Soft'a in the Rotate Zoomer Echology megademo. If anyone doesn’t understand something or wants to know more, ask someone FAQ echoes Demo.Design. Well, my personal opinion about all these Phong Shadings on the Spectrum with at a snail's speed (2-3 fps) - If you can't sleep, don't worry about it. Well, all this is not for the Spectrum! Demo should be nimble, not follow principle of the Energizer battery - NOTHING WORKS SO LONG. I wish you health, happiness and creative Uzbeks. Aleksey Malov aka VIVID/Brainwave.