复制 fbo 图像时内存泄漏
Memory leak while copy fbo image
正如您在图像中看到的那样,我在将 fbo 复制到 QImage
时发生了内存泄漏。有人知道可能的解决方案吗?
QOpenGLFramebufferObject *fbo;
void GLANN::render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Set random seed
//program.setUniformValue("seedX", ((float)qrand()/RAND_MAX));
//program.setUniformValue("seedY", ((float)qrand()/RAND_MAX));
//Set number of alredy rendered passes
//program.setUniformValue("numRenderPass",mRenderPasses);
//Set program to fbo render mode
program.setUniformValue("fbo",true);
//Bind last rendered Image
//pixelsRenderedImage = bindTexture(*renderedImage);
//Load Identity
//glLoadIdentity();
//Move to rendering point
//glTranslatef( -1.0, -1.0, 0.0f );
// Draw geometry
// Tell OpenGL which VBOs to use
// Render to our framebuffer
fbo->bind();
glViewport(0,0,TexWidth,TexHeight);
// Tell OpenGL which VBOs to use
glBindBuffer(GL_ARRAY_BUFFER, vboId0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId1);
// Offset for position
int offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program.attributeLocation("a_position");
program.enableAttributeArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void *)offset);
// Offset for texture coordinate
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program.attributeLocation("a_texcoord");
program.enableAttributeArray(texcoordLocation);
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void *)offset);
//glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pixelsRenderedImage);
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
// qDebug() << glGetError() << "Line 183";
fbo->release();
renderedImage.~QImage();
renderedImage = QImage();
renderedImage = fbo->toImage().copy();
pixelsRenderedImage = QGLWidget::bindTexture(renderedImage);
//Set Program to screen frendering
program.setUniformValue("fbo",false);
//Set Viewport back to default
glViewport(0,0,width,height);
//Render To Screen
//glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pixelsRenderedImage);
// Draw quad geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
}
发现了"bindTexture"部分的错误。
已添加:
glDeleteTextures(1,&pixelsRenderedImage);
解决了问题。
正如您在图像中看到的那样,我在将 fbo 复制到 QImage
时发生了内存泄漏。有人知道可能的解决方案吗?
QOpenGLFramebufferObject *fbo;
void GLANN::render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Set random seed
//program.setUniformValue("seedX", ((float)qrand()/RAND_MAX));
//program.setUniformValue("seedY", ((float)qrand()/RAND_MAX));
//Set number of alredy rendered passes
//program.setUniformValue("numRenderPass",mRenderPasses);
//Set program to fbo render mode
program.setUniformValue("fbo",true);
//Bind last rendered Image
//pixelsRenderedImage = bindTexture(*renderedImage);
//Load Identity
//glLoadIdentity();
//Move to rendering point
//glTranslatef( -1.0, -1.0, 0.0f );
// Draw geometry
// Tell OpenGL which VBOs to use
// Render to our framebuffer
fbo->bind();
glViewport(0,0,TexWidth,TexHeight);
// Tell OpenGL which VBOs to use
glBindBuffer(GL_ARRAY_BUFFER, vboId0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId1);
// Offset for position
int offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program.attributeLocation("a_position");
program.enableAttributeArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void *)offset);
// Offset for texture coordinate
offset += sizeof(QVector3D);
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
int texcoordLocation = program.attributeLocation("a_texcoord");
program.enableAttributeArray(texcoordLocation);
glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void *)offset);
//glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pixelsRenderedImage);
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
// qDebug() << glGetError() << "Line 183";
fbo->release();
renderedImage.~QImage();
renderedImage = QImage();
renderedImage = fbo->toImage().copy();
pixelsRenderedImage = QGLWidget::bindTexture(renderedImage);
//Set Program to screen frendering
program.setUniformValue("fbo",false);
//Set Viewport back to default
glViewport(0,0,width,height);
//Render To Screen
//glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pixelsRenderedImage);
// Draw quad geometry using indices from VBO 1
glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
}
发现了"bindTexture"部分的错误。
已添加:
glDeleteTextures(1,&pixelsRenderedImage);
解决了问题。