glTexImage2D 和加载图像的问题

发布于 2024-11-18 19:53:49 字数 3382 浏览 1 评论 0原文

我正在使用 Corona(加载图像)/C++/Glut。

我在加载纹理时遇到奇怪的问题。我知道问题出在 glTexImage2D 上,但很奇怪,当图像为 850 x 249850 x 251850 x 252 时一切正常。但是当它是 850 x 250 时,我在编译后收到错误:

在 0x024bfee9 处出现未处理的异常 [...].exe:0xC0000005:访问 违规读取位置 0x0265c001。

我正在使用:

glTexImage2D(GL_TEXTURE_2D, 0, TextureFormat, TextureImage->getWidth(), TextureImage->getHeight(), 0, TextureFormat, GL_UNSIGNED_BYTE, TextureImage->getPixels());

发生了什么事?

编辑

getpixels

edit2 - 代码

加载纹理代码:

GLuint LoadTexture(char *_filename)
{
    cout<<"loading image start"<<endl;
    GLuint ttemp=0;
    // Load texture

    cout<<"1"<<endl;
    corona::Image *TextureImage = corona::OpenImage(_filename);

    cout<<"2"<<endl;
    if (TextureImage)
    {
        glGenTextures(1, &ttemp);
        glBindTexture(GL_TEXTURE_2D, ttemp);
        
        // get the format of image
        GLuint TextureFormat;
        
        switch (TextureImage->getFormat())
        {
            case corona::PF_B8G8R8A8:
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8A8);
                TextureFormat = GL_RGBA; break;
                
            case corona::PF_R8G8B8A8:
                TextureFormat = GL_RGBA; break;

            case corona::PF_B8G8R8:
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8);
                TextureFormat = GL_RGB; break;
                
            case corona::PF_R8G8B8:
                TextureFormat = GL_RGB; break;
                
            case corona::PF_I8: // for example gif - convert to RGB
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8);
                TextureFormat = GL_RGB; break;
        }
        
        // Flip image
        FlipImage(TextureImage, corona::CA_X);
        
        // Generate The Texture
        cout<<"3"<<endl;
        cout<<"-"<<endl;
        
        cout<<"TextureFormat "              <<TextureFormat<<endl;
        cout<<"TextureImage->getWidth() "   <<TextureImage->getWidth()<<endl;
        cout<<"TextureImage->getHeight() "  <<TextureImage->getHeight()<<endl;
        cout<<"TextureImage->getPixels() "  <<TextureImage->getPixels()<<endl;
        

        cout<<"-"<<endl;

        glTexImage2D(GL_TEXTURE_2D, 0, TextureFormat, TextureImage->getWidth(), TextureImage->getHeight(), 0, TextureFormat, GL_UNSIGNED_BYTE, TextureImage->getPixels());
        
        cout<<"4"<<endl;
        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtering
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  // Linear Filtering
        cout<<"5"<<endl;
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        cout<<"6"<<endl;
        delete TextureImage;
        cout<<"7"<<endl;
    }
    cout<<"8"<<endl;
    return ttemp;
}

我这样使用它:

GLuint texttest;
texttest= LoadTexture("test.png");

I'm using Corona (to load images) / C++ / Glut.

I have strange problem with loading texture. I know that problem is with glTexImage2D, but it's strange , everything is OK when image is 850 x 249 or 850 x 251 or 850 x 252. But when it's 850 x 250 I get an error after compiling :

Unhandled exception at 0x024bfee9 in
[...].exe: 0xC0000005: Access
violation reading location 0x0265c001.

I'm using :

glTexImage2D(GL_TEXTURE_2D, 0, TextureFormat, TextureImage->getWidth(), TextureImage->getHeight(), 0, TextureFormat, GL_UNSIGNED_BYTE, TextureImage->getPixels());

What's going on ?

edit

getpixels

edit2 - code

Loading texture code :

GLuint LoadTexture(char *_filename)
{
    cout<<"loading image start"<<endl;
    GLuint ttemp=0;
    // Load texture

    cout<<"1"<<endl;
    corona::Image *TextureImage = corona::OpenImage(_filename);

    cout<<"2"<<endl;
    if (TextureImage)
    {
        glGenTextures(1, &ttemp);
        glBindTexture(GL_TEXTURE_2D, ttemp);
        
        // get the format of image
        GLuint TextureFormat;
        
        switch (TextureImage->getFormat())
        {
            case corona::PF_B8G8R8A8:
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8A8);
                TextureFormat = GL_RGBA; break;
                
            case corona::PF_R8G8B8A8:
                TextureFormat = GL_RGBA; break;

            case corona::PF_B8G8R8:
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8);
                TextureFormat = GL_RGB; break;
                
            case corona::PF_R8G8B8:
                TextureFormat = GL_RGB; break;
                
            case corona::PF_I8: // for example gif - convert to RGB
                TextureImage = ConvertImage(TextureImage, corona::PF_R8G8B8);
                TextureFormat = GL_RGB; break;
        }
        
        // Flip image
        FlipImage(TextureImage, corona::CA_X);
        
        // Generate The Texture
        cout<<"3"<<endl;
        cout<<"-"<<endl;
        
        cout<<"TextureFormat "              <<TextureFormat<<endl;
        cout<<"TextureImage->getWidth() "   <<TextureImage->getWidth()<<endl;
        cout<<"TextureImage->getHeight() "  <<TextureImage->getHeight()<<endl;
        cout<<"TextureImage->getPixels() "  <<TextureImage->getPixels()<<endl;
        

        cout<<"-"<<endl;

        glTexImage2D(GL_TEXTURE_2D, 0, TextureFormat, TextureImage->getWidth(), TextureImage->getHeight(), 0, TextureFormat, GL_UNSIGNED_BYTE, TextureImage->getPixels());
        
        cout<<"4"<<endl;
        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtering
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  // Linear Filtering
        cout<<"5"<<endl;
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        cout<<"6"<<endl;
        delete TextureImage;
        cout<<"7"<<endl;
    }
    cout<<"8"<<endl;
    return ttemp;
}

I use it this way :

GLuint texttest;
texttest= LoadTexture("test.png");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文