OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mesh.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2015-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 MESH_HPP__
22 #define MESH_HPP__
23 
24 #include "common.hpp"
25 #include "vector_math.h"
26 #include <vector>
27 
28 class Mesh
29 {
30  public:
31  struct RenderInfo
32  {
33  // MVP for rendering.
35 
36  // Frustum planes for culling.
38 
39  // Texture size of heightmap.
41 
42  // The world space size for heightmap tiles.
44 
45  // The frequency scale of high-frequency normal-map.
47 
48  // Camera position.
50 
51  // Height and displacement texture.
53 
54  // Gradient and jacobian texture.
56 
57  // Normalmap texture.
59 
60  // Skydome for reflection and fog.
62 
63  // Viewport
64  unsigned vp_width;
65  unsigned vp_height;
66 
67  // Downsampling factor displacement map.
69  };
70 
71  virtual ~Mesh();
72  virtual void render(const RenderInfo &info) = 0;
73 
74  protected:
75  Mesh(const char *vs_shader, const char *fs_shader);
76  Mesh(const char *vs_shader, const char *tc_shader, const char *te_shader,
77  const char *geom_shader, const char *fs_shader);
78  void bind_textures(const RenderInfo &info);
79  GLuint prog = 0;
80  GLuint vao = 0;
81  GLuint vbo = 0;
82  GLuint ibo = 0;
83 };
84 
85 class MorphedGeoMipMapMesh : public Mesh
86 {
87  public:
90 
93 
94  void render(const RenderInfo &info) override;
95 
96  private:
97  void build_lod(unsigned lod);
98  void build_patches();
99  void init_lod_tex();
100  void init();
101 
102  void calculate_lods(const RenderInfo &info);
103 
104  // Per-instance data.
105  struct PatchData
106  {
107  // .xy = World space offset for patch. .zw = Local offset in heightmap grid for purposes of sampling lod texture.
109  // LOD factors for left, top, right and bottom edges.
111  // .x = Inner LOD.
113  // Padding to align on 64-byte cacheline.
115  };
116 
117  struct LODMesh
118  {
119  LODMesh() = default;
120  LODMesh(unsigned offset, unsigned elems)
121  : offset(offset), elems(elems)
122  {}
123 
124  // Offset in index buffer.
125  unsigned offset;
126  // Number of indices.
127  unsigned elems;
128  // Number of instances to draw this mesh.
129  unsigned instances;
130  void draw(GLuint ubo, unsigned ubo_offset);
131  };
132 
133  struct LOD
134  {
135  unsigned full_vbo;
137  };
138 
139  struct Patch
140  {
142  float lod;
143  bool visible;
144  };
145 
146  std::vector<LOD> lod_meshes;
147  std::vector<Patch> patches;
150 
151  static constexpr unsigned patch_size = 64;
152  // Do not use lowest "quad" LOD since it forces popping when switching between lod 5 and 6.
153  static constexpr unsigned lods = 6;
154  static constexpr float lod0_distance = 50.0f;
155  static constexpr unsigned blocks_x = 32;
156  static constexpr unsigned blocks_z = 32;
157 
158  // 16KiB UBO limit. If we have more instances, split a LOD in more draw calls.
159  static constexpr unsigned max_instances = (16 * 1024) / sizeof(PatchData);
160 
162  std::vector<float> lod_buffer;
163 
164  struct Vertex
165  {
166  Vertex(GLubyte x, GLubyte y) : x(x), y(y) {}
167  // Local offset in the patch.
168  GLubyte x, y;
169  // Rounding factors. If less than patch_size / 2, this is 1, otherwise 0.
171  // Lod weights to select correct LOD in vertex shader.
172  GLubyte lod_weight[4];
173  };
174 
175  std::vector<Vertex> vertices;
176  std::vector<GLushort> indices;
177 };
178 
179 class TessellatedMesh : public Mesh
180 {
181  public:
182  TessellatedMesh();
183  void render(const RenderInfo &info) override;
184 
185  private:
186  void init_vao();
187  unsigned num_vertices;
188 
189  static constexpr float patch_size = 32.0f;
190  static constexpr float lod0_distance = 50.0f;
191 
192  static constexpr unsigned blocks_x = 64;
193  static constexpr unsigned blocks_z = 64;
194 };
195 
196 // Simple bounding sphere implementation for MorphedGeoMipMapMesh.
198 {
200  {
201  this->center = vec4(center, 1.0f);
202  this->radius = vec_length(radius);
203  }
204 
205  bool test_frustum(const vec4 *frustum) const;
206 
208  float radius;
209 };
210 
211 #endif
212 
vec2 tile_extent
Definition: mesh.hpp:43
void init_lod_tex()
Definition: mesh.cpp:370
void draw(GLuint ubo, unsigned ubo_offset)
Definition: mesh.cpp:270
static constexpr float patch_size
Definition: mesh.hpp:189
unsigned vp_width
Definition: mesh.hpp:64
GLuint height_displacement
Definition: mesh.hpp:52
void init_vao()
Definition: mesh.cpp:440
LODMesh(unsigned offset, unsigned elems)
Definition: mesh.hpp:120
Definition: matrix.h:51
std::vector< Patch > patches
Definition: mesh.hpp:147
GLuint vao
Definition: mesh.hpp:80
Definition: matrix.h:28
std::vector< float > lod_buffer
Definition: mesh.hpp:162
GLenum GLuint GLintptr offset
Definition: gl2ext.h:629
vec4 center
Definition: mesh.hpp:207
BoundingSphere(vec3 center, vec3 radius)
Definition: mesh.hpp:199
static constexpr unsigned max_instances
Definition: mesh.hpp:159
std::vector< Vertex > vertices
Definition: mesh.hpp:175
static constexpr unsigned blocks_x
Definition: mesh.hpp:192
GLuint ibo
Definition: mesh.hpp:82
bool test_frustum(const vec4 *frustum) const
Definition: mesh.cpp:44
Definition: matrix.h:75
GLuint normal
Definition: mesh.hpp:58
static constexpr float lod0_distance
Definition: mesh.hpp:190
void build_lod(unsigned lod)
Definition: mesh.cpp:317
void calculate_lods(const RenderInfo &info)
Definition: mesh.cpp:158
float vec_length(const T &vec)
Definition: vector_math.h:298
MorphedGeoMipMapMesh & operator=(MorphedGeoMipMapMesh &&)=delete
void bind_textures(const RenderInfo &info)
Definition: mesh.cpp:134
vec3 cam_pos
Definition: mesh.hpp:49
unsigned displacement_downsample
Definition: mesh.hpp:68
void render(const RenderInfo &info) override
Definition: mesh.cpp:283
static constexpr unsigned blocks_z
Definition: mesh.hpp:156
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLuint gradient_jacobian
Definition: mesh.hpp:55
std::vector< GLushort > indices
Definition: mesh.hpp:176
virtual void render(const RenderInfo &info)=0
static constexpr unsigned patch_size
Definition: mesh.hpp:151
static constexpr float lod0_distance
Definition: mesh.hpp:154
GLuint skydome
Definition: mesh.hpp:61
GLuint prog
Definition: mesh.hpp:79
float radius
Definition: mesh.hpp:208
static constexpr unsigned blocks_x
Definition: mesh.hpp:155
static constexpr unsigned lods
Definition: mesh.hpp:153
vec2 normal_scale
Definition: mesh.hpp:46
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
Definition: matrix.h:104
GLuint vbo
Definition: mesh.hpp:81
unsigned num_vertices
Definition: mesh.hpp:187
unsigned vp_height
Definition: mesh.hpp:65
static constexpr unsigned blocks_z
Definition: mesh.hpp:193
virtual ~Mesh()
Definition: mesh.cpp:80
std::vector< LOD > lod_meshes
Definition: mesh.hpp:146
Mesh(const char *vs_shader, const char *fs_shader)
Definition: mesh.cpp:52
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
vec4 frustum[6]
Definition: mesh.hpp:37
GLint y
Definition: gl2ext.h:179
uvec2 fft_size
Definition: mesh.hpp:40
Vertex(GLubyte x, GLubyte y)
Definition: mesh.hpp:166
void render(const RenderInfo &info) override
Definition: mesh.cpp:475
void build_patches()
Definition: mesh.cpp:363