Subversion Repositories Kolibri OS

Rev

Rev 1789 | 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. #include<menuet/os.h>
  16. #include <kosgl.h> //TinyGL
  17. #include <string.h>
  18. #include <math.h>
  19.  
  20. #include "SysCall.h"
  21. #include "ProcessTab.h"
  22.  
  23. int Fps (long x, long y);
  24. extern "C"{
  25. int main(void);
  26. }
  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 *title1 = "TinyGL in KolibriOS";
  37. char *title2 = "F full screen";
  38. char *title3 = "ESC - exit";
  39. char *fps    = "FPS:";
  40.  
  41. unsigned char FullScreen = 0;
  42. unsigned char skin = 3;
  43.  
  44. process_table_entry_* 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( void )
  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.    __menuet__get_process_table((process_table_entry*)pri,-1);
  223.    glViewport(0, 0, pri->winx_size, pri->winy_size-20);
  224.    glMatrixMode(GL_PROJECTION);
  225.    glLoadIdentity();
  226.    gluPerspective(45.0, (GLfloat)pri->winx_size/pri->winy_size, 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 Title()
  239. {
  240.      __menuet__write_text(300,8,0x10ffffff,fps,strlen(fps));
  241.      __menuet__write_text(8,8,0x10ffffff,title1,strlen(title1));
  242.          __menuet__write_text(180,8,0x00ffffff,title2,strlen(title2));
  243.      __menuet__write_text(600,8,0x00ffffff,title3,strlen(title3));
  244. }
  245.  
  246. void draw_window(void)
  247. {
  248.         // start redraw
  249.         __menuet__window_redraw(1);
  250.         // define&draw window
  251.         __menuet__define_window(win.x,win.y,win.dx,win.dy,TYPEWIN(0,0,0,1,skin,0,0,0),0,0);
  252.     // end redraw
  253.     __menuet__window_redraw(2);
  254.     // display string
  255.     Title();
  256. }
  257.  
  258. int main(void)          
  259. {
  260.  
  261.   win.x = 100;
  262.   win.y = 100;
  263.   win.dx = 400;
  264.   win.dy = 400;  
  265.  
  266.   draw_window();
  267.  
  268.   cgl = kosglCreateContext( 0, 0);
  269.   kosglMakeCurrent( 0, 20, win.dx, win.dy-20, cgl);
  270.  
  271.   init();
  272.  
  273.   pri=new process_table_entry_;
  274.   SysCall(66,1,1);
  275.  
  276.   reshape();
  277.  
  278. do{
  279.  
  280.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  281.  
  282.    glPushMatrix();
  283.    glRotatef( view_rotx, 1.0, 0.0, 0.0 );
  284.    glRotatef( view_roty, 0.0, 1.0, 0.0 );
  285.    glRotatef( view_rotz, 0.0, 0.0, 1.0 );
  286.  
  287.    glPushMatrix();
  288.    glTranslatef( -2.0, -2.0, 0.0 );
  289.    glRotatef( angle, 0.0, 0.0, 1.0 );
  290.    glCallList(gear1);
  291.    glPopMatrix();
  292.  
  293.    glPushMatrix();
  294.    glTranslatef( 4.1, -2.0, 0.0 );
  295.    glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 );
  296.    glCallList(gear2);
  297.    glPopMatrix();
  298.  
  299.    glPushMatrix();
  300.    glTranslatef( -2.1, 4.2, 0.0 );
  301.    glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 );
  302.    glCallList(gear3);
  303.    glPopMatrix();
  304.  
  305.    glPopMatrix();
  306.    
  307.  kosglSwapBuffers();
  308.  
  309.  angle += 0.01 + 0.3* Fps (330,8);
  310.                        
  311.                   switch(SysCall(11))
  312.                       {
  313.                           case 1: draw_window();                                                 
  314.                                                   reshape();
  315.                                                   break;
  316.                                                  
  317.                           case 2:                        
  318.                                    switch(__menuet__getkey()){
  319.  
  320.                                                    case KEY_F:
  321.                                     if(!FullScreen){                                                                     
  322.                                                                          skin=0;
  323.                                                                          SysCall(67,0,0,SysCall(14)>>16,SysCall(14)&0xffff);
  324.                                                                          draw_window();
  325.                                                                          reshape();
  326.                                                                          FullScreen = 1;
  327.                                                                         }
  328.                                                                         else{
  329.                                                                          skin=3;
  330.                                                                          draw_window();
  331.                                                                          SysCall(67,win.x,win.y,win.dx,win.dy);
  332.                                                                          reshape();
  333.                                                                          FullScreen = 0;
  334.                                                                         };
  335.                                                                         break;
  336.                          
  337.                            case KEY_ESC: disabletgl();
  338.                                                                                  return 0;}
  339.                                                                                  break;
  340.                                                                        
  341.                                   case 3: disabletgl();
  342.                                                   return 0;
  343.                       }
  344. }while(1);
  345. }
  346.