我创建了一个“乒乓”着色器类,它在两个纹理之间渲染着色器。由于某种原因,该程序有一个我无法找到的大量内存泄漏。我不在显示功能中使用单个新语句。代码如下所示:
显示功能:
void display()
{
r->Render();
glutSwapBuffers();
}
渲染:
void Effect2::Render()
{
shader.Start(time);
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
}
glEnd();
shader.End();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
shader.BindCurrentTexture();
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-1.0f, 1.0f);
}
glEnd();
glDisable(GL_TEXTURE_2D);
Renderer::Render();
}
shader start,end,bindtexture:
void PingPongShader::Start(float time)
{
if(!began)
{
//buffer
glGenFramebuffers(1, &frameBuffer);
GLenum drawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, drawBuffers);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[current], 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[abs(current-1)]);
glUseProgram(shader);
if(paramCallback != NULL)
paramCallback(shader);
//internal shader vars
glUniform2f(screenSizeLoc, (float)width, (float)height);
if(drawingRadius > 0)
{
glUniform1f(dr_radius, drawingRadius);
glUniform4f(dr_color, drawingColor[0], drawingColor[1], drawingColor[2], drawingColor[3]);
glUniform2f(dr_pos, drawingPos[0], drawingPos[1]);
}
else
{
glUniform1f(dr_radius, -1.0);
}
glUniform1f(timeLoc, time);
began = true;
}
else
{
printf("Could not call Start(). Did you forget to call End() ?");
}
}
void PingPongShader::End()
{
if(began)
{
glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_TEXTURE_2D);
current = abs(current-1);
began = false;
}
else
{
printf("Could not call End(). Did you forget to call Start() ?");
}
}
void PingPongShader::BindCurrentTexture()
{
glBindTexture(GL_TEXTURE_2D, textures[skip ? abs(current-1) : current]);
}
除了内存泄漏,输出正是我所期望的。
有什么想法吗?
答案 0 :(得分:4)
您在每个glGenFramebuffers
中调用Start()
,从而在每个帧中分配新的缓冲区对象。
旁注:1 - current
比abs(current - 1)
更有效(我会说更惯用),在0和1之间交替。