Main Page | Data Structures | File List | Data Fields | Globals

ex11.c

00001 /*=========================================================================
00002 
00003 GFX EXAMPLE CODE - #10
00004         "spinning solid"
00005 
00006 Copyright (C) 2004  Rafael de Oliveira Jannone
00007 
00008 This example's source code is Public Domain.
00009 
00010 WARNING: The author makes no guarantees and holds no responsibility for 
00011 any damage, injury or loss that may result from the use of this source 
00012 code. USE IT AT YOUR OWN RISK.
00013 
00014 Contact the author:
00015         by e-mail : rafael AT jannone DOT org
00016         homepage  : http://jannone.org/gfxlib
00017         ICQ UIN   : 10115284
00018 
00019 =========================================================================*/
00020 
00021 #include <stdlib.h>
00022 #include <math.h>
00023 #include "3d.h"
00024 #include "line.h"
00025 
00026 // build a pyramid mesh
00027 
00028 mesh_t* build_mesh() {
00029         int p;
00030         double c;
00031         mesh_t* mesh = mesh_new(5, 6);  // 5 vertexes, 6 triangles
00032         vector_t* points = mesh->points;
00033         triangle_t* triangles = mesh->triangles;
00034 
00035         double M_PI = 8.0 * atan(1.0);
00036 
00037         // define the base points
00038         for (p = 0; p < 4; p++) {
00039                 c = M_PI * p / 4;
00040                 points[p].x = (int)(10.0 * cos(c) * 64);
00041                 points[p].y = (int)(10.0 * sin(c) * 64);
00042                 points[p].z = i2f(5);
00043         }
00044         // define the top point
00045         points[4].x = 0;
00046         points[4].y = 0;
00047         points[4].z = i2f(-5);
00048 
00049         // define triangles
00050         // side a
00051         triangles[0].vertexes[0] = &points[4];
00052         triangles[0].vertexes[1] = &points[0];
00053         triangles[0].vertexes[2] = &points[1];
00054 
00055         // side b
00056         triangles[1].vertexes[0] = &points[4];
00057         triangles[1].vertexes[1] = &points[1];
00058         triangles[1].vertexes[2] = &points[2];
00059 
00060         // side c
00061         triangles[2].vertexes[0] = &points[4];
00062         triangles[2].vertexes[1] = &points[2];
00063         triangles[2].vertexes[2] = &points[3];
00064 
00065         // side d
00066         triangles[3].vertexes[0] = &points[4];
00067         triangles[3].vertexes[1] = &points[3];
00068         triangles[3].vertexes[2] = &points[0];
00069 
00070         // base 1
00071         triangles[4].vertexes[0] = &points[0];
00072         triangles[4].vertexes[1] = &points[3];
00073         triangles[4].vertexes[2] = &points[2];
00074 
00075         // base 2
00076         triangles[5].vertexes[0] = &points[2];
00077         triangles[5].vertexes[1] = &points[1];
00078         triangles[5].vertexes[2] = &points[0];
00079 
00080         return mesh;    // done! :)
00081 }
00082 
00083 main() {
00084         bool flat = false;
00085         int *low, *high;
00086         vector_t light;
00087 
00088         surface_t screen;
00089 
00090         // this is a vector buffer, for the transformations
00091         // our only object have 5 vertexes, but we'll make space for 32 anyway
00092         vector_t *pbuffer = newa(vector_t, 32);
00093 
00094         // off-screen surface buffer
00095         u_char* sbuffer = (u_char*)malloc(MODE2_MAX);
00096 
00097         // our solid :)
00098         object_t triangle;
00099         triangle.mesh = build_mesh();
00100         triangle.rot_x = triangle.rot_y = triangle.rot_z = 0;
00101         triangle.trans_x = triangle.trans_y = 0;
00102         triangle.trans_z = i2f(30);     // remember: we are using fixed-point numbers
00103 
00104         screen.data.ram = sbuffer;
00105 
00106         // polygon rendering buffers
00107         low = newa(int, MODE2_HEIGHT);
00108         high = newa(int, MODE2_HEIGHT);
00109 
00110         // light source
00111         light.x = light.y = light.z = i2f(1);
00112         vector_normalize(&light, &light);
00113 
00114         printf("spinning solid demo\n\n");
00115 
00116         printf("instructions:\n   press [UP] to toggle flat shading\n\n");
00117 
00118         printf("creating look-up tables, please wait\n");
00119         create_lookup_tables();
00120 
00121         // set screen to graphic mode
00122         set_color(15, 1, 1);
00123         set_mode(mode_2);
00124         fill(MODE2_ATTR, 0xF1, MODE2_MAX);
00125 
00126         surface_line(&screen, 0, 0, 0, 0); // FIXME: won't compile without this crap
00127 
00128         while (!get_trigger(0)) {
00129                 if (get_stick(0) == 1)
00130                         flat = !flat;
00131 
00132                 // rotate a bit
00133                 triangle.rot_y += 2;
00134                 triangle.rot_x += 3;
00135                 triangle.rot_z += 1;
00136 
00137                 // clear the off-screen buffer
00138                 memset(sbuffer, MODE2_MAX, 0);  // [*] 
00139 
00140                 // render the object
00141                 if (flat)
00142                         object_render_flatshading(&screen, &triangle, pbuffer, low, high, &light);
00143                 else
00144                         object_render_wireframe(&screen, &triangle, pbuffer);
00145 
00146                 // show the off-screen buffer
00147                 vwrite(screen.data.ram, 0, MODE2_MAX); // [*]
00148 
00149                 // [*] FIXME: there will be better ways of doing this (soon)
00150         }
00151 
00152         // go back to text mode
00153         set_mode(mode_0);
00154 
00155         // deallocate stuff
00156 
00157         mesh_delete(triangle.mesh);
00158         free(sbuffer);
00159         free(low);
00160         free(high);
00161         destroy_lookup_tables();
00162 }

Generated on Thu Mar 3 19:55:01 2005 for GFX lib by  doxygen 1.4.1