[英]C++ OpenGL Texture, can't find image
我在 CodeBlocks 上運行一個 Glut 項目,我有一個類“imageloader”,我用它來用位圖圖像對球體進行紋理處理。 當我像這樣指定圖像的位置時,它工作得很好loadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp"));
我創建了一個名為“images”的文件夾並將圖像復制到該文件夾中。 這是你運行它時的樣子
請注意,我在 THE SAME PLACE 內也有與 .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"));
}
如評論中所述,您的 IDE 的工作目錄可能與二進制文件(和您的圖像文件)的位置不同。 以我的經驗,它是您的“項目”文件的位置。
Code::Blocks 論壇上有一篇文章提到了如何更改它:
項目 -> 屬性 -> 構建目標 -> [目標名稱] -> 執行工作目錄
如果您不想更改設置,您可以從項目文件中提供相對路徑:
loadTexture(loadBMP("images/earth.bmp"));
我個人會單獨留下工作目錄並使用上面的示例。 然后,當您捆綁軟件進行發布時,二進制文件可以位於安裝目錄的根目錄,並且代碼仍然可以使用該相對路徑訪問映像。
例如:
/install_dir
/install_dir/program.exe
/install_dir/images/earth.bmp
如果您在 Windows 上,則代碼塊正在查看的路徑是 mingw bin 路徑,而不是項目的本地 bin 路徑,這意味着這一點。
要添加圖像,您需要將其添加到C:\Program Files (x86)\CodeBlocks\MinGW\bin
而不是 yourProject yourProject\bin\Debug\
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.