File: texwinalign.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (354 lines) | stat: -rw-r--r-- 9,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

/* Copyright (c) Mark J. Kilgard, 1998. */

/* This program is freely distributable without licensing fees 
   and is provided without guarantee or warrantee expressed or 
   implied. This program is -not- in the public domain. */

/* This program demonstrates how to use the texture matrix
   and texture coordinate generation (texgen) to generate
   window space texture coordinates for arbitrary 3D geometry.
   The basic technique is to generate texture coordinates
   directly matching the object coordinates and using the
   texture matrix to mimic the viewport, projection, and
   modelview transformations to convert the texture coordinates
   into window coordinates identically to how the actual
   object coordinates are transformed into window space.  It
   is important to have perspective correct texturing if you
   want perspective projections to look right. */

#include <stdlib.h>
#include <GL/glut.h>

GLfloat lightDiffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */

GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

GLfloat angle = -20.0;
int animating = 1;

#define TEX_WIDTH 16
#define TEX_HEIGHT 16

/* Nice circle texture tiling pattern. */
static char *circles[] = {
  "....xxxx........",
  "..xxxxxxxx......",
  ".xxxxxxxxxx.....",
  ".xxx....xxx.....",
  "xxx......xxx....",
  "xxx......xxx....",
  "xxx......xxx....",
  "xxx......xxx....",
  ".xxx....xxx.....",
  ".xxxxxxxxxx.....",
  "..xxxxxxxx......",
  "....xxxx........",
  "................",
  "................",
  "................",
  "................",
};

/* Nice grid texture tiling pattern. */
static char *grid[] = {
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "..............xx",
  "xxxxxxxxxxxxxxxx",
  "xxxxxxxxxxxxxxxx",
};

static void
makeTexture(char *pattern[])
{
  GLubyte floorTexture[TEX_WIDTH][TEX_HEIGHT][3];
  GLubyte *loc;
  int s, t;

  /* Setup RGB image for the texture. */
  loc = (GLubyte*) floorTexture;
  for (t = 0; t < TEX_HEIGHT; t++) {
    for (s = 0; s < TEX_WIDTH; s++) {
      if (pattern[t][s] == 'x') {
	/* Nice green. */
        loc[0] = 0x6f;
        loc[1] = 0x8f;
        loc[2] = 0x1f;
      } else {
	/* Light gray. */
        loc[0] = 0xaa;
        loc[1] = 0xaa;
        loc[2] = 0xaa;
      }
      loc += 3;
    }
  }

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, TEX_WIDTH, TEX_HEIGHT, 0,
    GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
}

void
drawBox(void)
{
  int i;

  for (i = 0; i < 6; i++) {
    glBegin(GL_QUADS);
    glNormal3fv(&n[i][0]);
    glVertex3fv(&v[faces[i][0]][0]);
    glVertex3fv(&v[faces[i][1]][0]);
    glVertex3fv(&v[faces[i][2]][0]);
    glVertex3fv(&v[faces[i][3]][0]);
    glEnd();
  }
}

void
display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
    glRotatef(angle, 0.0, 0.0, 1.0);
    drawBox();
  glPopMatrix();
  glutSwapBuffers();
}

int windowWidth;
int windowHeight;
int slideX = 0, slideY = 0;

void
configTextureMatrix(void)
{
  glMatrixMode(GL_TEXTURE);
  glLoadIdentity();
  /* Shift texture in pixel units (slideX,slideY).  You could use this
     to copensate for a viewport origin different from the window
     origin. */
  glTranslatef(slideX/(GLfloat)TEX_WIDTH,
               slideY/(GLfloat)TEX_HEIGHT,
	       0.0);
  /* Scale based on the window size in pixel. */
  glScalef(windowWidth/(GLfloat)TEX_WIDTH,
           windowHeight/(GLfloat)TEX_HEIGHT,
	   1.0);
  /* Mimic the scene's projection matrix setup. */
  gluPerspective( /* field of view in degree */ 40.0,
    /* aspect ratio */ 1.0,
    /* Z near */ 1.0, /* Z far */ 10.0);
  /* Mimic the scene's view matrix setup. */
  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
    0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    0.0, 1.0, 0.);      /* up is in positive Y direction */
  /* Mimic the scene's model matrix setup. */
  /* Adjust cube position to be aesthetic angle. */
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(60, 1.0, 0.0, 0.0);
  glRotatef(angle, 0.0, 0.0, 1.0);
  /* Switch back to the modelview matrix. */
  glMatrixMode(GL_MODELVIEW);

}

void
idle(void)
{
  /* Slowly rotate object. */
  angle += 0.5;
  if (angle > 360.0) {
    angle -= 360.0;
  }
  /* Make sure the texture matrix mimics the changing
     modelview matrix. */
  configTextureMatrix();
  glutPostRedisplay();
}

void
keyboard(unsigned char c, int x, int y)
{
  switch(c) {
  case 27: /* Escape */
    exit(0);
    break;
  case 'h':
    slideX--;
    break;
  case 'j':
    slideY--;
    break;
  case 'k':
    slideY++;
    break;
  case 'l':
    slideX++;
    break;
  case 'r':
    angle += 10;
    break;
  case 'a':
    animating = !animating;
    if (animating) {
      glutIdleFunc(idle);
    } else {
      glutIdleFunc(NULL);
    }
    break;
  }
  configTextureMatrix();
  glutPostRedisplay();
}

void
reshape(int width, int height)
{
  windowWidth = width;
  windowHeight = height;
  glViewport(0, 0, width, height);
  configTextureMatrix();
}

void
init(void)
{
  static GLfloat sPlane[4] = { 1.0, 0.0, 0.0, 0.0 };
  static GLfloat tPlane[4] = { 0.0, 1.0, 0.0, 0.0 };
  static GLfloat rPlane[4] = { 0.0, 0.0, 1.0, 0.0 };
  static GLfloat qPlane[4] = { 0.0, 0.0, 0.0, 1.0 };

  /* Setup cube vertex data. */
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

  /* Use depth buffering for hidden surface elimination. */
  glEnable(GL_DEPTH_TEST);

  /* Enable a single OpenGL light. */
  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  glEnable(GL_LIGHT0);

  /* Setup the view of the cube. */
  glMatrixMode(GL_PROJECTION);
  gluPerspective( /* field of view in degree */ 40.0,
    /* aspect ratio */ 1.0,
    /* Z near */ 1.0, /* Z far */ 10.0);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
    0.0, 0.0, 0.0,      /* center is at (0,0,0) */
    0.0, 1.0, 0.);      /* up is in positive Y direction */

  /* Texgen that maps object coordinates directly to texture
     coordinates. */
  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
  glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
  glTexGenfv(GL_R, GL_OBJECT_PLANE, rPlane);
  glTexGenfv(GL_Q, GL_OBJECT_PLANE, qPlane);
  glEnable(GL_TEXTURE_GEN_S);
  glEnable(GL_TEXTURE_GEN_T);
  glEnable(GL_TEXTURE_GEN_R);
  glEnable(GL_TEXTURE_GEN_Q);

  /* Enable texturing.  Perspective correct texturing is
     important to this demo! */
  glEnable(GL_TEXTURE_2D);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

  /* Adjust cube position to be aesthetic orientation. */
  glTranslatef(0.0, 0.0, -1.0);
  glRotatef(60, 1.0, 0.0, 0.0);
}

void
menu(int selection)
{
  switch (selection) {
  case 1:
    glEnable(GL_LIGHTING);
    glutPostRedisplay();
    break;
  case 2:
    glDisable(GL_LIGHTING);
    glutPostRedisplay();
    break;
  case 3:
    keyboard('a', 0, 0);
    break;
  case 4:
    makeTexture(circles);
    break;
  case 5:
    makeTexture(grid);
    break;
  case 666:
    exit(0);
  }
}

void
visibility(int state)
{
  if (state == GLUT_VISIBLE) {
    if (animating) {
      glutIdleFunc(idle);
    }
  } else {
    glutIdleFunc(NULL);
  }
}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("window space aligned textures");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutVisibilityFunc(visibility);
  init();
  makeTexture(grid);
  glutCreateMenu(menu);
  glutAddMenuEntry("Enable lighting", 1);
  glutAddMenuEntry("Disable lighting", 2);
  glutAddMenuEntry("Animating", 3);
  glutAddMenuEntry("Circles", 4);
  glutAddMenuEntry("Grid", 5);
  glutAddMenuEntry("Quit", 666);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}