'C++ OpenGL Texture, can't find image
I'm running a Glut project on CodeBlocks, I have a class "imageloader" that i'm using to texture a sphere with a bitmap image. It works just Fine when i specify the location of the image like thisloadTexture(loadBMP("C:\\Users\\Ndumiso\\Desktop\\Project1\\images\\earth.bmp")); i created a folder called "images" and copied the image into that folder.
Here's how it looks when you run it
mind you, I ALSO have the same image inside THE SAME PLACE as The .exe executable file is (i.e bin\Debug\earth.bmp)
But i fails when i do it like this loadTexture(loadBMP("earth.bmp")); It cannot find the image.
I cannot use the above method, the long absolute path, cause every time the project goes to a different computer, The path has to be changed every time, before running the project, otherwise it gives you an error. So I cannot submit my project like this.
here's just a snippet of the code in my main.cpp (let me know if you need more code) :
//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"));
}
Solution 1:[1]
As mentioned in the comments, its likely your IDE has a different working directory than the location of the binary (and your image file). In my experience it is the location of your "project" files.
There is a post on the Code::Blocks forums that mentions how to change it:
Project -> Properties -> Build targets -> [name of target] -> Execution working dir
If you don't want to change the settings you could give a relative path from your project files:
loadTexture(loadBMP("images/earth.bmp"));
I personally would leave the working directory alone and use the example above. Then when you bundle the software for release, the binary can sit at the root of the install directory and the code can still access the image using that relative path.
For example:
/install_dir
/install_dir/program.exe
/install_dir/images/earth.bmp
Solution 2:[2]
If you're on windows the path that codeblocks is looking at is it's mingw bin path, not the local bin of your project which means this.
To add an image you will need to add it to C:\Program Files (x86)\CodeBlocks\MinGW\bin instead of yourProject\bin\Debug\
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | omkobass |
