现在的位置: 首页 > 综合 > 正文

Using glFeedbackBuffer

2013年06月07日 ⁄ 综合 ⁄ 共 2826字 ⁄ 字号 评论关闭

For quite some time I had no idea what glFeedbackBuffer was, or even that it existed. I discovered it one day and found how incredibly
useful it is.

The FeedbackBuffer lets you
get feedback from OpenGL (yeah, go figure?). What does this mean? Say
you want to know the exact screen coordinates of where something will be
rendered in your 3d world. There's no accurate way to calculate this,
because the player could be anywhere in the game and see it from any
angle. But thanks to FeedbackBuffer we can find out with no calculations
at all.

What it does is it tells
OpenGL to *not* render something to the screen. Instead, it "pretends"
to render it, meaning it calculates where it will be on screen, because
OGL has to know where to draw each pixel, and then gives us feedback on
where it was placed. This can also be used to figure out what color
something was rendered at, even after light calculations were made.
Neat, huh?

This tutorial won't cover getting colors like I just mentioned, but maybe I'll do one if you request it
enough. Hint hint.

 

The Source Code

Ok, lets get to it. First we have to make a variable through which OpenGL returns our information.

GLfloat buf[4];

Second step is to tell OpenGL how we want to use the FeedbackBuffer. We have
to do this before we set our RenderMode to FeedbackBuffer, otherwise we get an error. Use this line of code to do it:

glFeedbackBuffer(4, GL_3D, (float *)buf);

What do these parameters do?

  • The "4" tells OGL how many values to
    return. While we only want x, y, and z coordinates, FeedbackBuffer needs
    one other value for some reason. Wierd, yes, but all I know is this
    extra value doesn't matter.
  • "GL_3D" means that we want to get the 3d coordinates of what
    will be rendered. Note that instead of returning an actual Z coordinate
    value, it returns a floating point decimal between 0 and 1, 0 meaning
    close to the viewer and 1 meaning far away from the viewer.
  • "(float *)buf" gives OpenGL the pointer to our return buffer.

Now lets turn on Feedback:

glRenderMode(GL_FEEDBACK);

Now we can do stuff to get feedback. A good way to do this is to simply render a point at the spot we want to test.

 

glBegin(GL_POINTS);
glVertex3f(x, y, z);
glEnd();

Finally, lets turn off Feedback and set our RenderMode back to GL_RENDER:

glRenderMode(GL_RENDER);

Move on to see how to use this feedback.

How to use glFeedbackBuffer's Feedback

After we render anything while in GL_FEEDBACK, our variable buf
is updated with the new information.

 

  • buf[0] = extra variable (mentioned earlier), unused
  • buf[1] = X screen coordinate of the object rendered
  • buf[2] = Y screen coordinate
  • buf[3] = Z value (between 0 and 1, 0 being close to the viewer)

The Z value represents how OpenGL does Z
Buffer, or Depth Testing. Since glFeedbackBuffer pretends to render
things to the screen, we can see how Depth Testing actually works. What
it does is, each time anything is rendered to the screen, OpenGL stores
it's Depth value (between 0 and 1) and keeps track of it. Next time it
has to render something to that pixel, it checks if the new pixel to be
rendered has a different Z value. If the new value is greater (farther
away) than the stored pixel value, it won't render the pixel. Otherwise,
it renders it and stores the new value in the Depth Buffer.

 

http://users.polytech.unice.fr/~buffa/cours/synthese_image/DOCS/Tutoriaux/glGameDeveloppers/view.cgi-V=tutorial_glfeedback&S=3.htm

抱歉!评论已关闭.