eglgears.c revision 32001f49
132001f49Smrg/* 232001f49Smrg * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. 332001f49Smrg * 432001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a 532001f49Smrg * copy of this software and associated documentation files (the "Software"), 632001f49Smrg * to deal in the Software without restriction, including without limitation 732001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 832001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the 932001f49Smrg * Software is furnished to do so, subject to the following conditions: 1032001f49Smrg * 1132001f49Smrg * The above copyright notice and this permission notice shall be included 1232001f49Smrg * in all copies or substantial portions of the Software. 1332001f49Smrg * 1432001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1532001f49Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1732001f49Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 1832001f49Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 1932001f49Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2032001f49Smrg */ 2132001f49Smrg 2232001f49Smrg/* 2332001f49Smrg * This is a port of the infamous "glxgears" demo to straight EGL 2432001f49Smrg * Port by Dane Rushton 10 July 2005 2532001f49Smrg * 2632001f49Smrg * No command line options. 2732001f49Smrg * Program runs for 5 seconds then exits, outputing framerate to console 2832001f49Smrg */ 2932001f49Smrg 3032001f49Smrg#include <math.h> 3132001f49Smrg#include "gl_wrap.h" 3232001f49Smrg#include <EGL/egl.h> 3332001f49Smrg 3432001f49Smrg#include "eglut.h" 3532001f49Smrg 3632001f49Smrgstatic GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0; 3732001f49Smrgstatic GLint gear1, gear2, gear3; 3832001f49Smrgstatic GLfloat angle = 0.0; 3932001f49Smrg 4032001f49Smrg/* 4132001f49Smrg * 4232001f49Smrg * Draw a gear wheel. You'll probably want to call this function when 4332001f49Smrg * building a display list since we do a lot of trig here. 4432001f49Smrg * 4532001f49Smrg * Input: inner_radius - radius of hole at center 4632001f49Smrg * outer_radius - radius at center of teeth 4732001f49Smrg * width - width of gear 4832001f49Smrg * teeth - number of teeth 4932001f49Smrg * tooth_depth - depth of tooth 5032001f49Smrg */ 5132001f49Smrgstatic void 5232001f49Smrggear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, 5332001f49Smrg GLint teeth, GLfloat tooth_depth) 5432001f49Smrg{ 5532001f49Smrg GLint i; 5632001f49Smrg GLfloat r0, r1, r2; 5732001f49Smrg GLfloat angle, da; 5832001f49Smrg GLfloat u, v, len; 5932001f49Smrg 6032001f49Smrg r0 = inner_radius; 6132001f49Smrg r1 = outer_radius - tooth_depth / 2.0; 6232001f49Smrg r2 = outer_radius + tooth_depth / 2.0; 6332001f49Smrg 6432001f49Smrg da = 2.0 * M_PI / teeth / 4.0; 6532001f49Smrg 6632001f49Smrg glShadeModel(GL_FLAT); 6732001f49Smrg 6832001f49Smrg glNormal3f(0.0, 0.0, 1.0); 6932001f49Smrg 7032001f49Smrg /* draw front face */ 7132001f49Smrg glBegin(GL_QUAD_STRIP); 7232001f49Smrg for (i = 0; i <= teeth; i++) { 7332001f49Smrg angle = i * 2.0 * M_PI / teeth; 7432001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); 7532001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); 7632001f49Smrg if (i < teeth) { 7732001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); 7832001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 7932001f49Smrg width * 0.5); 8032001f49Smrg } 8132001f49Smrg } 8232001f49Smrg glEnd(); 8332001f49Smrg 8432001f49Smrg /* draw front sides of teeth */ 8532001f49Smrg glBegin(GL_QUADS); 8632001f49Smrg da = 2.0 * M_PI / teeth / 4.0; 8732001f49Smrg for (i = 0; i < teeth; i++) { 8832001f49Smrg angle = i * 2.0 * M_PI / teeth; 8932001f49Smrg 9032001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); 9132001f49Smrg glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); 9232001f49Smrg glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), 9332001f49Smrg width * 0.5); 9432001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 9532001f49Smrg width * 0.5); 9632001f49Smrg } 9732001f49Smrg glEnd(); 9832001f49Smrg 9932001f49Smrg glNormal3f(0.0, 0.0, -1.0); 10032001f49Smrg 10132001f49Smrg /* draw back face */ 10232001f49Smrg glBegin(GL_QUAD_STRIP); 10332001f49Smrg for (i = 0; i <= teeth; i++) { 10432001f49Smrg angle = i * 2.0 * M_PI / teeth; 10532001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); 10632001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); 10732001f49Smrg if (i < teeth) { 10832001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 10932001f49Smrg -width * 0.5); 11032001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); 11132001f49Smrg } 11232001f49Smrg } 11332001f49Smrg glEnd(); 11432001f49Smrg 11532001f49Smrg /* draw back sides of teeth */ 11632001f49Smrg glBegin(GL_QUADS); 11732001f49Smrg da = 2.0 * M_PI / teeth / 4.0; 11832001f49Smrg for (i = 0; i < teeth; i++) { 11932001f49Smrg angle = i * 2.0 * M_PI / teeth; 12032001f49Smrg 12132001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 12232001f49Smrg -width * 0.5); 12332001f49Smrg glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), 12432001f49Smrg -width * 0.5); 12532001f49Smrg glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); 12632001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); 12732001f49Smrg } 12832001f49Smrg glEnd(); 12932001f49Smrg 13032001f49Smrg /* draw outward faces of teeth */ 13132001f49Smrg glBegin(GL_QUAD_STRIP); 13232001f49Smrg for (i = 0; i < teeth; i++) { 13332001f49Smrg angle = i * 2.0 * M_PI / teeth; 13432001f49Smrg 13532001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5); 13632001f49Smrg glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5); 13732001f49Smrg u = r2 * cos(angle + da) - r1 * cos(angle); 13832001f49Smrg v = r2 * sin(angle + da) - r1 * sin(angle); 13932001f49Smrg len = sqrt(u * u + v * v); 14032001f49Smrg u /= len; 14132001f49Smrg v /= len; 14232001f49Smrg glNormal3f(v, -u, 0.0); 14332001f49Smrg glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5); 14432001f49Smrg glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5); 14532001f49Smrg glNormal3f(cos(angle), sin(angle), 0.0); 14632001f49Smrg glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), 14732001f49Smrg width * 0.5); 14832001f49Smrg glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), 14932001f49Smrg -width * 0.5); 15032001f49Smrg u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da); 15132001f49Smrg v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da); 15232001f49Smrg glNormal3f(v, -u, 0.0); 15332001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 15432001f49Smrg width * 0.5); 15532001f49Smrg glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), 15632001f49Smrg -width * 0.5); 15732001f49Smrg glNormal3f(cos(angle), sin(angle), 0.0); 15832001f49Smrg } 15932001f49Smrg 16032001f49Smrg glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5); 16132001f49Smrg glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5); 16232001f49Smrg 16332001f49Smrg glEnd(); 16432001f49Smrg 16532001f49Smrg glShadeModel(GL_SMOOTH); 16632001f49Smrg 16732001f49Smrg /* draw inside radius cylinder */ 16832001f49Smrg glBegin(GL_QUAD_STRIP); 16932001f49Smrg for (i = 0; i <= teeth; i++) { 17032001f49Smrg angle = i * 2.0 * M_PI / teeth; 17132001f49Smrg glNormal3f(-cos(angle), -sin(angle), 0.0); 17232001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5); 17332001f49Smrg glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5); 17432001f49Smrg } 17532001f49Smrg glEnd(); 17632001f49Smrg} 17732001f49Smrg 17832001f49Smrg 17932001f49Smrgstatic void 18032001f49Smrgdraw(void) 18132001f49Smrg{ 18232001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 18332001f49Smrg 18432001f49Smrg glPushMatrix(); 18532001f49Smrg glRotatef(view_rotx, 1.0, 0.0, 0.0); 18632001f49Smrg glRotatef(view_roty, 0.0, 1.0, 0.0); 18732001f49Smrg glRotatef(view_rotz, 0.0, 0.0, 1.0); 18832001f49Smrg 18932001f49Smrg glPushMatrix(); 19032001f49Smrg glTranslatef(-3.0, -2.0, 0.0); 19132001f49Smrg glRotatef(angle, 0.0, 0.0, 1.0); 19232001f49Smrg glCallList(gear1); 19332001f49Smrg glPopMatrix(); 19432001f49Smrg 19532001f49Smrg glPushMatrix(); 19632001f49Smrg glTranslatef(3.1, -2.0, 0.0); 19732001f49Smrg glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0); 19832001f49Smrg glCallList(gear2); 19932001f49Smrg glPopMatrix(); 20032001f49Smrg 20132001f49Smrg glPushMatrix(); 20232001f49Smrg glTranslatef(-3.1, 4.2, 0.0); 20332001f49Smrg glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0); 20432001f49Smrg glCallList(gear3); 20532001f49Smrg glPopMatrix(); 20632001f49Smrg 20732001f49Smrg glPopMatrix(); 20832001f49Smrg} 20932001f49Smrg 21032001f49Smrg 21132001f49Smrgstatic void 21232001f49Smrgidle(void) 21332001f49Smrg{ 21432001f49Smrg static double t0 = -1.; 21532001f49Smrg double dt, t = eglutGet(EGLUT_ELAPSED_TIME) / 1000.0; 21632001f49Smrg if (t0 < 0.0) 21732001f49Smrg t0 = t; 21832001f49Smrg dt = t - t0; 21932001f49Smrg t0 = t; 22032001f49Smrg 22132001f49Smrg angle += 70.0 * dt; /* 70 degrees per second */ 22232001f49Smrg angle = fmod(angle, 360.0); /* prevents eventual overflow */ 22332001f49Smrg 22432001f49Smrg eglutPostRedisplay(); 22532001f49Smrg} 22632001f49Smrg 22732001f49Smrg/* new window size or exposure */ 22832001f49Smrgstatic void 22932001f49Smrgreshape(int width, int height) 23032001f49Smrg{ 23132001f49Smrg GLfloat h = (GLfloat) height / (GLfloat) width; 23232001f49Smrg 23332001f49Smrg glViewport(0, 0, (GLint) width, (GLint) height); 23432001f49Smrg 23532001f49Smrg glMatrixMode(GL_PROJECTION); 23632001f49Smrg glLoadIdentity(); 23732001f49Smrg glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0); 23832001f49Smrg 23932001f49Smrg glMatrixMode(GL_MODELVIEW); 24032001f49Smrg glLoadIdentity(); 24132001f49Smrg glTranslatef(0.0, 0.0, -40.0); 24232001f49Smrg} 24332001f49Smrg 24432001f49Smrg 24532001f49Smrg 24632001f49Smrgstatic void 24732001f49Smrginit(void) 24832001f49Smrg{ 24932001f49Smrg static GLfloat pos[4] = { 5.0, 5.0, 10.0, 0.0 }; 25032001f49Smrg static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 }; 25132001f49Smrg static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 }; 25232001f49Smrg static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 }; 25332001f49Smrg 25432001f49Smrg glLightfv(GL_LIGHT0, GL_POSITION, pos); 25532001f49Smrg glEnable(GL_CULL_FACE); 25632001f49Smrg glEnable(GL_LIGHTING); 25732001f49Smrg glEnable(GL_LIGHT0); 25832001f49Smrg glEnable(GL_DEPTH_TEST); 25932001f49Smrg 26032001f49Smrg /* make the gears */ 26132001f49Smrg gear1 = glGenLists(1); 26232001f49Smrg glNewList(gear1, GL_COMPILE); 26332001f49Smrg glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); 26432001f49Smrg gear(1.0, 4.0, 1.0, 20, 0.7); 26532001f49Smrg glEndList(); 26632001f49Smrg 26732001f49Smrg gear2 = glGenLists(1); 26832001f49Smrg glNewList(gear2, GL_COMPILE); 26932001f49Smrg glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); 27032001f49Smrg gear(0.5, 2.0, 2.0, 10, 0.7); 27132001f49Smrg glEndList(); 27232001f49Smrg 27332001f49Smrg gear3 = glGenLists(1); 27432001f49Smrg glNewList(gear3, GL_COMPILE); 27532001f49Smrg glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); 27632001f49Smrg gear(1.3, 2.0, 0.5, 10, 0.7); 27732001f49Smrg glEndList(); 27832001f49Smrg 27932001f49Smrg glEnable(GL_NORMALIZE); 28032001f49Smrg} 28132001f49Smrg 28232001f49Smrgint 28332001f49Smrgmain(int argc, char *argv[]) 28432001f49Smrg{ 28532001f49Smrg eglutInitWindowSize(300, 300); 28632001f49Smrg eglutInitAPIMask(EGLUT_OPENGL_BIT); 28732001f49Smrg eglutInit(argc, argv); 28832001f49Smrg 28932001f49Smrg eglutCreateWindow("eglgears"); 29032001f49Smrg 29132001f49Smrg eglutIdleFunc(idle); 29232001f49Smrg eglutReshapeFunc(reshape); 29332001f49Smrg eglutDisplayFunc(draw); 29432001f49Smrg 29532001f49Smrg init(); 29632001f49Smrg glDrawBuffer(GL_BACK); 29732001f49Smrg 29832001f49Smrg eglutMainLoop(); 29932001f49Smrg 30032001f49Smrg return 0; 30132001f49Smrg} 302