View Full Version : Loading and Using Fragment Shader

Jorj Pimm
12-10-2008, 09:50 AM
Hello, hope this is the place for the question.

Basically im trying to load a fragment shader into openGL and apply it to a mesh...

Shader Code:

void main()
gl_FragColor = vec4(0.4,0.4,0.8,1.0);

Loading Shader: (I've put a `cout<<glGetError<<endl;` after each line and they all seem to succeed)

const char *shaderArray = &amp;file_string;
shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource( shader, 1, &amp;shaderArray, NULL );
glCompileShader( shader );
program = glCreateProgram();
glAttachShader( program, shader );
glLinkProgram( program );

Then after the glUseProgram a GL_INVALID_OPERATION is generated...

if( glIsProgram( program ) )
glUseProgram( program );
cout<<"Error Code: "<<glGetError()<<endl;

I'm assuming its due to:
"GL_INVALID_OPERATION is generated if program could not be made part of current state."
OpenGL Reference Manual (http://www.opengl.org/sdk/docs/man/xhtml/glUseProgram.xml)

But I can't see why this is - the use Program is called once GL is initiated and everything...

I'm sure its some silly mistake, but any help would be greatly appreciated.

Thanks in Advance
- Jorj

12-10-2008, 09:55 AM
you should check if shader got compiled and linked correct. the first for sure. query the compile and link strings for errors or warning. i forgot the exact code though. google.

12-10-2008, 10:18 AM
If you do not know this tutorial already, I suggest you to check out the Lighthouse3D GLSL tutorial (http://www.lighthouse3d.com/opengl/glsl/). It is a good tutorial to start with glsl and you will find how to handle errors.

Jorj Pimm
12-10-2008, 10:19 AM
Awesome, there was an error, it was in loading the string into openGL, and converting char array to "const char **"'s...

Thanks for your help
- Jorj