Panda3D
shaderTerrainMesh.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file shaderTerrainMesh.I
10  * @author tobspr
11  * @date 2016-02-16
12  */
13 
14 /**
15  * @brief Sets the heightfield texture
16  * @details This sets the heightfield texture. It should be 16bit
17  * single channel, and have a power-of-two resolution greater than 32.
18  * Common sizes are 2048x2048 or 4096x4096.
19  *
20  * You should call generate() after setting the heightfield.
21  *
22  * @param filename Heightfield texture
23  */
24 INLINE void ShaderTerrainMesh::set_heightfield(Texture* heightfield) {
25  MutexHolder holder(_lock);
26  _heightfield_tex = heightfield;
27 }
28 
29 /**
30  * @brief Returns the heightfield
31  * @details This returns the terrain heightfield, previously set with
32  * set_heightfield()
33  *
34  * @return Path to the heightfield
35  */
37  MutexHolder holder(_lock);
38  return _heightfield_tex;
39 }
40 
41 /**
42  * @brief Sets the chunk size
43  * @details This sets the chunk size of the terrain. A chunk is basically the
44  * smallest unit in LOD. If the chunk size is too small, the terrain will
45  * perform bad, since there will be way too many chunks. If the chunk size
46  * is too big, you will not get proper LOD, and might also get bad performance.
47  *
48  * For terrains of the size 4096x4096 or 8192x8192, a chunk size of 32 seems
49  * to produce good results. For smaller resolutions, you should try out a
50  * size of 16 or even 8 for very small terrains.
51  *
52  * The amount of chunks generated for the last level equals to
53  * (heightfield_size / chunk_size) ** 2. The chunk size has to be a power
54  * of two.
55  *
56  * @param chunk_size Size of the chunks, has to be a power of two
57  */
58 INLINE void ShaderTerrainMesh::set_chunk_size(size_t chunk_size) {
59  MutexHolder holder(_lock);
60  _chunk_size = chunk_size;
61 }
62 
63 /**
64  * @brief Returns the chunk size
65  * @details This returns the chunk size, previously set with set_chunk_size()
66  * @return Chunk size
67  */
68 INLINE size_t ShaderTerrainMesh::get_chunk_size() const {
69  MutexHolder holder(_lock);
70  return _chunk_size;
71 }
72 
73 /**
74  * @brief Sets whether to generate patches
75  * @details If this option is set to true, GeomPatches will be used instead of
76  * GeomTriangles. This is required when the terrain is used with tesselation
77  * shaders, since patches are required for tesselation, whereas triangles
78  * are required for regular rendering.
79  *
80  * If this option is set to true while not using a tesselation shader, the
81  * terrain will not get rendered, or even produce errors. The same applies
82  * when this is option is not set, but the terrain is used with tesselation
83  * shaders.
84  *
85  * @param generate_patches [description]
86  */
87 INLINE void ShaderTerrainMesh::set_generate_patches(bool generate_patches) {
88  MutexHolder holder(_lock);
89  _generate_patches = generate_patches;
90 }
91 
92 /**
93  * @brief Returns whether to generate patches
94  * @details This returns whether patches are generated, previously set with
95  * set_generate_patches()
96  *
97  * @return Whether to generate patches
98  */
99 INLINE bool ShaderTerrainMesh::get_generate_patches() const {
100  MutexHolder holder(_lock);
101  return _generate_patches;
102 }
103 
104 
105 /**
106  * @brief Sets the desired triangle width
107  * @details This sets the desired width a triangle should have in pixels.
108  * A value of 10.0 for example will make the terrain tesselate everything
109  * in a way that each triangle edge roughly is 10 pixels wide.
110  * Of course this will not always accurately match, however you can use this
111  * setting to control the LOD algorithm of the terrain.
112  *
113  * @param target_triangle_width Desired triangle width in pixels
114  */
115 INLINE void ShaderTerrainMesh::set_target_triangle_width(PN_stdfloat target_triangle_width) {
116  MutexHolder holder(_lock);
117  _target_triangle_width = target_triangle_width;
118 }
119 
120 /**
121  * @brief Returns the target triangle width
122  * @details This returns the target triangle width, previously set with
123  * ShaderTerrainMesh::set_target_triangle_width()
124  *
125  * @return Target triangle width
126  */
127 INLINE PN_stdfloat ShaderTerrainMesh::get_target_triangle_width() const {
128  MutexHolder holder(_lock);
129  return _target_triangle_width;
130 }
131 
132 
133 /**
134  * @brief Sets whether to enable terrain updates
135  * @details This flag controls whether the terrain should be updated. If this value
136  * is set to false, no updating of the terrain will happen. This can be useful
137  * to debug the culling algorithm used by the terrain.
138  *
139  * @param update_enabled Whether to update the terrain
140  */
141 INLINE void ShaderTerrainMesh::set_update_enabled(bool update_enabled) {
142  MutexHolder holder(_lock);
143  _update_enabled = update_enabled;
144 }
145 
146 /**
147  * @brief Returns whether the terrain is getting updated
148  * @details This returns whether the terrain is getting updates, previously set with
149  * set_update_enabled()
150  *
151  * @return Whether to update the terrain
152  */
153 INLINE bool ShaderTerrainMesh::get_update_enabled() const {
154  MutexHolder holder(_lock);
155  return _update_enabled;
156 }
157 
158 /**
159  * @brief Clears all children
160  * @details This clears all children on the chunk and sets them to NULL. This will
161  * effectively free all memory consumed by this chunk and its children.
162  */
163 INLINE void ShaderTerrainMesh::Chunk::clear_children() {
164  for (size_t i = 0; i < 4; ++i) {
165  delete children[i];
166  children[i] = nullptr;
167  }
168 }
169 
170 /**
171  * @brief Chunk constructor
172  * @details This constructs a new chunk, and sets all children to NULL.
173  */
174 INLINE ShaderTerrainMesh::Chunk::Chunk() {
175  for (size_t i = 0; i < 4; ++i)
176  children[i] = nullptr;
177 }
178 
179 /**
180  * @brief Chunk destructor
181  * @details This destructs the chunk, freeing all used resources
182  */
183 INLINE ShaderTerrainMesh::Chunk::~Chunk() {
184  clear_children();
185 }
186 
187 /**
188  * @see ShaderTerrainMesh::uv_to_world(LTexCoord)
189  */
190 INLINE LPoint3 ShaderTerrainMesh::uv_to_world(PN_stdfloat u, PN_stdfloat v) const {
191  return uv_to_world(LTexCoord(u, v));
192 }
set_target_triangle_width
Sets the desired triangle width.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
set_update_enabled
Sets whether to enable terrain updates.
set_chunk_size
Sets the chunk size.
get_target_triangle_width
Returns the target triangle width.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
Definition: mutexHolder.h:25
set_generate_patches
Sets whether to generate patches.
LPoint3 uv_to_world(const LTexCoord &coord) const
Transforms a texture coordinate to world space.
set_heightfield
Sets the heightfield texture.
get_generate_patches
Returns whether to generate patches.
get_chunk_size
Returns the chunk size.
get_update_enabled
Returns whether the terrain is getting updated.
get_heightfield
Returns the heightfield.