- Remember which files hold the source code for a particular shader
- Be able to load the source code from file then compile and link the program
- Remember a list of all Uniform and Attribute variables that this shader will use
- Assign specific ID's for those variables when the shader is compiled
I think I've managed to hit all of those goals with this. Here's the public properties and methods:
1: public class GLShader {
2: public enum ErrorCodes {None, CompileFail, LinkFail, InvalidAttrName};
3:
4: public GLShader();
5:
6: public string VertexFile;
7: public string FragmentFile;
8: public string Name;
9: public int Program;
10: public string ErrorDescription;
11: public ErrorCodes ErrorCode;
12:
13: public bool AddUniform(string strName, int iID);
14: public bool AddAttribute(string strName, int iID);
15: public int FindUniformID(string strName);
16: public int FindAttributeID(string strName);
17: public bool BuildShader();
18: }
Properties:
- enum ErrorCodes provides a list of all the error codes that the functions might report. I chose to go this route instead of exceptions to allow the program to continue if a shader failed to compile.
- VertexFile is a string property which lets you set and retrieve the source file name for the vertex shader.
- FragmentFile is a string property which lets you set and retrieve the source file name for the fragment shader.
- Name is a property that let's you give this shader an arbitrary name. I thought this might be useful for finding specific shaders in an array.
- Program allows you to retrieve the program ID assigned to this shader by OpenGL.
- ErrorDescription provides a text description of any errors that were encountered during the most recent function call. It will contain the compiler and linker messages from attempts to build shaders.
- ErrorCode holds the code assigned to any failure encountered in the most recently called function.
Methods:
- GLShader() the constructor doesn't require any information.
- AddUniform() allows you to specify a new Uniform variable to be assigned to this shader. You also must specify an index value to assign this variable.
- AddAttribute() allows you to specify a new Attribute variable to be assigned to this shader. You also must specify an index value to assign this variable.
- FindUniformID() returns the ID for a particular uniform variable.
- FineAttributeID() returns the ID for a particular attribute variable.
- BuildShader() loads the shader source from file, compiles and links them, then assigns the uniform and attribute variables. Essentially does all the work to make this shader ready for use.
FindUniformID() and FindAttributeID() will return -1 if the specified variable name can't be found. All other functions will return true on success and false if some error occurred.
There's a number of private variables and a private class which are also a part of this. I'm hoping that the names provided explain what they do.
Naturally after I'd created this thing and used it a bit I came up with a number of complaints and suggestions for it. I probably will work them in eventually, but it's working at the moment and I don't want to mangle it too badly just yet. Still I don't want to forget them since eventually I'll feel the need to improve this.
- The error reporting was handled much like how OpenGL does. This works but it's cumbersome. In retrospect throwing exceptions might have been better, they can be caught to allow the program to resume and they don't interrupt the code with a bunch of if statements.
- Specifying variables shouldn't need assigned ID's. Since we can retrieve ID's by name everything ought to be fine if we just use the ID's OpenGL gives the values.
- There ought to be a way to specify the shader source as a string. It might not always be in a pair of external files after all.
- The program ID doesn't really need to be public. There's no reason a method of this class can't bind the shader for use.
I have a few other classes and functions I've put together to try and simplify and OpenGL stuff I attempt. I don't intend to publish is and promote it as a full library, but maybe it'll be useful to some one besides me.
To get the full source code go here.
No comments:
Post a Comment