Drawing the Height Map

Now that we have the height map loaded in, let’s get it drawn to the screen. Create a new file called heightmap_vertex_shader.glsl in your /res/raw folder, and add the following code:

Heightmap/res/raw/heightmap_vertex_shader.glsl
 
uniform mat4 u_Matrix;
 
attribute vec3 a_Position;
 
varying vec3 v_Color;
 
 
void​ main()
 
{
 
v_Color = mix(vec3(0.180, 0.467, 0.153), ​// A dark green
 
vec3(0.660, 0.670, 0.680), ​// A stony gray
 
a_Position.y);
 
 
gl_Position = u_Matrix * vec4(a_Position, 1.0);
 
}

This vertex shader uses a new shader function, mix, to smoothly interpolate between two different colors. We set up our height map so that the height is between 0 and 1, and we use this height as the ratio between the two colors. ...

Get OpenGL ES 2 for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.