# How can I determine the pixels covered by a transformed triangle (without drawing it)?

I have three points that form a triangle in a 3D scene. I want to apply an arbitrary rotation/translation transformation to this polygon and later check in which pixels of an image they would be rendered (if any), taking occlusion into account. I want to do this without having any window, so that no actual drawing is done (I know the camera parameters and the "virtual" rendering image size).

How can I accomplish this? I thought about OpenGL and the glReadPixels function, but I am not sure that I can query for a 3D point to get its rendered 2D pixel.

For reference, I am working on a computer vision application where I have an image taken by a camera and a 3D model of a virtual object. The virtual object is a mesh of a lot of triangles. What I actually want is to project the virtual object on the image and get the positions in the image of the non-occluded triangles, so that I can manipulate the image in those positions. As I said, I don't really need to draw, just the calculations. Now, I am doing it manually and I can get rid of those points that are occluded by other points, but I do not know how to take occlusion into account when instead of single points, they are planar triangles.

• N.B. that asking for libraries or tools is off-topic ("which technology to use"). – user1430 Mar 20 '14 at 3:02

I know you don't want to render, but is this a hard requirement? How about rendering to a texture, and then reading the resulting pixels from the texture? no screen rendering will take place, and you will be taking advantage of the hardware to do it for you. Here's a tutorial that seems to explain how render-to-texture works.

If you really don't want to do any rendering at all, you can always multiply your vertices by your projection matrix, divide by w, discard z, and your x and y are the coordinates of each vertex in screen space (their meaning is dependent on the projection matrix you used).

With your three vertices in screen space, all you have to do is rasterize them to know which pixels lie inside the triangle. Here's a tutorial that seems to explain rasterization.

• I didn't use the correct words, sorry. What I don't want is to draw in any window, but I do want that OpenGL or some other library does the rendering calculations. The render-to-texture is a nice approach, thanks, but I don't see that I can directly access a rendered 2D pixel given a 3D point. Now, I am computing all by myself, except for the occlusions, which are tricky. I think that relying on some well-established library is a smarter option. – ChronoTrigger Mar 20 '14 at 2:55
• Sure you can. If you just want to know the location of a 3D point (not triangle), you can just draw a point (for example with glBegin(GL_POINTS)), and then check the resulting texture to see where the pixel is lit. But if you just want to know where a 3D point would be in the screen, you may want to simply project it yourself. Both solutions are easy to implement anyways. – Panda Pajama Mar 20 '14 at 5:10
• How do I check what pixel is lit, by exhaustively searching the texture? – ChronoTrigger Mar 20 '14 at 5:36
• Yep. Thousands of optimizations come to my mind, but generally, yes. – Panda Pajama Mar 20 '14 at 7:37
• That is something I would like to avoid. – ChronoTrigger Mar 20 '14 at 8:05

OpenGL provides occlusion queries, described here. They are not exactly simple to use, but I believe they will do what you need.

Basically, you create an occlusion query object, then "activate" it with glBeginQuery during the rendering that you want to analyze. After you are done drawing the geometry of interest, call glEndQuery and glGetQueryObject, passing GL_QUERY_RESULT.

This technique can be used with the texture rendering technique described above.

For an interesting description of some performance issues and ways to get around them, check out this good page.