call glutInit()
[mesa.git] / progs / demos / texobj.c
1 /* $Id: texobj.c,v 1.5 2000/07/19 23:57:24 brianp Exp $ */
2
3 /*
4 * Example of using the 1.1 texture object functions.
5 * Also, this demo utilizes Mesa's fast texture map path.
6 *
7 * Brian Paul June 1996 This file is in the public domain.
8 */
9
10 #include <math.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "GL/glut.h"
14
15 static GLuint Window = 0;
16
17 static GLuint TexObj[2];
18 static GLfloat Angle = 0.0f;
19 static GLboolean UseObj = GL_FALSE;
20
21
22 #if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
23 # define TEXTURE_OBJECT 1
24 #elif defined(GL_EXT_texture_object)
25 # define TEXTURE_OBJECT 1
26 # define glBindTexture(A,B) glBindTextureEXT(A,B)
27 # define glGenTextures(A,B) glGenTexturesEXT(A,B)
28 # define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
29 #endif
30
31
32
33
34 static void draw( void )
35 {
36 glDepthFunc(GL_EQUAL);
37 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
38 glClear( GL_COLOR_BUFFER_BIT );
39
40 glColor3f( 1.0, 1.0, 1.0 );
41
42 /* draw first polygon */
43 glPushMatrix();
44 glTranslatef( -1.0, 0.0, 0.0 );
45 glRotatef( Angle, 0.0, 0.0, 1.0 );
46 if (UseObj) {
47 #ifdef TEXTURE_OBJECT
48 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
49 #endif
50 }
51 else {
52 glCallList( TexObj[0] );
53 }
54 glBegin( GL_POLYGON );
55 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
56 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
57 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
58 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
59 glEnd();
60 glPopMatrix();
61
62 /* draw second polygon */
63 glPushMatrix();
64 glTranslatef( 1.0, 0.0, 0.0 );
65 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
66 if (UseObj) {
67 #ifdef TEXTURE_OBJECT
68 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
69 #endif
70 }
71 else {
72 glCallList( TexObj[1] );
73 }
74 glBegin( GL_POLYGON );
75 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
76 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
77 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
78 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
79 glEnd();
80 glPopMatrix();
81
82 glutSwapBuffers();
83 }
84
85
86
87 static void idle( void )
88 {
89 Angle += 2.0;
90 glutPostRedisplay();
91 }
92
93
94
95 /* change view Angle, exit upon ESC */
96 static void key(unsigned char k, int x, int y)
97 {
98 (void) x;
99 (void) y;
100 switch (k) {
101 case 27:
102 #ifdef TEXTURE_OBJECT
103 glDeleteTextures( 2, TexObj );
104 #endif
105 glutDestroyWindow(Window);
106 exit(0);
107 }
108 }
109
110
111
112 /* new window size or exposure */
113 static void reshape( int width, int height )
114 {
115 glViewport(0, 0, (GLint)width, (GLint)height);
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
118 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
119 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
120 glMatrixMode(GL_MODELVIEW);
121 glLoadIdentity();
122 glTranslatef( 0.0, 0.0, -8.0 );
123 }
124
125
126 static void init( void )
127 {
128 static int width=8, height=8;
129 static GLubyte tex1[] = {
130 0, 0, 0, 0, 0, 0, 0, 0,
131 0, 0, 0, 0, 1, 0, 0, 0,
132 0, 0, 0, 1, 1, 0, 0, 0,
133 0, 0, 0, 0, 1, 0, 0, 0,
134 0, 0, 0, 0, 1, 0, 0, 0,
135 0, 0, 0, 0, 1, 0, 0, 0,
136 0, 0, 0, 1, 1, 1, 0, 0,
137 0, 0, 0, 0, 0, 0, 0, 0 };
138
139 static GLubyte tex2[] = {
140 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 0, 2, 2, 0, 0, 0,
142 0, 0, 2, 0, 0, 2, 0, 0,
143 0, 0, 0, 0, 0, 2, 0, 0,
144 0, 0, 0, 0, 2, 0, 0, 0,
145 0, 0, 0, 2, 0, 0, 0, 0,
146 0, 0, 2, 2, 2, 2, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0 };
148
149 GLubyte tex[64][3];
150 GLint i, j;
151
152
153 glDisable( GL_DITHER );
154
155 /* Setup texturing */
156 glEnable( GL_TEXTURE_2D );
157 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
158 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
159
160
161 /* generate texture object IDs */
162 if (UseObj) {
163 #ifdef TEXTURE_OBJECT
164 glGenTextures( 2, TexObj );
165 #endif
166 }
167 else {
168 TexObj[0] = glGenLists(2);
169 TexObj[1] = TexObj[0]+1;
170 }
171
172 /* setup first texture object */
173 if (UseObj) {
174 #ifdef TEXTURE_OBJECT
175 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
176 #endif
177 }
178 else {
179 glNewList( TexObj[0], GL_COMPILE );
180 }
181 /* red on white */
182 for (i=0;i<height;i++) {
183 for (j=0;j<width;j++) {
184 int p = i*width+j;
185 if (tex1[(height-i-1)*width+j]) {
186 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
187 }
188 else {
189 tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
190 }
191 }
192 }
193
194 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
195 GL_RGB, GL_UNSIGNED_BYTE, tex );
196 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
197 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
198 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
199 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
200 if (!UseObj) {
201 glEndList();
202 }
203 /* end of texture object */
204
205 /* setup second texture object */
206 if (UseObj) {
207 #ifdef TEXTURE_OBJECT
208 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
209 #endif
210 }
211 else {
212 glNewList( TexObj[1], GL_COMPILE );
213 }
214 /* green on blue */
215 for (i=0;i<height;i++) {
216 for (j=0;j<width;j++) {
217 int p = i*width+j;
218 if (tex2[(height-i-1)*width+j]) {
219 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
220 }
221 else {
222 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
223 }
224 }
225 }
226 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
227 GL_RGB, GL_UNSIGNED_BYTE, tex );
228 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
229 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
230 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
231 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
232 if (!UseObj) {
233 glEndList();
234 }
235 /* end texture object */
236
237 }
238
239
240
241 int main( int argc, char *argv[] )
242 {
243 glutInit(&argc, argv);
244 glutInitWindowPosition(0, 0);
245 glutInitWindowSize(300, 300);
246 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
247
248 Window = glutCreateWindow("Texture Objects");
249 if (!Window) {
250 exit(1);
251 }
252
253 /* check that renderer has the GL_EXT_texture_object extension
254 * or supports OpenGL 1.1
255 */
256 #ifdef TEXTURE_OBJECT
257 {
258 char *exten = (char *) glGetString( GL_EXTENSIONS );
259 char *version = (char *) glGetString( GL_VERSION );
260 if ( strstr( exten, "GL_EXT_texture_object" )
261 || strncmp( version, "1.1", 3 )==0
262 || strncmp( version, "1.2", 3 )==0 ) {
263 UseObj = GL_TRUE;
264 }
265 }
266 #endif
267
268 init();
269
270 glutReshapeFunc( reshape );
271 glutKeyboardFunc( key );
272 glutIdleFunc( idle );
273 glutDisplayFunc( draw );
274 glutMainLoop();
275 return 0;
276 }