Different shaders for different instances of primitives

  ... discussions regarding GLES2 ShaderPrograms (VertexShaders and FragmentShaders).

Different shaders for different instances of primitives

Postby kentsusai » Wed Feb 29, 2012 1:15 pm

Hi, has anyone done this yet?
kentsusai
 
Posts: 6
Joined: Sat Feb 25, 2012 3:25 am

Re: Different shaders for different instances of primitives

Postby CaptnIgnit » Thu Mar 01, 2012 9:42 am

Can you give an example of what you mean? If I understand what you are asking, then you should be able to call setShaderProgram() on each instance and pass in a new ShaderProgram.
CaptnIgnit
 
Posts: 87
Joined: Tue Feb 01, 2011 5:10 am

Re: Different shaders for different instances of primitives

Postby kentsusai » Thu Mar 01, 2012 11:00 am

Hi :)

Here you go ;)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ....
  2. Rectangle box1 = new Rectangle(0f, 0f, 10f, 10f, this.getVertexBufferObjectManager()) {
  3.  
  4.             private boolean mRenderTextureInitialized;
  5.             private RenderTexture mRenderTexture;
  6.             private UncoloredSprite mRenderTextureSprite;
  7.            
  8.             //static values for testing purposes    
  9.             int surfaceWidth = 10;
  10.             int surfaceHeight = 10;
  11.            
  12.             RenderTexture mRenderTextureBox2 = new RenderTexture(MainActivity.this.getTextureManager(), surfaceWidth, surfaceHeight);
  13.  
  14.             @Override
  15.             protected void preDraw(final GLState pGLState, final Camera pCamera) {
  16.                 this.setShaderProgram(SampleShaderProgram2.getInstance());
  17.                 super.preDraw(pGLState, pCamera);
  18.  
  19.             }
  20.  
  21.             @Override
  22.             protected void draw(final GLState pGLState, final Camera pCamera) {
  23.                 final boolean firstFrame = !mRenderTextureInitialized;
  24.  
  25.                 if (firstFrame) {
  26.                     this.initRenderTextures(pGLState);
  27.                     mRenderTextureInitialized = true;
  28.                 }
  29.  
  30.                 mRenderTexture.begin(pGLState);
  31.                 {
  32.                     /*
  33.                      * Draw current frame.
  34.                      */
  35.                     super.draw(pGLState, pCamera);
  36.                 }
  37.                 mRenderTexture.end(pGLState);
  38.  
  39.                 /*
  40.                  * Draw rendered texture with custom shader.
  41.                  */
  42.                 {
  43.                     pGLState.pushProjectionGLMatrix();
  44.                     pGLState.orthoProjectionGLMatrixf(0, surfaceWidth, 0, surfaceHeight, -1, 1);
  45.                     {
  46.                         mRenderTextureSprite.onDraw(pGLState, pCamera);
  47.                     }
  48.                     pGLState.popProjectionGLMatrix();
  49.                 }
  50.             }
  51.  
  52.             private void initRenderTextures(final GLState pGLState) {
  53.  
  54.                 mRenderTexture = new RenderTexture(MainActivity.this.getTextureManager(), surfaceWidth, surfaceHeight);
  55.                 mRenderTexture.init(pGLState);
  56.  
  57.                 final ITextureRegion renderTextureTextureRegion = TextureRegionFactory.extractFromTexture(mRenderTexture);
  58.                 mRenderTextureSprite = new UncoloredSprite(0, 0, renderTextureTextureRegion, this.getVertexBufferObjectManager()) {
  59.  
  60.                     @Override
  61.                     protected void preDraw(final GLState pGLState, final Camera pCamera) {
  62.                         if (MainActivity.this.customShaders) {
  63.                             this.setShaderProgram(SampleShaderProgram2.getInstance());
  64.                         } else {
  65.                             this.setShaderProgram(PositionTextureCoordinatesShaderProgram.getInstance());
  66.                         }
  67.                         super.preDraw(pGLState, pCamera);
  68.  
  69.                     }
  70.                 };
  71.             }
  72.  
  73.         };
  74.  
  75. box1.setShaderProgram(SampleShaderProgram2.getInstance());
  76.  
  77. mScene.attachChild(box1);
  78. ....
  79.  
  80. public static class SampleShaderProgram2 extends ShaderProgram {
  81.  
  82.         private boolean mRenderTextureInitialized;
  83.         private RenderTexture mRenderTexture;
  84.         private UncoloredSprite mRenderTextureSprite;
  85.         private static SampleShaderProgram2 instance;
  86.         Random r = new Random();
  87.  
  88.         public static SampleShaderProgram2 getInstance() {
  89.             if (instance == null) {
  90.                 instance = new SampleShaderProgram2();
  91.             }
  92.             return instance;
  93.         }
  94.         public static final String DEFAULT_VERTEXSHADER = "uniform mat4 " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + ";\n"
  95.                 + "attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n"
  96.                 + "void main()        \n"
  97.                 + "{  \n"
  98.                 + "   gl_Position = " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + " * " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n"
  99.                 + "}";
  100.         public static final String DEFAULT_FRAGMENTSHADER = "precision mediump float;           \n"
  101.                 + "uniform vec4 " + ShaderProgramConstants.UNIFORM_COLOR + ";             \n"
  102.                 + "void main()                                        \n"
  103.                 + "{                                                  \n"
  104.                 + "   gl_FragColor = " + ShaderProgramConstants.UNIFORM_COLOR + ";        \n"
  105.                 //+ "   gl_FragColor = " + "vec4(1.0, 0.0, 0.0, 1.0)" + ";        \n"
  106.                 + "}";
  107.  
  108.         private SampleShaderProgram2() {
  109.             super(DEFAULT_VERTEXSHADER, DEFAULT_FRAGMENTSHADER);
  110.         }
  111.         public static int sUniformModelViewPositionMatrixLocation = ShaderProgram.LOCATION_INVALID;
  112.         public static int sUniformTexture0Location = ShaderProgram.LOCATION_INVALID;
  113.         public static int sUniformColorLocation = ShaderProgram.LOCATION_INVALID;
  114.  
  115.         @Override
  116.         protected void link(final GLState pGLState) throws ShaderProgramLinkException {
  117.             GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION);
  118.             //GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR);
  119.             //GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES_LOCATION, ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES);
  120.  
  121.             super.link(pGLState);
  122.  
  123.             SampleShaderProgram2.sUniformModelViewPositionMatrixLocation = this.getUniformLocation(ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX);
  124.             SampleShaderProgram2.sUniformColorLocation = this.getUniformLocation(ShaderProgramConstants.UNIFORM_COLOR);
  125.             //SampleShaderProgram2.sUniformTexture0Location = this.getUniformLocation(ShaderProgramConstants.UNIFORM_TEXTURE_0);
  126.  
  127.         }
  128.  
  129.         @Override
  130.         public void bind(final GLState pGLState, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
  131.             //GLES20.glDisableVertexAttribArray(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION);
  132.  
  133.             super.bind(pGLState, pVertexBufferObjectAttributes);
  134.  
  135.             GLES20.glUniformMatrix4fv(SampleShaderProgram2.sUniformModelViewPositionMatrixLocation, 1, false, pGLState.getModelViewProjectionGLMatrix(), 0);
  136.             //GLES20.glUniform1i(SampleShaderProgram2.sUniformTexture0Location, 0);
  137.             GLES20.glUniform4f(SampleShaderProgram2.sUniformColorLocation, r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat());
  138.         }
  139.  
  140.         @Override
  141.         public void unbind(final GLState pGLState) throws ShaderProgramException {
  142.             //GLES20.glEnableVertexAttribArray(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION);
  143.  
  144.             super.unbind(pGLState);
  145.         }
  146.     }
  147.  
  148.  
  149.  
  150.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4
kentsusai
 
Posts: 6
Joined: Sat Feb 25, 2012 3:25 am

Re: Different shaders for different instances of primitives

Postby CaptnIgnit » Thu Mar 01, 2012 9:32 pm

So it looks like you have a flag variable setup, customShader, that determines if you use your shader or a default shader.

You shouldn't need to override all those rectangle calls if thats all you're trying to do.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (customShaders) {
  2.     box1.setShaderProgram(SampleShaderProgram2.getInstance());
  3. } else {
  4.     box1.setShaderProgram(PositionTextureCoordinatesShaderProgram.getInstance());
  5. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


should do the same thing, or you may simply be able to just call box1.setShaderProgram() instead of using the flag variable.
CaptnIgnit
 
Posts: 87
Joined: Tue Feb 01, 2011 5:10 am

Re: Different shaders for different instances of primitives

Postby kentsusai » Fri Mar 02, 2012 12:36 pm

originally that's what i was doing but with no avail

I'll give it another go this weekend and let you know how I go
kentsusai
 
Posts: 6
Joined: Sat Feb 25, 2012 3:25 am


Return to ShaderPrograms

Who is online

Users browsing this forum: No registered users and 3 guests