初学OpenGL,使用cmake编写着色器头文件的过程中,调用ifstream读取shader文件的相对路径,但是一直读取失败,使用绝对路径后是可以运行成功的,查询路径之后发现实际路径是编译之后所在的exe文件路径。想知道有没有什么办法可以直接使用项目中的相对路径。
drawGLSL1::main函数:
std::string vertexShader = "D:\\workstation\\CppProjects\\TestOpenGL\\Source\\StandAlone\\Practicing\\shader.vert";
std::string fragmentShader = "D:\\workstation\\CppProjects\\TestOpenGL\\Source\\StandAlone\\Practicing\\shader.frag";
std::cout << "main cur path:" << fs::current_path() << endl;
//main cur path:"D:\\workstation\\CppProjects\\TestOpenGL\\out\\build\\x64-Debug\\Source\\StandAlone"
Shader ourShader(vertexShader,fragmentShader);
shader_s.h
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::cout << "current path:" << fs::current_path() << std::endl;
//current path:"D:\\workstation\\CppProjects\\TestOpenGL\\out\\build\\x64-Debug\\Source\\StandAlone"
项目结构:
我试图以下这么写,但是会报错读取文件失败,这应该是正常的写法,想知道如何设置才能像下面这样写
std::string vertexShader = "Shaders/shader.vert";
std::string fragmentShader = "Shaders/shader.frag";