1

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;
}

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. I'm getting following error all the time, even with another copy&paste code from tutorials

 0:1(4): preprocessor error: syntax error, unexpected HASH_TOKEN

What else I can do?

I've read these posts: Reading a shader from a .txt file using a structure

read GLSL shaders from file

  • my bet is you have some nasty control code (ASCII code <32) messing up the compilation try to save the string as file and then see it in hex view and look for characters below 20h (not counting `0Ah, 0Dh, 09h` which are valid CR,LF,Tab). btw I am missing `+` between the substrings but that is most likely just because I used different compiler anyway maybe some macro encapsulation of the strings would solve your problem. – Spektre Jun 10 '18 at 06:21
  • In hex view everythings seems to be ok – Marek Kowal Jun 10 '18 at 10:07
  • then I would try to do everything in single line and use `\r\n` instead of `\n` if it make a difference. Also Some compilers requires to use `_T("text")` instead of `"text"` otherwise they convert it to unicode or different encoding messing up the stuff at least I saw people use that in such cases here in SO... – Spektre Jun 10 '18 at 13:13
  • Same question as https://stackoverflow.com/questions/50769270/converting-text-string-into-const-char-in-c – bernie Jun 11 '18 at 14:16

1 Answers1

0

Here is the code I use to load shader sources:

ifstream iStream(filename);
stringstream buffer;
buffer << iStream.rdbuf();
string source = buffer.str();

const char* sources[] = { source.c_str() };
glShaderSource(handle, 1, sources, 0);

As you can see, the string can be given directly to glShaderSource; there is no need to create a copy of it. Have a look at the spec for c_str() if you're not convinced.

Not sure what the problem is in your case. I have used both \r\n and \n line endings successfully. Maybe you have a U+FEFF BOM character at the start of the file? It could with the the "hash" mentioned in the compilation error.

bernie
  • 9,820
  • 5
  • 62
  • 92