【发布时间】:2017-04-26 22:35:13
【问题描述】:
我目前正在按照几个在线教程在 OpenGL 中编写图形渲染器。我最终得到了一个具有渲染管道的引擎,该管道基本上包括使用简单的 Phong 着色器渲染对象。我的 Phong 着色器有一个基本的顶点着色器,它根据变换修改顶点,还有一个片段着色器,看起来像这样:
// PhongFragment.glsl
uniform DirectionalLight dirLight;
...
vec3 calculateDirLight() { /* Calculates Directional Light using the uniform */ }
...
void main() {
gl_FragColor = calculateDirLight();
我的对象的实际绘图如下所示:
// Render a Mesh
bindPhongShader();
setPhongShaderUniform(transform);
setPhongShaderUniform(directionalLight1);
mesh->draw(); // glDrawElements using the Phong Shader
这种技术效果很好,但有一个明显的缺点,即我只能有一个定向光,除非我使用均匀阵列。我可以这样做,但我想看看还有哪些其他解决方案可用(主要是因为我不想在着色器中制作大量灯光的阵列并且其中大部分是空的),我偶然发现了这个一,这似乎真的很低效,但我不确定。它基本上涉及每次使用新灯光重新绘制网格,如下所示:
// New Render
bindBasicShader(); // just transforms vertices, and sets the frag color to white.
setBasicShaderUniform(transform); // Set transformation uniform
mesh->draw();
// Enable Blending so that all light contributions are added up...
bindDirectionalShader();
setDirectionalShaderUniform(transform); // Set transformation uniform
setDirectionalShaderUniform(directionalLight1);
mesh->draw(); // Draw the mesh using the directionalLight1
setDirectionalShaderUniform(directionalLight2);
mesh->draw(); // Draw the mesh using the directionalLight2
setDirectionalShaderUniform(directionalLight3);
mesh->draw(); // Draw the mesh using the directionalLight3
不过,这对我来说似乎非常低效。我不是一遍又一遍地重绘所有网格几何体吗?我已经实现了这个,它确实给了我想要的结果,多个定向灯,但是帧速率已经大大下降。这是渲染多个灯光的愚蠢方式,还是与使用着色器统一数组相提并论?
【问题讨论】:
-
是的,该教程使用统一数组,但我真的不想这样做,因为我最终可能会得到一个只有 1 个方向光的游戏,但我的着色器正在计算 4 个方向灯。
-
所以当只有 1 盏灯时使用不同的着色器?
-
您可以使用SSBO with an shader storage block of indeterminate array length,然后您可以摆脱着色器中的大数组。其余的可以与教程中的相同。除非您想多次绘制几何图形(这会很慢),否则在着色器中使用某种数组是唯一的方法。
-
可能是这个老QA:How lighting in building games with unlimited number of lights works?会发光......
标签: opengl graphics opengl-es rendering shader