home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 August - Disc 3 / chip_20018103_hu.iso / amiga / chipgame / 3dgpl.lha / demo.c < prev    next >
C/C++ Source or Header  |  2001-06-09  |  6KB  |  150 lines

  1. /** 3DGPL *************************************************\
  2.  * A demo allowing to rotate a polygon rendering it as    *
  3.  * either ambient, shaded or textured. ( <TAB> to change  *
  4.  * rendering option ).                                    *
  5.  *                                                        *
  6.  * WARNING: This demos uses some Amiga keycodes           *
  7.  *                              *
  8.  *  If it doesn't work:                                   *
  9.  *   1) make sure you are using right hardware interface  *
  10.  *      source and your system sutisfies requirements     *
  11.  *      described in it's head comment.                   *
  12.  *   2) if this application does quit on <ENTER> but      *
  13.  *      shows nothing, adjust colour intensities assigned *
  14.  *      in main(), hardware interface function doesn't    *
  15.  *      convert them.                                     *
  16.  *   3) if the demo crashes recompile with bigger stack.  *
  17.  *                                                        *
  18.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  19.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  20.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  21.  *  WITHOUT AUTHORISATION                                 *
  22. \**********************************************************/
  23.  
  24. #include <stdio.h>
  25. #include "3dgpl:hardware.h"
  26. #include "3dgpl:engine.h"
  27. #include "3dgpl:graphics.h"
  28. #include "3dgpl:trans.h"
  29.  
  30. unsigned char alp=0,bet=0,gam=0;
  31. int x=0,y=0,z=200;
  32.  
  33. unsigned char texture[][]=
  34. {
  35.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0},
  36.  {0xef,0x55,0x55,0x55,0x55,0x55,0x55,0xe8,0xe7,0x55,0x55,0x55,0x55,0x55,0xe1,0xe0},
  37.  {0xef,0xee,0xed,0xec,0xeb,0x55,0xe9,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  38.  {0xef,0xee,0xed,0xec,0x55,0xea,0xe9,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  39.  {0xef,0xee,0xed,0x55,0x55,0x55,0x55,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  40.  {0xef,0xee,0xed,0xec,0xeb,0xea,0x55,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  41.  {0xef,0x55,0x55,0x55,0x55,0x55,0xe9,0xe8,0xe7,0x55,0x55,0x55,0x55,0x55,0xe1,0xe0},
  42.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0},
  43.  {0xef,0xee,0x77,0x77,0xeb,0xea,0x76,0x76,0x76,0xe6,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  44.  {0xef,0x77,0xed,0xec,0x77,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  45.  {0xef,0x77,0xed,0xec,0xeb,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  46.  {0xef,0x77,0xed,0xec,0xeb,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  47.  {0xef,0x77,0xed,0x77,0x77,0xea,0x76,0x76,0x76,0xe6,0xe5,0x75,0xe3,0xe2,0x75,0xe0},
  48.  {0xef,0x77,0xed,0xec,0x77,0xea,0x76,0xe8,0xe7,0xe6,0xe5,0x75,0xe3,0xe2,0x75,0xe0},
  49.  {0xef,0xee,0x77,0x77,0x77,0xea,0x76,0xe8,0xe7,0xe6,0xe5,0x75,0x75,0x75,0x75,0xe0},
  50.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0}
  51. };
  52.  
  53. struct M_polygon a= {M_AMBIENT,155,NULL,0,0,0,0,4,{0,3,6,9,0}};
  54.          //id,color,NULL,0,0,0,0,no-edges,nr-edges bei shaded nr,farbe
  55. struct M_polygon s= {M_SHADED,0,NULL,0,0,0,0,4,{0,5,3,142,6,155,9,31,0,11}};
  56. struct M_polygon t= {M_TEXTURED,0,(unsigned char*)texture,4,6,0,3,4,
  57.                     {0,0,64,3,64,64,6,64,0,9,0,0,0,0,64}};
  58. struct M_polygon *p=&t;
  59. int m_no_verteces=4;
  60. int verteces[]={-50,50,0,50,50,0,50,-50,0,-50,-50,0};
  61. int m_no_vectors=2;
  62. int vectors[]={100,0,0,0,100,0};
  63.  
  64. /**********************************************************\
  65.  * being called to treat key press.                       *
  66. \**********************************************************/
  67.  
  68. void app_handler(int kk)
  69. {
  70.  
  71. if(kk>127)return;
  72.  
  73.  switch(kk)
  74.  {
  75.   case HW_KEY_ENTER:       HW_quit_event_loop(); break;
  76.   case HW_KEY_ARROW_RIGHT: gam+=10; break;
  77.   case HW_KEY_ARROW_LEFT:  gam-=10; break;
  78.   case HW_KEY_ARROW_UP:    bet+=10; break;
  79.   case HW_KEY_ARROW_DOWN:  bet-=10; break;
  80.  
  81. #define KEY_S              0x21
  82. #define KEY_X              0x32
  83. #define KEY_A              0x20
  84. #define KEY_Z              0x31
  85.  
  86.   case KEY_X:      alp+=10; break;
  87.   case KEY_S:      alp-=10; break;
  88.   case KEY_A:      z-=10;   break;
  89.   case KEY_Z:      z+=10;   break;
  90.  
  91.   case HW_KEY_TAB: if(p==&a) p=&s; else {if(p==&s) p=&t; else p=&a;}
  92.  }
  93. }
  94.  
  95. /**********************************************************\
  96.  * being called to render a frame.                        *
  97. \**********************************************************/
  98.  
  99. void app_main(void)
  100. {
  101.  int tmp_verteces[50];
  102.  int tmp_vectors[50];
  103.  char str[100];
  104.  
  105.  G_clear();
  106.  
  107.  T_set_self_rotation(alp,bet,gam);
  108.  T_self_rotation(verteces,tmp_verteces,m_no_verteces);
  109.  T_translation(tmp_verteces,tmp_verteces,m_no_verteces,x,y,z);
  110.  T_self_rotation(vectors,tmp_vectors,m_no_vectors);
  111.  M_render_polygon(p,tmp_verteces,tmp_vectors);
  112.  
  113.  sprintf(str,"3DGPL <%d %d %d %d>",alp,bet,gam,z); G_text(0,0,str,63);
  114.  if(p==&a) G_text(200,0,"AMBIENT",63);
  115.  if(p==&s) G_text(200,0,"SHADED",63);
  116.  if(p==&t) G_text(200,0,"TEXTURED",63);
  117.  G_text(0,190,"<ARROWS> <TAB> <A> <Z> <S> <X> <ENTER>",63);
  118.  
  119.  HW_blit();
  120.  
  121. if (alp<180)
  122.  alp++;
  123.  else { alp=0;  z-=20;}
  124. if (bet<360) bet+=3; else { bet=0; }
  125. if(z<-200)z=0;
  126.  
  127. }
  128.  
  129. /**********************************************************\
  130.  * assignes colours and starts the event loop.            *
  131. \**********************************************************/
  132.  
  133. int main(void)
  134. {
  135.  struct HW_colour pal[256];
  136.  int i;
  137.  
  138.  for(i=0;i<256;i++) { pal[i].hw_r=0; pal[i].hw_g=i; pal[i].hw_b=i; }
  139.  
  140.  
  141.  HW_open_screen(NULL,"3DGPL",pal,G_init_graphics()); 
  142.  T_init_math();
  143.  HW_run_event_loop(app_main,app_handler);
  144.  HW_close_screen();
  145.  
  146.  return(1);
  147. }
  148.  
  149. /**********************************************************/
  150.