Followers 0

GLSL - "cannot find void main()"

10 posts in this topic

I received the following error in my console window:

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()
{

}


void main()

{

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}


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.

0

0

Share on other sites

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).

Edited by Cornstalks
2

Share on other sites

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?

0

Share on other sites

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.

Edited by Cornstalks
2

Share on other sites

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

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!

Edited by Shawn619
0

Share on other sites

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:

0

Share on other sites
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)?
1

Share on other sites

Your change works perfectly, thanks again!

0

Share on other sites

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).
0

Share on other sites

Ah, I see. Thanks!

0

Create an account

Register a new account