带照明和不带照明的 OpenGL 顶点颜色 [英] OpenGL vertex colours with and without illumination

查看:54
本文介绍了带照明和不带照明的 OpenGL 顶点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个渲染立方体的 OpenGL 着色器程序.为了给立方体着色,我将每个顶点的法线传递给顶点着色器,并计算其相对于点光源的灰度阴影.

I have an OpenGL shader program which renders a cube. To colour the cube, I pass the normal of each vertex to the vertex shader, and calculate its greyscale shade with respect to a point light source.

然而,我现在还想渲染一个红色三角形,它的颜色总是红色,不依赖于光照.但是如果我像以前一样把法线传递给顶点着色器,三角形的颜色会受到光线的影响.

However, I now want to also render a red triangle, whose colour is always red and does not depend on lighting. But if I just pass the normal to the vertex shader as before, the triangle's colour will be affected by the light.

对此的最佳解决方案是什么?我应该在着色器之前计算顶点颜色,并将其传递给顶点着色器吗?或者这是不好的做法?

What is the best solution for this? Should I calculate the vertex colour before the shaders, and pass that to the vertex shader? Or is that bad practice?

推荐答案

主要有两个选项:

  1. 使用一个足够灵活的着色器程序来处理这两种情况.

  1. Use one shader program that is flexible enough to handle both cases.

对于应用基本光照的着色器,通常会传入确定光照方程中环境和漫反射项权重的值(通常作为统一值).使用这样的着色器,如果您想为部分对象使用纯色,您只需通过相应地设置统一调高环境项即可.

For a shader that applies basic lighting, it is common to pass in values (typically as uniforms) that determine the weight of the ambient and diffuse terms in the lighting equation. With a shader like this, if you want a solid color for part of your objects, you simply crank up the ambient term all the way by setting the uniform accordingly.

使用不同的着色器程序.

Use different shader programs.

每种都有好处,您必须找出最适合您的方法.

Each one has benefits, and you have to figure out which works best for you.

方法 1 的主要缺点是您的着色器可能会做比需要更多的工作.在这个例子中,它仍然会评估实体对象的光照方程的漫反射项,即使它对最终结果没有贡献.如果您绘制大量实体几何图形,则可能会影响性能.

The main downside of approach 1 is that your shader might do more work than needed. In this example, it will still evaluate the diffuse term of the lighting equation for the solid objects, even though it does not contribute to the final result. If you draw a lot of solid geometry, that could hurt performance.

方法 2 的主要缺点是您必须切换着色器.如果您经常在实体渲染和光照渲染之间切换,则会影响性能.解决此问题的一种方法是首先绘制所有发光对象,然后绘制所有实体对象,这样每帧只需切换一次着色器.根据您的软件架构,这可能很容易做到,也可能需要进行大量重组.

The main downside of approach 2 is that you have to switch shaders. If you frequently switch between solid and lighted rendering, that can hurt performance. One way to work around this is that you first draw all lighted objects, then all solid objects, so that you have to switch shaders only once per frame. Depending on your software architecture, that may be easy to do, or it could require significant restructuring.

这篇关于带照明和不带照明的 OpenGL 顶点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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