【问题标题】:C++ OpenGL Texture, can't find imageC ++ OpenGL纹理,找不到图像
【发布时间】:2016-09-02 20:24:58
【问题描述】:

我在 CodeBlocks 上运行一个 Glut 项目,我有一个类“imageloader”,我用它来用位图图像对球体进行纹理处理。当我像这样loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp")); 指定图像的位置时,它工作得很好,我创建了一个名为“images”的文件夹并将图像复制到该文件夹​​中。 Here's how it looks when you run it

请注意,我在 THE SAME PLACE 中也有与 .exe 可执行文件相同的图像(即 bin\Debug\earth.bmp)

但是当我这样做时我失败了loadTexture(loadBMP("earth.bmp"));它找不到图像。

我不能用上面的方法,绝对路径太长了,因为每次工程到不同的电脑,每次运行工程前都要改路径,否则报错。所以我不能像这样提交我的项目。

这只是我 main.cpp 中代码的一个 sn-p(如果您需要更多代码,请告诉我):

//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"));
}

【问题讨论】:

  • 当您在 IDE 中运行程序时,尤其是在附加了调试器的情况下,IDE 可能会更改当前工作目录。请查阅 IDE 的文档以了解如何更改工作目录。如果您使用 Visual Studio,则可以在调试器设置中的项目属性中完成。默认为 $(ProjectDir)(带有 .vcxproj 文件的文件夹)。

标签: c++ opengl texture-mapping


【解决方案1】:

如 cmets 中所述,您的 IDE 的工作目录可能与二进制文件(和您的图像文件)的位置不同。根据我的经验,它是您的“项目”文件的位置。

有一个post on the Code::Blocks forums 提到了如何更改它:

项目 -> 属性 -> 构建目标 -> [目标名称] -> 执行工作目录

如果您不想更改设置,您可以提供项目文件的相对路径:

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

我个人会单独保留工作目录并使用上面的示例。然后,当您捆绑软件进行发布时,二进制文件可以位于安装目录的根目录,并且代码仍然可以使用该相对路径访问映像。

例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2015-07-15
    • 2015-05-28
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多