Using glFeedbackBuffer

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值