-3

I'm trying to load a simple GLSL program from a .txt file. Here is the function which loads the file:

string getShaderSource(char path[])
{
    string out;
    ifstream mFile;
    stringstream buf;
    mFile.open(path);
    if (mFile.is_open())
    { 
        buf << mFile.rdbuf();
        out = buf.str();
    }
    else
        printf("Cannot open shader source file.");      
    mFile.close();
    //out.append("\0");
    return out;
}

And I use use the function like this:

string vs = getShaderSource("C:/vs.txt");
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, (const GLchar**)&vs, NULL);

Problem is the shader doesn't compiles no matter what, here is the error:

0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Very simple GLSL code:

#version 410 
layout (location = 0) in vec3 vp;
layout (location = 1) in vec3 color;
smooth out vec4 theColor;
void main () {
    gl_Position = vec4 (vp, 1.0);
    theColor = vec4(color, 0.0);
}

I know there are similar questions out there, I've tried a lot to fix the problem but no progress. What am I doing wrong?

Amol Borkar
  • 2,321
  • 7
  • 32
  • 63
  • You've shown the code that loads the shader source, but the problem is in the compilation of the shader source. Please show this. To rule out your shader source loader, temporarily hard code your shader source and see if you get the same message. –  Jan 26 '15 at 12:36
  • of what type is `vs`? – Axalo Jan 26 '15 at 12:37
  • @Axalo std::string, sorry that fs thing was a typo. – Amol Borkar Jan 26 '15 at 12:38
  • @Poldie Yep, I've tried to store the GLSL code as `const char*` and it works fine that way, but not when i try to load it from file. I think something is wrong with my txt file loading function. – Amol Borkar Jan 26 '15 at 12:40
  • It's better to accept a pointer to a character because arrays have to be initialised (they're references). OpenGL won't understand a string so returning it and then performing an ugly type conversion isn't advised. It's better to just convert the string to a char* then pass that in. Also, add a fragment shader or you won't see any colour. – Poriferous Jan 26 '15 at 12:44
  • @Poriferous fragment shader is also in another text file, the above error shows for both. – Amol Borkar Jan 26 '15 at 12:46

2 Answers2

2

In the following line:

glShaderSource(vertexShader, 1, (const GLchar**)&vs, NULL);

You are casting a std::string into a GLchar**. That won't work.

Change it to

const char *s = vs.c_str();
glShaderSource(vertexShader, 1, (const GLchar**)&s, NULL);

See here.

Community
  • 1
  • 1
Axalo
  • 2,953
  • 4
  • 25
  • 39
  • VS gives an Error saying: Expression must be an lvalue or a function designator – Amol Borkar Jan 26 '15 at 12:44
  • The program runs, the both vertex and fragement shader compile succesfully, but there is No color. Any ideas why?? – Amol Borkar Jan 26 '15 at 13:25
  • @EdwardMckinzie can you see anything at least? – Axalo Jan 26 '15 at 13:27
  • Yes, Its a basic rectangle with different colors, but only white rectangle is visible. You can check the whole code here: http://pastebin.com/download.php?i=Cegtj63J – Amol Borkar Jan 26 '15 at 13:33
  • @EdwardMckinzie that code doesn't really help. Anyway, I suggest you ask another question as there are many other people that could help you here as well. – Axalo Jan 26 '15 at 13:43
0
string fs = getShaderSource("C:/vs.txt");
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, (const GLchar**)&vs, NULL);

this is suspicious. The correct code would be something like:

string fs = getShaderSource("C:/vs.txt");
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

char* vs = fs.c_str();
glShaderSource(vertexShader, 1, (const GLchar**)&vs, NULL);

The first member of a std::string is not guaranteed to be the char* that holds the data, instead get the pointer explicitly in a variable and pass the pointer to that variable.

ratchet freak
  • 47,288
  • 5
  • 68
  • 106