2013-04-03 68 views
0

我遇到了一些問題,試圖用OpenGL和Cg做一個簡單的後期處理;看起來,問題在於將OpenGL Framebuffer對象連接到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); 

而我看到的是一個黑色的矩形,一個綠色的邊框。幫助,任何人?

回答

1

你有沒有想過這個答案?我有完全相同的問題,找不到任何樣品。您可能會丟失的一件事是:

GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; 
glDrawBuffers(1, DrawBuffers);