I played a little bit more with the external shader example.

Now also works without shaders.
This commit is contained in:
spartanj@gmail.com
2012-06-19 01:39:58 -03:00
parent def64c6995
commit dce1d97620
4 changed files with 119 additions and 36 deletions

View File

@@ -437,4 +437,24 @@ void cShaderProgram::SetReloadCb( ShaderProgramReloadCb Cb ) {
mReloadCb = Cb;
}
void cShaderProgram::EnableVertexAttribArray( const std::string& Name ) {
EnableVertexAttribArray( AttributeLocation( Name ) );
}
void cShaderProgram::EnableVertexAttribArray( const Int32& Location ) {
#ifdef EE_SHADERS_SUPPORTED
glEnableVertexAttribArray( Location );
#endif
}
void cShaderProgram::DisableVertexAttribArray( const std::string& Name ) {
DisableVertexAttribArray( AttributeLocation( Name ) );
}
void cShaderProgram::DisableVertexAttribArray( const Int32& Location ) {
#ifdef EE_SHADERS_SUPPORTED
glDisableVertexAttribArray( Location );
#endif
}
}}

View File

@@ -2,8 +2,9 @@
/// This example is based on the WebGL demo from http://minimal.be/lab/fluGL/
Uint32 ParticlesNum = 30000;
cWindow * win;
cShaderProgram * ShaderProgram;
cWindow * win = NULL;
cShaderProgram * ShaderProgram = NULL;
bool ShadersSupported = false;
eeFloat tw;
eeFloat th;
eeFloat aspectRatio;
@@ -43,9 +44,10 @@ void videoResize() {
/// eepp enables some this client states by default, and textures by default
GLi->Disable( GL_TEXTURE_2D );
GLi->DisableClientState( GL_TEXTURE_COORD_ARRAY );
GLi->DisableClientState( GL_COLOR_ARRAY );
/// GL_VERTEX_ARRAY is needed, so we keep it enabled
/// GL_VERTEX_ARRAY and GL_COLOR_ARRAY are needed, so we keep them enabled
GLi->EnableClientState( GL_VERTEX_ARRAY );
GLi->EnableClientState( GL_COLOR_ARRAY );
/// Reset the default blend func ( by default eepp use ALPHA_NORMAL )
cTextureFactory::instance()->SetPreBlendFunc( ALPHA_BLENDONE );
@@ -53,21 +55,23 @@ void videoResize() {
/// Set the line width
cGlobalBatchRenderer::instance()->SetLineWidth( 2 );
/// Rebind the Shader
ShaderProgram->Bind();
if ( ShadersSupported ) {
/// Rebind the Shader
ShaderProgram->Bind();
/// If you want to use the fixed-pipeline renderer you'll need to set up the projection and modelview matrix manually.
/// Or if you want to use another name to the projection matrix or the modelview matrix ( programmable pipelines use
/// dgl_ProjectionMatrix and dgl_ModelViewMatrix by default.
if ( GLv_2 == GLi->Version() ) {
ShaderProgram->SetUniformMatrix( "dgl_ProjectionMatrix", perspectiveMatrix );
/// If you want to use the programmable-pipeline renderer you'll need to set up the projection and modelview matrix manually.
/// Or if you want to use another name to the projection matrix or the modelview matrix ( eepp programmable-pipeline use
/// dgl_ProjectionMatrix and dgl_ModelViewMatrix by default.
if ( GLv_2 == GLi->Version() ) {
ShaderProgram->SetUniformMatrix( "dgl_ProjectionMatrix", perspectiveMatrix );
/// Get the identity matrix and set it to the modelview matrix
GLfloat modelMatrix[16];
GLi->LoadIdentity();
GLi->GetCurrentMatrix( GL_MODELVIEW_MATRIX, modelMatrix );
/// Get the identity matrix and set it to the modelview matrix
GLfloat modelMatrix[16];
GLi->LoadIdentity();
GLi->GetCurrentMatrix( GL_MODELVIEW_MATRIX, modelMatrix );
ShaderProgram->SetUniformMatrix( "dgl_ModelViewMatrix", modelMatrix );
ShaderProgram->SetUniformMatrix( "dgl_ModelViewMatrix", modelMatrix );
}
}
}
@@ -77,26 +81,44 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
if ( win->Created() )
{
/// This will work without shaders too
ShadersSupported = GLi->ShadersSupported();
cInput * imp = win->GetInput();
/// Disable the automatic shader conversion from fixed-pipeline to programmable-pipeline
cShader::Ensure = false;
/// We real don't need shaders for this, but the purpose of the example is to show how to work with external shaders
if ( ShadersSupported ) {
/// Disable the automatic shader conversion from fixed-pipeline to programmable-pipeline
cShader::Ensure = false;
std::string fs( "#ifdef GL_ES\n\
precision highp float;\n\
#endif\n\
void main() { gl_FragColor = vec4(0.4, 0.01, 0.08, 0.5); }" );
std::string fs( "#ifdef GL_ES\n\
precision highp float;\n\
#endif\n\
varying vec4 dgl_Color;\n\
void main() { gl_FragColor = dgl_Color; }" );
std::string vs( "#ifdef GL_ES\n\
precision highp float;\n\
#endif\n\
attribute vec3 dgl_Vertex;\n\
uniform mat4 dgl_ProjectionMatrix;\n\
uniform mat4 dgl_ModelViewMatrix;\n\
void main() { gl_Position = dgl_ProjectionMatrix * dgl_ModelViewMatrix * vec4(dgl_Vertex, 1.0); }" );
std::string vs( "#ifdef GL_ES\n\
precision highp float;\n\
#endif\n\
attribute vec3 dgl_Vertex;\n\
attribute vec4 dgl_FrontColor;\n\
varying vec4 dgl_Color;\n\
uniform mat4 dgl_ProjectionMatrix;\n\
uniform mat4 dgl_ModelViewMatrix;\n\
void main() {\n\
dgl_Color = dgl_FrontColor;\n\
gl_Position = dgl_ProjectionMatrix * dgl_ModelViewMatrix * vec4(dgl_Vertex, 1.0);\n\
}");
/// Create the new shader program
ShaderProgram = eeNew( cShaderProgram, ( vs.c_str(), vs.size(), fs.c_str(), fs.size() ) );
/// Since fixed-pipeline OpenGL use gl_FrontColor for glColorPointer, we need to replace the color attribute
/// This is all to show how it works, in a real world scenario, you will choose to work fixed-pipeline or programmable-pipeline.
if ( GLi->Version() == GLv_2 ) {
ReplaceSubStr( fs, "gl_FragColor = dgl_Color", "gl_FragColor = gl_FrontColor" );
}
/// Create the new shader program
ShaderProgram = eeNew( cShaderProgram, ( vs.c_str(), vs.size(), fs.c_str(), fs.size() ) );
}
/// Set the projection
videoResize();
@@ -107,11 +129,13 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
Uint32 i;
eeVector3ff * vertices = eeNewArray( eeVector3ff, ParticlesNum );
eeVector3ff * velocities = eeNewArray( eeVector3ff, ParticlesNum );
eeColorAf * colors = eeNewArray( eeColorAf, ParticlesNum );
for (i = 0; i < ParticlesNum; i++ )
{
vertices[i] = eeVector3ff( 0, 0, 1.83 );
velocities[i] = eeVector3ff( (eeRandf() * 2 - 1)*.05, (eeRandf() * 2 - 1)*.05, .93 + eeRandf()*.02 );
colors[i] = eeColorAf( eeRandf() * 0.5, 0.1, 0.8, 0.5 );
}
while ( win->Running() )
@@ -173,14 +197,14 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
if ( d < 2.f ) {
if ( d < 0.03f ) {
vertices[i+1].x = ( eeRandf() * 2 - 1 ) * aspectRatio;
vertices[i+1].y = eeRandf() * 2 - 1;
vertices[i+1].x = eeRandf( -1, 1 ) * aspectRatio;
vertices[i+1].y = eeRandf( -1, 1 );
velocities[i].x = 0;
velocities[i].y = 0;
} else {
dx /= d;
dy /= d;
d = ( 2 - d ) / 2;
d = ( 2 - d ) * 0.5;
d *= d;
velocities[i].x += dx * d * .01;
velocities[i].y += dy * d * .01;
@@ -190,10 +214,12 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
}
/// VertexPointer assigns values by default to the attribute "dgl_Vertex"
/// ColorPointer to "dgl_FrontColor"
/// TextureCoordPointer to "dgl_MultiTexCoord0"
GLi->VertexPointer( 3, GL_FP, sizeof(eeVector3ff), reinterpret_cast<char*> ( &vertices[0] ), 0 );
/// ColorPointer to "dgl_FrontColor"
GLi->ColorPointer( 4, GL_FP, sizeof(eeColorAf), reinterpret_cast<char*> ( &colors[0] ), 0 );
/// Draw the lines
GLi->DrawArrays( DM_LINES, 0, ParticlesNum );
@@ -202,6 +228,7 @@ EE_MAIN_FUNC int main (int argc, char * argv [])
eeSAFE_DELETE_ARRAY( vertices );
eeSAFE_DELETE_ARRAY( velocities );
eeSAFE_DELETE_ARRAY( colors );
}
cEngine::DestroySingleton();