我可以将纹理渲染到帧缓冲区,然后使用glCopyTexImage2D
复制它吗?更明显的方法是使用:
glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0, renderedTexture, 0);
然而,这在我的情况下不起作用,因为我想读取和写入相同的纹理,当我使用glFramebufferTexture
时,我不能使用与着色器输入相同的纹理同时。我不久前在Stackoverflow上问过这个问题:Writing and reading from the same texture for an iterative DE solver on OpenGL
glCopyTexImage2D
消除了这个问题:我首先渲染然后复制渲染到纹理的任何内容。我尝试了它,当我渲染到屏幕时它工作。但是,如果我想做一些屏幕外渲染并复制它呢?当我尝试使用FrameBuffer时,我不确定glCopyTexImage2D
是否有效。我试过了,它似乎没有起作用。可以这样做吗?在我的案例中是否有不同的方法来执行此操作?如果有可能,有人可以向我展示如何使用帧缓冲区和glCopyTexImage2D
的快速片段吗?
这个问题不是重复的。我不是试图同时读取和写入相同的纹理。我需要首先写入纹理,然后连续读取它。我完全理解同时做这些事情的操作没有明确定义。在我的情况下,这是一个首先阅读,然后复制,然后再次使用它的问题。
编辑:
所以这就是我试图使用glCopyTexImage2D以及它如何使用帧缓冲区失败:
// Set "renderTargetTexture" as our colour attachement #0
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTargetTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderTargetTexture);
// Give an empty image to OpenGL ( the last "0" means "empty" )
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, windowWidth, windowHeight, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
// Poor filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
do{
// Set to render to our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glViewport(0,0,windowWidth,windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right
// attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
//
// Processing
//
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(advectShaderID);
// Bind velocity texture to advection uniform in the shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, velocityTexture);
glUniform1i(advectionTextureID, 0);
// Draw the triangles
glDrawArrays(GL_TRIANGLES, 0, 6);
//Copy from framebuffer
glBindTexture(GL_TEXTURE_2D, velocityTexture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, windowWidth, windowHeight, 0);
//
// Render to screen
//
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Render on the whole framebuffer, complete from the lower left corner to the upper right
glViewport(0,0,windowWidth,windowHeight);
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our shader
glUseProgram(quad_programID);
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, velocityTexture);
// Set our "renderedTexture" sampler to user Texture Unit 0
glUniform1i(texID, 0);
// Draw the triangles
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
}
这给了我一个黑屏。但是,当我用glBindTexture(GL_TEXTURE_2D, velocityTexture);
替换glBindTexture(GL_TEXTURE_2D, renderTargetTexture);
时,即当我渲染帧缓冲区的输出时,它给出了我期望的结果。此外,如果我摆脱glCopyTexImage2D
并只渲染velocityTexture
,它会产生原始纹理。但是,只要我使用glCopyTexImage2D
,它就会将黑屏复制到velocityTexture中。