/** Show contents of depth buffer in middle of window */
static void
show_depth_fbo(void)
{
   GLfloat *zf;

   glViewport(1 * SIZE, 0, SIZE, SIZE); /* not really needed */

   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
   zf = read_float_z_image(0, 0);

   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);

   glWindowPos2i(SIZE, 0);
   glDrawPixels(SIZE, SIZE, GL_LUMINANCE, GL_FLOAT, zf);
   if (!piglit_check_gl_error(GL_NO_ERROR))
	    piglit_report_result(PIGLIT_FAIL);

#if DEBUG
   {
      GLfloat min, max, center;
      find_float_min_max_center(zf, SIZE * SIZE, &min, &max, &center);
      printf("depth fbo min %f  max %f  center %f\n", min, max, center);
   }

   {
      GLuint min, max, center;
      GLuint *zi;

      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
      zi = read_uint_z_image(0, 0);
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo);

      find_uint_min_max_center(zi, SIZE * SIZE, &min, &max, &center);
      printf("depth fbo min 0x%x  max 0x%x  center 0x%x\n", min, max, center);
      free(zi);
   }
#endif /* DEBUG */

   free(zf);
}
/** Show contents of depth buffer in middle of window */
static void
show_depth_fbo(void)
{
   GLfloat *zf;

   glViewport(1 * SIZE, 0, SIZE, SIZE); /* not really needed */

   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
   zf = read_float_z_image(0, 0);

   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

   glWindowPos2i(SIZE, 0);
   glDrawPixels(SIZE, SIZE, GL_LUMINANCE, GL_FLOAT, zf);
   assert(glGetError() == 0);

#if DEBUG
   {
      GLfloat min, max, center;
      find_float_min_max_center(zf, SIZE * SIZE, &min, &max, &center);
      printf("depth fbo min %f  max %f  center %f\n", min, max, center);
   }

   {
      GLuint min, max, center;
      GLuint *zi;

      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
      zi = read_uint_z_image(0, 0);
      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

      find_uint_min_max_center(zi, SIZE * SIZE, &min, &max, &center);
      printf("depth fbo min 0x%x  max 0x%x  center 0x%x\n", min, max, center);
      free(zi);
   }
#endif /* DEBUG */

   free(zf);
}
/**
 * Draw quad with fragment shader that compares fragment.z against the
 * depth texture value (draw on left side of window).
 * We draw on the left side of the window to easily convert gl_FragCoord
 * into a texture coordinate.
 */
static void
draw_sphere_with_fragment_shader_compare(void)
{
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

   glViewport(0 * SIZE, 0, SIZE, SIZE);

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-1.0, 1.0, -1.0, 1.0, -1, 1.0);

   glBindTexture(TexTarget, DepthTex);

   piglit_UseProgram(ShaderProg);

   glEnable(GL_DEPTH_TEST);

   if (1) {
      draw_sphere();
   }
   else {
      /* To test using gl_TexCoord[0].xy instead of gl_FragCoord.xy in the shader
       */
      static const GLfloat sPlane[4] = {0.5, 0, 0, 0.5};
      static const GLfloat tPlane[4] = {0, 0.5, 0, 0.5};

      glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
      glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
      glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
      glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
      glEnable(GL_TEXTURE_GEN_S);
      glEnable(GL_TEXTURE_GEN_T);
      
      draw_sphere();

      glDisable(GL_TEXTURE_GEN_S);
      glDisable(GL_TEXTURE_GEN_T);
   }

   glDisable(GL_DEPTH_TEST);

   piglit_UseProgram(0);

#if DEBUG
   {
      GLfloat *z = read_float_z_image(0, 0);
      GLfloat min, max, center;

      find_float_min_max_center(z, SIZE * SIZE, &min, &max, &center);
      printf("rendered  min %f  max %f  center %f\n", min, max, center);

      free(z);
   }
   {
      GLuint *z = read_uint_z_image(0, 0);
      GLuint min, max, center;

      find_uint_min_max_center(z, SIZE * SIZE, &min, &max, &center);
      printf("rendered  min 0x%x  max 0x%x  center 0x%x\n", min, max, center);

      free(z);
   }
#endif /* DEBUG */
}