blob: 3ec9e85f858276883472d6c7c13c6fd60f164cbe [file] [log] [blame]
Brian Paul95210bc2003-04-21 14:50:12 +00001/* $Id: texobj.c,v 1.7 2003/04/21 14:50:12 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
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
Brian Paul714cdd82002-01-04 21:05:57 +000010#include <assert.h>
jtgafb833d1999-08-19 00:55:39 +000011#include <math.h>
Brian Paul95210bc2003-04-21 14:50:12 +000012#include <stdio.h>
jtgafb833d1999-08-19 00:55:39 +000013#include <stdlib.h>
14#include <string.h>
15#include "GL/glut.h"
16
17static GLuint Window = 0;
18
19static GLuint TexObj[2];
20static GLfloat Angle = 0.0f;
Brian Paul0d24f7b2000-03-23 16:57:14 +000021static GLboolean UseObj = GL_FALSE;
jtgafb833d1999-08-19 00:55:39 +000022
23
Brian Paul058a3ab2000-03-01 03:36:40 +000024#if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
jtgafb833d1999-08-19 00:55:39 +000025# define TEXTURE_OBJECT 1
26#elif defined(GL_EXT_texture_object)
27# define TEXTURE_OBJECT 1
28# define glBindTexture(A,B) glBindTextureEXT(A,B)
29# define glGenTextures(A,B) glGenTexturesEXT(A,B)
30# define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
31#endif
32
33
34
35
36static void draw( void )
37{
38 glDepthFunc(GL_EQUAL);
39 /* glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );*/
40 glClear( GL_COLOR_BUFFER_BIT );
41
42 glColor3f( 1.0, 1.0, 1.0 );
43
44 /* draw first polygon */
45 glPushMatrix();
46 glTranslatef( -1.0, 0.0, 0.0 );
47 glRotatef( Angle, 0.0, 0.0, 1.0 );
Brian Paul0d24f7b2000-03-23 16:57:14 +000048 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +000049#ifdef TEXTURE_OBJECT
50 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
51#endif
52 }
53 else {
54 glCallList( TexObj[0] );
55 }
56 glBegin( GL_POLYGON );
57 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
58 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
59 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
60 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
61 glEnd();
62 glPopMatrix();
63
64 /* draw second polygon */
65 glPushMatrix();
66 glTranslatef( 1.0, 0.0, 0.0 );
67 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
Brian Paul0d24f7b2000-03-23 16:57:14 +000068 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +000069#ifdef TEXTURE_OBJECT
70 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
71#endif
72 }
73 else {
Brian Paul4ceb5612000-02-25 23:24:06 +000074 glCallList( TexObj[1] );
jtgafb833d1999-08-19 00:55:39 +000075 }
76 glBegin( GL_POLYGON );
77 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
78 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
79 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
80 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
81 glEnd();
82 glPopMatrix();
83
84 glutSwapBuffers();
85}
86
87
88
89static void idle( void )
90{
91 Angle += 2.0;
92 glutPostRedisplay();
93}
94
95
96
97/* change view Angle, exit upon ESC */
98static void key(unsigned char k, int x, int y)
99{
100 (void) x;
101 (void) y;
102 switch (k) {
103 case 27:
104#ifdef TEXTURE_OBJECT
105 glDeleteTextures( 2, TexObj );
106#endif
107 glutDestroyWindow(Window);
108 exit(0);
109 }
110}
111
112
113
114/* new window size or exposure */
115static void reshape( int width, int height )
116{
117 glViewport(0, 0, (GLint)width, (GLint)height);
118 glMatrixMode(GL_PROJECTION);
119 glLoadIdentity();
120 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
121 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
122 glMatrixMode(GL_MODELVIEW);
123 glLoadIdentity();
124 glTranslatef( 0.0, 0.0, -8.0 );
125}
126
127
128static void init( void )
129{
130 static int width=8, height=8;
131 static GLubyte tex1[] = {
132 0, 0, 0, 0, 0, 0, 0, 0,
133 0, 0, 0, 0, 1, 0, 0, 0,
134 0, 0, 0, 1, 1, 0, 0, 0,
135 0, 0, 0, 0, 1, 0, 0, 0,
136 0, 0, 0, 0, 1, 0, 0, 0,
137 0, 0, 0, 0, 1, 0, 0, 0,
138 0, 0, 0, 1, 1, 1, 0, 0,
139 0, 0, 0, 0, 0, 0, 0, 0 };
140
141 static GLubyte tex2[] = {
142 0, 0, 0, 0, 0, 0, 0, 0,
143 0, 0, 0, 2, 2, 0, 0, 0,
144 0, 0, 2, 0, 0, 2, 0, 0,
145 0, 0, 0, 0, 0, 2, 0, 0,
146 0, 0, 0, 0, 2, 0, 0, 0,
147 0, 0, 0, 2, 0, 0, 0, 0,
148 0, 0, 2, 2, 2, 2, 0, 0,
149 0, 0, 0, 0, 0, 0, 0, 0 };
150
151 GLubyte tex[64][3];
152 GLint i, j;
153
154
155 glDisable( GL_DITHER );
156
157 /* Setup texturing */
158 glEnable( GL_TEXTURE_2D );
159 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
160 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
161
162
163 /* generate texture object IDs */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000164 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000165#ifdef TEXTURE_OBJECT
166 glGenTextures( 2, TexObj );
167#endif
168 }
169 else {
170 TexObj[0] = glGenLists(2);
171 TexObj[1] = TexObj[0]+1;
172 }
173
174 /* setup first texture object */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000175 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000176#ifdef TEXTURE_OBJECT
177 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
Brian Paul714cdd82002-01-04 21:05:57 +0000178 assert(glIsTexture(TexObj[0]));
jtgafb833d1999-08-19 00:55:39 +0000179#endif
180 }
181 else {
182 glNewList( TexObj[0], GL_COMPILE );
183 }
184 /* red on white */
185 for (i=0;i<height;i++) {
186 for (j=0;j<width;j++) {
187 int p = i*width+j;
188 if (tex1[(height-i-1)*width+j]) {
189 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
190 }
191 else {
192 tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
193 }
194 }
195 }
196
197 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
198 GL_RGB, GL_UNSIGNED_BYTE, tex );
199 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
200 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
201 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
202 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
Brian Paul0d24f7b2000-03-23 16:57:14 +0000203 if (!UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000204 glEndList();
205 }
206 /* end of texture object */
207
208 /* setup second texture object */
Brian Paul0d24f7b2000-03-23 16:57:14 +0000209 if (UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000210#ifdef TEXTURE_OBJECT
211 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
Brian Paul714cdd82002-01-04 21:05:57 +0000212 assert(glIsTexture(TexObj[1]));
jtgafb833d1999-08-19 00:55:39 +0000213#endif
Brian Paul714cdd82002-01-04 21:05:57 +0000214 assert(!glIsTexture(TexObj[1] + 999));
jtgafb833d1999-08-19 00:55:39 +0000215 }
216 else {
217 glNewList( TexObj[1], GL_COMPILE );
218 }
219 /* green on blue */
220 for (i=0;i<height;i++) {
221 for (j=0;j<width;j++) {
222 int p = i*width+j;
223 if (tex2[(height-i-1)*width+j]) {
224 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
225 }
226 else {
227 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
228 }
229 }
230 }
231 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
232 GL_RGB, GL_UNSIGNED_BYTE, tex );
233 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
234 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
235 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
236 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
Brian Paul0d24f7b2000-03-23 16:57:14 +0000237 if (!UseObj) {
jtgafb833d1999-08-19 00:55:39 +0000238 glEndList();
239 }
240 /* end texture object */
241
242}
243
244
245
246int main( int argc, char *argv[] )
247{
Brian Paulcf89b892000-07-19 23:57:24 +0000248 glutInit(&argc, argv);
jtgafb833d1999-08-19 00:55:39 +0000249 glutInitWindowPosition(0, 0);
250 glutInitWindowSize(300, 300);
251 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
252
253 Window = glutCreateWindow("Texture Objects");
254 if (!Window) {
255 exit(1);
256 }
257
258 /* check that renderer has the GL_EXT_texture_object extension
259 * or supports OpenGL 1.1
260 */
261#ifdef TEXTURE_OBJECT
262 {
263 char *exten = (char *) glGetString( GL_EXTENSIONS );
264 char *version = (char *) glGetString( GL_VERSION );
265 if ( strstr( exten, "GL_EXT_texture_object" )
Brian Paul058a3ab2000-03-01 03:36:40 +0000266 || strncmp( version, "1.1", 3 )==0
267 || strncmp( version, "1.2", 3 )==0 ) {
Brian Paul0d24f7b2000-03-23 16:57:14 +0000268 UseObj = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000269 }
270 }
271#endif
272
273 init();
274
275 glutReshapeFunc( reshape );
276 glutKeyboardFunc( key );
277 glutIdleFunc( idle );
278 glutDisplayFunc( draw );
279 glutMainLoop();
280 return 0;
281}