Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
DaeHelpers.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef DaHelpers_INCLUDE_ONCE
33 #define DaHelpers_INCLUDE_ONCE
34 
35 // needs to be but here because the awesome geniouses that programmed collada-dom 2.3 cleverly decided to use the same macro names as OpenGL, see domTypes.h
36 #ifdef _MSC_VER
37  #pragma warning(disable:4100)
38  #pragma warning(disable:4355)
39  #pragma warning(disable:4512)
40 #endif
41  #include <dae.h>
42  #include <dae/domAny.h>
43  #include <dom.h>
44  #include <dom/domCOLLADA.h>
45  #include <dom/domProfile_COMMON.h>
46 #ifdef _MSC_VER
47  #pragma warning(default:4100)
48  #pragma warning(default:4355)
49  #pragma warning(default:4512)
50 #endif
51 
52 #include <vlGraphics/Actor.hpp>
53 #include <vlGraphics/Geometry.hpp>
54 #include <vlGraphics/Effect.hpp>
55 #include <vlGraphics/Shader.hpp>
56 #include <vlGraphics/Light.hpp>
58 #include <vlCore/glsl_math.hpp>
59 
60 namespace vl
61 {
62  namespace Dae
63  {
64  //-----------------------------------------------------------------------------
66  //-----------------------------------------------------------------------------
67  typedef enum { OM_A_ONE, OM_RGB_ZERO } EOpaqueMode;
68  //-----------------------------------------------------------------------------
69  typedef enum
70  {
96  //-----------------------------------------------------------------------------
98  struct Vert
99  {
100  static const int MAX_ATTRIBS = 8;
101 
103  {
104  memset(mAttribIndex, 0xFF, sizeof(mAttribIndex));
105  mIndex = (size_t)-1;
106  }
107 
108  bool operator<(const Dae::Vert& other) const
109  {
110  for(int i=0; i<MAX_ATTRIBS; ++i)
111  {
112  if (mAttribIndex[i] != other.mAttribIndex[i])
113  return mAttribIndex[i] < other.mAttribIndex[i];
114  }
115  return false;
116  }
117 
119  size_t mIndex;
120  };
121  //-----------------------------------------------------------------------------
123  struct Source: public Object
124  {
126  {
127  mFloatSource = NULL;
128  mIntSource = NULL;
129  mBoolSource = NULL;
130  mFieldsMask = 0;
131  mStride = 0;
132  mOffset = 0;
133  mCount = 0;
134  mDataSize = 0;
135  }
136 
138  void init(domFloat_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
139  {
140  mFloatSource = data_src;
141  mIntSource = NULL;
142  mBoolSource = NULL;
143  mCount = (size_t)count;
144  mStride = (size_t)stride;
145  mOffset = (size_t)offset;
146  mFieldsMask = fields_mask;
147 
148  // count the number of scalars that will be read.
149  mDataSize = 0;
150  for(size_t i=0; i<32; ++i)
151  if (mFieldsMask & (1<<i))
152  mDataSize++;
153  }
154 
156  void init(domInt_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
157  {
158  mFloatSource = NULL;
159  mIntSource = data_src;
160  mBoolSource = NULL;
161  mCount = (size_t)count;
162  mStride = (size_t)stride;
163  mOffset = (size_t)offset;
164  mFieldsMask = fields_mask;
165 
166  // count the number of scalars that will be read.
167  mDataSize = 0;
168  for(size_t i=0; i<32; ++i)
169  if (mFieldsMask & (1<<i))
170  mDataSize++;
171  }
172 
174  void init(domBool_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
175  {
176  mFloatSource = NULL;
177  mIntSource = NULL;
178  mBoolSource = data_src;
179  mCount = (size_t)count;
180  mStride = (size_t)stride;
181  mOffset = (size_t)offset;
182  mFieldsMask = fields_mask;
183 
184  // count the number of scalars that will be read.
185  mDataSize = 0;
186  for(size_t i=0; i<32; ++i)
187  if (mFieldsMask & (1<<i))
188  mDataSize++;
189  }
190 
192  void readData(size_t n, float* output)
193  {
194  size_t read_pos = mOffset + n * mStride;
195 
196  size_t pos = 0;
197 
198  if(mFloatSource)
199  {
200  VL_CHECK( (n < mCount) || (read_pos < mFloatSource->getValue().getCount() - mDataSize) )
201  for(size_t i=0; i<32 && i<mStride; ++i)
202  if (mFieldsMask & (1<<i))
203  output[pos++] = (float)mFloatSource->getValue()[read_pos+i];
204  }
205  else
206  if(mIntSource)
207  {
208  VL_CHECK( (n < mCount) || (read_pos < mIntSource->getValue().getCount() - mDataSize) )
209  for(size_t i=0; i<32 && i<mStride; ++i)
210  if (mFieldsMask & (1<<i))
211  output[pos++] = (float)mIntSource->getValue()[read_pos+i];
212  }
213  else
214  if(mBoolSource)
215  {
216  VL_CHECK( (n < mCount) || (read_pos < mBoolSource->getValue().getCount() - mDataSize) )
217  for(size_t i=0; i<32 && i<mStride; ++i)
218  if (mFieldsMask & (1<<i))
219  output[pos++] = (float)mBoolSource->getValue()[read_pos+i];
220  }
221  }
222 
224  size_t count() const { return mCount; }
225 
227  size_t dataSize() const { return mDataSize; }
228 
229  protected:
230  size_t mFieldsMask;
231  size_t mDataSize;
232  domFloat_arrayRef mFloatSource;
233  domInt_arrayRef mIntSource;
234  domBool_arrayRef mBoolSource;
235  size_t mStride;
236  size_t mOffset;
237  size_t mCount;
238  };
239  //-----------------------------------------------------------------------------
241  struct Input: public Object
242  {
244  {
245  mSemantic = Dae::IS_UNKNOWN;
246  mOffset = 0;
247  mSet = 0;
248  }
249 
252  size_t mOffset;
253  size_t mSet;
254  };
255  //-----------------------------------------------------------------------------
257  struct Primitive: public Object
258  {
260  {
261  mType = Dae::PT_UNKNOWN;
262  mCount = 0;
263  mIndexStride = 0;
264  }
265 
267  std::string mMaterial;
268  std::vector< ref<Dae::Input> > mChannels;
269  size_t mCount;
270  std::vector<domPRef> mP;
271  size_t mIndexStride;
273  };
274  //-----------------------------------------------------------------------------
276  struct Mesh: public Object
277  {
278  std::vector< ref<Dae::Input> > mVertexInputs;
279  std::vector< ref<Dae::Primitive> > mPrimitives;
280  };
281  //-----------------------------------------------------------------------------
283  struct Node: public Object
284  {
286  {
287  mTransform = new Transform;
288  }
289 
291  std::vector< ref<Dae::Node> > mChildren;
292  std::vector< ref<Dae::Mesh> > mMesh;
293  std::vector< ref<Actor> > mActors;
294  };
295  //-----------------------------------------------------------------------------
297  struct Surface: public Object
298  {
299  // mic fixme: for the moment we only support 2D images.
300  ref<Image> mImage; // <init_from>
301  };
302  //-----------------------------------------------------------------------------
304  struct Sampler2D: public Object
305  {
307  {
308  // default values if no tags are found.
309  mMinFilter = TPF_LINEAR;
310  mMagFilter = TPF_LINEAR;
311  mWrapS = TPW_REPEAT;
312  mWrapT = TPW_REPEAT;
313  }
314 
320 
321  ref<Texture> mTexture; // actual VL texture implementing the sampler
322  };
323  //-----------------------------------------------------------------------------
325  struct NewParam: public Object
326  {
330  };
331  //-----------------------------------------------------------------------------
333  struct ColorOrTexture: public Object
334  {
337  std::string mTexCoord;
338  };
339  //-----------------------------------------------------------------------------
341  struct TechniqueCOMMON: public Object
342  {
344  {
345  mMode = Unknown;
346 
347  mEmission.mColor = fvec4(0, 0, 0, 1);
348  mAmbient.mColor = fvec4(0, 0, 0, 1);
349  mDiffuse.mColor = fvec4(1, 0, 1, 1);
350  mSpecular.mColor = fvec4(0, 0, 0, 1);
351  mShininess = 40;
352 
353  mReflective.mColor = fvec4(1, 1, 1, 1);
354  mReflectivity = 0;
355 
356  mTransparent.mColor = fvec4(0, 0, 0, 1);
357  mOpaqueMode = Dae::OM_A_ONE;
358  mTransparency = 1;
359 
360  mIndexOfRefraction = 0;
361 
362  mBlendingOn = false;
363  }
364 
365  enum { Unknown, Blinn, Phong, Lambert } mMode;
366 
368 
373  float mShininess;
380  };
381  //-----------------------------------------------------------------------------
383  struct Effect: public Object
384  {
386  {
387  mDoubleSided = false;
388  }
389 
390  std::vector<ref<Dae::NewParam> > mNewParams;
393  };
394  //-----------------------------------------------------------------------------
396  struct Material: public Object
397  {
399  };
400  //-----------------------------------------------------------------------------
401  }
402 }
403 
404 #endif
void init(domBool_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
Initializes an accessor. An accessor can read only up to 32 floats.
Definition: DaeHelpers.hpp:174
COLLADA sampler2D.
Definition: DaeHelpers.hpp:304
COLLADA common technique.
Definition: DaeHelpers.hpp:341
size_t mAttribIndex[MAX_ATTRIBS]
Definition: DaeHelpers.hpp:118
ref< Dae::Surface > mDaeSurface
Definition: DaeHelpers.hpp:315
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
ref< Dae::TechniqueCOMMON > mDaeTechniqueCOMMON
Definition: DaeHelpers.hpp:391
std::vector< ref< Dae::Primitive > > mPrimitives
Definition: DaeHelpers.hpp:279
Vector4< float > fvec4
A 4 components vector with float precision.
Definition: Vector4.hpp:279
ref< Image > mImage
Definition: DaeHelpers.hpp:300
ETexParamFilter
std::vector< ref< Dae::Input > > mVertexInputs
Definition: DaeHelpers.hpp:278
Dae::ColorOrTexture mSpecular
Definition: DaeHelpers.hpp:372
COLLADA data source.
Definition: DaeHelpers.hpp:123
vl::ETexParamWrap mWrapT
Definition: DaeHelpers.hpp:319
Dae::ColorOrTexture mReflective
Definition: DaeHelpers.hpp:374
COLLADA node.
Definition: DaeHelpers.hpp:283
std::vector< ref< Dae::Node > > mChildren
Definition: DaeHelpers.hpp:291
COLLADA material.
Definition: DaeHelpers.hpp:396
std::vector< domPRef > mP
Definition: DaeHelpers.hpp:270
vl::ETexParamWrap mWrapS
Definition: DaeHelpers.hpp:318
Dae::ColorOrTexture mDiffuse
Definition: DaeHelpers.hpp:371
COLLADA mesh.
Definition: DaeHelpers.hpp:276
ref< Geometry > mGeometry
Definition: DaeHelpers.hpp:272
std::vector< ref< Dae::NewParam > > mNewParams
Definition: DaeHelpers.hpp:390
ref< Dae::Surface > mDaeSurface
Definition: DaeHelpers.hpp:328
Visualization Library main namespace.
ref< Dae::Effect > mDaeEffect
Definition: DaeHelpers.hpp:398
COLLADA color or texture input.
Definition: DaeHelpers.hpp:333
COLLADA primitive.
Definition: DaeHelpers.hpp:257
Dae::EPrimitiveType mType
Definition: DaeHelpers.hpp:266
Dae::ColorOrTexture mEmission
Definition: DaeHelpers.hpp:369
The base class for all the reference counted objects.
Definition: Object.hpp:158
ref< Dae::Sampler2D > mSampler
Definition: DaeHelpers.hpp:336
void readData(size_t n, float *output)
Reads an element of data at the n-th position and writes it into &#39;output&#39;. The number of elements tha...
Definition: DaeHelpers.hpp:192
COLLADA input stream.
Definition: DaeHelpers.hpp:241
Implements the OpenGL Shading Language convenience functions for scalar and vector operations...
domInt_arrayRef mIntSource
Definition: DaeHelpers.hpp:233
void init(domFloat_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
Initializes an accessor. An accessor can read only up to 32 floats.
Definition: DaeHelpers.hpp:138
Dae::EInputSemantic mSemantic
Definition: DaeHelpers.hpp:251
bool operator<(const Dae::Vert &other) const
Definition: DaeHelpers.hpp:108
std::vector< ref< Dae::Input > > mChannels
Definition: DaeHelpers.hpp:268
size_t dataSize() const
The number of elements written by readData().
Definition: DaeHelpers.hpp:227
vl::ETexParamFilter mMinFilter
Definition: DaeHelpers.hpp:316
#define NULL
Definition: OpenGLDefs.hpp:81
std::vector< ref< Dae::Mesh > > mMesh
Definition: DaeHelpers.hpp:292
size_t count() const
The number of elements in the source.
Definition: DaeHelpers.hpp:224
ref< Texture > mTexture
Definition: DaeHelpers.hpp:321
Dae::ColorOrTexture mTransparent
Definition: DaeHelpers.hpp:376
ETexParamWrap
ref< Transform > mTransform
Definition: DaeHelpers.hpp:290
vl::ETexParamFilter mMagFilter
Definition: DaeHelpers.hpp:317
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
COLLADA effect.
Definition: DaeHelpers.hpp:383
Dae::ColorOrTexture mAmbient
Definition: DaeHelpers.hpp:370
static const int MAX_ATTRIBS
Definition: DaeHelpers.hpp:100
COLLADA surface.
Definition: DaeHelpers.hpp:297
domFloat_arrayRef mFloatSource
Definition: DaeHelpers.hpp:232
void init(domInt_arrayRef data_src, domUint count, domUint stride, domUint offset, size_t fields_mask)
Initializes an accessor. An accessor can read only up to 32 floats.
Definition: DaeHelpers.hpp:156
std::vector< ref< Actor > > mActors
Definition: DaeHelpers.hpp:293
ref< Dae::Sampler2D > mDaeSampler2D
Definition: DaeHelpers.hpp:327
#define VL_CHECK(expr)
Definition: checks.hpp:73
Dae::EOpaqueMode mOpaqueMode
Definition: DaeHelpers.hpp:367
ref< Dae::Source > mSource
Definition: DaeHelpers.hpp:250
std::string mMaterial
Definition: DaeHelpers.hpp:267
COLLADA newparam.
Definition: DaeHelpers.hpp:325
COLLADA vertex.
Definition: DaeHelpers.hpp:98
domBool_arrayRef mBoolSource
Definition: DaeHelpers.hpp:234