如何在OpenGL中绘制镜像的镜像? [英] How do I draw a mirror mirroring something in OpenGL?

查看:485
本文介绍了如何在OpenGL中绘制镜像的镜像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,要在OpenGL中进行镜像,您基本上是绘制场景,然后将所有内容翻转过来并再次绘制,除了只能通过镜子看到它之外,从而在镜子中创建完美翻转的图像.但是我看到的问题是,执行此操作时,可以看到其他镜像的唯一镜像是在先前镜像之后渲染的镜像.因此,如果我先渲染镜子1,再渲染镜子2,那么镜子1看不到镜子2,但是镜子2可以看到镜子1.如何有效地镜像镜子 而没有发生?

From my understanding is that to mirror in OpenGL, you basically draw the scene, then you flip everything over and draw it again, except only make it visible through the mirror, thus creating a perfectly flipped image in the mirror. But the problem I see, is that when doing this, the only mirrors that can see other mirrors are ones rendered after the previous mirrors. So if I render mirror 1 then mirror 2, mirror 1 can't see mirror 2, but mirror 2 can see mirror 1. How do I effectively mirror a mirror without this happening?

推荐答案

有两种常见的方式使用多次/递归反射来呈现反射...

There are two common ways to render reflections with multiple/recursive reflections...

  1. 渲染纹理,并将该纹理应用于镜面.

  1. Render to a texture and apply that texture to the mirror surface.

此方法的一个优点是您可以使用前一帧中其他镜像的镜像纹理.尽管这会带来很小的延迟,但是当您要通过多个镜子进行查看时,并不会影响性能.如果这是一个问题,则可以在渲染主摄影机之前几次将其重新渲染到反射纹理.

An advantage of this is you can use the mirror textures of other mirrors from the previous frame. While this introduces small delay, it doesn't hurt performance when you want to see through many mirrors. If this is an issue you could re-render to the reflection textures a few times before the main camera render.

如果镜子不是完全平面的,您也可以使用此方法,例如,此方法对于水中的涟漪效果很好.实际上,甚至可以使用立方体贴图对其进行扩展,以支持近似的任意反射.

You can also use this method if the mirror isn't perfectly planar, for example this works well for ripples in water. In fact this can even be extended with cube maps to support approximate arbitrary reflections.

  • http://www.bonzaisoftware.com/tnp/gl-water-tutorial/ (overkill for planar reflections, but quite related)
  • http://ivanleben.blogspot.com.au/2008/03/water-reflections-with-opengl.html
  • http://renderingwonders.wordpress.com/2011/01/03/chapter-08-%E2%80%93-buffer-objects-storage-is-now-in-your-hands-%E2%80%93-fbo-textures/
  • http://cgvr.cs.uni-bremen.de/teaching/cg_literatur/Cube_map_tutorial/cube_map.html

门户渲染,如您所说,在其中使用模板缓冲区来遮盖镜面,围绕镜面翻转场景,然后重新渲染.

Portal rendering where, as you say, use the stencil buffer to mask off the mirror surface, flip the scene around the mirror's plane, and re-render.

镜像渲染只是门户渲染的一种特殊情况.这可以递归完成,但由于必须正确管理深度缓冲区,因此变得相当复杂.您还将需要为场景提供空间数据结构,因此不必完全重新渲染,而只需渲染通过镜子可以看到的内容(否则它会变得非常慢).如果您走这条路,我建议您慢慢走,并包括很多 视觉调试,并使用简单直观的测试场景.

Mirror rendering is just a special case of portal rendering. This can be done recursively but it gets quite complicated as you have to manage the depth buffer correctly. You will also need a spatial data structure for your scene, so you don't have to do a full re-render and only render what you can see through the mirror (or it gets very slow very quickly). If you go down this path, I'd suggest taking it slowly and include lots of visual debugging and use a simple and intuitive test scene.

(关于单反射模板缓冲区使用的页面很多,但这不是您想要的,所以我不会列出一些内容)

(There are lots of pages on single-reflection stencil buffer use, but this isn't what you're after so I won't bother listing some)

  • http://en.wikibooks.org/wiki/OpenGL_Programming/Mini-Portal
  • http://en.wikibooks.org/wiki/OpenGL_Programming/Mini-Portal_Recursive
  • http://www.fairyengine.com/articles/portals.htm
  • http://www.benjaminklingler.com/portal-rendering.html

*请注意,这些链接只是几分钟的搜索结果.随时进行编辑和删除/添加.

*Please note these links are just the result of a few minutes googling. Feel free to edit and remove/add.

这篇关于如何在OpenGL中绘制镜像的镜像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆