Panda3D
 All Classes Functions Variables Enumerations
physxHeightFieldDesc.cxx
1 // Filename: physxHeightFieldDesc.cxx
2 // Created by: enn0x (15Oct09)
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 "physxHeightFieldDesc.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: PhysxHeightFieldDesc::set_convex_edge_threshold
19 // Access: Published
20 // Description:
21 ////////////////////////////////////////////////////////////////////
22 void PhysxHeightFieldDesc::
23 set_convex_edge_threshold(float threshold) {
24 
25  _desc.convexEdgeThreshold = threshold;
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: PhysxHeightFieldDesc::set_thickness
30 // Access: Published
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 void PhysxHeightFieldDesc::
34 set_thickness(float thickness) {
35 
36  _desc.thickness = thickness;
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: PhysxHeightFieldDesc::set_image
41 // Access: Published
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void PhysxHeightFieldDesc::
45 set_image(const PNMImage &image, unsigned short materialIndex) {
46 
47  NxU32 _32K = 32767; // (1<<15)-1;
48  NxU32 _64K = 65535; // (1<<16)-1;
49 
50  NxU32 nbRows = image.get_x_size();
51  NxU32 nbColumns = image.get_y_size();
52 
53  set_size(nbRows, nbColumns);
54 
55  NxU8 *currentByte = (NxU8 *)(_desc.samples);
56 
57  for (NxU32 row=0; row < nbRows; row++) {
58  for (NxU32 column=0; column < nbColumns; column++) {
59 
60  NxHeightFieldSample* currentSample = (NxHeightFieldSample *)currentByte;
61 
62  NxReal fvalue = image.get_bright(row, column);
63  NxI16 ivalue = (NxI16)((fvalue - 0.5f) * _32K);
64 
65  currentSample->height = (NxI16)ivalue;
66  currentSample->tessFlag = (NxU8)0;
67  currentSample->materialIndex0 = (NxU8)materialIndex;
68  currentSample->materialIndex1 = (NxU8)materialIndex;
69 
70  currentByte += _desc.sampleStride;
71  }
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: PhysxHeightFieldDesc::set_material_index
77 // Access: Published
78 // Description:
79 ////////////////////////////////////////////////////////////////////
80 void PhysxHeightFieldDesc::
81 set_material_index(unsigned int row, unsigned int column, unsigned short materialIndex0, unsigned short materialIndex1) {
82 
83  nassertv(_desc.samples);
84  nassertv(row < _desc.nbRows);
85  nassertv(column < _desc.nbColumns);
86 
87  NxU32 idx = row * _desc.nbColumns + column;
88  NxU8 *byte = (NxU8 *)_desc.samples + _desc.sampleStride * idx;
89  NxHeightFieldSample* sample = (NxHeightFieldSample *)byte;
90 
91  //NxHeightFieldSample* sample = ((NxHeightFieldSample *)_desc.samples) + idx;
92 
93  sample->materialIndex0 = (NxU8)materialIndex0;
94  sample->materialIndex1 = (NxU8)materialIndex1;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: PhysxHeightFieldDesc::set_height
99 // Access: Published
100 // Description:
101 ////////////////////////////////////////////////////////////////////
102 void PhysxHeightFieldDesc::
103 set_height(unsigned int row, unsigned int column, short height) {
104 
105  nassertv(_desc.samples);
106  nassertv(row < _desc.nbRows);
107  nassertv(column < _desc.nbColumns);
108 
109  NxU32 idx = row * _desc.nbColumns + column;
110  NxU8 *byte = (NxU8 *)_desc.samples + _desc.sampleStride * idx;
111  NxHeightFieldSample* sample = (NxHeightFieldSample *)byte;
112 
113  sample->height = (NxI16)height;
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: PhysxHeightFieldDesc::set_tess_flag
118 // Access: Published
119 // Description:
120 ////////////////////////////////////////////////////////////////////
121 void PhysxHeightFieldDesc::
122 set_tess_flag(unsigned int row, unsigned int column, unsigned short value) {
123 
124  nassertv(_desc.samples);
125  nassertv(row < _desc.nbRows);
126  nassertv(column < _desc.nbColumns);
127 
128  NxU32 idx = row * _desc.nbColumns + column;
129  NxU8 *byte = (NxU8 *)_desc.samples + _desc.sampleStride * idx;
130  NxHeightFieldSample* sample = (NxHeightFieldSample *)byte;
131 
132  nassertv(value < 2);
133  sample->tessFlag = (NxU8)value;
134 }
135 
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
float get_bright(int x, int y) const
Returns the linear brightness of the given xel, as a linearized float in the range 0...
Definition: pnmImage.I:1020
int get_x_size() const
Returns the number of pixels in the X direction.
int get_y_size() const
Returns the number of pixels in the Y direction.