【发布时间】: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