Panda3D
 All Classes Functions Variables Enumerations
heightfieldTesselator.h
1 // Filename: heightfieldTesselator.h
2 // Created by: jyelon (17jul06)
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 #ifndef HEIGHTFIELDTESSELATOR_H
16 #define HEIGHTFIELDTESSELATOR_H
17 
18 #include "pandabase.h"
19 
20 #include "luse.h"
21 #include "pandaNode.h"
22 #include "pointerTo.h"
23 #include "namable.h"
24 #include "pnmImage.h"
25 #include "geom.h"
26 #include "geomTristrips.h"
27 #include "geomTriangles.h"
28 #include "geomVertexWriter.h"
29 #include "geomVertexFormat.h"
30 #include "nodePath.h"
31 
32 ////////////////////////////////////////////////////////////////////
33 // Class : HeightfieldTesselator
34 // Description : Converts a height field in the form of a greyscale
35 // image into a scene consisting of a number of GeomNodes.
36 //
37 // The tesselation uses an LOD algorithm. You
38 // supply a "focal point" (X,Y) which tells the
39 // tesselator where the bulk of the detail should
40 // be concentrated. The intent is that as the player
41 // walks around the terrain, you should occasionally
42 // move the focal point to wherever the player is.
43 // You should not move the focal point every frame:
44 // tesselation is not that fast. Also, changing the
45 // focal point may cause popping, so it is best to
46 // minimize the number of changes. There are a number
47 // of parameters that you can use to control tesselation,
48 // such as a target polygon count, and a visibility
49 // radius.
50 //
51 // The heightfield needs to be a multiple of 128 pixels
52 // in each dimension. It does not need to be square,
53 // and it does not need to be a power of two. For
54 // example, a 384 x 640 heightfield is fine.
55 // Be aware that tesselation time is proportional to
56 // heightfield area, so if you plan to use a size larger
57 // than about 512x512, it may be desirable to benchmark.
58 //
59 // Altering parameters, such as the poly count, the
60 // view radius, or the focal point, does not alter any
61 // GeomNodes already generated. Parameter changes only
62 // affect subsequently-generated GeomNodes. It is
63 // possible to cache many different tesselations of the
64 // same terrain.
65 //
66 ////////////////////////////////////////////////////////////////////
67 
68 class EXPCL_PANDA_GRUTIL HeightfieldTesselator : public Namable {
69 PUBLISHED:
70  INLINE HeightfieldTesselator(const string &name);
71  INLINE ~HeightfieldTesselator();
72 
73  INLINE PNMImage &heightfield();
74  INLINE bool set_heightfield(const Filename &filename, PNMFileType *type = NULL);
75  INLINE void set_poly_count(int n);
76  INLINE void set_visibility_radius(int r);
77  INLINE void set_focal_point(int x, int y);
78  INLINE void set_horizontal_scale(double h);
79  INLINE void set_vertical_scale(double v);
80  INLINE void set_max_triangles(int n);
81 
82  double get_elevation(double x, double y);
83 
84  NodePath generate();
85 
86 private:
87 
88  // These are initialized during the first 'generate'
89  int _radii[16];
90  bool _radii_calculated;
91 
92  // These are only valid during the generate process.
93  int *_triangle_totals;
94  int *_vertex_index;
95  int *_dirty_vertices;
96 
97  // These are only valid when a geom is open.
98  int _next_index;
99  int _last_vertex_a;
100  int _last_vertex_b;
101  PT(GeomVertexData) _vdata;
102  GeomVertexWriter *_vertex_writer;
103  GeomVertexWriter *_normal_writer;
104  PT(GeomTriangles) _triangles;
105 
106  INLINE bool subdivide(int scale, int x, int y);
107  void calculate_radii(int scale);
108  void generate_square(NodePath root, int scale, int x, int y, bool forceclose);
109  int count_triangles(int scale, int x, int y);
110  int get_vertex(int x, int y);
111  void add_quad_to_strip(int v1, int v2, int v3, int v4);
112  void add_quad(int v1, int v2, int v3, int v4);
113  void fix_heightfield(int size);
114  void open_geom();
115  void close_geom(NodePath root);
116 
117  PNMImage _heightfield;
118  int _poly_count;
119  int _visibility_radius;
120  int _focal_x;
121  int _focal_y;
122  double _horizontal_scale;
123  double _vertical_scale;
124  int _max_triangles;
125 };
126 
127 #include "heightfieldTesselator.I"
128 
129 #endif
130 
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
A base class for all things which can have a name.
Definition: namable.h:29
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
Defines a series of disconnected triangles.
Definition: geomTriangles.h:25
Converts a height field in the form of a greyscale image into a scene consisting of a number of GeomN...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165