Subversion Repositories Kolibri OS

Rev

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

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