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