1

To keep it short: As we all know, opengl draws many triangles on the screen.

What I want to know:

If opengl draws triangles, it has to know, which triangle belongs to which pixel on the screen, right?

Can I get this information too? For example call a function after a draw (like gl_retrieve_object_map() or anything similar) where I get to know which pixel shows which triangle?

its like doing picking for every pixel

I am thinking of an 2 dimensional array telling me the triangle index or whatever:

on a 10x10 pixel screen for example:

0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 2 0
0 0 0 0 1 1 1 2 2 0
0 0 0 0 0 1 1 2 2 0
0 0 0 0 0 2 1 2 2 0
0 0 0 0 2 2 2 2 2 0
0 0 0 2 2 2 2 2 2 0
0 0 2 2 2 2 2 2 2 0
0 2 2 2 2 2 2 2 2 0
0 0 0 0 0 0 0 0 0 0

for an image like:

   ___
  \  |
   \ |  
    \|/|
     / |
    /  |
   /   |
  /____|

is there anything that i could use?

bricklore
  • 4,125
  • 1
  • 34
  • 62

2 Answers2

5

OpenGL actually doesn’t have this information after something is drawn. OpenGL has a few buffers, including (among others) the color buffer and depth buffer, but it does not have a buffer that holds an index for each primitive drawn.

But that doesn’t mean you’re out of luck. One thing I have seen applications do is remove all lighting effects and give each object a unique color. Then, it renders the scene to an off-screen buffer. Then that off-screen buffer has exactly the information you want.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Even though this is a big hack, its a really cool solution! Thank you very much! But musnt opengl store that information at drawing time? how would it get the color and the right shaders for the object else? – bricklore Dec 28 '14 at 22:47
  • @Malte: OpenGL has variables storing the current shaders and colors to use when drawing an object. When it draws that object, for each pixel it figures out the depth of the object at that place, and if it’s closer than the previously-recorded depth, it will calculate the color of that pixel and overwrite that in the color buffer and note the new minimum depth in the depth buffer. Then, when you go to render a new object, it overwrites those variables. The only trace of the previous object having been rendered is the colors in the color buffer and the depths in the depth buffer. – icktoofay Dec 28 '14 at 22:51
  • okay thas good and bad :) my first thought was: shouldnt this method from ogl be terribly slow? and also if it can determine the object there, why cant we? – bricklore Dec 28 '14 at 22:55
  • @Malte: Well, it’s quite demonstrably not slow. To the best of my knowledge, Direct3D does this too. Among its advantages, it is extremely amenable to parallelization, hence modern GPUs being tailored to highly parallel tasks. – icktoofay Dec 28 '14 at 23:00
  • @Malte: And part of the point is that it *doesn’t* know what object is there. After an object is drawn, there is no record of it any more; it is in the bitmap, flattened. No trace of any primitive data or index any more. It renders purely based on the bitmap it has already drawn and an additional primitive, yielding a new bitmap. Again, to emphasize, ***no data about what it just drew is recorded***. – icktoofay Dec 28 '14 at 23:03
  • yeah what i wanted to mention is, if opengl can determine those informations **temporarily** at drawing time, I thought there should be a way to repeat the process manually afterwards, but either way, ive got a solution now :) – bricklore Dec 28 '14 at 23:08
  • @MalteSchmitz: Well, in modern OpenGL you can get back the information at where triangles got placed, through a mechanism called "transform feedback". It got introduced into modern OpenGL only because GPUs got programmable and re-doing the transformation steps on the CPU no longer was a sensible thing. But it's quite possible to do the whole transformation setup in software as well. – datenwolf Dec 29 '14 at 02:42
1

If opengl draws triangles, it has to know, which triangle belongs to which pixel on the screen, right?

No. OpenGL draws the triangles in the order they are asked it to draw, one triangle at a time. The program asks it to "draw a triangle between coordinates A, B and C" and OpenGL will do it then and there (well, actually it will batch these commands up).

So before the triangle has been drawn there are just the vertex attributes, that determine its position. Then for each triangle in the list it gets drawn and then the only thing left behind are colored pixels without and information on which triangles created them.

datenwolf
  • 159,371
  • 13
  • 185
  • 298