   Projection/Unprojection with linear depth (again!)
  Hi,I've been trying to unproject fragments with linear (view space) depth using the ray technique.But when I reproject the point following the traditional gl way (http://www.songho.ca/opengl/gl_transform.html), I do not get the same results !I use this to project/unproject : (http://www.michimichbeck.de/img/Projektion.png) which is the same than interpolating the far plane frustum corners... I really can't find what's wrong !My linear depth is not clamped and values are in [-zmin, -zfar](computed in glsl with matModelview*vertexPos)Here's some C code (using glm library) which illustrates my results(note : window size hard coded to 800x800)#include <iostream>#include <cmath>#include <glm/glm.hpp>#include <glm/gtc/matrix_projection.hpp>glm::vec3 unproject(glm::vec2 pixel, float depth, float tanfovy, float ratio){    glm::vec3 result;    result.x = ((pixel.x*2.0)-1.0)*tanfovy*ratio;    result.y = ((pixel.y*2.0)-1.0)*tanfovy;    result.z = -1.0;    result = result * -depth;return result;} glm::vec2 project(glm::vec3 vertex, float tanfovy, float ratio){    glm::vec2 result;result.x = 0.5 - (vertex.x/(vertex.z*tanfovy*ratio*2.0));    result.y = 0.5 - (vertex.y/(vertex.z*tanfovy*2.0));return result;} glm::vec2 perspproject(glm::vec3 vertex, const glm::mat4& pj){    glm::vec4 clip = pj * glm::vec4(vertex, 1.0);    glm::vec3 ndc;    ndc.x = clip.x/clip.w;    ndc.y = clip.y/clip.w;    ndc.z = clip.z/clip.w;    glm::vec2 screen;    screen.x = 0.5*ndc.x + 0.5;    screen.y = 0.5*ndc.y + 0.5;    return screen;} int main(){// projection matrix    glm::mat4 pj;// projection matrix    pj = glm::perspective(45.0,1.0,0.125,128.0); //fragment : on far clip plane so x,y should be -tan(fovy*0.5)*zFar    glm::vec2 frag(0.0,0.0);// linear eye depth    float z = -128; // unproject : works    glm::vec3 unpj = unproject(frag, z, tan(45.0/2.0), 1.0);    std::cout << "vertex :\n"    << unpj.x << " "    << unpj.y << " "    << unpj.z << "\n"; // reproject like the GL and check if same than original : // ( but doesn't yield same results !)frag = perspproject(unpj, pj);    std::cout << "frag :\n"    << frag.x << " "    << frag.y << "\n";// reproject with unproject reciproqual (this works of course, since it// cancels the unproject operation    frag = project(unpj, tan(45.0/2.0), 1.0);    std::cout << "frag :\n"    << frag.x << " "    << frag.y << "\n";}[Edited by - at0m on May 25, 2010 8:51:39 AM]