OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GroundMesh.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 GROUND_MESH_H__
22 #define GROUND_MESH_H__
23 
24 #include <vector>
25 #include <GLES3/gl3.h>
26 #include <stddef.h>
27 #include "vector_math.h"
28 #include "Frustum.h"
29 
31 {
32 public:
33  GroundMesh(unsigned int size, unsigned int levels, float clip_scale);
34  ~GroundMesh();
35 
36  void set_frustum(const Frustum& frustum) { view_proj_frustum = frustum; }
37  void update_level_offsets(const vec2& camera_pos);
38  const std::vector<vec2>& get_level_offsets() const { return level_offsets; }
39 
40  void render();
41 
42 private:
44  unsigned int size;
45  unsigned int level_size;
46  unsigned int levels;
48  size_t num_indices;
49 
51 
52  struct Block
53  {
54  size_t offset;
55  size_t count;
57  };
58 
59  struct InstanceData
60  {
61  vec2 offset; // Offset of the block in XZ plane (world space). This is prescaled.
62  vec2 texture_scale; // Scale factor of local offsets (vertex coordinates) translated into texture coordinates.
63  vec2 texture_offset; // Offset for texture coordinates, similar to offset. Also prescaled.
64  float scale; // Scale factor of local offsets (vertex coordinates).
65  float level; // Clipmap LOD level of block.
66  };
67 
80 
81  void setup_vertex_buffer(unsigned int size);
82  void setup_index_buffer(unsigned int size);
83  void setup_block_ranges(unsigned int size);
84  void setup_uniform_buffer();
85  void setup_vertex_array();
86 
87  void update_draw_list();
88  void render_draw_list();
89  struct DrawInfo
90  {
93  unsigned int indices;
94  unsigned int instances;
95  };
96  std::vector<DrawInfo> draw_list;
98 
99  std::vector<vec2> level_offsets;
100 
101  typedef bool (*TrimConditional)(const vec2& offset);
102 
103  vec2 get_offset_level(const vec2& camera_pos, unsigned int level);
104  void update_draw_list(DrawInfo& info, size_t& ubo_offset);
108  DrawInfo get_draw_info_degenerate(InstanceData *instance_data, const Block& block, const vec2& offset, const vec2& ring_offset);
114  DrawInfo get_draw_info_trim(InstanceData *instance_data, const Block& block, TrimConditional cond);
119 
120  bool intersects_frustum(const vec2& offset, const vec2& range, unsigned int level);
121 
123 };
124 
125 #endif
DrawInfo get_draw_info_vert_fixup(InstanceData *instance_data)
Definition: GroundMesh.cpp:160
vec2 get_offset_level(const vec2 &camera_pos, unsigned int level)
[Snapping clipmap level to a grid]
Definition: GroundMesh.cpp:56
void setup_vertex_buffer(unsigned int size)
void setup_block_ranges(unsigned int size)
[Generating index buffer]
Block trim_full
Definition: GroundMesh.h:71
void set_frustum(const Frustum &frustum)
Definition: GroundMesh.h:36
unsigned int levels
Definition: GroundMesh.h:46
Block horizontal
Definition: GroundMesh.h:70
unsigned int indices
Definition: GroundMesh.h:93
GLsizei range
Definition: gl2ext.h:2467
DrawInfo get_draw_info_degenerate_top(InstanceData *instance_data)
Definition: GroundMesh.cpp:251
GLint uniform_buffer_align
Definition: GroundMesh.h:97
DrawInfo get_draw_info_trim_bottom_left(InstanceData *instance_data)
Definition: GroundMesh.cpp:365
GLuint vertex_array
Definition: GroundMesh.h:43
GLuint index_buffer
Definition: GroundMesh.h:43
Definition: matrix.h:28
GLenum GLuint GLintptr offset
Definition: gl2ext.h:629
Frustum view_proj_frustum
Definition: GroundMesh.h:122
DrawInfo get_draw_info_degenerate_bottom(InstanceData *instance_data)
Definition: GroundMesh.cpp:256
Block degenerate_right
Definition: GroundMesh.h:78
void setup_uniform_buffer()
float clipmap_scale
Definition: GroundMesh.h:50
DrawInfo get_draw_info_trim_top_left(InstanceData *instance_data)
Definition: GroundMesh.cpp:355
size_t uniform_buffer_offset
Definition: GroundMesh.h:92
bool(* TrimConditional)(const vec2 &offset)
Definition: GroundMesh.h:101
DrawInfo get_draw_info_degenerate_left(InstanceData *instance_data)
Definition: GroundMesh.cpp:241
DrawInfo get_draw_info_horiz_fixup(InstanceData *instance_data)
Definition: GroundMesh.cpp:107
GLuint uniform_buffer
Definition: GroundMesh.h:43
void update_level_offsets(const vec2 &camera_pos)
[Snapping clipmap level to a grid]
Definition: GroundMesh.cpp:81
DrawInfo get_draw_info_degenerate_right(InstanceData *instance_data)
Definition: GroundMesh.cpp:246
DrawInfo get_draw_info_trim(InstanceData *instance_data, const Block &block, TrimConditional cond)
Definition: GroundMesh.cpp:287
DrawInfo get_draw_info_degenerate(InstanceData *instance_data, const Block &block, const vec2 &offset, const vec2 &ring_offset)
Definition: GroundMesh.cpp:204
Block trim_top_right
Definition: GroundMesh.h:72
void update_draw_list()
Definition: GroundMesh.cpp:482
Block block
Definition: GroundMesh.h:68
Block trim_bottom_left
Definition: GroundMesh.h:74
size_t index_buffer_offset
Definition: GroundMesh.h:91
Block trim_top_left
Definition: GroundMesh.h:75
DrawInfo get_draw_info_trim_top_right(InstanceData *instance_data)
Definition: GroundMesh.cpp:350
void setup_index_buffer(unsigned int size)
GroundMesh(unsigned int size, unsigned int levels, float clip_scale)
Definition: GroundMesh.cpp:31
Block degenerate_top
Definition: GroundMesh.h:77
void render()
[Rendering the entire terrain]
Definition: GroundMesh.cpp:575
DrawInfo get_draw_info_blocks(InstanceData *instance_data)
Definition: GroundMesh.cpp:371
unsigned int level_size
Definition: GroundMesh.h:45
size_t num_indices
Definition: GroundMesh.h:48
GLsizei levels
Definition: gl2ext.h:1816
void setup_vertex_array()
std::vector< DrawInfo > draw_list
Definition: GroundMesh.h:96
const std::vector< vec2 > & get_level_offsets() const
Definition: GroundMesh.h:38
GLenum GLuint GLintptr GLsizeiptr size
Definition: gl2ext.h:629
std::vector< vec2 > level_offsets
Definition: GroundMesh.h:99
void render_draw_list()
[Rendering the entire terrain]
Definition: GroundMesh.cpp:556
GLenum GLuint GLint level
Definition: gl2ext.h:385
Block trim_bottom_right
Definition: GroundMesh.h:73
bool intersects_frustum(const vec2 &offset, const vec2 &range, unsigned int level)
Definition: GroundMesh.cpp:444
Block degenerate_left
Definition: GroundMesh.h:76
GLuint vertex_buffer
Definition: GroundMesh.h:43
unsigned int instances
Definition: GroundMesh.h:94
Block degenerate_bottom
Definition: GroundMesh.h:79
unsigned int size
Definition: GroundMesh.h:44
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
size_t uniform_buffer_size
Definition: GroundMesh.h:47
DrawInfo get_draw_info_trim_full(InstanceData *instance_data)
Definition: GroundMesh.cpp:262
Block vertical
Definition: GroundMesh.h:69
DrawInfo get_draw_info_trim_bottom_right(InstanceData *instance_data)
Definition: GroundMesh.cpp:360