我正在研究一些 OpenGL 的东西,并且在加载纹理时,我使用的是绝对路径,这显然是不好的。我的问题是我找不到如何将这些路径转换为相对路径的资源。这是采用路径的代码。
语言:C++
操作系统:Windows 10(虽然我更希望有一个跨平台的解决方案)
此处使用的库和头文件:OpenGL 4.6(通过 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);
}
}
到目前为止,我还无法用一个有效的、不太具体和冗长的路径替换该路径。