GLSL中的Phong Flat和Gouraud阴影在同一应用程序中 [英] Phong Flat and Gouraud shadings in GLSL in the same application

查看:136
本文介绍了GLSL中的Phong Flat和Gouraud阴影在同一应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一应用程序中编写Flat,Gouraud和Phong底纹.
我认为使用"if"语句在同一着色器中编写它们是一个问题,因为某些变量应该在平面中是统一的,而在Phong中应该是变化的.

如果我编写3种不同的着色器,是否意味着无论何时我要切换阴影,都必须将它们绑定或分别删除?

我可以编写所有3种着色器都可以在GLSL中使用的函数吗?应该在哪里写?在哪些文件中?

I want to write Flat, Gouraud and Phong shadings in the same application.
I think it''s a problem writing them in the same shader with "if" statements, because some of the variables are supposed to be uniform in flat while supposed to be varying in Phong.

If I write 3 different shaders, does it mean that I will have to bind them or remove them separately, whenever I want to switch the shading?

Can I write functions that all 3 shaders can use in GLSL? Where should they be written? in which files?

Thanks!

推荐答案

从这三个经典着色器开始是一个好主意.我建议您使用三个单独的着色器,不要尝试将它们集成为一个.着色器就像短函数,将被加载到GPU中执行. GPU仅具有有限的内存来存储着色器.随着时间的推移,它已经变得越来越大,但是最好使它们尽可能短.同样,为每个像素执行着色器的代码,并且区分这三种情况的任何额外开销都会减慢GPU的运行速度.也许不是很多,但这仍然是浪费.

我看了很长时间的OpenGL,现在我主要使用DirectX和XNA.因此,我无法告诉您在不同的着色器之间切换时必须执行的操作,但是在切换着色器时,就像在首次加载模型时所做的那样,设置要渲染的模型是合乎逻辑的.

关于您的第三个问题:由于内存限制和着色器的类似插件的特性,通常无法选择为GPU编写函数库.这个想法不错,将来可能会出现,但是到目前为止,着色器被视为简单的独立功能.

我只是查了一下GLSL,发现通常用户定义的功能是可能的.最好查阅GLSL手册或参考,以了解它们的用途或存在哪些限制.

编辑^ 2:还有一个使着色器尽可能简单的原因:如果要支持较旧的图形硬件,则必须遵守较早版本的规范.如果使用最新功能,则只有具有最新硬件的用户才能运行您的程序.这是您的决定,您要走多远才能使程序更加兼容,以及在哪里划清界限.许多程序具有多个用于相同目的的着色器,每个着色器设计用于不同的版本.这通常是图形详细信息和质量设置的一部分.
Starting with those three classic shaders is a good idea. I would suggest that you use three separate shaders and do not tryto integrate them into one. Shaders are like short functions which are going to be loaded into the GPU for execution. The GPU has only limited memory to store the shaders. It has grown larger over time, but still it is better to keep them as short as possible. Also, the shader''s code is executed for every pixel and any additional overhead to distinguish between the three cases will slow the GPU down. Perhaps not very much, but it still is a waste.

It has been a long time I had a look at OpenGL, now I mostly use DirectX and XNA. Therefore I can''t tell you what must be done to switch between different shaders, but it sounds logical to set up your model for rendering when switching shaders, just as you do when you first load the model.

As to your third question: Due to the memory limitation and the plugin-like character of the shaders it would be unusual to have the option to write function libraries for the GPU. The idea is not bad and may be possible in the future, but up to now shaders were seen as simple stand alone functions.

I just looked up GLSL and found out that user defined functions generally are possible. It would be best to consult the GLSL manual or reference to see what they can be used for or what kinds of limitations exist.

Edit^2: There also is another reason for keeping shaders as simple as possible: If you want to support older graphics hardware, then you must stay within the specifications of earlier versions. If you use the latest features, only users with the newest hardware can run your program. It''s your decision how far you go back in order to make your program more cmpatible and where you draw the line. Many programs have several shaders for the same purpose, each one designed for a different version. This usually is part of the graphics detail and quality settings.


这篇关于GLSL中的Phong Flat和Gouraud阴影在同一应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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