0

EDIT

I've read all recommended posts, I've tried those solutions, but non of them helped.

In short problem lies in the third argument of

glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

This code work:

 const char *vertexShaderSource = "#version 120 \n"
                             "attribute vec3 pos;\n"
                             "attribute vec2 texCoord;\n"
                             "varying vec2 texCoord0;\n"
                             "uniform mat4 transform;\n"
                             "void main()\n"
                             "{\n"
                             "   gl_Position = transform * vec4(pos, 1.0);\n"
                             "   texCoord0 = texCoord;\n"
                            "}\0";

But I want to read it from a file, following code works

std::string s= "vertex";
std::ifstream file(s.c_str());
std::stringstream buffer;

buffer << file.rdbuf();
std::string str = buffer.str();
std::cout << str; 

And is outputing:

 #version 120
 attribute vec3 pos;
 attribute vec2 texCoord;
 varying vec2 texCoord0;
 uniform mat4 transform;

 void main()
 {
     gl_Position = transform * vec4(pos, 1.0);
     texCoord0 = texCoord;
 }

From your answers I know that I cannot just simply convert string with code like this:

 const char *vertexShaderSource = str.c_str();

And pass it into:

 glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

So I've used following code to prevent it from ceasing to exist:

 char * writable = new char[str.size() + 1];
 std::copy(str.begin(), str.end(), writable);
 writable[str.size()] = '\0';

Passing glShaderSource(vertexShader, 1, &writable, NULL); does not work also. What else I can do?

END OF EDIT


I'am trying to rewrite a code to a function which takes file name as a parameter, and returns format accepted by glShaderSource, and somewhere I'am making silly mistake, that's the function:

processFile:

const char* processFile(const std::string fileName){

    std::ifstream file;
    file.open(fileName.c_str(), std::ios::in);

    std::string output;
    std::string line;

  if(file.is_open())
  {
      while(file.good())
      {
        getline(file, line);
        output.append(line + "\n");
      }
  }
  else
  {
    std::cerr << "Unable to load shader"  << std::endl;
  }


     const char * shaderCode = output.c_str();
     return shaderCode;

   //I've tried also:
   //  char* result = new char[output.length()+1];
   //  strcpy(result,output.c_str());
   //  return result;
 }

Function call:

const char *vertexShaderSource = processFile("./vertex"); //I am writing on linux

And bellow the code which works:

const char *vertexShaderSource = "#version 120\n"
                                 "attribute vec3 pos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(pos, 1.0f);\n"
                                 "}\0";



vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);

Error messages:

0:1(1): error: syntax error, unexpected $end
0:1(1): error: syntax error, unexpected $undefined

What I'am doing wrong?

  • 1
    At least read https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –  Jun 08 '18 at 22:57
  • What do you get when you print the `output` string? – einpoklum Jun 08 '18 at 23:01
  • @einpoklum #version 120 ... attribute vec3 pos; ... and so on as in working code – Nevil Uchiha Jun 08 '18 at 23:15
  • 1
    `output` in `processFile()` ceases to exist when the function returns, so does the data pointed to by `output.c_str()`. If the caller uses the return value from the function, the behaviour is undefined. Storing the value of the pointer in `const char *` variables does not prevent the object, which contains the data, from ceasing to exist. – Peter Jun 08 '18 at 23:18
  • Seems `processFile()` should accept a `const char*` instead. – Deduplicator Jun 08 '18 at 23:32
  • @Peter I've created `output` outside of a function, and just before passing it into glShaderSource I'm converting it into `const GLchar* vertexShaderSource = output.c_str();` but still the same errors – Nevil Uchiha Jun 08 '18 at 23:56
  • @NevilUchiha: Please read the duplicates. `std::string` doesn't work the way you think it does, and `c_str()` is not a magical buffer that can store anything. – Nicol Bolas Jun 09 '18 at 00:04
  • Same question as https://stackoverflow.com/questions/50779381/glsl-shader-compilation-error – bernie Jun 11 '18 at 14:15

0 Answers0