   reverse ray tracing (solved)
  hi, so im trying to do a reverse ray tracing, basically i want to find a point on a world space correspond to the screen space, knowing a the camera position and angle. so here is the explanation:i find a 3D position in the world space, eg: x, y, z. now i want to find what is the 2D screen position of this x, y, z position on the screen, ie. x, y position. i cant project it directly since its not an orthogonal camera. its a perspective camera with a certain widen angle. how can i do that? thanks in advance[Edited by - svnstrk on April 14, 2010 3:25:55 PM]
  If you already have a projrction matrix for your camera you could use that. Another way could be to calculate the intersection between the ray and the view plane, and then convert the resultin coordinates in screen coords...   ;  well im not sure about matrix, i dont think i have such matrix. i only have a camera position (xyz), a point to look at (xyz) and an up vector (xyz), basically the whole gluLookAt function. im not sure about ray/plane intersection because how can i define the direction because the pixel may drop anywhere and it will give lots of direction, not one. imagine this very basic ray tracing concept:http://morawietz.me/images/raytracing_1.png(image found by googling, ssorry if copyright violence :D )now instead of shooting a ray from the pixel and find the intersection point to an object of the scene, im trying to do the opposite, which is, knowing a point on the scene, how can i find the pixel that represent it on the screen?any idea? thanks in advance   ;  so i have this idea to implement this:find the inverse of camera matrixmultiply the 3D point to transform it to non-camera transformed positionuse the z value to find corresponding x and y on the screenim not sure how to work on this but im currently doing the first step: find the inverse of camera matrix. i understand how to do the camera matrix knowing the position, looking position and up vector, but how to find the inverse?thanks in advance   ;Quote:Original post by svnstrkwell im not sure about matrix, i dont think i have such matrix. i only have a camera position (xyz), a point to look at (xyz) and an up vector (xyz), basically the whole gluLookAt function. The gluLookAt function generates a view matrix.Somewhere along the line, i'm guessing you also call gluPerspective, that generates a projection matrix. You can extract both of those matricies back out, by using glGetFloatv, then use those matricies to do the transform.Similarly, there is also a gluProject and gluUnproject which do the 3d to screen and vice versa for you.;  hi, i did not use gluLookAt explicitly in my program. so basically this is what im trying to do.i have given an image of a scene, fully rendered. now i also have information about the triangles and its xyz, uv coordinate in the scene, and the camera position/look at/up and its perspective angle. now what im trying to do is to find the texture of each tringle on the screen. one wy to to that is to loop each pixel on the texture, based on the given uv, find the xyz coordinate in the world space, and try to find the corresponding pixel on the screen. i am able to find the xyz coordinate of the triangle, but the question is, with given information above, how can i find the corresponding pixel on the screen with the perspective effects the camera?my first solution is to translate the triangles position to camera relative coordinate. then from the new triangle xyz, i can find the ratio to find the corresponding xy on the screen.now what im trying to do is to translate the whole triangle position, the xyz based on the world coordinate, to the camera based coordinate, knowing only the position, look at, and up of the camera. im working on it so hopefully come up with something soon. any idea/advise would be greatthanks in advance   ;  hi, so i found out about the glGetFloatv() and i try to put my gluLookAt and gluPerspective matrix by using those method. after i got those two matrix, what should i do with it? should i multiply it to the world matrix and get my xy screen position?basically what im asking is what can i do with those matrix and how can i do it?thank in advance   ;  If you're using OpenGL(or basicall any rendering api), the series of transformations that you use isLocal->WorldWorld->Camerathis is encoded in the model view matrix I assume you're not using a Local->World transform.Camera->NDC[normalized device coordinates]this is encoded in the perspective matrixNDC->ScreenScreen->ViewportI'll assume you're not using a viewport since you don't mention it.NDC coordinates are from [-1, 1][-1, 1][0, 1] and you want to map it to [0, w][0, h] where w/h are the width/height of the screen and just throw away the z. S'.x = w * (1+S.x) / 2S'.y = h * (1+S.y) / 2orS'.x = w/2 + w * S.x / 2S'.y = h/2 + h * S.y / 2or as a 4x4 matrix[w/2, 0, 0, w/2,0, h/2, 0, h/2, 0, 0, 0, 0,0, 0, 0, 1]So you have a point X in world space and you want X' in screen space all you need to do is multiply it by all the matrices.X' = screen*perspective*modelView*Xyou modelview and perspective matrices can be gotten by glGetFloat or you can look at the red book or something if you want to see how they're formed by the various GL calls.   ;  There is no single "point" that corresponds to a point in screenspace. It is a ray.Getting it with OpenGL is pretty easy:Somewhere in your code you need variables to remember the Setup of the last rendered Frame:GLdouble m_curModelView[16];GLdouble m_curProjection[16];GLint m_curViewport[4];Everytime you start to render a new Frame (after you set the viewport, modelview and projection matrix) you remember the setup by doing this:glGetDoublev(GL_MODELVIEW_MATRIX, m_curModelView);glGetDoublev(GL_PROJECTION_MATRIX, m_curProjection);glGetIntegerv(GL_VIEWPORT, m_curViewport);Now you can get the ray for a pixel in the previous rendered frame (that what the user sees) by doing this:GLdouble xA, yA, zA;gluUnProject(pixelX, pixelY, 0, m_curModelView, m_curProjection, m_curViewport, &xA, &yA, &zA);GLdouble xB, yB, zB;gluUnProject(pixelX, pixelY, 1, m_curModelView, m_curProjection, m_curViewport, &xB, &yB, &zB);Now the line segment from point A to point B points to the direction the useer sees at the pixel. This is in worldspace so no more transformations are needed.;  hi, here is the code ive been through by considering your idea:http://pastie.org/918245assuming everythings working, here is the explanation:there are two drawing method, drawTriangles() (line 140) which draw the real triangle based on the given perspective (line 174) and gluLookAt (line 178). the other drawing method, drawConvertedTriangles() (line 159) draws the converted point (line 104-107) i expect both the method to draw the same triangle on the screen. but apparently the result is not. i called this method in method renderScene() (lne 113) where the first one i draw with projection and lookat turned on, and the second one i draw after setting both projection and lookat to identity. im not sure the problem is me not getting the viewport, the viewport is a vector4, how can i multiply it by applying scene * projection * modelviewwhich is v4*matrix4*matrix4 which is not possible. still i dont think thats the problem, any idea how to deal with this?thanks in advance   