jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Example of how one might use GLUT with the 3Dfx driver in full-screen mode. |
| 4 | * Note: this only works with X since we're using Mesa's GLX "hack" for |
| 5 | * using Glide. |
| 6 | * |
| 7 | * Goals: |
| 8 | * easy setup and input event handling with GLUT |
| 9 | * use 3Dfx hardware |
| 10 | * automatically set MESA environment variables |
| 11 | * don't lose mouse input focus |
| 12 | * |
| 13 | * Brian Paul This file is in the public domain. |
| 14 | */ |
| 15 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <math.h> |
| 20 | #include <GL/glut.h> |
| 21 | |
| 22 | |
| 23 | #define WIDTH 640 |
| 24 | #define HEIGHT 480 |
| 25 | |
| 26 | |
| 27 | static int Window = 0; |
| 28 | static int ScreenWidth, ScreenHeight; |
| 29 | static GLuint Torus = 0; |
| 30 | static GLfloat Xrot = 0.0, Yrot = 0.0; |
| 31 | |
| 32 | |
| 33 | |
| 34 | static void Display( void ) |
| 35 | { |
| 36 | static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0}; |
| 37 | static GLfloat red[4] = {1.0, 0.2, 0.2, 1.0}; |
| 38 | static GLfloat green[4] = {0.2, 1.0, 0.2, 1.0}; |
| 39 | |
| 40 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 41 | |
| 42 | glPushMatrix(); |
| 43 | glRotatef(Xrot, 1, 0, 0); |
| 44 | glRotatef(Yrot, 0, 1, 0); |
| 45 | |
| 46 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue); |
| 47 | glCallList(Torus); |
| 48 | |
| 49 | glRotatef(90.0, 1, 0, 0); |
| 50 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red); |
| 51 | glCallList(Torus); |
| 52 | |
| 53 | glRotatef(90.0, 0, 1, 0); |
| 54 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green); |
| 55 | glCallList(Torus); |
| 56 | |
| 57 | glPopMatrix(); |
| 58 | |
| 59 | glutSwapBuffers(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void Reshape( int width, int height ) |
| 64 | { |
| 65 | float ratio = (float) width / (float) height; |
| 66 | |
| 67 | ScreenWidth = width; |
| 68 | ScreenHeight = height; |
| 69 | |
| 70 | /* |
| 71 | * The 3Dfx driver is limited to 640 x 480 but the X window may be larger. |
| 72 | * Enforce that here. |
| 73 | */ |
| 74 | if (width > WIDTH) |
| 75 | width = WIDTH; |
| 76 | if (height > HEIGHT) |
| 77 | height = HEIGHT; |
| 78 | |
| 79 | glViewport( 0, 0, width, height ); |
| 80 | glMatrixMode( GL_PROJECTION ); |
| 81 | glLoadIdentity(); |
| 82 | glFrustum( -ratio, ratio, -1.0, 1.0, 5.0, 30.0 ); |
| 83 | glMatrixMode( GL_MODELVIEW ); |
| 84 | glLoadIdentity(); |
| 85 | glTranslatef( 0.0, 0.0, -20.0 ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static void Key( unsigned char key, int x, int y ) |
| 90 | { |
| 91 | (void) x; |
| 92 | (void) y; |
| 93 | switch (key) { |
| 94 | case 27: |
| 95 | glutDestroyWindow(Window); |
| 96 | exit(0); |
| 97 | break; |
| 98 | } |
| 99 | glutPostRedisplay(); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static void SpecialKey( int key, int x, int y ) |
| 104 | { |
| 105 | (void) x; |
| 106 | (void) y; |
| 107 | switch (key) { |
| 108 | case GLUT_KEY_UP: |
| 109 | break; |
| 110 | case GLUT_KEY_DOWN: |
| 111 | break; |
| 112 | case GLUT_KEY_LEFT: |
| 113 | break; |
| 114 | case GLUT_KEY_RIGHT: |
| 115 | break; |
| 116 | } |
| 117 | glutPostRedisplay(); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void MouseMove( int x, int y ) |
| 122 | { |
| 123 | Xrot = y - ScreenWidth / 2; |
| 124 | Yrot = x - ScreenHeight / 2; |
| 125 | glutPostRedisplay(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static void Init( void ) |
| 130 | { |
| 131 | Torus = glGenLists(1); |
| 132 | glNewList(Torus, GL_COMPILE); |
| 133 | glutSolidTorus(0.5, 2.0, 10, 20); |
| 134 | glEndList(); |
| 135 | |
| 136 | glEnable(GL_LIGHTING); |
| 137 | glEnable(GL_LIGHT0); |
| 138 | |
| 139 | glEnable(GL_DEPTH_TEST); |
| 140 | glEnable(GL_CULL_FACE); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | int main( int argc, char *argv[] ) |
| 145 | { |
| 146 | #ifndef _WIN32 |
| 147 | printf("NOTE: if you've got 3Dfx VooDoo hardware you must run this"); |
| 148 | printf(" program as root.\n\n"); |
| 149 | printf("Move the mouse. Press ESC to exit.\n\n"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
| 152 | /* Tell Mesa GLX to use 3Dfx driver in fullscreen mode. */ |
| 153 | putenv("MESA_GLX_FX=fullscreen"); |
| 154 | |
| 155 | /* Disable 3Dfx Glide splash screen */ |
| 156 | putenv("FX_GLIDE_NO_SPLASH="); |
| 157 | |
| 158 | /* Give an initial size and position so user doesn't have to place window */ |
| 159 | glutInitWindowPosition(0, 0); |
| 160 | glutInitWindowSize(WIDTH, HEIGHT); |
| 161 | glutInit( &argc, argv ); |
| 162 | |
| 163 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); |
| 164 | |
| 165 | Window = glutCreateWindow(argv[0]); |
| 166 | if (!Window) { |
| 167 | printf("Error, couldn't open window\n"); |
| 168 | exit(1); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Want the X window to fill the screen so that we don't have to |
| 173 | * worry about losing the mouse input focus. |
| 174 | * Note that we won't actually see the X window since we never draw |
| 175 | * to it, hence, the original X screen's contents aren't disturbed. |
| 176 | */ |
| 177 | glutFullScreen(); |
| 178 | |
| 179 | Init(); |
| 180 | |
| 181 | glutReshapeFunc( Reshape ); |
| 182 | glutKeyboardFunc( Key ); |
| 183 | glutSpecialFunc( SpecialKey ); |
| 184 | glutDisplayFunc( Display ); |
| 185 | glutPassiveMotionFunc( MouseMove ); |
| 186 | |
| 187 | glutMainLoop(); |
| 188 | return 0; |
| 189 | } |