title: 2d terrain generation using shader
body: <p>I am trying to render a random terrain using shader scripts</p>
  
  <h1>Create function..</h1>
  
  <pre><code>tex = new Texture(Gdx.files.internal("ground.png"));
  top = new Texture(Gdx.files.internal("top.png"));
  ShaderProgram.pedantic = false;
  shader = new ShaderProgram(VERT, FRAG);
  shader.begin();
  shader.setUniformi("u_top", 1);
  shader.end();
  top.bind(1);
  Gdx.gl.glActiveTexture(GL10.GL_TEXTURE0);
  batch = new SpriteBatch(1000, shader);
  batch.setShader(shader);
  </code></pre>
  
  <h1>Render function</h1>
  
  <pre><code>batch.begin();
  for (int i = 0; i &lt; 4; i++)
      batch.draw(tex, i * 256 * Initiate.getScale(), 0,
                          256 * Initiate.getScale(), 256 * Initiate.getScale());
  batch.end();
  </code></pre>
  
  <h1>Vertex shader</h1>
  
  <pre><code>attribute vec4 a_position;
  attribute vec4 a_color;
  attribute vec2 a_texCoord0;
  
  uniform mat4 u_projTrans;
  
  varying vec4 vColor;
  varying vec2 vTexCoord;
  
  void main()
  {
      vTexCoord = a_texCoord0;
      gl_Position =  u_projTrans * a_position;
  }
  </code></pre>
  
  <h1>Fragment shader</h1>
  
  <pre><code>#ifdef GL_ES
  #define LOWP lowp
  precision mediump float;
  #else
  #define LOWP 
  #endif
  
  varying LOWP vec4 vColor;
  varying vec2 vTexCoord;
  
  uniform sampler2D u_texture1;
  uniform sampler2D u_texture2;
  
  
  
   void main() 
      vec4 texColor ;
  //calculate top vertex 
      float clamp = 0.5 + slope*(sin(vTexCoord0.x*mfrq)/mfrq+sin(vTexCoord0.x*frq)/frq+sin(vTexCoord0.x*nfrq)/nfrq);
  //if larger the draw texture
      if(vTexCoord0.y &gt; clamp){
        texColor = texture2D(u_texture1, vTexCoord1);
      }
  
  
   // else map coordinate for top texture and draw 
          else{
          float tempy = 16.0*(vTexCoord0.y + 0.0625- clamp);
          texColor = texture2D(u_texture2, vec2 (vTexCoord1.x,tempy));
          }
          gl_FragColor = texColor;
  </code></pre>
  
  <h1>Output</h1>
  
  <p><img src="https://i.stack.imgur.com/qDXWe.png" alt="Image"></p>
  
  <h1>So the question is..</h1>
  
  <ul>
  <li>How do a add a texture at the top of this terrain? </li>
  <li>Is there any simple way of redering such a terrain?</li>
  </ul>