Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice shall be included
  12.  * in all copies or substantial portions of the Software.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  17.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  */
  21.  
  22. /*
  23.  * This is a port of the infamous "glxgears" demo to straight EGL
  24.  * Port by Dane Rushton 10 July 2005
  25.  *
  26.  * No command line options.
  27.  * Program runs for 5 seconds then exits, outputing framerate to console
  28.  */
  29.  
  30. #include <math.h>
  31. #include <sys/time.h>
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include "eglut.h"
  35.  
  36. static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
  37. static GLint gear1, gear2, gear3;
  38. GLfloat angle = 0.0;
  39.  
  40. /*
  41.  *
  42.  *  Draw a gear wheel.  You'll probably want to call this function when
  43.  *  building a display list since we do a lot of trig here.
  44.  *
  45.  *  Input:  inner_radius - radius of hole at center
  46.  *          outer_radius - radius at center of teeth
  47.  *          width - width of gear
  48.  *          teeth - number of teeth
  49.  *          tooth_depth - depth of tooth
  50.  */
  51. static void gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
  52.      GLint teeth, GLfloat tooth_depth)
  53. {
  54.    GLint i;
  55.    GLfloat r0, r1, r2;
  56.    GLfloat angle, da;
  57.    GLfloat u, v, len;
  58.  
  59.    r0 = inner_radius;
  60.    r1 = outer_radius - tooth_depth / 2.0;
  61.    r2 = outer_radius + tooth_depth / 2.0;
  62.  
  63.    da = 2.0 * M_PI / teeth / 4.0;
  64.  
  65.    glShadeModel(GL_FLAT);
  66.  
  67.    glNormal3f(0.0, 0.0, 1.0);
  68.  
  69.    /* draw front face */
  70.    glBegin(GL_QUAD_STRIP);
  71.    for (i = 0; i <= teeth; i++) {
  72.       angle = i * 2.0 * M_PI / teeth;
  73.       glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  74.       glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  75.       if (i < teeth) {
  76.          glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  77.          glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  78.                     width * 0.5);
  79.       }
  80.    }
  81.    glEnd();
  82.  
  83.    /* draw front sides of teeth */
  84.    glBegin(GL_QUADS);
  85.    da = 2.0 * M_PI / teeth / 4.0;
  86.    for (i = 0; i < teeth; i++) {
  87.       angle = i * 2.0 * M_PI / teeth;
  88.  
  89.       glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  90.       glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  91.       glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  92.                  width * 0.5);
  93.       glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  94.                  width * 0.5);
  95.    }
  96.    glEnd();
  97.  
  98.    glNormal3f(0.0, 0.0, -1.0);
  99.  
  100.    /* draw back face */
  101.    glBegin(GL_QUAD_STRIP);
  102.    for (i = 0; i <= teeth; i++) {
  103.       angle = i * 2.0 * M_PI / teeth;
  104.       glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  105.       glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  106.       if (i < teeth) {
  107.          glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  108.                     -width * 0.5);
  109.          glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  110.       }
  111.    }
  112.    glEnd();
  113.  
  114.    /* draw back sides of teeth */
  115.    glBegin(GL_QUADS);
  116.    da = 2.0 * M_PI / teeth / 4.0;
  117.    for (i = 0; i < teeth; i++) {
  118.       angle = i * 2.0 * M_PI / teeth;
  119.  
  120.       glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  121.                  -width * 0.5);
  122.       glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  123.                  -width * 0.5);
  124.       glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  125.       glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  126.    }
  127.    glEnd();
  128.  
  129.    /* draw outward faces of teeth */
  130.    glBegin(GL_QUAD_STRIP);
  131.    for (i = 0; i < teeth; i++) {
  132.       angle = i * 2.0 * M_PI / teeth;
  133.  
  134.       glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
  135.       glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
  136.       u = r2 * cos(angle + da) - r1 * cos(angle);
  137.       v = r2 * sin(angle + da) - r1 * sin(angle);
  138.       len = sqrt(u * u + v * v);
  139.       u /= len;
  140.       v /= len;
  141.       glNormal3f(v, -u, 0.0);
  142.       glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
  143.       glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
  144.       glNormal3f(cos(angle), sin(angle), 0.0);
  145.       glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  146.                  width * 0.5);
  147.       glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da),
  148.                  -width * 0.5);
  149.       u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
  150.       v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
  151.       glNormal3f(v, -u, 0.0);
  152.       glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  153.                  width * 0.5);
  154.       glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da),
  155.                  -width * 0.5);
  156.       glNormal3f(cos(angle), sin(angle), 0.0);
  157.    }
  158.  
  159.    glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
  160.    glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
  161.  
  162.    glEnd();
  163.  
  164.    glShadeModel(GL_SMOOTH);
  165.  
  166.    /* draw inside radius cylinder */
  167.    glBegin(GL_QUAD_STRIP);
  168.    for (i = 0; i <= teeth; i++) {
  169.       angle = i * 2.0 * M_PI / teeth;
  170.       glNormal3f(-cos(angle), -sin(angle), 0.0);
  171.       glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
  172.       glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
  173.    }
  174.    glEnd();
  175. }
  176.  
  177.  
  178. void draw(void)
  179. {
  180.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  181.  
  182.    glPushMatrix();
  183.    glRotatef(view_rotx, 1.0, 0.0, 0.0);
  184.    glRotatef(view_roty, 0.0, 1.0, 0.0);
  185.    glRotatef(view_rotz, 0.0, 0.0, 1.0);
  186.  
  187.    glPushMatrix();
  188.    glTranslatef(-3.0, -2.0, 0.0);
  189.    glRotatef(angle, 0.0, 0.0, 1.0);
  190.    glCallList(gear1);
  191.    glPopMatrix();
  192.  
  193.    glPushMatrix();
  194.    glTranslatef(3.1, -2.0, 0.0);
  195.    glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
  196.    glCallList(gear2);
  197.    glPopMatrix();
  198.  
  199.    glPushMatrix();
  200.    glTranslatef(-3.1, 4.2, 0.0);
  201.    glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
  202.    glCallList(gear3);
  203.    glPopMatrix();
  204.  
  205.    glPopMatrix();
  206. }
  207.  
  208.  
  209. /* new window size or exposure */
  210. void reshape(int width, int height)
  211. {
  212.    GLfloat h = (GLfloat) height / (GLfloat) width;
  213.  
  214.    glViewport(0, 0, (GLint) width, (GLint) height);
  215.  
  216.    glMatrixMode(GL_PROJECTION);
  217.    glLoadIdentity();
  218.    glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
  219.  
  220.    glMatrixMode(GL_MODELVIEW);
  221.    glLoadIdentity();
  222.    glTranslatef(0.0, 0.0, -40.0);
  223. }
  224.  
  225. void init(void)
  226. {
  227.    static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 };
  228.    static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
  229.    static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
  230.    static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
  231.  
  232.    glLightfv(GL_LIGHT0, GL_POSITION, pos);
  233.    glEnable(GL_CULL_FACE);
  234.    glEnable(GL_LIGHTING);
  235.    glEnable(GL_LIGHT0);
  236.    glEnable(GL_DEPTH_TEST);
  237.  
  238.    /* make the gears */
  239.    gear1 = glGenLists(1);
  240.    glNewList(gear1, GL_COMPILE);
  241.    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
  242.    gear(1.0, 4.0, 1.0, 20, 0.7);
  243.    glEndList();
  244.  
  245.    gear2 = glGenLists(1);
  246.    glNewList(gear2, GL_COMPILE);
  247.    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  248.    gear(0.5, 2.0, 2.0, 10, 0.7);
  249.    glEndList();
  250.  
  251.    gear3 = glGenLists(1);
  252.    glNewList(gear3, GL_COMPILE);
  253.    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
  254.    gear(1.3, 2.0, 0.5, 10, 0.7);
  255.    glEndList();
  256.  
  257.    glEnable(GL_NORMALIZE);
  258. }
  259.  
  260. void keyboard(unsigned char key)
  261. {
  262.     if (key == EGLUT_KEY_LEFT) {
  263.         view_roty += 5.0;
  264.     }
  265.     else if (key == EGLUT_KEY_RIGHT) {
  266.         view_roty -= 5.0;
  267.     }
  268.     else if (key == EGLUT_KEY_UP) {
  269.         view_rotx += 5.0;
  270.     }
  271.     else if (key == EGLUT_KEY_DOWN) {
  272.         view_rotx -= 5.0;
  273.     }
  274. }
  275.  
  276.