| 1 | /* spin.c */ | 
|---|
| 2 |  | 
|---|
| 3 |  | 
|---|
| 4 | /* | 
|---|
| 5 |  * Spinning box.  This program is in the public domain. | 
|---|
| 6 |  * | 
|---|
| 7 |  * Brian Paul | 
|---|
| 8 |  */ | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | #include <math.h> | 
|---|
| 12 | #include <stdio.h> | 
|---|
| 13 |  | 
|---|
| 14 | #include <GL/glx.h>  | 
|---|
| 15 | #include <GL/gl.h>  | 
|---|
| 16 | #include "ui.h" | 
|---|
| 17 |  | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | static GLfloat Xrot, Xstep; | 
|---|
| 22 | static GLfloat Yrot, Ystep; | 
|---|
| 23 | static GLfloat Zrot, Zstep; | 
|---|
| 24 | static GLfloat Step = 5.0; | 
|---|
| 25 | static GLfloat Scale = 1.0; | 
|---|
| 26 | static GLuint Object; | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | static GLuint make_object( void ) | 
|---|
| 32 | { | 
|---|
| 33 |    GLuint list; | 
|---|
| 34 |  | 
|---|
| 35 |    list = glGenLists( 1 ); | 
|---|
| 36 |  | 
|---|
| 37 |    glNewList( list, GL_COMPILE ); | 
|---|
| 38 |  | 
|---|
| 39 |    glBegin( GL_LINE_LOOP ); | 
|---|
| 40 |    glColor3f( 1.0, 1.0, 1.0 ); | 
|---|
| 41 |    glVertex3f(  1.0,  0.5, -0.4 ); | 
|---|
| 42 |    glColor3f( 1.0, 0.0, 0.0 ); | 
|---|
| 43 |    glVertex3f(  1.0, -0.5, -0.4 ); | 
|---|
| 44 |    glColor3f( 0.0, 1.0, 0.0 ); | 
|---|
| 45 |    glVertex3f( -1.0, -0.5, -0.4 ); | 
|---|
| 46 |    glColor3f( 0.0, 0.0, 1.0 ); | 
|---|
| 47 |    glVertex3f( -1.0,  0.5, -0.4 ); | 
|---|
| 48 |    glEnd(); | 
|---|
| 49 |  | 
|---|
| 50 |    glColor3f( 1.0, 1.0, 1.0 ); | 
|---|
| 51 |  | 
|---|
| 52 |    glBegin( GL_LINE_LOOP ); | 
|---|
| 53 |    glVertex3f(  1.0,  0.5, 0.4 ); | 
|---|
| 54 |    glVertex3f(  1.0, -0.5, 0.4 ); | 
|---|
| 55 |    glVertex3f( -1.0, -0.5, 0.4 ); | 
|---|
| 56 |    glVertex3f( -1.0,  0.5, 0.4 ); | 
|---|
| 57 |    glEnd(); | 
|---|
| 58 |  | 
|---|
| 59 |    glBegin( GL_LINES ); | 
|---|
| 60 |    glVertex3f(  1.0,  0.5, -0.4 );   glVertex3f(  1.0,  0.5, 0.4 ); | 
|---|
| 61 |    glVertex3f(  1.0, -0.5, -0.4 );   glVertex3f(  1.0, -0.5, 0.4 ); | 
|---|
| 62 |    glVertex3f( -1.0, -0.5, -0.4 );   glVertex3f( -1.0, -0.5, 0.4 ); | 
|---|
| 63 |    glVertex3f( -1.0,  0.5, -0.4 );   glVertex3f( -1.0,  0.5, 0.4 ); | 
|---|
| 64 |    glEnd(); | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 |    glEndList(); | 
|---|
| 68 |  | 
|---|
| 69 |    return list; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 | void reshape( int width, int height ) | 
|---|
| 75 | { | 
|---|
| 76 |    glViewport(0, 0, (GLint)width, (GLint)height); | 
|---|
| 77 |    glMatrixMode(GL_PROJECTION); | 
|---|
| 78 |    glLoadIdentity(); | 
|---|
| 79 |    glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 ); | 
|---|
| 80 |    glMatrixMode(GL_MODELVIEW); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | GLenum key(int k, GLenum mask) | 
|---|
| 85 | { | 
|---|
| 86 |    switch (k) { | 
|---|
| 87 |    case KEY_ESCAPE: | 
|---|
| 88 |        exit(0); | 
|---|
| 89 |    } | 
|---|
| 90 |    return GL_FALSE; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | void draw( void ) | 
|---|
| 95 | { | 
|---|
| 96 |    glClear( GL_COLOR_BUFFER_BIT ); | 
|---|
| 97 |  | 
|---|
| 98 |    glPushMatrix(); | 
|---|
| 99 |  | 
|---|
| 100 |    glTranslatef( 0.0, 0.0, -10.0 ); | 
|---|
| 101 |    glScalef( Scale, Scale, Scale ); | 
|---|
| 102 |    if (Xstep) { | 
|---|
| 103 |       glRotatef( Xrot, 1.0, 0.0, 0.0 ); | 
|---|
| 104 |    } | 
|---|
| 105 |    else if (Ystep) { | 
|---|
| 106 |       glRotatef( Yrot, 0.0, 1.0, 0.0 ); | 
|---|
| 107 |    } | 
|---|
| 108 |    else { | 
|---|
| 109 |       glRotatef( Zrot, 0.0, 0.0, 1.0 ); | 
|---|
| 110 |    } | 
|---|
| 111 |  | 
|---|
| 112 |    glCallList( Object ); | 
|---|
| 113 |  | 
|---|
| 114 |    glPopMatrix(); | 
|---|
| 115 |  | 
|---|
| 116 |    glFlush(); | 
|---|
| 117 |    tkSwapBuffers(); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | void idle( void ) | 
|---|
| 122 | { | 
|---|
| 123 |    Xrot += Xstep; | 
|---|
| 124 |    Yrot += Ystep; | 
|---|
| 125 |    Zrot += Zstep; | 
|---|
| 126 |  | 
|---|
| 127 |    if (Xrot>=360.0) { | 
|---|
| 128 |       Xrot = Xstep = 0.0; | 
|---|
| 129 |       Ystep = Step; | 
|---|
| 130 |    } | 
|---|
| 131 |    else if (Yrot>=360.0) { | 
|---|
| 132 |       Yrot = Ystep = 0.0; | 
|---|
| 133 |       Zstep = Step; | 
|---|
| 134 |    } | 
|---|
| 135 |    else if (Zrot>=360.0) { | 
|---|
| 136 |       Zrot = Zstep = 0.0; | 
|---|
| 137 |       Xstep = Step; | 
|---|
| 138 |    } | 
|---|
| 139 |    draw(); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | void init(void) | 
|---|
| 143 | { | 
|---|
| 144 |    Object = make_object(); | 
|---|
| 145 |    glCullFace( GL_BACK ); | 
|---|
| 146 | /*   glEnable( GL_CULL_FACE );*/ | 
|---|
| 147 |    glDisable( GL_DITHER ); | 
|---|
| 148 |    glShadeModel( GL_FLAT ); | 
|---|
| 149 | /*   glEnable( GL_DEPTH_TEST ); */ | 
|---|
| 150 |  | 
|---|
| 151 |    Xrot = Yrot = Zrot = 0.0; | 
|---|
| 152 |    Xstep = Step; | 
|---|
| 153 |    Ystep = Zstep = 0.0; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | int main( int argc, char *argv[] ) | 
|---|
| 158 | { | 
|---|
| 159 |     return ui_loop(argc, argv, "spin"); | 
|---|
| 160 | } | 
|---|