I am working on some OpenGL stuff and when loading textures I am using an absolute path which is bad for obvious reasons. My problem is that I cannot find recourses on how to convert paths like these into relative paths. This is the code where the path is taken.
Language: C++
Operating System: Windows 10 (though I would rather have a solution to this that is cross-platform)
Libraries and Header Files used here: OpenGL 4.6 (loaded via GLAD), stb_image.h
IDE: Visual Studio 2019
// Where the path is used
unsigned int texture = loadTexture("C:\\Users\\Name\\OneDrive\\Desktop\\C++ Projects\\Working Game Engine\\texture.png");
// This is the function where that path is inputted
unsigned int loadTexture(char const * path) {
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data) {
GLenum format;
if (nrComponents == 1) {
format = GL_RED;
}
else if (nrComponents == 3) {
format = GL_RGB;
}
else if (nrComponents == 4) {
format = GL_RGBA;
}
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else {
std::cout << "Texture failed to load at path: " << path << "\n";
stbi_image_free(data);
}
}
So far I have not been able to replace the path with a working, less specific and verbose path.