~ubuntu-branches/ubuntu/quantal/mesa/quantal

« back to all changes in this revision

Viewing changes to progs/tests/texwrap.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-02-21 12:44:07 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: james.westby@ubuntu.com-20070221124407-rgcacs32mycrtadl
ImportĀ upstreamĀ versionĀ 6.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: texwrap.c,v 1.8 2005-08-25 03:09:12 brianp Exp $ */
2
 
 
3
 
/*
4
 
 * Test texture wrap modes.
5
 
 * Press 'b' to toggle texture image borders.  You should see the same
6
 
 * rendering whether or not you're using borders.
7
 
 *
8
 
 * Brian Paul   March 2001
9
 
 */
10
 
 
11
 
 
12
 
#define GL_GLEXT_PROTOTYPES
13
 
#include <stdio.h>
14
 
#include <stdlib.h>
15
 
#include <math.h>
16
 
#include <GL/glut.h>
17
 
 
18
 
 
19
 
#ifndef GL_CLAMP_TO_BORDER
20
 
#define GL_CLAMP_TO_BORDER 0x812D
21
 
#endif
22
 
 
23
 
#ifndef GL_MIRRORED_REPEAT
24
 
#define GL_MIRRORED_REPEAT 0x8370
25
 
#endif
26
 
 
27
 
#ifndef GL_EXT_texture_mirror_clamp
28
 
#define GL_MIRROR_CLAMP_EXT               0x8742
29
 
#define GL_MIRROR_CLAMP_TO_EDGE_EXT       0x8743
30
 
#define GL_MIRROR_CLAMP_TO_BORDER_EXT     0x8912
31
 
#endif
32
 
 
33
 
#define BORDER_TEXTURE 1
34
 
#define NO_BORDER_TEXTURE 2
35
 
 
36
 
#define SIZE 8
37
 
static GLubyte BorderImage[SIZE+2][SIZE+2][4];
38
 
static GLubyte NoBorderImage[SIZE][SIZE][4];
39
 
static GLuint Border = 0;
40
 
 
41
 
#define TILE_SIZE 110
42
 
 
43
 
#define WRAP_MODE(m)        { m , # m, GL_TRUE,  1.0, { NULL, NULL } }
44
 
#define WRAP_EXT(m,e1,e2,v) { m , # m, GL_FALSE, v,   { e1,   e2   } }
45
 
    
46
 
struct wrap_mode {
47
 
   GLenum       mode;
48
 
   const char * name;
49
 
   GLboolean    supported;
50
 
   GLfloat      version;
51
 
   const char * extension_names[2];
52
 
};
53
 
 
54
 
static struct wrap_mode modes[] = {
55
 
   WRAP_MODE( GL_REPEAT ),
56
 
   WRAP_MODE( GL_CLAMP ),
57
 
   WRAP_EXT ( GL_CLAMP_TO_EDGE,   "GL_EXT_texture_edge_clamp",
58
 
                                  "GL_SGIS_texture_edge_clamp",
59
 
              1.2 ),
60
 
   WRAP_EXT ( GL_CLAMP_TO_BORDER, "GL_ARB_texture_border_clamp",
61
 
                                  "GL_SGIS_texture_border_clamp",
62
 
              1.3 ),
63
 
   WRAP_EXT ( GL_MIRRORED_REPEAT, "GL_ARB_texture_mirrored_repeat",
64
 
                                  "GL_IBM_texture_mirrored_repeat",
65
 
              1.4 ),
66
 
   WRAP_EXT ( GL_MIRROR_CLAMP_EXT, "GL_ATI_texture_mirror_once",
67
 
                                   "GL_EXT_texture_mirror_clamp",
68
 
              999.0 ),
69
 
   WRAP_EXT ( GL_MIRROR_CLAMP_TO_BORDER_EXT, "GL_EXT_texture_mirror_clamp",
70
 
                                             NULL,
71
 
              999.0 ),
72
 
   WRAP_EXT ( GL_MIRROR_CLAMP_TO_EDGE_EXT, "GL_ATI_texture_mirror_once",
73
 
                                           "GL_EXT_texture_mirror_clamp",
74
 
              999.0 ),
75
 
   { 0 }
76
 
};
77
 
 
78
 
static void
79
 
PrintString(const char *s)
80
 
{
81
 
   while (*s) {
82
 
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
83
 
      s++;
84
 
   }
85
 
}
86
 
 
87
 
 
88
 
static void Display( void )
89
 
{
90
 
   GLenum i, j;
91
 
   GLint offset;
92
 
   GLfloat version;
93
 
 
94
 
   /* Fill in the extensions that are supported.
95
 
    */
96
 
    
97
 
   version = atof( (char *) glGetString( GL_VERSION ) );
98
 
   for ( i = 0 ; modes[i].mode != 0 ; i++ ) {
99
 
      if ( ((modes[i].extension_names[0] != NULL)
100
 
            && glutExtensionSupported(modes[i].extension_names[0]))
101
 
           || ((modes[i].extension_names[1] != NULL)
102
 
               && glutExtensionSupported(modes[i].extension_names[1])) ) {
103
 
         modes[i].supported = GL_TRUE;
104
 
      }
105
 
      else if ( !modes[i].supported && (modes[i].version <= version) ) {
106
 
         fprintf( stderr, "WARNING: OpenGL library meets minimum version\n"
107
 
                          "         requirement for %s, but the\n"
108
 
                          "         extension string is not advertised.\n"
109
 
                          "         (%s%s%s)\n",
110
 
                  modes[i].name,
111
 
                  modes[i].extension_names[0],
112
 
                  (modes[i].extension_names[1] != NULL) 
113
 
                      ? " or " : "",
114
 
                  (modes[i].extension_names[1] != NULL)
115
 
                      ? modes[i].extension_names[1] : "" );
116
 
         modes[i].supported = GL_TRUE;
117
 
      }
118
 
   }
119
 
 
120
 
 
121
 
   glClearColor(0.5, 0.5, 0.5, 1.0);
122
 
   glClear( GL_COLOR_BUFFER_BIT );
123
 
 
124
 
#if 0
125
 
   /* draw texture as image */
126
 
   glDisable(GL_TEXTURE_2D);
127
 
   glWindowPos2iARB(1, 1);
128
 
   glDrawPixels(6, 6, GL_RGBA, GL_UNSIGNED_BYTE, (void *) TexImage);
129
 
#endif
130
 
 
131
 
   glBindTexture(GL_TEXTURE_2D, Border ? BORDER_TEXTURE : NO_BORDER_TEXTURE);
132
 
 
133
 
 
134
 
   /* loop over min/mag filters */
135
 
   for (i = 0; i < 2; i++) {
136
 
      offset = 0;
137
 
 
138
 
      if (i) {
139
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
141
 
      }
142
 
      else {
143
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
144
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
145
 
      }
146
 
 
147
 
      /* loop over border modes */
148
 
      for (j = 0; modes[j].mode != 0; j++) {
149
 
         const GLfloat x0 = 0, y0 = 0, x1 = (TILE_SIZE - 10), y1 = (TILE_SIZE - 10);
150
 
         const GLfloat b = 1.2;
151
 
         const GLfloat s0 = -b, t0 = -b, s1 = 1.0+b, t1 = 1.0+b;
152
 
 
153
 
         if ( modes[j].supported != GL_TRUE )
154
 
             continue;
155
 
 
156
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, modes[j].mode);
157
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, modes[j].mode);
158
 
 
159
 
         glPushMatrix();
160
 
            glTranslatef(offset * TILE_SIZE + 10, i * TILE_SIZE + 40, 0);
161
 
            offset++;
162
 
 
163
 
            glEnable(GL_TEXTURE_2D);
164
 
            glColor3f(1, 1, 1);
165
 
            glBegin(GL_POLYGON);
166
 
            glTexCoord2f(s0, t0);  glVertex2f(x0, y0);
167
 
            glTexCoord2f(s1, t0);  glVertex2f(x1, y0);
168
 
            glTexCoord2f(s1, t1);  glVertex2f(x1, y1);
169
 
            glTexCoord2f(s0, t1);  glVertex2f(x0, y1);
170
 
            glEnd();
171
 
 
172
 
            /* draw red outline showing bounds of texture at s=0,1 and t=0,1 */
173
 
            glDisable(GL_TEXTURE_2D);
174
 
            glColor3f(1, 0, 0);
175
 
            glBegin(GL_LINE_LOOP);
176
 
            glVertex2f(x0 + b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
177
 
            glVertex2f(x1 - b * (x1-x0) / (s1-s0), y0 + b * (y1-y0) / (t1-t0));
178
 
            glVertex2f(x1 - b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
179
 
            glVertex2f(x0 + b * (x1-x0) / (s1-s0), y1 - b * (y1-y0) / (t1-t0));
180
 
            glEnd();
181
 
 
182
 
         glPopMatrix();
183
 
      }
184
 
   }
185
 
 
186
 
   glDisable(GL_TEXTURE_2D);
187
 
   glColor3f(1, 1, 1);
188
 
   offset = 0;
189
 
   for (i = 0; modes[i].mode != 0; i++) {
190
 
      if ( modes[i].supported ) {
191
 
         glWindowPos2iARB( offset * TILE_SIZE + 10, 5 + ((offset & 1) * 15) );
192
 
         PrintString(modes[i].name);
193
 
         offset++;
194
 
      }
195
 
   }
196
 
 
197
 
   glutSwapBuffers();
198
 
}
199
 
 
200
 
 
201
 
static void Reshape( int width, int height )
202
 
{
203
 
   glViewport( 0, 0, width, height );
204
 
   glMatrixMode( GL_PROJECTION );
205
 
   glLoadIdentity();
206
 
   glOrtho(0, width, 0, height, -1, 1);
207
 
   glMatrixMode( GL_MODELVIEW );
208
 
   glLoadIdentity();
209
 
}
210
 
 
211
 
 
212
 
static void Key( unsigned char key, int x, int y )
213
 
{
214
 
   (void) x;
215
 
   (void) y;
216
 
   switch (key) {
217
 
      case 'b':
218
 
         Border = !Border;
219
 
         printf("Texture Border Size = %d\n", Border);
220
 
         break;
221
 
      case 27:
222
 
         exit(0);
223
 
         break;
224
 
   }
225
 
   glutPostRedisplay();
226
 
}
227
 
 
228
 
 
229
 
static void Init( void )
230
 
{
231
 
   static const GLubyte border[4] = { 0, 255, 0, 255 };
232
 
   static const GLfloat borderf[4] = { 0, 1.0, 0, 1.0 };
233
 
   GLint i, j;
234
 
 
235
 
   for (i = 0; i < SIZE+2; i++) {
236
 
      for (j = 0; j < SIZE+2; j++) {
237
 
         if (i == 0 || j == 0 || i == SIZE+1 || j == SIZE+1) {
238
 
            /* border color */
239
 
            BorderImage[i][j][0] = border[0];
240
 
            BorderImage[i][j][1] = border[1];
241
 
            BorderImage[i][j][2] = border[2];
242
 
            BorderImage[i][j][3] = border[3];
243
 
         }
244
 
         else if ((i + j) & 1) {
245
 
            /* white */
246
 
            BorderImage[i][j][0] = 255;
247
 
            BorderImage[i][j][1] = 255;
248
 
            BorderImage[i][j][2] = 255;
249
 
            BorderImage[i][j][3] = 255;
250
 
         }
251
 
         else {
252
 
            /* black */
253
 
            BorderImage[i][j][0] = 0;
254
 
            BorderImage[i][j][1] = 0;
255
 
            BorderImage[i][j][2] = 0;
256
 
            BorderImage[i][j][3] = 0;
257
 
         }
258
 
      }
259
 
   }
260
 
 
261
 
   glBindTexture(GL_TEXTURE_2D, BORDER_TEXTURE);
262
 
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE+2, SIZE+2, 1,
263
 
                GL_RGBA, GL_UNSIGNED_BYTE, (void *) BorderImage);
264
 
 
265
 
   for (i = 0; i < SIZE; i++) {
266
 
      for (j = 0; j < SIZE; j++) {
267
 
         if ((i + j) & 1) {
268
 
            /* white */
269
 
            NoBorderImage[i][j][0] = 255;
270
 
            NoBorderImage[i][j][1] = 255;
271
 
            NoBorderImage[i][j][2] = 255;
272
 
            NoBorderImage[i][j][3] = 255;
273
 
         }
274
 
         else {
275
 
            /* black */
276
 
            NoBorderImage[i][j][0] = 0;
277
 
            NoBorderImage[i][j][1] = 0;
278
 
            NoBorderImage[i][j][2] = 0;
279
 
            NoBorderImage[i][j][3] = 0;
280
 
         }
281
 
      }
282
 
   }
283
 
 
284
 
   glBindTexture(GL_TEXTURE_2D, NO_BORDER_TEXTURE);
285
 
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
286
 
                GL_RGBA, GL_UNSIGNED_BYTE, (void *) NoBorderImage);
287
 
   glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderf);
288
 
}
289
 
 
290
 
 
291
 
int main( int argc, char *argv[] )
292
 
{
293
 
   glutInit( &argc, argv );
294
 
   glutInitWindowPosition( 0, 0 );
295
 
   glutInitWindowSize( 1000, 270 );
296
 
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
297
 
   glutCreateWindow(argv[0]);
298
 
   glutReshapeFunc( Reshape );
299
 
   glutKeyboardFunc( Key );
300
 
   glutDisplayFunc( Display );
301
 
   Init();
302
 
   glutMainLoop();
303
 
   return 0;
304
 
}