"Flag" Like Ripple

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

"Flag" Like Ripple

Postby kbarresi » Wed Feb 27, 2013 5:48 am

I'm trying to create a flag-like ripple effect, for a rectangular sprite. Eventually, I want it to move along with time (making it look like a flag waving in the wind), but for now, I just want to make it static until I figure everything out.

So my first idea was to change the Y component of each vertex depending on the X position, so I came up with this:

Syntax: [ Download ] [ Hide ]
Using cpp Syntax Highlighting
  1. public static String VERTEX =
  2.         "uniform mat4 " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + ";\n" +
  3.         "attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n" +
  4.         "attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_COLOR + ";\n" +
  5.         "attribute vec2 " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
  6.         "varying vec4 " + ShaderProgramConstants.VARYING_COLOR + ";\n" +
  7.         "varying vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +
  8.         "void main() {\n" +
  9.         "       float x = " + ShaderProgramConstants.ATTRIBUTE_POSITION + ".x;\n" +
  10.         "       float y = " + ShaderProgramConstants.ATTRIBUTE_POSITION + ".y;\n" +
  11.         "       float z = " + ShaderProgramConstants.ATTRIBUTE_POSITION + ".z;\n" +
  12.         "       y += 100.0 * sin(x * 200.0);\n" +
  13.         "       " + ShaderProgramConstants.VARYING_COLOR + " = " + ShaderProgramConstants.ATTRIBUTE_COLOR + ";\n" +
  14.         "       " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + " = " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
  15.         "       gl_Position = " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + " * vec4(x,y,z,1)" + ";\n" +
  16.         "}";
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


Basically, using sin with the X position to get a Y offset.

Then I realized that my Sprite only has 4 vertices...oops. I'm very new to shaders, and getting this far was a bit of a challenge ;)

Should I try doing this through a fragment shader instead? If so, can someone point me in the right direction, or give me some sample code that might help? Thanks!
Help save the penguins! --Penguin Panic
User avatar
kbarresi
 
Posts: 93
Joined: Fri Aug 03, 2012 3:31 pm

Return to ShaderPrograms

Who is online

Users browsing this forum: No registered users and 1 guest