我试图用OpenGL和Cg进行简单的后期处理时遇到了问题;看来,问题在于将OpenGL Framebuffer对象连接到Cg纹理参数。
我正在创建一个像这样的FBO:
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
///snip - add depth buffer that I don't use
if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
exit(EXIT_FAILURE);
}
...按如下方式设置Cg:
if(!(cgContext = cgCreateContext())) exit(EXIT_FAILURE);
cgGLRegisterStates(cgContext);
if(!(cgEffect = cgCreateEffectFromFile(cgContext, fx_file_path, NULL))) {
fprintf(stderr, "Failed to load effect file: %s\n\n%s\n", fx_file_path, cgGetLastListing(cgContext));
exit(EXIT_FAILURE);
}
for(cgTechnique = cgGetFirstTechnique(cgEffect);
cgTechnique && cgValidateTechnique(cgTechnique) == CG_FALSE;
cgTechnique = cgGetNextTechnique(cgTechnique));
if (!cgTechnique) exit(EXIT_FAILURE);
if( !(shaderTexture = cgGetNamedEffectParameter(cgEffect, "gSampler"))) {
exit(EXIT_FAILURE);
}
到目前为止,所有这一切似乎都有效。然后我连接(或至少我认为我)FBO中的纹理与Cg着色器中的纹理:
cgGLSetupSampler( shaderTexture, tex);
然后呈现给FBO:
cgSetPassState(cgRenderPass);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glClearColor(0.0, 0.0, 0.5, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0);
现在,如果我渲染到屏幕而不是FBO,这可以正常工作。在那之后,我尝试使用这个FBO作为四边形的纹理(比屏幕略小,所以我可以看到它周围的边框):
glBindFramebuffer(GL_FRAMEBUFFER, 0);
cgSetPassState(cgPostProcessPass);
glClearColor(0.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, rectbuffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,(void*)0);
cgGLEnableTextureParameter( shaderTexture );
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
cgGLDisableTextureParameter( shaderTexture );
glDisableVertexAttribArray(0);
我所看到的只是一个黑色矩形和一个绿色边框。帮忙,有人吗?
答案 0 :(得分:1)
你有没有想出答案?我有完全相同的问题,找不到任何样品。你可能缺少的一件事是:
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);