C++ OpenGL纹理,找不到图像

r1zhe5dt  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(213)

我在CodeBlocks上运行一个Glut项目,我有一个类“imageloader”,我用它来用位图图像纹理化一个球体。当我指定图像的位置时,它工作得很好,就像这样loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp"));我创建了一个名为“images”的文件夹,并将图像复制到该文件夹中。Here's how it looks when you run it
请注意,我也有相同的图像在相同的地方作为.exe可执行文件是(即bin\Debug\earth.bmp)
但是我失败了,当我这样做loadTexture(loadBMP("earth.bmp"));它找不到图像。
我不能使用上面的方法,长绝对路径,原因每次项目去不同的计算机,路径必须改变每一次,在运行项目之前,否则它会给你一个错误。所以我不能提交我的项目这样。
下面是我的main.cpp中的一段代码(如果需要更多代码,请告诉我):

//Makes the image into a texture, and returns the id of the texture
GLuint loadTexture(Image* image) {
    GLuint textureId;
    glGenTextures(1, &textureId); //Make room for our texture
    glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
    //Map the image to the texture
    glTexImage2D(GL_TEXTURE_2D,                //Always GL_TEXTURE_2D
                 0,                            //0 for now
                 GL_RGB,                       //Format OpenGL uses for image
                 image->width, image->height,  //Width and height
                 0,                            //The border of the image
                 GL_RGB, //GL_RGB, because pixels are stored in RGB format
                 GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
                                   //as unsigned numbers
                 image->pixels);               //The actual pixel data
    return textureId; //Returns the id of the texture
} 

GLuint _textureId2;

void initRendering() {
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    quad = gluNewQuadric();

    _textureId2 = loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\TestClasses\\images\\earth.bmp"));
}
g52tjvyc

g52tjvyc1#

正如在注解中提到的,IDE的工作目录可能与二进制文件(和图像文件)的位置不同。根据我的经验,它是“项目”文件的位置。
在Code::Blocks论坛上有一篇文章提到了如何更改它:
项目-〉属性-〉生成目标-〉[目标名称] -〉执行工作目录
如果您不想更改设置,可以给予项目文件的相对路径:

loadTexture(loadBMP("images/earth.bmp"));

我个人会把工作目录放在一边,使用上面的例子,然后当你捆绑软件发布时,二进制文件可以放在安装目录的根目录下,代码仍然可以使用那个相对路径访问镜像。
例如:

/install_dir
/install_dir/program.exe
/install_dir/images/earth.bmp
ego6inou

ego6inou2#

如果你在windows上,代码块查看的路径是mingw bin路径,而不是你项目的本地bin,也就是说。
要添加图像,您需要将其添加到C:\Program Files (x86)\CodeBlocks\MinGW\bin,而不是yourProject\bin\Debug\

相关问题