summaryrefslogtreecommitdiff
path: root/dist/Mesa/progs/tests/persp_hint.c
blob: 27140d1f567667fa6c9641b54795c949db3e860d (plain)
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
/*
 * Test the GL_PERSPECTIVE_CORRECTION_HINT setting and its effect on
 * color interpolation.
 *
 * Press 'i' to toggle between GL_NICEST/GL_FASTEST/GL_DONT_CARE.
 *
 * Depending on the driver, the hint may make a difference, or not.
 */


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


static GLenum PerspHint = GL_DONT_CARE;


static void
init(void)
{
   GLubyte image[256][256][4];
   GLuint i, j;
   for (i = 0; i < 256; i++) {
      for (j = 0; j < 256; j++) {
         image[i][j][0] = j;
         image[i][j][1] = j;
         image[i][j][2] = j;
         image[i][j][3] = 255;
      }
   }
   glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0,
                GL_RGBA, GL_UNSIGNED_BYTE, image);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
}


static void
display(void)
{
   switch (PerspHint) {
   case GL_NICEST:
      printf("hint = GL_NICEST\n");
      break;
   case GL_FASTEST:
      printf("hint = GL_FASTEST\n");
      break;
   case GL_DONT_CARE:
      printf("hint = GL_DONT_CARE\n");
      break;
   default:
      ;
   }

   glClear(GL_COLOR_BUFFER_BIT);

#if 1
   glBegin(GL_QUADS);
   /* exercise perspective interpolation */
   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, 1.0, -1.0);
   glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, 1.0, -7.0);
   glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, -1.0, -7.0);

   /* stripe of linear interpolation */
   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -0.1, -1.001);
   glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0,  0.1, -1.001);
   glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0,  0.1, -1.001);
   glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0, -0.1, -1.001);
   glEnd();
#else
   glEnable(GL_TEXTURE_2D);
   glBegin(GL_QUADS);
   glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 0.0, -1.0);
   glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
   glTexCoord2f(1.0, 1.0); glVertex3f( 5.0, 1.0, -7.0);
   glTexCoord2f(1.0, 0.0); glVertex3f( 5.0, 0.0, -7.0);

   glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.001);
   glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0, -1.001);
   glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 0.0, -1.001);
   glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.001);
   glEnd();
#endif

   glFlush();
}


static void
reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(90.0, (GLfloat) w / h, 1.0, 300.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


static void
key(unsigned char k, int x, int y)
{
   switch (k) {
   case 27:  /* Escape */
      exit(0);
      break;
   case 'i':
      if (PerspHint == GL_FASTEST)
         PerspHint = GL_NICEST;
      else if (PerspHint == GL_NICEST)
         PerspHint = GL_DONT_CARE;
      else
         PerspHint = GL_FASTEST;
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, PerspHint);
      break;
   default:
      return;
   }
   glutPostRedisplay();
}


int
main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutCreateWindow (argv[0]);
    glutReshapeFunc (reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);

    printf("Main quad: perspective projection\n");
    printf("Middle stripe: linear interpolation\n");
    printf("Press 'i' to toggle interpolation hint\n");
    init();

    glutMainLoop();
    return 0;
}