内存资源不停地增加

Memory resource increase non stop

本文关键字:增加 资源 内存      更新时间:2023-10-16

我是GLFW的新手,做了一个简单的纹理映射程序。问题是在运行程序时内存资源不停地增加,我可以在任务管理器中清楚地看到。

运行程序几分钟后,计算机的风扇加速,出现发热问题。如何解决此问题?

这是纹理加载功能的代码

GLuint LoadTexture(const char* TextureName)
{
    GLuint Texture;  //variable for texture
    glGenTextures(1,&Texture); //allocate the memory for texture
    glBindTexture(GL_TEXTURE_2D,Texture); //Binding the texture
    if(glfwLoadTexture2D(TextureName, GLFW_BUILD_MIPMAPS_BIT)){
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        return Texture;
    }else return -1;
}

这是绘制函数的代码

void display()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //clear background screen to black
    //Clear information from last draw
    glClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective
    glTranslatef(0.0f,0.0f,-35.0f); //Translate whole scene to -ve z-axis by -35 unit

    GLuint text2D;
    text2D = LoadTexture("cicb.tga"); //loading image for texture
    glEnable(GL_TEXTURE_2D); //Enable texture
    glBindTexture(GL_TEXTURE_2D,text2D);//Binding texture
    glPushMatrix();
    glBegin(GL_POLYGON); //Begin quadrilateral coordinates
    glNormal3f(0.0f, 0.0f, 1.0f);//normal vector
    glTexCoord2f(0.0f, 0.0f); //Texture co-ordinate origin or  lower left corner
    glVertex3f(-10.0f,-11.0f,5.0f);
    glTexCoord2f(1.0f, 0.0f); //Texture co-ordinate lower right corner
    glVertex3f(10.0f,-11.0f,5.0f);
    glTexCoord2f(1.0f, 1.0f);//Texture co-ordinate top right corner
    glVertex3f(10.0f,-1.0f,-15.0f);
    glTexCoord2f(0.0f, 1.0f);//Texture co-ordinate top left corner
    glVertex3f(-10.0f,-1.0f,-15.0f);
    glEnd(); //End quadrilateral coordinates
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,text2D);
    glPushMatrix();
    glBegin(GL_POLYGON);
    glNormal3f(0.0f, 0.0f, 1.0f);
    glTexCoord2f(0.0f, 0.0f);//Texture co-ordinate origin or lower left corner
    glVertex3f(-10.0f,-1.0f,-15.0f);
    glTexCoord2f(10.0f, 0.0f); //Texture co-ordinate for repeating image ten times form
    //origin to lower right corner
    glVertex3f(10.0f,-1.0f,-15.0f);
    glTexCoord2f(10.0f, 10.0f);//repeat texture ten times form lower to top right corner.
    glVertex3f(10.0f,15.0f,-15.0f);
    glTexCoord2f(0.0f, 10.0f);//repeat texture ten time form top right to top left corner.
    glVertex3f(-10.0f,15.0f,-15.0f);
    glEnd();
    glPopMatrix();
    glDisable(GL_TEXTURE_2D); //Disable the texture
    glfwSwapBuffers();
}

如果有人想通过运行exe来查看问题,那么我可以提供下载链接。

每次调用display()时,您似乎都在加载纹理。(基本上每帧一次)我想这就是在某个时候占用你所有记忆的原因。您只需要在显示功能之外执行此操作一次。