Original Post
I am trying to implement an averaging and downsampling filter. This is how I do it:- 1) I load a 16x16 image into texture memory and set the texture filtering to GL_NEAREST. 2) I draw a quad of 4x4 and the size of the viewport is the same as the quad (width = 4, height = 4) 3) Also I am using gluOrtho2D() From here on correct me if I am wrong:- -------------------------------------- 1) I am assuming that the rasterizer would generate no more than 4x4 fragments and the lower left corner of the GLUT window corresponds to the gl_FragCoord.x = 0.0 and gl_FragCoord.y = 0.0 inside the pixel shader. Below is the pixel shader that does the averaging and the size (4x4) of the quad that I drawing automatically does the downsampling. So eventualy I should end up with 4x4 image. Each pixel in the final image is nothing but the average of the current location, right pixel, diagonally up pixel and up pixel. I am using an identity matrix in gl_TextureMatrix[0]. Fragment Shader:- ------------------------------------------------------------------------------- uniform mat2 kernel; uniform samplerRECT original; void main(void) { vec3 color; mat3 texMatrix = mat3(gl_TextureMatrix[0]); vec3 texCoord = 2.0 * vec3(gl_FragCoord.x, gl_FragCoord.y, 0.5); /* First value */ vec3 tmp = texMatrix * texCoord; tmp.z = 1.0 / tmp.z; tmp.xy *= tmp.z; if(tmp.x < 0.0 || tmp.x >= 640.0) discard; if(tmp.y < 0.0 || tmp.y >= 480.0) discard; color = (vec3) texRECT(original, vec2(tmp)); /* Second value */ tmp = texMatrix * vec3(texCoord.x + 1, texCoord.y, 1.0); tmp.z = 1.0 / tmp.z; tmp.xy *= tmp.z; if(tmp.x < 0.0 || tmp.x >= 640.0) discard; if(tmp.y < 0.0 || tmp.y >= 480.0) discard; color += (vec3) texRECT(original, vec2(tmp)); /* Third value */ tmp = texMatrix * vec3(texCoord.x + 1, texCoord.y + 1, 1.0); tmp.z = 1.0 / tmp.z; tmp.xy *= tmp.z; if(tmp.x < 0.0 || tmp.x >= 640.0) discard; if(tmp.y < 0.0 || tmp.y >= 480.0) discard; color += (vec3) texRECT(original, vec2(tmp)); /* Fourth value */ tmp = texMatrix * vec3(texCoord.x, texCoord.y + 1, 1.0); tmp.z = 1.0 / tmp.z; tmp.xy *= tmp.z; if(tmp.x < 0.0 || tmp.x >= 640.0) discard; if(tmp.y < 0.0 || tmp.y >= 480.0) discard; color += (vec3) texRECT(original, vec2(tmp)); color *= 0.25; gl_FragColor = vec4(color, 1.0); } -------------------------------------------------------------------------------