从资源中读取图像时,QImage为空

时间:2014-01-03 18:58:58

标签: c++ qt opengl qimage qtgui

我正在为一个大学项目开发​​一个简单的fps射击游戏。我正在使用C ++,Qt和OpenGL,我在纹理加载过程中遇到了问题。我将纹理放入资源文件中。加载后,存储纹理的QImage对象为null。 这是我的资源文件:

<RCC>
    <qresource prefix="/sounds">
        <file alias="rifle">rifle.wav</file>
    </qresource>
    <qresource prefix="/textures">
        <file alias="ground">ground00.tga</file>
        <file alias="sky_back">sky_back.tga</file>
        <file alias="sky_front">sky_front.tga</file>
        <file alias="sky_left">sky_left.tga</file>
        <file alias="sky_right">sky_right.tga</file>
        <file alias="sky_up">sky_up.tga</file>
        <file alias="wall05">wall05.tga</file>
        <file alias="wall06">wall06.tga</file>
        <file alias="wall07">wall07.tga</file>
        <file alias="wall08">wall08.tga</file>
        <file alias="wall09">wall09.tga</file>
    </qresource>
</RCC>

加载纹理的函数:

void GLWidget::load_texture(const QString  & file, GLuint texHandle)
{
   //load image
   QImage tex(file);
   if(tex.isNull())
   {
       std::cerr<<"File "<<file.toStdString()<<" not found"<<std::endl;
       return;
   }

   tex = QGLWidget::convertToGLFormat(tex);
   int w = tex.width();
   int h = tex.height();

   //generate the texture
   glGenTextures(1,&texture[texHandle]);
   glBindTexture(GL_TEXTURE_2D,texture[texHandle]);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
   glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,w,h,0,GL_RGBA, GL_UNSIGNED_BYTE,(GLvoid*)tex.bits());
}

不幸的是,当我尝试调用此函数时,它会打印“File ... not found”并返回。 我试着调用这个函数:

load_texture(":/textures/ground00.tga",0);//using filename
load_texture(":/textures/ground",0);//using alias
但是,这两个电话都失败了。 QImageReader支持tga文件,“tga”在

的输出中
qDebug() << QImageReader::supportedImageFormats ();

编辑: 我在load_texture函数

的顶部添加了以下行
if(QFile::exists(file))
{
    std::cout<<"File "<<file.toStdString()<<" exists"<<std::endl;
}

对于我使用资源别名load_texture(":/textures/ground",0);的情况,它返回true

0 个答案:

没有答案
相关问题