Subversion Repositories Kolibri OS

Rev

Rev 8528 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.         Ïðèìåð âçÿò èç íàáîðà ïðèìåðîâ áèáëèîòåêè Mesa
  3.  
  4.         iadn
  5.         http://www.iadn.narod.ru
  6.         iadn@bk.ru
  7. */
  8.  
  9. /*
  10.  * 3-D gear wheels.  This program is in the public domain.
  11.  *
  12.  * Brian Paul
  13.  */
  14.  
  15. /*
  16.  * Newlib port by maxcodehack
  17.  */
  18.  
  19. #include <kos32sys.h>
  20. #include <kosgl.h> // TinyGL
  21. #include <string.h>
  22. #include <math.h>
  23.  
  24. #include "SysCall.h"
  25.  
  26. int Fps (long x, long y);
  27.  
  28. struct {
  29.         int x,y;
  30.         int dx,dy;
  31. } win;
  32.  
  33. #define KEY_ESC       1
  34. #define KEY_F            33
  35.  
  36. char *title = "Gears (F - full screen, ESC - exit)";
  37. char *fps    = "FPS:";
  38.  
  39. unsigned char FullScreen = 0;
  40. unsigned char skin = 0x34;
  41.  
  42. oskey_t key;
  43.  
  44. proc_info* pri;
  45. KOSGLContext cgl;
  46.  
  47. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  48. static GLint gear1, gear2, gear3;
  49. static GLfloat angle = 0.0;
  50.  
  51. static GLuint limit;
  52. static GLuint count = 1;
  53.  
  54. /*
  55.  * Draw a gear wheel.  You'll probably want to call this function when
  56.  * building a display list since we do a lot of trig here.
  57.  *
  58.  * Input:  inner_radius - radius of hole at center
  59.  *         outer_radius - radius at center of teeth
  60.  *         width - width of gear
  61.  *         teeth - number of teeth
  62.  *         tooth_depth - depth of tooth
  63.  */
  64. static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  65.                   GLint teeth, GLfloat tooth_depth )
  66. {
  67.         GLint i;
  68.         GLfloat r0, r1, r2;
  69.         GLfloat angle, da;
  70.         GLfloat u, v, len;
  71.  
  72.         r0 = inner_radius;
  73.         r1 = outer_radius - tooth_depth/2.0;
  74.         r2 = outer_radius + tooth_depth/2.0;
  75.  
  76.         da = 2.0*M_PI / teeth / 4.0;
  77.  
  78.         glShadeModel( GL_FLAT );
  79.  
  80.         glNormal3f( 0.0, 0.0, 1.0 );
  81.  
  82.         /* draw front face */
  83.         glBegin( GL_QUAD_STRIP );
  84.         for (i=0;i<=teeth;i++) {
  85.           angle = i * 2.0*M_PI / teeth;
  86.           glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  87.           glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 );
  88.           glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  89.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  90.         }
  91.         glEnd();
  92.  
  93.         /* draw front sides of teeth */
  94.         glBegin( GL_QUADS );
  95.         da = 2.0*M_PI / teeth / 4.0;
  96.         for (i=0;i<teeth;i++) {
  97.           angle = i * 2.0*M_PI / teeth;
  98.  
  99.           glVertex3f( r1*cos(angle),      r1*sin(angle),      width*0.5 );
  100.           glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   width*0.5 );
  101.           glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 );
  102.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 );
  103.         }
  104.         glEnd();
  105.  
  106.         glNormal3f( 0.0, 0.0, -1.0 );
  107.  
  108.         /* draw back face */
  109.         glBegin( GL_QUAD_STRIP );
  110.         for (i=0;i<=teeth;i++) {
  111.           angle = i * 2.0*M_PI / teeth;
  112.           glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 );
  113.           glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  114.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  115.           glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  116.         }
  117.         glEnd();
  118.  
  119.         /* draw back sides of teeth */
  120.         glBegin( GL_QUADS );
  121.         da = 2.0*M_PI / teeth / 4.0;
  122.         for (i=0;i<teeth;i++) {
  123.           angle = i * 2.0*M_PI / teeth;
  124.  
  125.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  126.           glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  127.           glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   -width*0.5 );
  128.           glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  129.         }
  130.         glEnd();
  131.  
  132.         /* draw outward faces of teeth */
  133.         glBegin( GL_QUAD_STRIP );
  134.         for (i=0;i<teeth;i++) {
  135.           angle = i * 2.0*M_PI / teeth;
  136.  
  137.           glVertex3f( r1*cos(angle),      r1*sin(angle),       width*0.5 );
  138.           glVertex3f( r1*cos(angle),      r1*sin(angle),      -width*0.5 );
  139.           u = r2*cos(angle+da) - r1*cos(angle);
  140.           v = r2*sin(angle+da) - r1*sin(angle);
  141.           len = sqrt( u*u + v*v );
  142.           u /= len;
  143.           v /= len;
  144.           glNormal3f( v, -u, 0.0 );
  145.           glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),    width*0.5 );
  146.           glVertex3f( r2*cos(angle+da),   r2*sin(angle+da),   -width*0.5 );
  147.           glNormal3f( cos(angle), sin(angle), 0.0 );
  148.           glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da),  width*0.5 );
  149.           glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 );
  150.           u = r1*cos(angle+3*da) - r2*cos(angle+2*da);
  151.           v = r1*sin(angle+3*da) - r2*sin(angle+2*da);
  152.           glNormal3f( v, -u, 0.0 );
  153.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da),  width*0.5 );
  154.           glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 );
  155.           glNormal3f( cos(angle), sin(angle), 0.0 );
  156.         }
  157.  
  158.         glVertex3f( r1*cos(0.0), r1*sin(0.0), width*0.5 );
  159.         glVertex3f( r1*cos(0.0), r1*sin(0.0), -width*0.5 );
  160.  
  161.         glEnd();
  162.  
  163.         glShadeModel( GL_SMOOTH );
  164.  
  165.         /* draw inside radius cylinder */
  166.         glBegin( GL_QUAD_STRIP );
  167.         for (i=0;i<=teeth;i++) {
  168.           angle = i * 2.0*M_PI / teeth;
  169.           glNormal3f( -cos(angle), -sin(angle), 0.0 );
  170.           glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 );
  171.           glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 );
  172.         }
  173.         glEnd();
  174. }
  175.  
  176. void init()
  177. {
  178.         static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 };
  179.         static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 };
  180.         static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 };
  181.         static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 };
  182.  
  183.         glLightfv( GL_LIGHT0, GL_POSITION, pos );
  184.         glEnable( GL_CULL_FACE );
  185.         glEnable( GL_LIGHTING );
  186.         glEnable( GL_LIGHT0 );
  187.         glEnable( GL_DEPTH_TEST );
  188.  
  189.         /* make the gears */
  190.         gear1 = glGenLists(1);
  191.         glNewList(gear1, GL_COMPILE);
  192.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red );
  193.         gear( 1.0, 4.0, 1.0, 20, 0.7 );
  194.         glEndList();
  195.  
  196.         gear2 = glGenLists(1);
  197.         glNewList(gear2, GL_COMPILE);
  198.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
  199.         gear( 0.5, 2.0, 2.0, 10, 0.7 );
  200.         glEndList();
  201.  
  202.         gear3 = glGenLists(1);
  203.         glNewList(gear3, GL_COMPILE);
  204.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue );
  205.         gear( 1.3, 2.0, 0.5, 10, 0.7 );
  206.         glEndList();
  207.  
  208.         glEnable( GL_NORMALIZE );
  209.  
  210.         glViewport(0, 0, (GLint)500, (GLint)480);
  211.         glMatrixMode(GL_PROJECTION);
  212.         glLoadIdentity();
  213.         glFrustum( -1.0, 1.0, -1, 1, 5.0, 60.0 );
  214.         glMatrixMode(GL_MODELVIEW);
  215.         glLoadIdentity();
  216.         glTranslatef( 0.0, 0.0, -40.0 );
  217.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  218. }
  219.  
  220. void reshape()
  221. {  
  222.         get_proc_info((char*)pri);
  223.         glViewport(0, 0, pri->width, pri->height-20);
  224.         glMatrixMode(GL_PROJECTION);
  225.         glLoadIdentity();
  226.         gluPerspective(45.0, (GLfloat)pri->width/pri->height, 1.0, 60.0);
  227.         glTranslatef( 0.0, 0.0, 20.0 );
  228.         glMatrixMode(GL_MODELVIEW);
  229.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  230. }
  231.  
  232. void disabletgl()
  233. {
  234.         kosglDestroyContext(cgl);
  235.         delete pri;
  236. }
  237.  
  238. void kos_text(int x, int y, int color, const char* text, int len)
  239. {
  240.         asm volatile ("int $0x40"::"a"(4),"b"((x<<16) | y),"c"(color),"d"((unsigned long)text),"S"(len));
  241. };
  242.  
  243. void Title()
  244. {
  245.      kos_text(300,8,0x10ffffff,fps,strlen(fps));
  246.          /*kos_text(180,8,0x90ffffff,title2,strlen(title2));
  247.      kos_text(600,8,0x90ffffff,title3,strlen(title3));*/
  248. }
  249.  
  250. void draw_window()
  251. {
  252.         // start redraw
  253.         begin_draw();
  254.         // define&draw window
  255.         sys_create_window(win.x, win.y, win.dx, win.dy, title, 0, /*TYPEWIN(0,0,0,1,skin,0,0,0)*/skin);
  256.     // end redraw
  257.     end_draw();
  258.     // display string
  259.     Title();
  260. }
  261.  
  262. int main()          
  263. {
  264.         win.x = 100;
  265.         win.y = 100;
  266.         win.dx = 400;
  267.         win.dy = 400;    
  268.  
  269.         draw_window();
  270.  
  271.         cgl = kosglCreateContext( 0, 0);
  272.         kosglMakeCurrent( 0, 0, win.dx, win.dy, cgl);
  273.  
  274.         init();
  275.  
  276.         pri = new proc_info;
  277.         SysCall(66,1,1);
  278.  
  279.         reshape();
  280.  
  281.         do {
  282.                 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  283.  
  284.                 glPushMatrix();
  285.                 glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  286.                 glRotatef( view_roty, 0.0, 1.0, 0.0 );
  287.                 glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  288.  
  289.                 glPushMatrix();
  290.                 glTranslatef( -2.0, -2.0, 0.0 );
  291.                 glRotatef( angle, 0.0, 0.0, 1.0 );
  292.                 glCallList(gear1);
  293.                 glPopMatrix();
  294.  
  295.                 glPushMatrix();
  296.                 glTranslatef( 4.1, -2.0, 0.0 );
  297.                 glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  298.                 glCallList(gear2);
  299.                 glPopMatrix();
  300.  
  301.                 glPushMatrix();
  302.                 glTranslatef( -2.1, 4.2, 0.0 );
  303.                 glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
  304.                 glCallList(gear3);
  305.                 glPopMatrix();
  306.  
  307.                 glPopMatrix();
  308.  
  309.                 kosglSwapBuffers();
  310.  
  311.                 angle += 0.01 + 0.3 * Fps (330, 8);
  312.                                
  313.                 switch(check_os_event())
  314.                 {
  315.                         case 1:
  316.                                 draw_window();
  317.                                 reshape();
  318.                                 break;
  319.                                          
  320.                         case 2:          
  321.                                    key = get_key();
  322.                                    switch(key.code) {
  323.                                                 case KEY_F:
  324.                                                         if(!FullScreen){                                                                         
  325.                                                                 skin=0x01;
  326.                                                                 SysCall(67,0,0,SysCall(14)>>16,SysCall(14)&0xffff);
  327.                                                                 draw_window();
  328.                                                                 reshape();
  329.                                                                 FullScreen = 1;
  330.                                                         }
  331.                                                         else{
  332.                                                                 skin=0x34;
  333.                                                                 draw_window();
  334.                                                                 SysCall(67,win.x,win.y,win.dx,win.dy);
  335.                                                                 reshape();
  336.                                                                 FullScreen = 0;
  337.                                                         };
  338.                                                         break;
  339.                          
  340.                                            case KEY_ESC:
  341.                                                                 disabletgl();
  342.                                                                 return 0;
  343.                                         }
  344.                                         break;
  345.                                                                
  346.                           case 3:
  347.                                         disabletgl();
  348.                                         return 0;
  349.                 }
  350.         } while(1);
  351. }
  352.