1

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.

Casey
  • 10,297
  • 11
  • 59
  • 88
  • What is your working directory? This is either the directory where the exe is located (in case you double-click it to start) or a setting in VS. – BDL Jun 14 '21 at 13:22
  • 2
    You might also mean _relative_ to the executable's location. Is that what you mean? The first step in making a relative path is answering the question "Relative to _what_?" – Drew Dormann Jun 14 '21 at 13:30
  • 1
    The default working directory in Visual Studio when running an application from the IDE is the folder containing the project. It's defined in the Debugger settings as `$(ProjectDir) ` which is a Visual Studio variable that contains the path of the folder containing the project . This is not the location of the executable. With this said if you execute your program in the file explorer and not the IDE the working directory changes to the location of the executable. – drescherjm Jun 14 '21 at 14:21
  • Do you really want to convert an absolute path to a relative path, and if so relative to what path, the exe? Or is your question how to get a path to _texture.png_ if it resides in the exe directory? Your question is not clear. – acraig5075 Jun 14 '21 at 14:22

1 Answers1

0

Like the other user says, you should figure out where is VS saving/loading your executable. So maybe try this tutorial from Tutorials Point to find the present working directory and you can craft your relative path from there:

#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif

#include<iostream>
using namespace std;

std::string get_current_dir() {
   char buff[FILENAME_MAX]; //create string buffer to hold path
   GetCurrentDir( buff, FILENAME_MAX );
   string current_working_dir(buff);
   return current_working_dir;
}

main() {
   cout << get_current_dir() << endl;
}

EDIT: User Khoi V accurately commented below on why relative path is better for compiled code, my apologies. Will leave this here for future reference:

But I want to ask why is an absolute path bad in your opinion? There are reasons why absolute path is preferred:

  • absolute paths are clearer: who will have to maintain/modify your script (you or others) will be able to know every time what directories are involved;
  • with absolute paths you are sure the involved directories are the one with the exact path you are writing in the script;
  • relative paths are shorter, but you need to be sure of the subtree you're working with.
Viet Than
  • 164
  • 1
  • 10
  • 3
    Consider putting the answer directly in your response. "Try [this](https://www.tutorialspoint.com/find-out-the-current-working-directory-in-c-cplusplus)" depends on that linked page always existing and being available to the reader. – Drew Dormann Jun 14 '21 at 13:50
  • 1
    Compile code isn't same as script code, with script code you can edit path every time with text editor, but with compile code you can't. When you distribute your executable file to users, they can't edit it. So relative path is right choice here – Khoi V Jun 14 '21 at 14:58
  • Thanks @DrewDormann, have edited answers. – Viet Than Jun 14 '21 at 15:45