Sharing your openGL Shader struct

bioinfornatics bioinfornatics at fedoraproject.org
Fri Apr 20 08:11:47 PDT 2012


I start to move to openGL3, i share mmine first struct for use Shader
with openGL3. Enhance or share your struct.

------------------------------------
struct Shader{
    private:

        void initializeTypeShader( GLuint* shader, GLenum type, string
source ){
            if( glCreateShader is null )
                throw new Exception("Shader unsported by ypur graphic
card" );
            switch(type){
                case GL_VERTEX_SHADER:
                    *shader = glCreateShader(type); break;
                case GL_FRAGMENT_SHADER:
                    *shader = glCreateShader(type); break;
                default:
                    glDeleteShader(*shader);
                    throw new Exception("Error unkonwn shader type  ( %d
)".format( type ) );
            }

            char[] sourceCode;
            File f = File( source, "r" );
            f.rawRead( sourceCode );
            f.close();
            char* sourceCodePtr = sourceCode.ptr;

            GLint compilError, sizeError;
            glShaderSource(*shader, 1, &sourceCodePtr, null);
            glCompileShader(*shader);

            glGetShaderiv(*shader, GL_COMPILE_STATUS, &compilError);

            if( compilError != GL_TRUE ){
                glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &sizeError);
                char[] error = new char[]( sizeError );
                glGetShaderInfoLog(*shader, sizeError, &sizeError,
error.ptr);
                throw new Exception("Error shader compilation failled
( %d )".format( type ) );
            }
        }

        void bindAttribLocation(){
            glBindAttribLocation( _programID, 0, "in_Vertex"    );
            glBindAttribLocation( _programID, 1, "in_Color"     );
            glBindAttribLocation( _programID, 2, "in_TexCoord0" );
            glBindAttribLocation( _programID, 3, "in_Normal"    );
        }

        string  _vertex;
        string  _fragment;
        GLuint  _vertexID;
        GLuint  _fragmentID;
        GLuint  _programID;
        bool    _isInitialized;

    public:

        this( string vertex, string fragment){
            _vertexID       = 0;
            _fragmentID     = 0;
            _programID      = 0;
            _isInitialized  = false;
        }

        ~this(){
            glDeleteShader( _vertexID   );
            glDeleteShader( _fragmentID );
            glDeleteProgram( _programID );
        }

        @property
        void initialize(){
            initializeTypeShader( &_vertexID, GL_VERTEX_SHADER, _vertex
);
            initializeTypeShader( &_fragmentID, GL_FRAGMENT_SHADER,
_fragment );

            _programID = glCreateProgram();
            glAttachShader( _programID,  _vertexID );
            glAttachShader( _programID,  _fragmentID );

            bindAttribLocation();
            glLinkProgram( _programID );

            GLint link = 0;
            glGetProgramiv( _programID, GL_LINK_STATUS, &link );

            if( link != GL_TRUE ){
                GLint  errorSize = 0;
                char[] error     = null;
                glGetProgramiv( _programID, GL_INFO_LOG_LENGTH,
&errorSize);

                error = new char[]( errorSize );

                glGetProgramInfoLog( _programID, errorSize, &errorSize,
error.ptr);
                throw new Exception( "OpenGL program link error: %s",
to!string( error ) );
            }

            _isInitialized = true;
        }

        @property
        programID(){
            return _programID;
        }

        @property
        Shader dup(){
            Shader duplicate = Shader( _vertex, _fragment);
            duplicate.initialize();
            return duplicate;
        }

}




More information about the Digitalmars-d mailing list