我正在CodeBlocks上运行Glut项目,我有一个类“imageloader”,我正在使用纹理与位图图像的球体。它工作的很好,当我指定像这样的图像的位置loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp"));
我创建了一个名为“图像”的文件夹并将图像复制到该文件夹中。 Here's how it looks when you run itC++ OpenGL纹理,找不到图像
你要知道,我也有相同的地方内的同一图像的.exe可执行文件(即BIN \调试\ 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"));
}
当运行在IDE的程序,特别是与调试器连接,当前工作目录可能是由IDE来改变。请参阅IDE的文档以了解如何更改工作目录。如果使用Visual Studio,则可以在调试器设置中的项目属性中完成。它是$(ProjectDir)默认情况下(与.vcxproj文件的文件夹)。 – Drop