3es  0.7
3esmeshshape.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESMESHSHAPE_H_
5 #define _3ESMESHSHAPE_H_
6 
7 #include "3es-core.h"
8 #include "3esshape.h"
9 #include "3esmeshmessages.h"
10 
11 namespace tes
12 {
16  class _3es_coreAPI MeshShape : public Shape
17  {
18  protected:
20  MeshShape();
21 
22  public:
25  {
26  SDT_Vertices,
27  SDT_Indices,
28  SDT_Normals,
30  SDT_UniformNormal
31  };
32 
40  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
41  const V3Arg &position = V3Arg(0, 0, 0),
42  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
43  const V3Arg &scale = V3Arg(1, 1, 1));
44 
52  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
53  const unsigned *indices, unsigned indexCount,
54  const V3Arg &position = V3Arg(0, 0, 0),
55  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
56  const V3Arg &scale = V3Arg(1, 1, 1));
57 
66  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
67  uint32_t id,
68  const V3Arg &position = V3Arg(0, 0, 0),
69  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
70  const V3Arg &scale = V3Arg(1, 1, 1));
71 
80  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
81  const unsigned *indices, unsigned indexCount, uint32_t id,
82  const V3Arg &position = V3Arg(0, 0, 0),
83  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
84  const V3Arg &scale = V3Arg(1, 1, 1));
85 
95  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
96  uint32_t id, uint16_t category,
97  const V3Arg &position = V3Arg(0, 0, 0),
98  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
99  const V3Arg &scale = V3Arg(1, 1, 1));
100 
110  MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
111  const unsigned *indices, unsigned indexCount,
112  uint32_t id, uint16_t category,
113  const V3Arg &position = V3Arg(0, 0, 0),
114  const QuaternionArg &rotation = QuaternionArg(0, 0, 0, 1),
115  const V3Arg &scale = V3Arg(1, 1, 1));
116 
118  ~MeshShape();
119 
121  inline bool isComplex() const override { return true; }
122 
124  bool calculateNormals() const;
127  MeshShape &setCalculateNormals(bool calculate);
128 
140  MeshShape &setNormals(const float *normals, size_t normalByteSize);
141 
146  MeshShape &setUniformNormal(const Vector3f &normal);
147 
157  MeshShape &expandVertices();
158 
159  inline unsigned vertexCount() const { return _vertexCount; }
160  inline const float *vertices() const { return _vertices; }
162  inline size_t vertexStride() const { return _vertexStride; }
163  inline size_t vertexByteStride() const { return _vertexStride * sizeof(float); }
164  inline const float *normals() const { return _normals; }
165  inline size_t normalsStride() const { return _normalsStride; }
166  inline size_t normalsByteStride() const { return _normalsStride * sizeof(float); }
167  inline size_t normalsCount() const { return _normalsCount; }
168  inline DrawType drawType() const { return _drawType; }
169 
177  bool writeCreate(PacketWriter &stream) const override;
178  int writeData(PacketWriter &stream, unsigned &progressMarker) const override;
179 
182  Shape *clone() const override;
183 
184  protected:
185  void onClone(MeshShape *copy) const;
186 
187  float *allocateVertices(unsigned count);
188  void freeVertices(const float *&vertices);
189 
190  unsigned *allocateIndices(unsigned count);
191  void freeIndices(const unsigned *&indices);
192 
193  const float *_vertices;
194  unsigned _vertexStride;
195  unsigned _vertexCount;
196  const float *_normals;
197  unsigned _normalsStride;
198  unsigned _normalsCount;
199  const unsigned *_indices;
202  unsigned _indexCount;
205  bool _ownNormals;
206  };
207 
208 
210  : Shape(SIdMeshShape)
211  , _vertices(nullptr)
212  , _vertexStride(3)
213  , _vertexCount(0)
214  , _normals(nullptr)
215  , _normalsStride(3)
216  , _normalsCount(0)
217  , _indices(nullptr)
218  , _indexCount(0)
219  , _drawType(DtTriangles)
220  , _ownPointers(false)
221  , _ownNormals(false)
222  {
223  }
224 
225 
226  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
227  const V3Arg &position,
228  const QuaternionArg &rotation,
229  const V3Arg &scale)
230  : Shape(SIdMeshShape)
231  , _vertices(vertices)
232  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
233  , _vertexCount(vertexCount)
234  , _normals(nullptr)
235  , _normalsStride(3)
236  , _normalsCount(0)
237  , _indices(nullptr)
238  , _indexCount(0)
239  , _drawType(drawType)
240  , _ownPointers(false)
241  , _ownNormals(false)
242  {
243  setPosition(position);
244  setRotation(rotation);
245  setScale(scale);
246  }
247 
248 
249  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
250  const unsigned *indices, unsigned indexCount,
251  const V3Arg &position,
252  const QuaternionArg &rotation,
253  const V3Arg &scale)
254  : Shape(SIdMeshShape)
255  , _vertices(vertices)
256  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
257  , _vertexCount(vertexCount)
258  , _normals(nullptr)
259  , _normalsStride(3)
260  , _normalsCount(0)
261  , _indices(indices)
262  , _indexCount(indexCount)
263  , _drawType(drawType)
264  , _ownPointers(false)
265  , _ownNormals(false)
266  {
267  setPosition(position);
268  setRotation(rotation);
269  setScale(scale);
270  }
271 
272 
273  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
274  uint32_t id,
275  const V3Arg &position,
276  const QuaternionArg &rotation,
277  const V3Arg &scale)
278  : Shape(SIdMeshShape, id)
279  , _vertices(vertices)
280  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
281  , _vertexCount(vertexCount)
282  , _normals(nullptr)
283  , _normalsStride(3)
284  , _normalsCount(0)
285  , _indices(nullptr)
286  , _indexCount(0)
287  , _drawType(drawType)
288  , _ownPointers(false)
289  , _ownNormals(false)
290  {
291  setPosition(position);
292  setRotation(rotation);
293  setScale(scale);
294  }
295 
296 
297  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
298  const unsigned *indices, unsigned indexCount,
299  uint32_t id,
300  const V3Arg &position,
301  const QuaternionArg &rotation,
302  const V3Arg &scale)
303  : Shape(SIdMeshShape, id)
304  , _vertices(vertices)
305  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
306  , _vertexCount(vertexCount)
307  , _normals(nullptr)
308  , _normalsStride(3)
309  , _normalsCount(0)
310  , _indices(indices)
311  , _indexCount(indexCount)
312  , _drawType(drawType)
313  , _ownPointers(false)
314  , _ownNormals(false)
315  {
316  setPosition(position);
317  setRotation(rotation);
318  setScale(scale);
319  }
320 
321 
322  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
323  uint32_t id, uint16_t category,
324  const V3Arg &position,
325  const QuaternionArg &rotation,
326  const V3Arg &scale)
327  : Shape(SIdMeshShape, id, category)
328  , _vertices(vertices)
329  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
330  , _vertexCount(vertexCount)
331  , _normals(nullptr)
332  , _normalsStride(3)
333  , _normalsCount(0)
334  , _indices(nullptr)
335  , _indexCount(0)
336  , _drawType(drawType)
337  , _ownPointers(false)
338  , _ownNormals(false)
339  {
340  setPosition(position);
341  setRotation(rotation);
342  setScale(scale);
343  }
344 
345 
346  inline MeshShape::MeshShape(DrawType drawType, const float *vertices, unsigned vertexCount, size_t vertexByteSize,
347  const unsigned *indices, unsigned indexCount,
348  uint32_t id, uint16_t category,
349  const V3Arg &position,
350  const QuaternionArg &rotation,
351  const V3Arg &scale)
352  : Shape(SIdMeshShape, id, category)
353  , _vertices(vertices)
354  , _vertexStride(unsigned(vertexByteSize / sizeof(float)))
355  , _vertexCount(vertexCount)
356  , _normals(nullptr)
357  , _normalsStride(3)
358  , _normalsCount(0)
359  , _indices(indices)
360  , _indexCount(indexCount)
361  , _drawType(drawType)
362  , _ownPointers(false)
363  , _ownNormals(false)
364  {
365  setPosition(position);
366  setRotation(rotation);
367  setScale(scale);
368  }
369 
370 
372  {
373  if (_ownPointers)
374  {
375  freeVertices(_vertices);
376  freeIndices(_indices);
377  }
378  if (_ownNormals)
379  {
380  freeVertices(_normals);
381  }
382  }
383 
384 
385  inline bool MeshShape::calculateNormals() const
386  {
387  return (_data.flags & MeshShapeCalculateNormals) != 0;
388  }
389 
390 
392  {
394  _data.flags |= MeshShapeCalculateNormals * !!calculate;
395  return *this;
396  }
397 }
398 
399 #endif // _3ESMESHSHAPE_H_
A base class for encapsulating a shape which is to be represented remotely.
Definition: 3esshape.h:39
const float * _vertices
Mesh vertices.
Definition: 3esmeshshape.h:193
~MeshShape()
Destructor.
Definition: 3esmeshshape.h:371
DrawType
Defines the primitives for a mesh.
Definition: 3esmeshmessages.h:135
MeshShape & setCalculateNormals(bool calculate)
Should normals be calculated for the mesh by the viewer?
Definition: 3esmeshshape.h:391
bool calculateNormals() const
Calculate vertex normals in the viewer?
Definition: 3esmeshshape.h:385
A Shape which uses vertices and indices to render.
Definition: 3esmeshshape.h:16
Definition: 3esbounds.h:13
const unsigned * _indices
Optional triangle indices.
Definition: 3esmeshshape.h:201
const float * _normals
Normals array, one per vertex.
Definition: 3esmeshshape.h:196
bool _ownPointers
Does this instance own its vertices and indices?
Definition: 3esmeshshape.h:204
DrawType _drawType
The primitive to render.
Definition: 3esmeshshape.h:203
Represents a vector in R3.
Definition: 3esvector3.h:14
A helper structure used to convert from float or double pointers to Vector3f arguments.
Definition: 3esv3arg.h:14
size_t vertexStride() const
Vertex stride in float elements.
Definition: 3esmeshshape.h:162
MeshShape()
Constructor for cloning.
Definition: 3esmeshshape.h:209
uint16_t flags
Flags controlling the appearance and creation of the object.
Definition: 3esmessages.h:406
bool _ownNormals
Does this instance own its normals? Always true if _ownPointers is true.
Definition: 3esmeshshape.h:205
unsigned _normalsCount
Number of _normals.
Definition: 3esmeshshape.h:198
bool isComplex() const override
Mark as complex to ensure writeData() is called.
Definition: 3esmeshshape.h:121
A helper structure used to convert from float or double pointers to Quaternionf arguments.
Definition: 3esquaternionarg.h:14
unsigned _normalsStride
Stride into _normals in float elements, not bytes.
Definition: 3esmeshshape.h:197
unsigned _vertexCount
Number of _vertices.
Definition: 3esmeshshape.h:195
unsigned _indexCount
Number of indices. Divide by 3 for the triangle count.
Definition: 3esmeshshape.h:202
A utility class for writing payload data to a PacketHeader.
Definition: 3espacketwriter.h:34
Calculate normals and rendering with lighting.
Definition: 3esmessages.h:132
SendDataType
Codes for writeData(). Note: normals must be sent before completing vertices and indices. Best done first.
Definition: 3esmeshshape.h:24
unsigned _vertexStride
Stride into _vertices in float elements, not bytes.
Definition: 3esmeshshape.h:194