1

我有一个 Texture 类,在某些时候需要清除其像素数据并从文件中加载新的纹理像素数据。它加载并显示它的第一个纹理就好了。但是在更改其显示的纹理时,它只是以新的图像尺寸显示其旧像素数据。

我正在尝试使用对glBindBuffers. 我首先在这个函数中删除了旧的纹理名称reloadTexture()

void Texture::reloadTexture(string filename)
{
    //first and foremost clear the image and buffer vectors back down to nothing so we can start afresh 
    buffer.clear();
    image.clear();
    w = 0;
    h = 0;
    //also delete the texture name we were using before
    glDeleteTextures(1, &textureID[0]);

    const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
    //printf(fnPtr);

    lodepng::load_file(buffer, fnPtr);//load the file into a buffer

    unsigned error = lodepng::decode(image,w,h,buffer);//lodepng's decode function will load the pixel data into image vector from the buffer
    //display any errors with the texture
    if(error)
    {
        cout << "\ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
    }
    //execute the code that'll throw exceptions to do with the images size
    checkPOT(w);
    checkPOT(h);



    //loop through and //printf our pixel data
    /*for(GLuint i = 0; i<image.size(); i+=4)
    {
    //printf("\n%i,%i,%i,%i,", image.at(i),image.at(i+1),image.at(i+2),image.at(i+3));

    }*/

    ////printf("\nImage size is %i", image.size());

    //image now contains our pixeldata. All ready for  to do its thing

    //let's get this texture up in the video memoryOpenGL
    texGLSecondaryInit();

    Draw_From_Corner = CENTER;
}

然后在函数texGLSecondaryInit()中,我尝试清除任何残留的像素数据并从另一个文件加载新数据。

void Texture::texGLSecondaryInit()
{

    glGenTextures(1, &textureID[0]);
    ////printf("\ntextureID = %u", textureID[0]);

    glBindTexture(GL_TEXTURE_2D, textureID[0]);//evrything we're about to do is about this texture
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    //glDisable(GL_COLOR_MATERIAL);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER,0);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,w,h,0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);

    //we COULD free the image vectors memory right about now.
    image.clear();
}

现在为了尝试清除缓冲区以确保它是纹理从中提取的新像素数据,建议我glBindBuffer(GL_PIXEL_UNPACK_BUFFER,0);这个问题中使用。但是,为此,我会收到未处理的访问冲突异常错误。

An unhandled exception of type 'System.AccessViolationException' occurred in Spritey.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

我还注意到这是一个系统错误。这意味着这可能与Texture该类不是托管代码这一事实有关。但是,我让编译器意识到这一点

#pragma managed(off,push)

有人知道为什么我不能安全地打电话给glBindBuffer这里吗?或者让我清除openGL拥有的任何纹理数据的替代方案?

4

0 回答 0