www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Sharing your openGL Shader struct

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
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 =3D glCreateShader(type); break;
                case GL_FRAGMENT_SHADER:
                    *shader =3D glCreateShader(type); break;
                default:
                    glDeleteShader(*shader);
                    throw new Exception("Error unkonwn shader type  ( %d
)".format( type ) );
            }

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

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

            glGetShaderiv(*shader, GL_COMPILE_STATUS, &compilError);

            if( compilError !=3D GL_TRUE ){
                glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &sizeError);
                char[] error =3D 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       =3D 0;
            _fragmentID     =3D 0;
            _programID      =3D 0;
            _isInitialized  =3D false;
        }

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

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

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

            bindAttribLocation();
            glLinkProgram( _programID );

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

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

                error =3D new char[]( errorSize );

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

            _isInitialized =3D true;
        }

         property
        programID(){
            return _programID;
        }

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

}
Apr 20 2012
parent reply "Felix Hufnagel" <suicide xited.de> writes:
Dav1d has one : https://bitbucket.org/dav1d/glamour/overview

what we have in Derp:  
https://github.com/svenstaro/derp/blob/master/derp/graphics/shader.d
Apr 20 2012
parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le vendredi 20 avril 2012 =C3=A0 22:03 +0200, Felix Hufnagel a =C3=A9crit :
 Dav1d has one : https://bitbucket.org/dav1d/glamour/overview
=20
 what we have in Derp: =20
 https://github.com/svenstaro/derp/blob/master/derp/graphics/shader.d
Derp sound good i found only one probblem impossible to build with given makefile with another compiler as ldc2 or gdc
Apr 20 2012