我在CodeBlocks上运行Glut项目,我有一个类"图像加载器"我用来用位图图像纹理球体。当我指定像loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp"));
这样的图像的位置时,它工作正常。我创建了一个名为"图像"的文件夹。并将图像复制到该文件夹中。
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"));
}
答案 0 :(得分:0)
正如评论中所提到的,您的IDE可能具有与二进制文件(和您的图像文件)不同的工作目录。根据我的经验,它是“项目”文件的位置。
有post on the Code::Blocks forums提及如何更改它:
项目 - >属性 - >构建目标 - > [目标名称] - >执行工作目录
如果您不想更改设置,可以从项目文件中提供相对路径:
loadTexture(loadBMP("images/earth.bmp"));
我个人会单独留下工作目录并使用上面的示例。然后,当您将软件捆绑发布时,二进制文件可以位于安装目录的根目录下,代码仍然可以使用该相对路径访问该图像。
例如:
/install_dir
/install_dir/program.exe
/install_dir/images/earth.bmp