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