OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shaders.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef SHADERS_H__
22 #define SHADERS_H__
23 
24 // Put shader sources here to avoid having to deal with asset loading.
25 
26 // A heightmap like this would also have a corresponding normal map.
27 // For simplicitly, this is ignored here.
28 // Normals could be computed on-the-fly by sampling neighboring vertices as well in the vertex shader.
29 
30 static const char vertex_shader_source[] =
31  "#version 300 es\n"
32  "layout(std140) uniform;\n"
33 
34  "uniform mediump sampler2DArray sHeightmap;\n"
35 
36  "uniform mat4 uViewProjection;\n"
37  "uniform vec3 uCameraPos;\n"
38  "uniform float uInvLevelSize[10]; // GL doesn't allow unsized array when accessed from non-constant.\n"
39 
40  "struct PerInstanceData\n"
41  "{\n"
42  " vec2 offset; // World-space offset in XZ plane.\n"
43  " vec2 texture_scale;\n"
44  " vec2 texture_offset; // Same as for world-space offset/scale, just for texture coordinates\n"
45  " float scale; // Scaling factor for vertex offsets (per-instance)\n"
46  " float level; // LOD-level to use when sampling heightmap\n"
47  "};\n"
48 
49  "uniform InstanceData\n"
50  "{\n"
51  " PerInstanceData instance[256];\n"
52  "};\n"
53 
54  "#define LOCATION_VERTEX 0\n"
55  "#define HEIGHTMAP_MIN -20.0 // Depends on the heightmap.\n"
56  "#define HEIGHTMAP_MAX 20.0\n"
57 
58  "layout(location = LOCATION_VERTEX) in vec2 aVertex;\n"
59  "out float vHeight;\n"
60  "out vec2 vLod;\n"
61  "out float vFog;\n"
62 
63  "void main()\n"
64  "{\n"
65  " vec2 local_offset = aVertex * instance[gl_InstanceID].scale;\n"
66  " vec2 pos = instance[gl_InstanceID].offset + local_offset;\n"
67 
68  " float level = instance[gl_InstanceID].level;\n"
69  " vec2 tex_offset = (aVertex + 0.5) * instance[gl_InstanceID].texture_scale; // 0.5 offset to sample mid-texel.\n"
70  " vec2 texcoord = instance[gl_InstanceID].texture_offset + tex_offset;\n"
71 
72  " vec2 heights = texture(sHeightmap, vec3(texcoord, level)).rg;\n"
73 
74  " // Find blending factors for heightmap. The detail level must not have any discontinuities or it shows as 'artifacts'.\n"
75  " vec2 dist = abs(pos - uCameraPos.xz) * uInvLevelSize[int(level)];\n"
76  " vec2 a = clamp((dist - 0.325) * 8.0, 0.0, 1.0);\n"
77  " float lod_factor = max(a.x, a.y);\n"
78  " float height = mix(heights.x, heights.y, lod_factor);\n"
79 
80  " height = clamp(height, HEIGHTMAP_MIN, HEIGHTMAP_MAX); // To ensure frustum culling assumptions are met.\n"
81 
82  " vec4 vert = vec4(pos.x, height, pos.y, 1.0);\n"
83 
84  " gl_Position = uViewProjection * vert;\n"
85  " vHeight = height;\n"
86  " vLod = vec2(level, lod_factor);\n"
87 
88  " vec3 dist_camera = uCameraPos - vert.xyz;\n"
89  " vFog = clamp(dot(dist_camera, dist_camera) / 250000.0, 0.0, 1.0); // Simple per-vertex fog.\n"
90  "}";
91 
92 static const char fragment_shader_source[] =
93  "#version 300 es\n"
94  "layout(std140) uniform;\n"
95  "precision highp float;\n"
96 
97  "out vec4 FragColor;\n"
98  "in float vHeight;\n"
99  "in vec2 vLod;\n"
100  "in float vFog;\n"
101 
102  "// Compress (-inf, +inf) to (0, 1).\n"
103  "float map_height(float h)\n"
104  "{\n"
105  " return 1.0 / (1.0 + exp(-h / 20.0));\n"
106  "}\n"
107 
108  "// Make the heightmap look somewhat cloudy and fluffy.\n"
109 
110  "void main()\n"
111  "{\n"
112  " vec3 color = vec3(1.2, 1.2, 1.0) * vec3(map_height(vHeight) + (vLod.x + vLod.y) * 0.1);\n"
113  " vec3 final_color = mix(color, vec3(0.5), vFog);\n"
114  " FragColor = vec4(final_color, 1.0);\n"
115  "}\n";
116 
117 #endif
118 
static const char vertex_shader_source[]
Definition: shaders.h:30
static const char fragment_shader_source[]
Definition: shaders.h:92