Panda3D
 All Classes Functions Variables Enumerations
stTerrain.cxx
1 // Filename: stTerrain.cxx
2 // Created by: drose (11Oct10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "stTerrain.h"
16 #include "indent.h"
17 
18 TypeHandle STTerrain::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: STTerrain::Constructor
22 // Access: Protected
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 STTerrain::
26 STTerrain() {
27  _is_valid = false;
28  _min_height = 0.0f;
29  _max_height = 1.0f;
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: STTerrain::Copy Constructor
34 // Access: Protected
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 STTerrain::
38 STTerrain(const STTerrain &copy) :
39  TypedReferenceCount(copy),
40  Namable(copy),
41  _is_valid(copy._is_valid),
42  _normal_map(copy._normal_map),
43  _splat_layers(copy._splat_layers),
44  _min_height(copy._min_height),
45  _max_height(copy._max_height)
46 {
47  set_vertex_format(copy._vertex_format);
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: STTerrain::Destructor
52 // Access: Published, Virtual
53 // Description:
54 ////////////////////////////////////////////////////////////////////
55 STTerrain::
56 ~STTerrain() {
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: STTerrain::clear
61 // Access: Published, Virtual
62 // Description: Resets the terrain to its initial, unloaded state.
63 ////////////////////////////////////////////////////////////////////
64 void STTerrain::
65 clear() {
66  _is_valid = false;
67  _min_height = 0.0f;
68  _max_height = 1.0f;
69 
70  _normal_map = "";
71  _splat_map = "";
72  _splat_layers.clear();
73 
74  set_vertex_format(NULL);
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: STTerrain::load_data
79 // Access: Published, Virtual
80 // Description: This will be called at some point after
81 // initialization. It should be overridden by a derived
82 // class to load up the terrain data from its source and
83 // fill in the data members of this class appropriately,
84 // especially _is_valid. After this call, if _is_valid
85 // is true, then get_height() etc. will be called to
86 // query the terrain's data.
87 ////////////////////////////////////////////////////////////////////
88 void STTerrain::
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: STTerrain::get_height
94 // Access: Published, Virtual
95 // Description: After load_data() has been called, this should return
96 // the computed height value at point (x, y) of the
97 // terrain, where x and y are unbounded and may refer to
98 // any 2-d point in space.
99 ////////////////////////////////////////////////////////////////////
100 PN_stdfloat STTerrain::
101 get_height(PN_stdfloat x, PN_stdfloat y) const {
102  return 0.0f;
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: STTerrain::get_smooth_height
107 // Access: Published, Virtual
108 // Description: After load_data() has been called, this should return
109 // the approximate average height value over a circle of
110 // the specified radius, centered at point (x, y) of the
111 // terrain.
112 ////////////////////////////////////////////////////////////////////
113 PN_stdfloat STTerrain::
114 get_smooth_height(PN_stdfloat x, PN_stdfloat y, PN_stdfloat radius) const {
115  return get_height(x, y);
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: STTerrain::get_slope
120 // Access: Published, Virtual
121 // Description: After load_data() has been called, this should return
122 // the directionless slope at point (x, y) of the
123 // terrain, where 0.0 is flat and 1.0 is vertical. This
124 // is used for determining the legal points to place
125 // trees and grass.
126 ////////////////////////////////////////////////////////////////////
127 PN_stdfloat STTerrain::
128 get_slope(PN_stdfloat x, PN_stdfloat y) const {
129  return 0.0f;
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: STTerrain::placement_is_acceptable
134 // Access: Published
135 // Description: Returns true if the elevation and slope of point (x,
136 // y) fall within the requested limits, false otherwise.
137 ////////////////////////////////////////////////////////////////////
138 bool STTerrain::
139 placement_is_acceptable(PN_stdfloat x, PN_stdfloat y,
140  PN_stdfloat height_min, PN_stdfloat height_max,
141  PN_stdfloat slope_min, PN_stdfloat slope_max) {
142  PN_stdfloat height = get_height(x, y);
143  if (height < height_min || height > height_max) {
144  return false;
145  }
146 
147  PN_stdfloat slope = get_slope(x, y);
148  if (slope < slope_min || slope > slope_max) {
149  return false;
150  }
151 
152  return true;
153 }
154 
155 ////////////////////////////////////////////////////////////////////
156 // Function: STTerrain::fill_vertices
157 // Access: Published, Virtual
158 // Description: After load_data() has been called, this will be
159 // called occasionally to populate the vertices for a
160 // terrain cell.
161 //
162 // It will be passed a GeomVertexData whose format will
163 // match get_vertex_format(), and already allocated with
164 // num_xy * num_xy rows. This method should fill the
165 // rows of the data with the appropriate vertex data for
166 // the terrain, over the grid described by the corners
167 // (start_x, start_y) up to and including (start_x +
168 // size_x, start_y + size_xy)--a square of the terrain
169 // with num_xy vertices on a side, arranged in row-major
170 // order.
171 ////////////////////////////////////////////////////////////////////
172 void STTerrain::
174  PN_stdfloat start_x, PN_stdfloat start_y,
175  PN_stdfloat size_xy, int num_xy) const {
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: STTerrain::output
180 // Access: Published, Virtual
181 // Description:
182 ////////////////////////////////////////////////////////////////////
183 void STTerrain::
184 output(ostream &out) const {
185  Namable::output(out);
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: STTerrain::write
190 // Access: Published, Virtual
191 // Description:
192 ////////////////////////////////////////////////////////////////////
193 void STTerrain::
194 write(ostream &out, int indent_level) const {
195  indent(out, indent_level)
196  << *this << "\n";
197 }
198 
199 ////////////////////////////////////////////////////////////////////
200 // Function: STTerrain::get_st_vertex_format
201 // Access: Public
202 // Description: Returns a pointer to the SpeedTree array of vertex
203 // attribs that defines the vertex format for SpeedTree.
204 ////////////////////////////////////////////////////////////////////
205 const SpeedTree::SVertexAttribDesc *STTerrain::
207  // return SpeedTree::std_vertex_format;
208  nassertr(!_st_vertex_attribs.empty(), NULL);
209 
210  return &_st_vertex_attribs[0];
211 }
212 
213 ////////////////////////////////////////////////////////////////////
214 // Function: STTerrain::set_vertex_format
215 // Access: Protected
216 // Description: Should be called in load_data() by a derived class to
217 // fill in the _vertex_format member. This will also
218 // compute and store the appropriate value for
219 // _st_vertex_attribs.
220 ////////////////////////////////////////////////////////////////////
221 bool STTerrain::
222 set_vertex_format(const GeomVertexFormat *format) {
223  if (format == NULL) {
224  _vertex_format = NULL;
225  _st_vertex_attribs.clear();
226  _is_valid = false;
227  return true;
228  }
229 
230  _vertex_format = GeomVertexFormat::register_format(format);
231  if (!convert_vertex_format(_st_vertex_attribs, _vertex_format)) {
232  _is_valid = false;
233  return false;
234  }
235 
236  return true;
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function: STTerrain::convert_vertex_format
241 // Access: Protected, Static
242 // Description: Populates the indicated st_vertex_attribs vector with
243 // an array of SpeedTree vertex attribute entries that
244 // corresponds to the requested format. Returns true on
245 // success, or false if the format cannot be represented
246 // in SpeedTree.
247 ////////////////////////////////////////////////////////////////////
248 bool STTerrain::
249 convert_vertex_format(STTerrain::VertexAttribs &st_vertex_attribs,
250  const GeomVertexFormat *format) {
251  st_vertex_attribs.clear();
252 
253  if (format->get_num_arrays() != 1) {
254  speedtree_cat.error()
255  << "Cannot represent multi-array vertex format in SpeedTree.\n";
256  return false;
257  }
258 
259  const GeomVertexArrayFormat *array = format->get_array(0);
260 
261  int num_columns = array->get_num_columns();
262  for (int ci = 0; ci < num_columns; ++ci) {
263  const GeomVertexColumn *column = array->get_column(ci);
264  st_vertex_attribs.push_back(SpeedTree::SVertexAttribDesc());
265  SpeedTree::SVertexAttribDesc &attrib = st_vertex_attribs.back();
266  if (!convert_vertex_column(attrib, column)) {
267  st_vertex_attribs.clear();
268  return false;
269  }
270  }
271 
272  st_vertex_attribs.push_back(SpeedTree::st_attrib_end);
273 
274  return true;
275 }
276 
277 ////////////////////////////////////////////////////////////////////
278 // Function: STTerrain::convert_vertex_column
279 // Access: Protected, Static
280 // Description: Converts the indicated vertex column definition to
281 // the corresponding SpeedTree::SVertexAttribDesc
282 // format. Returns true on success, false on failure.
283 ////////////////////////////////////////////////////////////////////
284 bool STTerrain::
285 convert_vertex_column(SpeedTree::SVertexAttribDesc &st_attrib,
286  const GeomVertexColumn *column) {
287  switch (column->get_numeric_type()) {
288  case GeomEnums::NT_stdfloat:
289  st_attrib.m_eDataType = SpeedTree::VERTEX_ATTRIB_TYPE_FLOAT;
290  break;
291 
292  default:
293  speedtree_cat.error()
294  << "Unsupported vertex numeric type for " << *column << "\n";
295  return false;
296  }
297 
298  st_attrib.m_uiNumElements = column->get_num_components();
299 
300  if (column->get_name() == InternalName::get_vertex()) {
301  st_attrib.m_eSemantic = SpeedTree::VERTEX_ATTRIB_SEMANTIC_POS;
302 
303  } else if (column->get_name() == InternalName::get_texcoord()) {
304  st_attrib.m_eSemantic = SpeedTree::VERTEX_ATTRIB_SEMANTIC_TEXCOORD0;
305 
306  } else {
307  speedtree_cat.error()
308  << "Unsupported vertex semantic name for " << *column << "\n";
309  return false;
310  }
311 
312  nassertr(st_attrib.SizeOfAttrib() == column->get_total_bytes(), false);
313 
314  return true;
315 }
316 
virtual void fill_vertices(GeomVertexData *data, PN_stdfloat start_x, PN_stdfloat start_y, PN_stdfloat size_xy, int num_xy) const
After load_data() has been called, this will be called occasionally to populate the vertices for a te...
Definition: stTerrain.cxx:173
int get_total_bytes() const
Returns the number of bytes used by each element of the column: component_bytes * num_components...
const InternalName * get_name() const
Returns the name of this particular data field, e.g.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This is the abstract base class that defines the interface needed to describe a terrain for rendering...
Definition: stTerrain.h:39
int get_num_components() const
Returns the number of components of the column: the number of instances of the NumericType in each el...
virtual PN_stdfloat get_smooth_height(PN_stdfloat x, PN_stdfloat y, PN_stdfloat radius) const
After load_data() has been called, this should return the approximate average height value over a cir...
Definition: stTerrain.cxx:114
This defines how a single column is interleaved within a vertex array stored within a Geom...
A base class for all things which can have a name.
Definition: namable.h:29
virtual PN_stdfloat get_slope(PN_stdfloat x, PN_stdfloat y) const
After load_data() has been called, this should return the directionless slope at point (x...
Definition: stTerrain.cxx:128
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
virtual void clear()
Resets the terrain to its initial, unloaded state.
Definition: stTerrain.cxx:65
void output(ostream &out) const
Outputs the Namable.
Definition: namable.I:97
NumericType get_numeric_type() const
Returns the token representing the numeric type of the data storage.
virtual PN_stdfloat get_height(PN_stdfloat x, PN_stdfloat y) const =0
After load_data() has been called, this should return the computed height value at point (x...
Definition: stTerrain.cxx:101
virtual void load_data()=0
This will be called at some point after initialization.
Definition: stTerrain.cxx:89
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
const SpeedTree::SVertexAttribDesc * get_st_vertex_format() const
Returns a pointer to the SpeedTree array of vertex attribs that defines the vertex format for SpeedTr...
Definition: stTerrain.cxx:206
bool placement_is_acceptable(PN_stdfloat x, PN_stdfloat y, PN_stdfloat height_min, PN_stdfloat height_max, PN_stdfloat slope_min, PN_stdfloat slope_max)
Returns true if the elevation and slope of point (x, y) fall within the requested limits...
Definition: stTerrain.cxx:139