#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "util.h" #include "lights.h" #include "init.h" #include "event.h" #include "render_menu.h" #include "geom.h" #include "render.h" /* no header files actually include these */ extern unsigned long *longimagedata(char *file_name); extern void sizeofimage(char *file_name, int *sizex, int *sizey); /* identitiy matrix */ Matrix id = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }; /* cummulative matrix */ Matrix cm = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }; /* for the cursor */ unsigned short finger[64] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f8, 0x0000, 0x0106, 0x0000, 0x0201, 0x0000, 0x0400, 0x8000, 0x0800, 0x4000, 0x0800, 0x4000, 0x1000, 0x2000, 0x1000, 0x2000, 0x2000, 0x1000, 0x2400, 0x1000, 0x2400, 0x1000, 0x2400, 0x1000, 0x2400, 0x1000, 0x2400, 0x1000, 0x1c00, 0x1000, 0x04c9, 0x2000, 0x04b6, 0xc000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0480, 0x0000, 0x0300, 0x0000 }; /* globals */ Object CurrObj, Scherk, Collins; EventState gevents; RenderState gstate; unsigned short *cursor_type; char *image_file; /* the main calls motif_main in motif mode or does an infinite loop that handles events */ void main(int argc, char *argv[]) { Initialize(argc, argv); render(); if (gstate.motif_mode) { motif_main(argc, argv); } else { while (1) { while(!qtest()) { handle_event_changes(&gevents, &gstate); } handle_events(&gevents, &gstate); } } } #define SQRT_1_3 (1.0/sqrt(3)) void DrawCube() { float verts[8][3] = { {-0.5, -0.5, 0.5}, { 0.5, -0.5, 0.5}, {-0.5, 0.5, 0.5}, { 0.5, 0.5, 0.5}, {-0.5, 0.5, -0.5}, { 0.5, 0.5, -0.5}, {-0.5, -0.5, -0.5}, { 0.5, -0.5, -0.5} }; float norms[8][3] = { {-SQRT_1_3, -SQRT_1_3, SQRT_1_3}, { SQRT_1_3, -SQRT_1_3, SQRT_1_3}, {-SQRT_1_3, SQRT_1_3, SQRT_1_3}, { SQRT_1_3, SQRT_1_3, SQRT_1_3}, {-SQRT_1_3, SQRT_1_3, -SQRT_1_3}, { SQRT_1_3, SQRT_1_3, -SQRT_1_3}, {-SQRT_1_3, -SQRT_1_3, -SQRT_1_3}, { SQRT_1_3, -SQRT_1_3, -SQRT_1_3} }; lmbind(MATERIAL, 31); lmbind(BACKMATERIAL, 31); texbind(TX_TEXTURE_0, 1); bgnqstrip(); EnvMapUV(norms[0], cm); n3f(norms[0]); v3f(verts[0]); EnvMapUV(norms[1], cm); n3f(norms[1]); v3f(verts[1]); EnvMapUV(norms[2], cm); n3f(norms[2]); v3f(verts[2]); EnvMapUV(norms[3], cm); n3f(norms[3]); v3f(verts[3]); endqstrip(); bgnqstrip(); EnvMapUV(norms[2], cm); n3f(norms[2]); v3f(verts[2]); EnvMapUV(norms[3], cm); n3f(norms[3]); v3f(verts[3]); EnvMapUV(norms[4], cm); n3f(norms[4]); v3f(verts[4]); EnvMapUV(norms[5], cm); n3f(norms[5]); v3f(verts[5]); endqstrip(); bgnqstrip(); EnvMapUV(norms[4], cm); n3f(norms[4]); v3f(verts[4]); EnvMapUV(norms[5], cm); n3f(norms[5]); v3f(verts[5]); EnvMapUV(norms[6], cm); n3f(norms[6]); v3f(verts[6]); EnvMapUV(norms[7], cm); n3f(norms[7]); v3f(verts[7]); endqstrip(); bgnqstrip(); EnvMapUV(norms[6], cm); n3f(norms[6]); v3f(verts[6]); EnvMapUV(norms[7], cm); n3f(norms[7]); v3f(verts[7]); EnvMapUV(norms[0], cm); n3f(norms[0]); v3f(verts[0]); EnvMapUV(norms[1], cm); n3f(norms[1]); v3f(verts[1]); endqstrip(); bgnqstrip(); EnvMapUV(norms[6], cm); n3f(norms[6]); v3f(verts[6]); EnvMapUV(norms[0], cm); n3f(norms[0]); v3f(verts[0]); EnvMapUV(norms[4], cm); n3f(norms[4]); v3f(verts[4]); EnvMapUV(norms[2], cm); n3f(norms[2]); v3f(verts[2]); endqstrip(); bgnqstrip(); EnvMapUV(norms[1], cm); n3f(norms[1]); v3f(verts[1]); EnvMapUV(norms[7], cm); n3f(norms[7]); v3f(verts[7]); EnvMapUV(norms[3], cm); n3f(norms[3]); v3f(verts[3]); EnvMapUV(norms[5], cm); n3f(norms[5]); v3f(verts[5]); endqstrip(); texbind(TX_TEXTURE_0, 0); } /* the objects to be drawn are here depending on CurrObj it draws that object */ void draw_objects() { /* bind the materials */ /* lmbind(MATERIAL, SCHERK_FIRST_MATERIAL); lmbind(BACKMATERIAL, SCHERK_FIRST_MATERIAL); */ if (gstate.texmap) { /* turn on texturing */ tevbind(TV_ENV0, 1); /*texbind(TX_TEXTURE_0, 1);*/ } /* DrawCube();*/ /* if we're on the motif menu window or we are environment mapping, we need to recreate the display list, since in motif window, the geometry values might change, and in case we're environment mapping, the texture coordinates change based on orientation */ if ((gstate.motif_mode && gevents.cursor_on_menu) || (gstate.texmap && gstate.envmap)) { gstate.num_polygons = geometry_display_list(); } else { callobj((long)CurrObj); } if (gstate.texmap) { /* turn off texturing */ tevbind(TV_ENV0, 0); /*texbind(TX_TEXTURE_0, 0);*/ } } /* sets up the viewport, clears the screen and the zbuffer setting the background color, sets up the views, applys the transformations on the objects, and draws them to the backbuffer displays the frames per second rate if no other window is on finally swaps the front and back buffers */ void render() { static char str[MAX_STR]; static int len; int npolys; float time_diff; int vp_left, vp_right, vp_bottom, vp_top; vp_left = 0; vp_right = gstate.sizeview[X]; vp_bottom = 0; vp_top = gstate.sizeview[Y]; set_viewport(gstate); winset(gstate.winview); win_clear(gstate.bg_r, gstate.bg_g, gstate.bg_b); /* write the background image data to the screen */ if (gstate.bg_on) { /* need to turn off zbuffering so the image goes to the background */ zbuffer(FALSE); if (gstate.stereo_on) { lrectwrite(0, 0, (gstate.sizeview[X]>>1)-1, gstate.bg_size[Y]-1, gstate.bg_data); lrectwrite(gstate.sizeview[X]>>1, 0, (gstate.sizeview[X]>>1)+gstate.bg_size[X]-1, gstate.bg_size[Y]-1, gstate.bg_data); } else { lrectwrite(0, 0, gstate.bg_size[X]-1, gstate.bg_size[Y]-1, gstate.bg_data); } zbuffer(gstate.zbuffer_on); } pushmatrix(); if (gstate.stereo_on) { viewport((vp_right + vp_left)>>1, vp_right, vp_bottom, vp_top); gstate.dcop[X] = gstate.eye_sep; gstate.dvrp[X] = -gstate.eye_sep; } set_view(gstate); transformations(gstate); draw_objects(); popmatrix(); if (gstate.stereo_on) { npolys = (gstate.num_polygons)<<1; pushmatrix(); viewport(vp_left, (vp_right + vp_left)>>1, vp_bottom, vp_top); gstate.dcop[X] = -gstate.eye_sep; gstate.dvrp[X] = gstate.eye_sep; set_view(gstate); gstate.dcop[X] = 0.0; gstate.dvrp[X] = 0.0; transformations(gstate); draw_objects(); popmatrix(); } else { npolys = gstate.num_polygons; } swapbuffers(); gflush(); gstate.frames++; time_diff = elapsed_time(&(gstate.frame_time)); /* update the frame rate once every second */ if (time_diff >= 1.0) { sprintf(str, "fps: %6.2f, ", ((float)gstate.frames)/time_diff); len = strlen(str); gstate.frames = 0; init_timer(&(gstate.frame_time)); } /* update the polygons per frame every frame */ sprintf(&str[len], "ppf: %6d", npolys); wintitle(str); }