PDA

View Full Version : Having trouble loading my shaders from file.



inlinevoid
04-27-2013, 04:57 PM
I've been google searching different methods to load text from files for the past few hours, but each time I get the same errors when trying to compile my shaders.

When I try to load/compile my shaders with this function...


string GetFile(const char * filename)
{
ifstream file(filename, ios::in);
string content;
if(file.good())
{
stringstream buffer;
buffer << file.rdbuf();
content = buffer.str();
}
return content;
}

I get these errors...


Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors. No code generated

Fragment shader failed to compile with the following errors
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors. No code generated


I'm 99% sure it's not a problem with my OpenGL code either because I can copy the same exact shader into a hardcoded string and it compiles fine. I also know that my frag/vert files are being read correctly because I can output the content to the console and it looks fine.

My shaders aren't complicated at all, either. Here's my vertex shader:

#version 150
in vec2 position;
void main()
{
gl_Position = vec4( position, 0.0, 1.0 );
}

Has anyone run into this issue before? I've tried googling the error messages but they seem to be pretty generic and thus I'm getting no helpful results. It'd be nice if someone could try the GetFile function to load a shader and tell me if it works for them or not.

The Little Body
04-27-2013, 06:12 PM
Have you test to display the string content of the ouput of your GetFile() function using printf("%s", GetFile("yourshader.txt")) ?

Because I don't see any "<" in your shader ...

And/or have you test using a GetFile(const char *filename, char *content) instead to use a string object at output ?

Because the firsts bytes of a string object is perhaps the size and/or the type of the string ...
(and I see "ERROR: 0:1" so it's certainly one error at the begining of the first line)

inlinevoid
04-27-2013, 06:20 PM
Yes, I've even compared the character count to the hardcoded string and they are exactly the same.

This is what I'm outputting to the console:



Hardcoded string, chars = 88:
#version 150
out vec4 outColor;
void main()
{
outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}

GetFile string, chars = 88:
#version 150
out vec4 outColor;
void main()
{
outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}

I don't see any "<" either which is why this is so frustrating, especially when I'm just trying to learn OpenGL.

The Little Body
04-27-2013, 06:35 PM
This work without the "#version 150" ?

Can you please give the code source you use ?
(for to can see if the problem is not on other part of it)

inlinevoid
04-27-2013, 06:41 PM
Nevermind, I realize the problem...


const char * vertexSource = GetFile("basic.vert").c_str();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

const char * fragmentSource = GetFile("basic.frag").c_str();
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

should be...


string vertexSourceString = GetFile("basic.vert");
const char * vertexSource = vertexSourceString.c_str();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);

string fragmentSourceString = GetFile("basic.frag");
const char * fragmentSource = fragmentSourceString.c_str();
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);

I was using a grabage pointer. Stupid mistake.

Thanks for the help though, The Little Body!

Alfonse Reinheart
04-27-2013, 06:51 PM
How are you passing this string to OpenGL?