Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Having trouble loading my shaders from file.

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    17

    Having trouble loading my shaders from file.

    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...
    Code :
    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...
    Code :
    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:
    Code :
    #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.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    248
    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)
    Last edited by The Little Body; 04-27-2013 at 06:32 PM.
    @+
    Yannoo

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    17
    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:

    Code :
    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.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    248
    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)
    @+
    Yannoo

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    17
    Nevermind, I realize the problem...

    Code :
    	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...
    Code :
    	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!

  6. #6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •