From dce1d97620cc9021d2773cecfcabfb51a5bd7b76 Mon Sep 17 00:00:00 2001 From: "spartanj@gmail.com" Date: Tue, 19 Jun 2012 01:39:58 -0300 Subject: [PATCH] I played a little bit more with the external shader example. Now also works without shaders. --- include/eepp/graphics/cshaderprogram.hpp | 12 +++ projects/android-project/jni/Android.mk | 26 ++++- src/eepp/graphics/cshaderprogram.cpp | 20 ++++ .../external_shader/external_shader.cpp | 97 ++++++++++++------- 4 files changed, 119 insertions(+), 36 deletions(-) diff --git a/include/eepp/graphics/cshaderprogram.hpp b/include/eepp/graphics/cshaderprogram.hpp index 56a89f3f5..62c98271a 100644 --- a/include/eepp/graphics/cshaderprogram.hpp +++ b/include/eepp/graphics/cshaderprogram.hpp @@ -116,6 +116,18 @@ class EE_API cShaderProgram { /** Set a reload callback ( needed to reset shader states ). */ void SetReloadCb( ShaderProgramReloadCb Cb ); + + /** Enable a vertex attribute array */ + void EnableVertexAttribArray( const std::string& Name ); + + /** Enable a vertex attribute array */ + void EnableVertexAttribArray( const Int32& Location ); + + /** Disable a vertex attribute array */ + void DisableVertexAttribArray( const std::string& Name ); + + /** Disable a vertex attribute array */ + void DisableVertexAttribArray( const Int32& Location ); protected: std::string mName; Uint32 mHandler; diff --git a/projects/android-project/jni/Android.mk b/projects/android-project/jni/Android.mk index c31738d7d..af691ffae 100644 --- a/projects/android-project/jni/Android.mk +++ b/projects/android-project/jni/Android.mk @@ -211,7 +211,7 @@ LOCAL_STATIC_LIBRARIES := eepp include $(BUILD_SHARED_LIBRARY) #************ empty_window ************ -#************* full_test ************* +#************* external_shader ************* include $(CLEAR_VARS) LOCAL_PATH := $(MY_PATH) @@ -224,6 +224,30 @@ LOCAL_CFLAGS := $(MY_C_FLAGS) LOCAL_C_INCLUDES := $(MY_C_INCLUDES) +CORE_SRCS := \ + $(MY_SDL_MAIN_PATH) \ + ../examples/external_shader/*.cpp + +LOCAL_SRC_FILES := $(foreach F, $(CORE_SRCS), $(addprefix $(dir $(F)),$(notdir $(wildcard $(LOCAL_PATH)/$(F))))) + +LOCAL_STATIC_LIBRARIES := eepp + +include $(BUILD_SHARED_LIBRARY) +#************ external_shader ************ + +#************* full_test ************* +include $(CLEAR_VARS) + +LOCAL_PATH := $(MY_PATH) + +LOCAL_MODULE := eetest + +LOCAL_LDLIBS := $(MY_LDLIBS) + +LOCAL_CFLAGS := $(MY_C_FLAGS) + +LOCAL_C_INCLUDES := $(MY_C_INCLUDES) + CORE_SRCS := \ $(MY_SDL_MAIN_PATH) \ ../test/*.cpp diff --git a/src/eepp/graphics/cshaderprogram.cpp b/src/eepp/graphics/cshaderprogram.cpp index 4633e789f..4e9ea5f09 100644 --- a/src/eepp/graphics/cshaderprogram.cpp +++ b/src/eepp/graphics/cshaderprogram.cpp @@ -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 +} + }} diff --git a/src/examples/external_shader/external_shader.cpp b/src/examples/external_shader/external_shader.cpp index 0ef244fe3..d2293440c 100644 --- a/src/examples/external_shader/external_shader.cpp +++ b/src/examples/external_shader/external_shader.cpp @@ -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 ( &vertices[0] ), 0 ); + /// ColorPointer to "dgl_FrontColor" + GLi->ColorPointer( 4, GL_FP, sizeof(eeColorAf), reinterpret_cast ( &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();