blob: f30ede464c01dcf658a60b1d8a7486d763d4897d [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
3 * Example of using the 1.1 texture object functions.
4 * Also, this demo utilizes Mesa's fast texture map path.
5 *
6 * Brian Paul June 1996 This file is in the public domain.
7 */
8
Brian Paul714cdd82002-01-04 21:05:57 +00009#include <assert.h>
jtgafb833d1999-08-19 00:55:39 +000010#include <math.h>
Brian Paul95210bc2003-04-21 14:50:12 +000011#include <stdio.h>
jtgafb833d1999-08-19 00:55:39 +000012#include <stdlib.h>
13#include <string.h>
14#include "GL/glut.h"
15
16static GLuint Window = 0;
17
18static GLuint TexObj[2];
19static GLfloat Angle = 0.0f;
Brian Paul0d24f7b2000-03-23 16:57:14 +000020static GLboolean UseObj = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +000021
22
Brian Paul058a3ab2000-03-01 03:36:40 +000023#if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
jtgafb833d1999-08-19 00:55:39 +000024# define TEXTURE_OBJECT 1
25#elif defined(GL_EXT_texture_object)
26# define TEXTURE_OBJECT 1
27# define glBindTexture(A,B) glBindTextureEXT(A,B)
28# define glGenTextures(A,B) glGenTexturesEXT(A,B)
29# define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
30#endif
31
32
33
34
35static void draw( void )
36{
jtgafb833d1999-08-19 00:55:39 +000037 glClear( GL_COLOR_BUFFER_BIT );
38
39 glColor3f( 1.0, 1.0, 1.0 );
40
41 /* draw first polygon */
42 glPushMatrix();
43 glTranslatef( -1.0, 0.0, 0.0 );
44 glRotatef( Angle, 0.0, 0.0, 1.0 );
Brian Paul0d24f7b2000-03-23 16:57:14 +000045 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +000046#ifdef TEXTURE_OBJECT
47 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
48#endif
49 }
50 else {
51 glCallList( TexObj[0] );
52 }
53 glBegin( GL_POLYGON );
54 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
55 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
56 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
57 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
58 glEnd();
59 glPopMatrix();
60
61 /* draw second polygon */
62 glPushMatrix();
63 glTranslatef( 1.0, 0.0, 0.0 );
64 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
Brian Paul0d24f7b2000-03-23 16:57:14 +000065 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +000066#ifdef TEXTURE_OBJECT
67 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
68#endif
69 }
70 else {
Brian Paul4ceb5612000-02-25 23:24:06 +000071 glCallList( TexObj[1] );
jtgafb833d1999-08-19 00:55:39 +000072 }
73 glBegin( GL_POLYGON );
74 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
75 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
76 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
77 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
78 glEnd();
79 glPopMatrix();
80
81 glutSwapBuffers();
82}
83
84
85
86static void idle( void )
87{
88 Angle += 2.0;
89 glutPostRedisplay();
90}
91
92
93
94/* change view Angle, exit upon ESC */
95static void key(unsigned char k, int x, int y)
96{
97 (void) x;
98 (void) y;
99 switch (k) {
100 case 27:
101#ifdef TEXTURE_OBJECT
102 glDeleteTextures( 2, TexObj );
103#endif
104 glutDestroyWindow(Window);
105 exit(0);
106 }
107}
108
109
110
111/* new window size or exposure */
112static void reshape( int width, int height )
113{
114 glViewport(0, 0, (GLint)width, (GLint)height);
115 glMatrixMode(GL_PROJECTION);
116 glLoadIdentity();
117 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
118 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
119 glMatrixMode(GL_MODELVIEW);
120 glLoadIdentity();
121 glTranslatef( 0.0, 0.0, -8.0 );
122}
123
124
125static void init( void )
126{
127 static int width=8, height=8;
128 static GLubyte tex1[] = {
129 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 0, 0, 1, 0, 0, 0,
131 0, 0, 0, 1, 1, 0, 0, 0,
132 0, 0, 0, 0, 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, 1, 1, 1, 0, 0,
136 0, 0, 0, 0, 0, 0, 0, 0 };
137
138 static GLubyte tex2[] = {
139 0, 0, 0, 0, 0, 0, 0, 0,
140 0, 0, 0, 2, 2, 0, 0, 0,
141 0, 0, 2, 0, 0, 2, 0, 0,
142 0, 0, 0, 0, 0, 2, 0, 0,
143 0, 0, 0, 0, 2, 0, 0, 0,
144 0, 0, 0, 2, 0, 0, 0, 0,
145 0, 0, 2, 2, 2, 2, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0 };
147
148 GLubyte tex[64][3];
149 GLint i, j;
150
151
152 glDisable( GL_DITHER );
153
154 /* Setup texturing */
155 glEnable( GL_TEXTURE_2D );
156 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
157 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
158
159
160 /* generate texture object IDs */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000161 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000162#ifdef TEXTURE_OBJECT
163 glGenTextures( 2, TexObj );
164#endif
165 }
166 else {
167 TexObj[0] = glGenLists(2);
168 TexObj[1] = TexObj[0]+1;
169 }
170
171 /* setup first texture object */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000172 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000173#ifdef TEXTURE_OBJECT
174 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
Brian Paul714cdd82002-01-04 21:05:57 +0000175 assert(glIsTexture(TexObj[0]));
jtgafb833d1999-08-19 00:55:39 +0000176#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 );
Brian Paul0d24f7b2000-03-23 16:57:14 +0000200 if (!UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000201 glEndList();
202 }
203 /* end of texture object */
204
205 /* setup second texture object */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000206 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000207#ifdef TEXTURE_OBJECT
208 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
Brian Paul714cdd82002-01-04 21:05:57 +0000209 assert(glIsTexture(TexObj[1]));
jtgafb833d1999-08-19 00:55:39 +0000210#endif
Brian Paul714cdd82002-01-04 21:05:57 +0000211 assert(!glIsTexture(TexObj[1] + 999));
jtgafb833d1999-08-19 00:55:39 +0000212 }
213 else {
214 glNewList( TexObj[1], GL_COMPILE );
215 }
216 /* green on blue */
217 for (i=0;i<height;i++) {
218 for (j=0;j<width;j++) {
219 int p = i*width+j;
220 if (tex2[(height-i-1)*width+j]) {
221 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
222 }
223 else {
224 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
225 }
226 }
227 }
228 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
229 GL_RGB, GL_UNSIGNED_BYTE, tex );
230 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
231 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
232 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
233 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
Brian Paul0d24f7b2000-03-23 16:57:14 +0000234 if (!UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000235 glEndList();
236 }
237 /* end texture object */
238
239}
240
241
242
243int main( int argc, char *argv[] )
244{
Brian Paulcf89b892000-07-19 23:57:24 +0000245 glutInit(&argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000246 glutInitWindowPosition(0, 0);
247 glutInitWindowSize(300, 300);
248 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
249
250 Window = glutCreateWindow("Texture Objects");
251 if (!Window) {
252 exit(1);
253 }
254
255 /* check that renderer has the GL_EXT_texture_object extension
256 * or supports OpenGL 1.1
257 */
258#ifdef TEXTURE_OBJECT
259 {
260 char *exten = (char *) glGetString( GL_EXTENSIONS );
261 char *version = (char *) glGetString( GL_VERSION );
262 if ( strstr( exten, "GL_EXT_texture_object" )
Brian Paul058a3ab2000-03-01 03:36:40 +0000263 || strncmp( version, "1.1", 3 )==0
264 || strncmp( version, "1.2", 3 )==0 ) {
Brian Paul0d24f7b2000-03-23 16:57:14 +0000265 UseObj = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000266 }
267 }
268#endif
269
270 init();
271
272 glutReshapeFunc( reshape );
273 glutKeyboardFunc( key );
274 glutIdleFunc( idle );
275 glutDisplayFunc( draw );
276 glutMainLoop();
277 return 0;
278}