Game Development Reference
In-Depth Information
std::string ShaderStr = "#version 150 core\n";
#else
std::string ShaderStr = "#version 100\n";
ShaderStr += "precision highp float;\n";
ShaderStr += "#define USE_OPENGL_ES_2\n";
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"texture(", "texture2D(" );
if ( Target == GL_VERTEX_SHADER )
{
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"in ", "attribute " );
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"out ", "varying " );
}
if ( Target == GL_FRAGMENT_SHADER )
{
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"out vec4 out_FragColor;", "" );
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"out_FragColor", "gl_FragColor" );
ShaderCodeUsed = Str_ReplaceAllSubStr( ShaderCodeUsed,
"in ", "varying " );
}
#endif
This kind of search and replace implies some restrictions on the shaders
source code. For example, it will invalidate shaders containing identiiers
such as grayin and sprout . However, the code above is very simple
and was used successfully in a couple of released commercial projects.
We store our shaders in GLSL 1.5 source code and just do a simple search and replace to use
them on Android. It is very easy and transparent.
How it works…
The complete implementation is presented in the clGLSLShaderProgram class from the
3_ShadersAndVertexArrays example. After the code is downgraded, in case of need, it is
uploaded into OpenGL:
GLuint Shader = LGL3->glCreateShader( Target );
const char* Code = ShaderStr.c_str();
LGL3->glShaderSource( Shader, 1, &Code, NULL );
LOGI( "Compiling shader for stage: %X\n", Target );
LGL3->glCompileShader( Shader );
 
Search WWH ::




Custom Search