// ### BOILERPLATE ### // Orthographic Cube Firmware // Copyright (C) 2006 Peter Todd // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // ### BOILERPLATE ### // Cube rendering stuff. // The cube is stored as x,y,z co-ordinates. However the co-ordinates are // either 0 or 1, corresponding to -l and +l where l is the length of the sides // of the cube. // A vertex is the end of a wire. Stored as x,y typedef struct { uint8_t x; uint8_t y; } vertex_t; // The current orientation of the cube in terms of rotation around the x and y axis. uint8_t xrot,yrot; #include // Initalizes all the wires into the shape of a cube and sets the initial orientation. void cube_init(){ xrot = yrot = 0; #if 0 #define wasdasdf(i,ax,ay,az,bx,by,bz) {wire[i].a.x = ax; wire[i].a.y = ay; wire[i].a.z = az; wire[i].b.x = bx; wire[i].b.y = by; wire[i].b.z = bz; } // Front w( 0, 0,0,0, 1,0,0); w( 1, 1,0,0, 1,1,0); w( 2, 1,1,0, 0,1,0); w( 3, 0,1,0, 0,0,0); #undef w #endif } // Encapsulates the proj_lookup #defines #define pl_x(x,y,z) proj_lookup_x(x,y,z,xrot,yrot) #define pl_y(x,y,z) proj_lookup_y(x,y,z,xrot,yrot) // Takes the wireframe buffer and renders it. Each 3d wire is rotated with // respect to the current orientation, and then projected onto a 2d line // orthographically, IE, by throwing away z. void render_cube(){ static vertex_t a,b,c,d,e,f,g,h; // Since the only thing we ever do is draw a cube, everything is hardcoded. // // We also reuse results as needed to avoid recalculating co-ordinates // To save on typing all this is encapsulated into the #define l // // x,y,z are the co-ordinates to lookup, j is which of the temporary vertexes to put the results in #define l(vx,vy,vz,j) do {j.x = pl_x(vx,vy,vz); j.y = pl_y(vx,vy,vz); } while (0); // Our second time-saver is d, which draws a line between vertex's a and b #define d(a,b) draw_line(a.x,a.y,b.x,b.y); // front // // a,b,c,d are saved to create the sides l(0,0,0,a); l(1,0,0,b); l(1,1,0,c); l(0,1,0,d); d(a,b); d(b,c); d(c,d); d(d,a); // backside, also doing sides at the same time l(0,0,1,e); l(1,0,1,f); l(1,1,1,g); l(0,1,1,h); d(e,f); d(f,g); d(g,h); d(h,e); d(a,e); d(b,f); d(c,g); d(d,h); }