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