在OpenGL中在场景上渲染阴影的最简单方法是什么? [英] What is the simplest method for rendering shadows on a scene in OpenGL?

查看:545
本文介绍了在OpenGL中在场景上渲染阴影的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LWJGL包,并且能够创建基本场景并绘制形状(带有或不带有纹理),移动自定义相机"对象并旋转它以相应地渲染场景.但是,在创建阴影时,我很茫然.

I am using the LWJGL package and am able to create a basic scene and draw shapes (with or without textures), move a custom 'Camera' object and rotate it to render the scene accordingly. However, when it comes to creating shadows, I am at a loss.

我可以想到创建阴影的基本算法.
1)从摄影机的视角渲染场景,就像在阴影中一样.
2)从光源的视点渲染场景,照亮场景的可见部分(也许当场景离光源越来越远时会变暗吗?).
3)从摄像机的视角重新渲染场景.

I can think of the basic algorithm for creating shadows.
1) Render the scene from the camera's view as if in shadow.
2) Render the scene from the light's view, lighting up the visible part of the scene (maybe darken the scene as it becomes farther away from the light source?).
3) Re-render the scene from the camera's view.

但是,我不知道OpenGL的特定方法(尤其是LWJGL).在对该主题进行研究之后,我只遇到了需要几何图形的所有点和平面的教程,或者只包含了我似乎无法为自己的项目工作的部分代码.

However, I do not know the particular methods for OpenGL (particularly LWJGL). Upon research on this topic, I have only come across tutorials that require all the points and planes of the geometry or contain only partial code that I cannot seem to get working for my own project.

我是否正确地考虑了这一点?如果是这样,我需要为阴影模型的可见部分(或做其他事情)使用的方法是什么?

Am I thinking about this correctly? If so, what is (are) the method(s) that I need to use for shading the visible parts of the model (or doing something else)?

推荐答案

最简单的方法是,如您所设计的那样,从光的角度渲染场景.将深度图的内容另存为 shadowmap 纹理:每个深度最接近光源.然后再次渲染场景,并为每个片段计算相对于灯光位置的世界"空间中的纹理坐标.将值与阴影贴图进行比较:如果该值更大,则此片段将被遮盖.

The simplest way is, as you've worked out, to render the scene from the viewpoint of the light. Save the contents of the depthmap as a shadowmap texture: each depth is the closest to the light source. Then render the scene again, and calculate for each fragment texture coordinates in a 'world' space relative to the position of the light. Compare the value to the shadowmap: if it's greater, this fragment is shadowed.

很多年前,我编写了一些旧的(着色前)OpenGL代码来演示这一点.对于GLUT,它在C语言中,但是在LWJGL或JOGL中,所有OpenGL位都具有1:1等效项. http://cs.anu.edu.au/~hugh.fisher /3dstuff/shadows.tar

Many years ago I wrote some old (pre-shader) OpenGL code to demonstrate this. It's in C for GLUT, but all the OpenGL bits have 1:1 equivalents in LWJGL or JOGL. http://cs.anu.edu.au/~hugh.fisher/3dstuff/shadows.tar

将其转换为等效的现代着色器应该不难.

Translating it to a modern shader equivalent shouldn't be hard.

希望这会有所帮助.

这篇关于在OpenGL中在场景上渲染阴影的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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