Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Cg在OpenGL中实现多个渲染目标_Opengl_Framebuffer_Shader_Cg_Render To Texture - Fatal编程技术网

使用Cg在OpenGL中实现多个渲染目标

使用Cg在OpenGL中实现多个渲染目标,opengl,framebuffer,shader,cg,render-to-texture,Opengl,Framebuffer,Shader,Cg,Render To Texture,我试图(徒劳地)使用OpenGL和NVIDIA的Cg着色器系统设置MRT,最终目标是延迟渲染。我已经成功地使单个目标的着色器和“渲染到纹理”(Render To Texture)都可以工作,但只要我尝试将一个对象渲染到多个渲染目标,它就无法在其中任何一个目标中显示 我将按以下方式设置帧缓冲区对象: // Initialize textures // (I'll spare you the code since I've done that without issues) // Initial

我试图(徒劳地)使用OpenGL和NVIDIA的Cg着色器系统设置MRT,最终目标是延迟渲染。我已经成功地使单个目标的着色器和“渲染到纹理”(Render To Texture)都可以工作,但只要我尝试将一个对象渲染到多个渲染目标,它就无法在其中任何一个目标中显示

我将按以下方式设置帧缓冲区对象:

// Initialize textures
// (I'll spare you the code since I've  done that without issues)

// Initialize the FBO
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenRenderbuffers(1, &depthBuff);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER, depthBuff);

// -snip-

// Bind each texture we want to use to the FBO at rtNum
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + rtNum,
                       GL_TEXTURE_2D, tex->getID(), 0);
// Check to make sure we're good to go
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    throw Exceptions::GLException("Frame buffer object is not complete.",
                                __FUNCTION__);
}

// -snip-

// Call glDrawBuffers with the RTs we want to render to
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
unique_ptr<GLenum[]> arr(new GLenum[num]);
for (size_t c = 0; c < num; ++c)
    arr[c] = GL_COLOR_ATTACHMENT0 + c;
glDrawBuffers(num, arr.get());
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    throw Exceptions::GLException("Frame buffer object is not complete.",
                                __FUNCTION__);

// -snip-

// Set up the FBO for rendering
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Draw calls here

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPopAttrib();

有人知道这可能失败的原因吗?

上述方法是正确的。问题似乎是硬件和Cg的结合。在我的集成芯片组笔记本电脑上,尽管Cg没有返回任何错误,但MRT失败,并出现了一系列与着色器相关的问题。在我的带有GeForce GTX260的台式机上,一切都运行得很好。

您在注释中添加了
//Call glDrawBuffers
,但实际上我在任何地方都没有看到此调用。那只是复制错误吗?@Tim:是的,只是复制错误。我现在已经修复了。顺便说一句,你应该在戳完帧缓冲区后才检查它的完整性。进行检查的全部意义在于,您可以在从一个完整FBO状态到另一个完整FBO状态的过程中通过不完整的帧缓冲区状态。Cg的问题似乎更深层次。当我有了更多的了解后,我会发布更多。我发布的问题似乎是造成问题的原因。
struct VertexShaderOutput {
    float4 position : POSITION;
    float3 normal : TEXCOORD0;
    float4 color : COLOR;
};

VertexShaderOutput VS_Main(float4 local : POSITION,
                        float4 normal : NORMAL,
                        float4 color : COLOR,
                        uniform float4x4 modelViewProj,
                        uniform float4x4 modelViewIT)
{
    VertexShaderOutput OUT;
    OUT.position = mul(modelViewProj, local);
    OUT.normal= mul(modelViewIT, normal).xyz;
    OUT.color = color;
    return OUT;
}

struct FragmentShaderOutput {
    float4 unlit : COLOR0;
    float4 normAndDepth : COLOR1;
};

FragmentShaderOutput FS_Main(VertexShaderOutput vsOut)
{
    FragmentShaderOutput OUT;
    OUT.unlit = float4(1.0f, 1.0f, 0.0f, 1.0f);//vsOut.color;
    OUT.normAndDepth.rgb = vsOut.normal;
    // Calculate depth later
    OUT.normAndDepth.a = 1.0f;
    return OUT;
}