GLSL - "cannot find void main()"

Started by
9 comments, last by Shawn619 11 years ago

Following the shader program setup tutorial at http://www.arcsynthesis.org/gltut/Basics/Tut01%20Making%20Shaders.html

I received the following error in my console window:

20qi7ms.jpg

By this error, I assume that the program locates and reads the .vs and .fs files from the appropriate directory, so that can't be the problem.

My calls that creates each shader are as follows:


void InitializeProgram()
{
    std::vector<GLuint> shaderList;
 
    shaderList.push_back(CreateShader(GL_VERTEX_SHADER, "shader"));
    shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, "shader"));
 
    shaderOne = CreateProgram(shaderList);
    std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
}

shader.vs


void main()

{

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

shader.fs


void main()

{ 

    gl_FragColor = vec4(1.0,0.0,0.0,0.0);

}

Also, I thought there might be a problem with having the file extension .vs instead of .vert, but neither work.

Advertisement

Shouldn't you call CreateShader with the full name? (ie, "shader.vs" and "shader.fs" )

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Assuming you're using the code from that website, you need to pass the contents of the file to CreateShader, not the file name (with or without extension).

Funny enough, someone else was following the exact same tutorial and made the exact same error (I suggest you read this thread and my answer).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Oh, I'm pretty sure I know exactly what you mean. I think google let me down by not leading me to that thread with my same error. Is there a function that will parse my .txt shader files and put them in a std::string?

Oh, I'm pretty sure I know exactly what you mean. I think google let me down by not leading me to that thread with my same error. Is there a function that will parse my .txt shader files and put them in a std::string?

No, but it's pretty easy to do yourself. There are many solutions, and which you choose probably doesn't matter in your situation (you don't need reading shader files to be blazing fast, as it's a one time thing and they're usually not massive files). I'd probably wrap it up in a function.

Also, just to nitpick a technicality, you don't need to parse your shader files (parsing has to do with syntactic analysis). You just need to read the source into memory so you can pass it on to OpenGL to compile the shader source.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Sorry, my concept of the word "parsing" was wrong, and i'll know not to use it, again. Thanks smile.png

Yeah, I'll write a basic function to extract the contents on my own. I'm sure it will look a lot nicer than explicitly writing it as an argument:


shaderList.push_back(CreateShader(GL_VERTEX_SHADER, "varying vec4 v; void main() { v = gl_Color; gl_Position = gl_ModelViewProjectionMatrix *gl_Vertex; }"));

Haha, thanks again!

One last question,

The function I wrote for reading .txt contents into a string is not working properly:


std::string readFiletoString(string fileName){
 
    std:string str;
    ifstream infile;
    infile.open(fileName);
 
 
    while(!infile.eof()) // To get you all the lines.
    {
        getline(infile,str); 
 
        // Saves the line in STRING.
        cout<<str; 
 
        // Prints our STRING.
    }
    infile.close();
    cout << str << endl;
    cout << "void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}" << endl;
 
 
    if (str=="void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}"){
        cout << "true" << endl;
    }else{
        cout << "false" << endl;
    }
 
 
    return str;
}

Somehow my function to read the same contents as opposed to manually writing it out is different, and gives me a shader error as a result:

15cddm9.jpg

First, I'd suggest making these changes to reading the file contents:

std::string readFiletoString(string fileName){
    std:string contents;
    std:string line;
    ifstream infile(fileName);
 
 
    while (getline(infile, line)) // This is better than checking for .eof() (as it also handles other I/O errors)
    {
        contents += line + "\n";
    }
    return contents;
}

As for why your code is printing out false instead of true... I don't know, I'd have to use a debugger. Have you tried stepping through the code with your debugger? Also, how are you calling CreateShader() (are you sure you're giving it the shader source)?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Your change works perfectly, thanks again!

Your change works perfectly, thanks again!

Ah, I just read through your code again and I realize why it wasn't working. The first time your while loop runs, it reads the line and prints it out (note: str doesn't have a newline character on it (getline strips it off, which is why I added it back on in my code)). Great (except for the fact that it didn't save the string (note that getline doesn't accumulate lines into the string)). However, you haven't reached eof() yet (which is what you're while loop is checking for), so the loop runs again, and gets another line fromt he file (which is probably an empty line). Now str is empty (since getline read an empty line, and thus made str an empty string). Printing it out results in nothing being printed. Your if statement results in false because str is empty. Now you're returning an empty string, and your OpenGL code complains saying it can't find main (because it was given empty shader source).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement