1

I am trying to implement a simple deferred renderer using OpenGL and having read various tutorials and papers describing the topic, most only offer very minimalistic or abstract description to the structure of a G-Buffer - usually containing diffuse, depth and normal buffers.

Yet even a simple graphics engine wouldn't be complete without some sort of material rendering, using textures, adjustable specular lighting and more. Adding additional buffers for all properties would obviously bloat the G-Buffer a lot so what is the preffered method of obtaining all that per-material data in the second rendering pass?

In my OpenGL implementation i use Framebuffers rendering diffuse, normal and depth to GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 and GL_DEPTH_ATTACHMENT respectively. Is there a way to even store an ID or similar for materials, preferrably in OpenGL 3.3 core?

4

1 回答 1

2

存储材质 ID 的一种方法是将该 ID 作为统一传递给片段着色器,然后将其写入 G 缓冲区。假设您的缓冲区是 4 通道 RGBA8,并且材质 ID 是无符号的 8 位 int,它支持 265 种材质,您在 G-buffer 通道中的片段着色器将如下所示(伪):

uniform uint inMatId;
uniform sampler2D inTexture;
void main()
{
    outputColor = vec4(texture(ourTexture, TexCoord).rgb, inMatId/255); 
}

当然,像这样绘制的最好方法是按材质对对象进行排序,然后绘制。请注意,我将材质 ID 除以 255 以将其映射到 [0,1] 范围。在着色过程中,您可以通过乘以 255 来读取它。同样的方法可以用于您需要的几乎所有其他类型的数据。

于 2015-03-04T12:54:15.563 回答