【发布时间】:2015-03-21 14:38:03
【问题描述】:
我有一个 fbo,里面有 8 个纹理(g-buffer)。我要做的是将不透明对象的所有信息渲染到前四个(颜色、法线、位置、深度),并将透明对象的信息渲染到最后四个纹理。但是,无论我做什么,openGL 似乎都只想渲染到第四个。此外,应用程序启动时的第一帧是我用于透明对象的纹理的一些奇怪原因。
写入 g-buffer 的着色器:
in vec2 texcoord;
in vec3 worldpos;
in mat3 tbnmatrix;
uniform sampler2d diffuse;
uniform sampler2d normalmap;
uniform sampler2d dispmap;
uniform float dispmapbias;
uniform float dispmapscale;
uniform float specularintensity;
uniform float specularpower;
uniform float opacity;
uniform vec3 c_eyepos;
layout (location = 0) out vec4 diffuseColor;
layout (location = 1) out vec4 normalColor;
layout (location = 2) out vec4 positionColor;
layout (location = 3) out float depthcolor;
layout (location = 4) out vec4 diffuseColorT;
layout (location = 5) out vec4 normalColorT;
layout (location = 6) out vec4 positionColorT;
layout (location = 7) out float depthColorT;
void main(){
//只计算位移的纹理坐标 vec2 texcoords = calcParallaxTexcoords(dispmap, tbnmatrix, normalize(c_eyepos - worldpos), texcoord, dispmapscale, dispmapbias);
//Calculating normals
vec3 n = normalize(tbnmatrix * (255.0/128.0 * texture(normalmap, texcoords).xyz - 1));
if(opacity == 1){
diffuseColor = vec4(texture(diffuse, texcoords).xyz, 1);
normalColor = vec4(n, specularintensity);
positionColor = vec4(worldpos, specularpower);
depthColor = gl_FragCoord.z;
}
else{
diffuseColorT = vec4(texture(diffuse, texcoords).xyz, opacity);
normalColorT = vec4(n, specularintensity);
positionColorT = vec4(worldpos, specularpower);
depthColorT = gl_FragCoord.z;
}
}
我会发布创建我的 FBO 的代码,但这将是非常多的代码,并且难以理解(因为我的架构)。以下是我传入的值:
//Filters to be used in rendertargets
GLenum filters[8] = { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR };
//What I pass in to the third parameter of glTexImage2D
GLenum internalFormats[8] = { GL_RGBA, GL_RGBA16F, GL_RGBA16F, GL_R32F, GL_RGBA, GL_RGBA16F, GL_RGBA16F, GL_R32F };
//Formats, not really important here
GLenum formats[8] = { GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA };
//Attachment to be used in glFramebufferTexture2D (and glDrawBuffers)
GLenum attachments[8] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5,
GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7 };
【问题讨论】:
-
你检查过
GL_MAX_COLOR_ATTACHMENTS吗? -
你在哪里打电话给
glCheckFramebufferStatus()?也许,它是否返回GL_FRAMEBUFFER_UNSUPPORTED?据我了解,支持超过 4 种颜色附件的实现相当罕见,而且它们通常必须具有相同的像素格式。 -
好吧,我正在调用它,它正在返回 GL_FRAMEBUFFER_COMPLETE。如果真的很难支持它,也许我最好把它分成 2 个 4 纹理 fbo
标签: opengl textures fragment-shader fbo