我試圖通過將紋理的內容複製到附加到FBO的輸出紋理來編寫GPGPU程序。但是,我發現輸入的紋理數據未正確加載/未正確綁定。我不明白什麼是錯的。從紋理渲染到離屏紋理(FBO)
這裏是我的代碼:
void render()
{
int i;
float * result, *in;
//allocating and filling data in the input texture
result = (float *) malloc(4*32*32*sizeof(float));
in = (float *) malloc(4*32*32*sizeof(float));
for(i=0;i<32*32*4; i++)
{
in[i]=1.0f;
}
makeBuffers(); //makes the vertex and element buffers
makeTexture(in); //makes the input textures
makeShaders(); //makes the vertex n pixel shaders
makeProgram(); //makes the program
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, res.vertexBuffer);
glVertexAttribPointer(res.position, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, (void*)0);
glEnableVertexAttribArray(res.position);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, res.elementBuffer);
res.texture= glGetUniformLocation(res.shaderProgram, "intexture");
/*綁定質地*/ makefbo(); //使離屏幀緩衝器對象
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, res.texture);
glUniform1i(res.texture, 0);
glDrawElements(GL_TRIANGLE_STRIP,4, GL_UNSIGNED_SHORT, (void*)0);
// glFlush(); // i think drawing would be enough now.
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); //reading back data from the framebuffer
glReadPixels(0, 0, 32, 32,GL_RGBA,GL_FLOAT,result);
printf(" float values \n");
for(i=0; i< 32*4 ; i++)
printf(" %f ", result[i]);
printf(" \n");
}
這裏是我maketexture()函數
glGenTextures (1, &res.texture);
glBindTexture(GL_TEXTURE_2D,res.texture);
// set texture parameters
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP);
// define texture with floating point format
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32F,
texSize,texSize,0,GL_RGBA,GL_FLOAT,0);
// transfer data to texture
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,texSize,texSize,
GL_RGBA,GL_FLOAT,data);
這裏是我framebufferoject準備和輸出紋理招標:
glGenFramebuffers(1, &res.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, res.fbo);
glViewport(0, 0, 32, 32);
glGenTextures(1, &res.tex);
glBindTexture(GL_TEXTURE_2D, res.fbo);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
32, 32, 0, GL_RGBA, GL_FLOAT, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, res.tex, 0);
數據似乎沒有正確讀取。我嘗試將輸入紋理附加到幀緩衝區對象,並嘗試立即讀回紋理數據,然後數據顯示正常。但是當我嘗試渲染到屏幕外紋理時,它不會給出正確的輸出。它在像素着色器執行結束時只給出一堆零。
像素着色器獲取輸入紋理並將數據複製到輸出紋理中:outColor = texture2D(intexture,texcoord);
有人可以看到這個問題嗎?
請不要說你每次渲染幀時都會創建紋理,fbo,vao等。 – Vallentin
我不是每次都渲染,這是一次性操作。我只需要將數據處理一次。 –