2 * Test conversion of a perspective-interpolated value to be a linearly
15 #include "glut_wrap.h"
16 #include "shaderutil.h"
19 static GLuint VertShader1
, VertShader2
;
20 static GLuint FragShader1
, FragShader2
;
21 static GLuint Program1
, Program2
;
23 static int Width
= 600, Height
= 300;
31 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
33 /* draw left and right perspective projected quads */
34 for (i
= 0; i
< 2; i
++) {
38 glUseProgram(Program1
);
39 glViewport(0, 0, 300, 300);
42 glUseProgram(Program2
);
43 glViewport(300, 0, 300, 300);
46 glScalef(0.9, 0.9, 1.0);
49 glTexCoord2f(0, 0); glVertex3f(-1, -1, 0);
50 glTexCoord2f(1, 0); glVertex3f( 1, -1, 0);
51 glTexCoord2f(1, 1); glVertex3f( 1, 1, -12);
52 glTexCoord2f(0, 1); glVertex3f(-1, 1, -12);
58 /* draw grayscale quad in middle */
60 glViewport(0, 0, Width
, Height
);
62 glTranslatef(0.0, -0.38, 0);
63 glScalef(0.05, 0.52, 1.0);
65 glColor3f(0, 0, 0); glVertex2f(-1, -1);
66 glColor3f(0, 0, 0); glVertex2f( 1, -1);
67 glColor3f(1, 1, 1); glVertex2f( 1, 1);
68 glColor3f(1, 1, 1); glVertex2f(-1, 1);
73 GLfloat buf1
[300*4], buf2
[300*4];
75 glReadPixels(Width
* 1 / 4, 0, 1, Height
, GL_RGBA
, GL_FLOAT
, buf1
);
76 glReadPixels(Width
* 3 / 4, 0, 1, Height
, GL_RGBA
, GL_FLOAT
, buf2
);
77 for (i
= 1; i
< Height
; i
++) {
78 printf("row %d: %f (delta %f) %f (delta %f)\n",
80 buf1
[i
*4], buf1
[i
*4] - buf1
[i
*4-4],
81 buf2
[i
*4], buf2
[i
*4] - buf2
[i
*4-4]);
90 Reshape(int width
, int height
)
92 glViewport(0, 0, width
, height
);
93 glMatrixMode(GL_PROJECTION
);
95 glFrustum(-1, 1, -1, 1, 2, 200);
96 glMatrixMode(GL_MODELVIEW
);
98 glTranslatef(0, 0, -2);
108 glDeleteShader(FragShader1
);
109 glDeleteShader(FragShader2
);
110 glDeleteShader(VertShader1
);
111 glDeleteShader(VertShader2
);
112 glDeleteProgram(Program1
);
113 glDeleteProgram(Program2
);
114 glutDestroyWindow(Win
);
119 Key(unsigned char key
, int x
, int y
)
138 * Regular perspective interpolation
140 static const char *VertShaderText1
=
142 " vec4 pos = ftransform();\n"
143 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
144 " gl_Position = pos;\n"
147 static const char *FragShaderText1
=
149 " float gray = gl_TexCoord[0].y; \n"
150 " gl_FragColor = vec4(gray); \n"
154 * Perspective interpolation, converted to linear interpolation
155 * (put window position W in texcoord.w)
157 static const char *VertShaderText2
=
159 " vec4 pos = ftransform();\n"
160 " gl_TexCoord[0] = gl_MultiTexCoord0 * pos.w; \n"
161 " gl_TexCoord[0].w = pos.w; \n"
162 " gl_Position = pos;\n"
165 static const char *FragShaderText2
=
167 " float gray = gl_TexCoord[0].y / gl_TexCoord[0].w; \n"
168 " gl_FragColor = vec4(gray); \n"
171 if (!ShadersSupported())
174 VertShader1
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText1
);
175 VertShader2
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText2
);
177 FragShader1
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText1
);
178 FragShader2
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText2
);
180 Program1
= LinkShaders(VertShader1
, FragShader1
);
181 Program2
= LinkShaders(VertShader2
, FragShader2
);
183 glClearColor(0.5f
, 0.5f
, 0.5f
, 0.0f
);
184 glEnable(GL_DEPTH_TEST
);
186 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
188 assert(glIsProgram(Program1
));
189 assert(glIsProgram(Program2
));
190 assert(glIsShader(VertShader1
));
191 assert(glIsShader(VertShader2
));
192 assert(glIsShader(FragShader1
));
193 assert(glIsShader(FragShader2
));
200 main(int argc
, char *argv
[])
202 glutInit(&argc
, argv
);
203 glutInitWindowSize(Width
, Height
);
204 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
205 Win
= glutCreateWindow(argv
[0]);
207 glutReshapeFunc(Reshape
);
208 glutKeyboardFunc(Key
);
209 glutDisplayFunc(Redisplay
);