void Shader::Compile(const std::string &pVSName, const std::string &pFSName, const std::string &pGSName){ std::string all = String(pVSName)+pFSName+pGSName; Shader* tmp = ResourceManager::Call().Get<Shader>(all); if(tmp != 0){ //Shader existe deja, on copie ses identifiants dans celui ci mProgram = tmp->mProgram; }else{ //Shader n'existe pas, on le cree // Ajout du shader au ResourceManager ResourceManager::Call().Add(all, this); // Chargement des fichiers shaders LoadVertexShader(pVSName); LoadFragmentShader(pFSName); if(!pGSName.empty()) LoadGeometryShader(pGSName); // Creation du shader mProgram mProgram = glCreateProgram(); // Linkage des deux shaders précédemment créés glAttachShader(mProgram, mVShader); glAttachShader(mProgram, mFShader); if(mGShader) glAttachShader(mProgram, mGShader); // Linkage du mProgramme a OGL glLinkProgram(mProgram); GLint error; glGetProgramiv(mProgram, GL_LINK_STATUS, &error); if(!error){ char log[1024]; glGetProgramInfoLog(mProgram, 1024, NULL, log); if(mVShader) glDeleteShader(mVShader); if(mFShader) glDeleteShader(mFShader); if(mGShader) glDeleteShader(mGShader); throw Exception(String("Shader Link Error :\n")+log); } // Destruction des Shaders. ils sont maintenant dans le program glDeleteShader(mVShader); glDeleteShader(mFShader); if(mGShader) glDeleteShader(mGShader); // Attribution des texCoords glUseProgram(mProgram); glUniform1i(glGetUniformLocation(mProgram, "tex"), 0); glBindFragDataLocation(mProgram, 0, "finalColor"); glUseProgram(0); } }
////////////////////////////////////////////////////////////////////// // powi¹zanie shadera geometrii, reprezentowanego przez kod // Ÿród³owy, z obiektem programu; w razie potrzeby tworzony // jest nowy obiekt programu ////////////////////////////////////////////////////////////////////// void AttachGeometryShader( GLuint &program, const char **src ) { // odczyt kodu shadera i jego kompilacja GLuint shader = LoadGeometryShader( src ); // w razie potrzeby tworzymy obiekt programu if( !glIsProgram( program ) ) program = glCreateProgram(); // do³¹czenie obiektu shadera do obiektu programu glAttachShader( program, shader ); }
////////////////////////////////////////////////////////////////////// // powi¹zanie shadera wierzcho³ków, shadera geometrii i shadera // fragmentów, reprezentowanego przez kod Ÿród³owy, z obiektem // programu; w razie potrzeby tworzony jest nowy obiekt programu ////////////////////////////////////////////////////////////////////// void AttachVertexGeometryFragmentShader( GLuint &program, const char **vsSrc, const char **gsSrc, const char **fsSrc ) { // odczyt kodu shadera wierzcho³ków i jego kompilacja GLuint vShader = LoadVertexShader( vsSrc ); // odczyt kodu shadera geometrii i jego kompilacja GLuint gShader = LoadGeometryShader( gsSrc ); // odczyt kodu shadera fragmentów i jego kompilacja GLuint fShader = LoadFragmentShader( fsSrc ); // w razie potrzeby tworzymy obiekt programu if( !glIsProgram( program ) ) program = glCreateProgram(); // do³¹czenie obiektów shaderów do obiektu programu glAttachShader( program, vShader ); glAttachShader( program, gShader ); glAttachShader( program, fShader ); }
void GLSLProgram::Create( char *vfile, char *gfile, char *ffile ) { CanDoFragmentShader = IsExtensionSupported( "GL_ARB_fragment_shader" ); CanDoGeometryShader = IsExtensionSupported( "GL_EXT_geometry_shader4" ); CanDoVertexShader = IsExtensionSupported( "GL_ARB_vertex_shader" ); InputTopology = GL_TRIANGLES; OutputTopology = GL_TRIANGLE_STRIP; Vshader = Gshader = Fshader = 0; Program = 0; AttributeLocs.clear(); UniformLocs.clear(); Verbose = false; this->Program = glCreateProgram(); CheckGlErrors( "glCreateProgram" ); if( vfile != NULL && vfile[0] != '\0' ) { if( ! CanDoVertexShader ) { fprintf( stderr, "Warning: this system cannot handle vertex shaders\n" ); } this->Vshader = LoadVertexShader( vfile ); int status = CompileShader( this->Vshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", vfile ); AttachShader( this->Vshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", vfile ); } } if( gfile != NULL && gfile[0] != '\0' ) { if( ! CanDoGeometryShader ) { fprintf( stderr, "Warning: this system cannot handle geometry shaders\n" ); } this->Gshader = LoadGeometryShader( gfile ); int status = CompileShader( this->Gshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", gfile ); AttachShader( Gshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", gfile ); } } if( ffile != NULL && ffile[0] != '\0' ) { if( ! CanDoFragmentShader ) { fprintf( stderr, "Warning: this system cannot handle fragment shaders\n" ); } this->Fshader = LoadFragmentShader( ffile ); int status = CompileShader( this->Fshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", ffile ); AttachShader( Fshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", ffile ); } } LinkProgram(); };