file.open()找不到文件、c++、Xcode、OpenGL

file.open() not finding file, c++, Xcode, OpenGL

本文关键字:c++ Xcode OpenGL 文件 找不到 open file      更新时间:2023-10-16

我已经试了一整天了,但没有找到有效的答案。我正在尝试在我的opengl程序中加载着色器,然而,该程序似乎无法找到文件。如有任何帮助,我们将不胜感激。这是代码

#include "Shader.hpp"
#include <fstream>
#include <iostream>
static std::string loadShader(const std::string& filename);
static void checkShaderError(GLuint shader, GLuint flag, bool isProgram, const          std::string& errMessage);
static GLuint createShader(const std::string& text, GLenum shaderType);
Shader::Shader(const std::string& filename){
  m_program = glCreateProgram();
  m_shaders[0] = createShader(loadShader(filename + ".vs"), GL_VERTEX_SHADER);
  m_shaders[1] = createShader(loadShader(filename +".fs"),   GL_FRAGMENT_SHADER);
  for(unsigned int i = 0; i < NUM_SHADERS; i++ ){
      glAttachShader(m_program, m_shaders[i]);
  }
  glBindAttribLocation(m_program, 0, "position");
  glLinkProgram(m_program);
  checkShaderError(m_program, GL_LINK_STATUS, true, "Error: Program failed to   link!");
  glValidateProgram(m_program);
  checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Program is invalid!");
}
Shader::~Shader(){
  for(int i = 0; i < NUM_SHADERS; i++)
  {
    glDetachShader(m_program, m_shaders[i]);
    glDeleteShader(m_shaders[i]);
  }
  glDeleteProgram(m_program);
}
static std::string loadShader(const std::string& filename){
  std::ifstream file;
  file.open((filename).c_str());
  std::string output;
  std::string line;
  if(file.is_open()){
      while(file.good()){
          getline(file, line);
          output.append(line + "n");
      }
  }else{
      std::cerr << "Shader could not be loaded, shader id: " << filename <<       std::endl;
  }
  file.close();
  return output;
}

我遇到了完全相同的问题。完全重新生成项目,然后尝试再次运行。

编辑:更改代码中的文件路径有一种奇怪的行为。我个人总是需要重建文件来解决这个问题。