1/*
2 * Test the GL_PERSPECTIVE_CORRECTION_HINT setting and its effect on
3 * color interpolation.
4 *
5 * Press 'i' to toggle between GL_NICEST/GL_FASTEST/GL_DONT_CARE.
6 *
7 * Depending on the driver, the hint may make a difference, or not.
8 */
9
10
11#include <stdlib.h>
12#include <stdio.h>
13#include "glut_wrap.h"
14
15
16static GLenum PerspHint = GL_DONT_CARE;
17
18
19static void
20init(void)
21{
22   GLubyte image[256][256][4];
23   GLuint i, j;
24   for (i = 0; i < 256; i++) {
25      for (j = 0; j < 256; j++) {
26         image[i][j][0] = j;
27         image[i][j][1] = j;
28         image[i][j][2] = j;
29         image[i][j][3] = 255;
30      }
31   }
32   glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0,
33                GL_RGBA, GL_UNSIGNED_BYTE, image);
34   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
35   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
36   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
37   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
38   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
39
40   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
41}
42
43
44static void
45display(void)
46{
47   switch (PerspHint) {
48   case GL_NICEST:
49      printf("hint = GL_NICEST\n");
50      break;
51   case GL_FASTEST:
52      printf("hint = GL_FASTEST\n");
53      break;
54   case GL_DONT_CARE:
55      printf("hint = GL_DONT_CARE\n");
56      break;
57   default:
58      ;
59   }
60
61   glClear(GL_COLOR_BUFFER_BIT);
62
63#if 1
64   glBegin(GL_QUADS);
65   /* exercise perspective interpolation */
66   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
67   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, 1.0, -1.0);
68   glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, 1.0, -7.0);
69   glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, -1.0, -7.0);
70
71   /* stripe of linear interpolation */
72   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -0.1, -1.001);
73   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0,  0.1, -1.001);
74   glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0,  0.1, -1.001);
75   glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0, -0.1, -1.001);
76   glEnd();
77#else
78   glEnable(GL_TEXTURE_2D);
79   glBegin(GL_QUADS);
80   glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 0.0, -1.0);
81   glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
82   glTexCoord2f(1.0, 1.0); glVertex3f( 5.0, 1.0, -7.0);
83   glTexCoord2f(1.0, 0.0); glVertex3f( 5.0, 0.0, -7.0);
84
85   glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.001);
86   glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0, -1.001);
87   glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 0.0, -1.001);
88   glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.001);
89   glEnd();
90#endif
91
92   glFlush();
93}
94
95
96static void
97reshape(int w, int h)
98{
99   glViewport(0, 0, w, h);
100   glMatrixMode(GL_PROJECTION);
101   glLoadIdentity();
102   gluPerspective(90.0, (GLfloat) w / h, 1.0, 300.0);
103   glMatrixMode(GL_MODELVIEW);
104   glLoadIdentity();
105}
106
107
108static void
109key(unsigned char k, int x, int y)
110{
111   switch (k) {
112   case 27:  /* Escape */
113      exit(0);
114      break;
115   case 'i':
116      if (PerspHint == GL_FASTEST)
117         PerspHint = GL_NICEST;
118      else if (PerspHint == GL_NICEST)
119         PerspHint = GL_DONT_CARE;
120      else
121         PerspHint = GL_FASTEST;
122      glHint(GL_PERSPECTIVE_CORRECTION_HINT, PerspHint);
123      break;
124   default:
125      return;
126   }
127   glutPostRedisplay();
128}
129
130
131int
132main(int argc, char** argv)
133{
134    glutInit(&argc, argv);
135    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
136    glutInitWindowSize (500, 500);
137    glutCreateWindow (argv[0]);
138    glutReshapeFunc (reshape);
139    glutDisplayFunc(display);
140    glutKeyboardFunc(key);
141
142    printf("Main quad: perspective projection\n");
143    printf("Middle stripe: linear interpolation\n");
144    printf("Press 'i' to toggle interpolation hint\n");
145    init();
146
147    glutMainLoop();
148    return 0;
149}
150