MRT,多渲染对象,用来将场景同时渲染到多个目标中去。
1. 设置4个渲染目标
- bool initFramebuffer(int w, int h)
- {
- glGenRenderbuffers( 1, &g_DepthRenderBuffer);
- glBindRenderbuffer( GL_RENDERBUFFER, g_DepthRenderBuffer );
- glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h);
- CHECK_GL_ERROR
- glGenFramebuffers(1, &g_FrameBuffer);
- glBindFramebuffer(GL_FRAMEBUFFER, g_FrameBuffer);
- CHECK_GL_ERROR
- for(int i = 0; i <= 3; ++i)
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_TEXTURE_2D, g_Texture2Ds[i], 0);
- glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,g_DepthRenderBuffer );
- CHECK_GL_ERROR
- GLenum DrawBuffers[4];
- DrawBuffers[0] = GL_COLOR_ATTACHMENT0;
- DrawBuffers[1] = GL_COLOR_ATTACHMENT1;
- DrawBuffers[2] = GL_COLOR_ATTACHMENT2;
- DrawBuffers[3] = GL_COLOR_ATTACHMENT3;
- glDrawBuffers(4, DrawBuffers);
- CHECK_GL_ERROR
- if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
- return false;
- // glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- // glBindRenderbuffer( GL_RENDERBUFFER, 0 );
- return true;
- }
2. 渲染到4个渲染目标
- #version 300 es
- #ifdef GL_FRAGMENT_PRECISION_HIGH
- precision highp float;
- #else
- precision mediump float;
- #endif
- #define FRAG_RED 0
- #define FRAG_GREEN 1
- #define FRAG_BLUE 2
- #define FRAG_RGBA 3
- in vec3 vColor;
- in vec3 vPos;
- layout(location = FRAG_RED) out vec4 Red;
- layout(location = FRAG_GREEN) out vec4 Green;
- layout(location = FRAG_BLUE) out vec4 Blue;
- layout(location = FRAG_RGBA) out vec4 RGBA;
- void main()
- {
- vec4 Color = vec4(vColor, 1.0) * 0.7f;
- float light = 1.0 / (length(vPos)+0.5f);
- Red = vec4(Color.r * light, 0, 0, 1);
- Green = vec4(0, Color.g * light, 0, 1);
- Blue = vec4(0, 0, Color.b * light,1);
- RGBA = Color * light;
- }