Mondschein Engine  0.3.0
types.h
Go to the documentation of this file.
1 #ifndef TYPES_H_INCLUDED
2 #define TYPES_H_INCLUDED
3 
4 /* Mondschein Engine is a fast, powerful, and easy-to-use 3D realtime rendering engine.
5  *
6  * Copyright (C) 2009-2013 by Andreas Amereller
7  * E-Mail: andreas.amereller.dev@googlemail.com
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 3 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
24 
25 #include "eigen3.h"
26 
27 #include <boost/weak_ptr.hpp>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <boost/exception/all.hpp>
30 #include <boost/graph/adjacency_list.hpp>
31 
32 #include <cstdint>
33 #include <typeinfo>
34 #include <list>
35 #include <string>
36 #include <vector>
37 #include <map>
38 #include <array>
39 
40 #define TO_RAD 3.141592654/180
41 #define TO_DEG 180/3.141592654
42 #define PI 3.141592654
43 
44 #ifdef DEBUG
45 #include <iostream>
46 #endif
47 
55 namespace mondschein
56 {
58  typedef uint8_t uint8;
59 
61  typedef uint16_t uint16;
62 
64  typedef uint32_t uint32;
65 
67  typedef uint64_t uint64;
68 
70  typedef int8_t int8;
71 
73  typedef int16_t int16;
74 
76  typedef int32_t int32;
77 
79  typedef int64_t int64;
80 
82  typedef float_t float32;
83 
85  typedef double_t float64;
86 
88  struct exception : virtual public boost::exception, virtual public std::exception {};
89 
91  typedef boost::error_info<struct tag_exceprion_error,std::string> exception_error;
92 
104  {
105  ONN,ONL,OLN,OLL,NNN,NNL,NLN,NLL,LNN,LNL,LLN,LLL
106  };
107 
114  namespace display
115  {
118  {
127  inline visual_attribs_t() : width(0), height(0), bpp(0), fullscreen(false) { return; };
128  inline visual_attribs_t(uint32 _w,uint32 _h,uint32 _bpp, bool _fs) :
129  width(_w), height(_h), bpp(_bpp), fullscreen(_fs) { return; };
130  };
131 
132  class Visual;
133  typedef boost::shared_ptr<Visual> Visual_p;
134  typedef boost::shared_ptr<const Visual> Visual_c;
135  }
136 
142  namespace scene
143  {
144  class Scenegraph;
145  typedef boost::shared_ptr<Scenegraph> Scenegraph_p;
146  typedef boost::shared_ptr<const Scenegraph> Scenegraph_c;
147 
148  class Scenenode;
149  typedef boost::shared_ptr<Scenenode> Scenenode_p;
150  typedef boost::shared_ptr<const Scenenode> Scenenode_c;
151 
152  class Camera;
153  typedef boost::shared_ptr<Camera> Camera_p;
154  typedef boost::shared_ptr<const Camera> Camera_c;
155 
156  class Light;
157  typedef boost::shared_ptr<Light> Light_p;
158  typedef boost::shared_ptr<const Light> Light_c;
159 
160  class Material;
161  typedef boost::shared_ptr<Material> Material_p;
162  typedef boost::shared_ptr<const Material> Material_c;
163 
164  class Mesh;
165  typedef boost::shared_ptr<Mesh> Mesh_p;
166  typedef boost::shared_ptr<const Mesh> Mesh_c;
167 
168  class Pose;
169  typedef boost::shared_ptr<Pose> Pose_p;
170  typedef boost::shared_ptr<const Pose> Pose_c;
171 
172  class Texture;
173  typedef boost::shared_ptr<Texture> Texture_p;
174  typedef boost::shared_ptr<const Texture> Texture_c;
175 
178  {
195  inline camera_attribs_t() : fovy(60.0), aspect(16.0/9.0), near(1.0), far(100.0), x(0), y(0), w(640), h(360) { return; };
196  inline camera_attribs_t(float64 _fovy,float64 _aspect,float64 _near,float64 _far,uint32 _x,uint32 _y,uint32 _w,uint32 _h) :
197  fovy(_fovy), aspect(_aspect), near(_near), far(_far), x(_x), y(_y), w(_w), h(_h) { return; };
198  };
199 
202  {
204  Eigen::Vector4d ambient;
206  Eigen::Vector4d diffuse;
208  Eigen::Vector4d specular;
210  Eigen::Vector4d position;
212  Eigen::Vector4d direction;
225  inline light_attribs_t() : ambient(Eigen::Vector4d(0.0,0.0,0.0,1.0)), diffuse(Eigen::Vector4d(1.0,1.0,1.0,1.0)),
226  specular(Eigen::Vector4d(0.0,0.0,0.0,1.0)), position(Eigen::Vector4d(0.0,0.0,0.0,1.0)),
227  direction(Eigen::Vector4d(0.0,-1.0,0.0,1.0)), exponent(1.0), cutoff(180.0), constant_attenuation(1.0),
228  linear_attenuation(0.0), quadratic_attenuation(0.0), unit(0) { return; };
229  inline light_attribs_t(const Eigen::Vector4d &_ambient,const Eigen::Vector4d &_diffuse,const Eigen::Vector4d _specular,
230  const Eigen::Vector4d &_position,const Eigen::Vector4d &_direction,float64 _exponent,float64 _cutoff,
231  float64 _constant_attenuation,float64 _linear_attenuation,float64 _quadratic_attenuation,uint8 _unit) :
232  ambient(_ambient), diffuse(_diffuse), specular(_specular), position(_position), direction(_direction),
233  exponent(_exponent), cutoff(_cutoff), constant_attenuation(_constant_attenuation),
234  linear_attenuation(_linear_attenuation), quadratic_attenuation(_quadratic_attenuation), unit(_unit) { return; };
235  };
236 
239  {
241  Eigen::Vector4d ambient;
243  Eigen::Vector4d diffuse;
245  Eigen::Vector4d specular;
247  Eigen::Vector4d emission;
250  inline material_attribs_t() : ambient(Eigen::Vector4d(1.0,1.0,1.0,1.0)), diffuse(Eigen::Vector4d(1.0,1.0,1.0,1.0)),
251  specular(Eigen::Vector4d(1.0,1.0,1.0,1.0)), emission(Eigen::Vector4d(0.0,0.0,0.0,1.0)), shininess(0.0) { return; };
252  inline material_attribs_t(const Eigen::Vector4d &_ambient,const Eigen::Vector4d &_diffuse,const Eigen::Vector4d _specular,
253  const Eigen::Vector4d &_emission,float64 _shininess) :
254  ambient(_ambient), diffuse(_diffuse), specular(_specular), emission(_emission), shininess(_shininess) { return; };
255  };
256 
259  {
261  std::vector<Eigen::Vector4d> v;
263  std::vector<Eigen::Vector3d> vn;
265  std::vector<Eigen::Vector4d> vt;
267  std::vector<Eigen::Vector4d> vc;
268  inline mesh_attribs_t() : v(), vn(), vt(), vc() { return; };
269  inline mesh_attribs_t(const std::vector<Eigen::Vector4d> &_v,const std::vector<Eigen::Vector3d> &_vn,
270  const std::vector<Eigen::Vector4d> &_vt,const std::vector<Eigen::Vector4d> &_vc) :
271  v(_v), vn(_vn), vt(_vt), vc(_vc) { return; };
272  };
273 
276  {
284  bool rgb;
292  std::vector<uint8> data;
293  inline texture_attribs_t() : width(0), height(0), bpp(0), rgb(true), texture_filter(ONN), anisotropic_filter(0), unit(0),
294  data() { return; };
295  inline texture_attribs_t(uint32 _width,uint32 _height,uint8 _bpp,bool _rgb,texture_filtering_e _texture_filter,
296  uint8 _anisotropic_filter,uint8 _unit,const std::vector<uint8> &_data) :
297  width(_width), height(_height), bpp(_bpp), rgb(_rgb), texture_filter(_texture_filter),
298  anisotropic_filter(_anisotropic_filter), unit(_unit), data(_data) { return; };
299  };
300 
303  {
305  std::vector<scene::Scenenode_c> nodes;
307  boost::adjacency_list<> edges;
308  inline scenegraph_attribs_t() : nodes(), edges() {return;}
309  inline scenegraph_attribs_t(const std::vector<scene::Scenenode_c> &_nodes,const boost::adjacency_list<> &_edges) :
310  nodes(_nodes), edges(_edges) { return; };
311  };
312  }
313 
321  namespace math
322  {
333  {
334  FLAT,
335  FLAT_INV,
336  INTERMEDIATE,
337  INTERMEDIATE_INV
338  };
339 
340  class Beziercurve;
341  typedef boost::shared_ptr<Beziercurve> Beziercurve_p;
342  typedef boost::shared_ptr<const Beziercurve> Beziercurve_c;
343 
344  class Bezierpatch;
345  typedef boost::shared_ptr<Bezierpatch> Bezierpatch_p;
346  typedef boost::shared_ptr<const Bezierpatch> Bezierpatch_c;
347 
348  class NURBScurve;
349  typedef boost::shared_ptr<NURBScurve> NURBScurve_p;
350  typedef boost::shared_ptr<const NURBScurve> NURBScurve_c;
351 
352  class NURBSpatch;
353  typedef boost::shared_ptr<NURBSpatch> NURBSpatch_p;
354  typedef boost::shared_ptr<const NURBSpatch> NURBSpatch_c;
355 
356  class Normal_Grid;
357  typedef boost::shared_ptr<Normal_Grid> Normal_Grid_p;
358  typedef boost::shared_ptr<const Normal_Grid> Normal_Grid_c;
359 
360  class Pol_Int;
361  typedef boost::shared_ptr<Pol_Int> Pol_Int_p;
362  typedef boost::shared_ptr<const Pol_Int> Pol_Int_c;
363 
364  class Aitken_Neville;
365  typedef boost::shared_ptr<Aitken_Neville> Aitken_Neville_p;
366  typedef boost::shared_ptr<const Aitken_Neville> Aitken_Neville_c;
367  }
368 
374  namespace io
375  {
378  {
380  std::map<std::string,scene::Scenenode_p> nodes;
382  std::map<std::string,scene::Scenegraph_p> scenegraphs;
384  std::map<std::string,math::Beziercurve_p> beziercurves;
386  std::map<std::string,math::Bezierpatch_p> bezierpatches;
388  std::map<std::string,math::NURBScurve_p> nurbscurves;
390  std::map<std::string,math::NURBSpatch_p> nurbspatches;
392  { return; };
393  inline io_objects_t(const std::map<std::string,scene::Scenenode_p> &_nodes,
394  const std::map<std::string,scene::Scenegraph_p> &_scenegraphs,
395  const std::map<std::string,math::Beziercurve_p> &_beziercurves,
396  const std::map<std::string,math::Bezierpatch_p> &_bezierpatches,
397  const std::map<std::string,math::NURBScurve_p> &_nurbscurves,
398  const std::map<std::string,math::NURBSpatch_p> &_nurbspatches) :
399  nodes(_nodes), scenegraphs(_scenegraphs), beziercurves(_beziercurves), bezierpatches(_bezierpatches),
400  nurbscurves(_nurbscurves), nurbspatches(_nurbspatches) { return; };
401  };
402 
403  class Buffer;
404  typedef boost::shared_ptr<Buffer> Buffer_p;
405  typedef boost::shared_ptr<const Buffer> Buffer_c;
406  }
407 
414  namespace renderer
415  {
416  class Renderer;
417  typedef boost::shared_ptr<Renderer> Renderer_p;
418  typedef boost::shared_ptr<const Renderer> Renderer_c;
419 
420  class Context;
421  typedef boost::shared_ptr<Context> Context_p;
422  typedef boost::shared_ptr<const Context> Context_c;
423 
424  class Renderable;
425  typedef boost::shared_ptr<Renderable> Renderable_p;
426  typedef boost::shared_ptr<const Renderable> Renderable_c;
427 
430  {
432  Eigen::Vector4i color_depth;
434  Eigen::Vector4i accum_depth;
441  inline context_attribs_t() : color_depth(Eigen::Vector4i(0,0,0,0)), accum_depth(Eigen::Vector4i(0,0,0,0)),
442  depth_buffer_size(0), stencil_buffer_size(0), doublebuffer(false) { return; };
443  inline context_attribs_t(const Eigen::Vector4i &_color_depth,const Eigen::Vector4i &_accum_depth,
444  int32 _depth_buffer_size,int32 _stencil_buffer_size,bool _doublebuffer) :
445  color_depth(_color_depth), accum_depth(_accum_depth), depth_buffer_size(_depth_buffer_size),
446  stencil_buffer_size(_stencil_buffer_size), doublebuffer(_doublebuffer) { return; };
447  };
448  }
449 
458  class Core
459  {
460  protected:
461  explicit Core();
462  virtual ~Core();
463 
464  public:
472  static scene::Camera_p create_camera();
496  static scene::Camera_p create_camera(const std::string &_id,float64 _v,float64 _a,float64 _n,float64 _f,
497  float64 _x,float64 _y,float64 _w,float64 _h);
507  static scene::Camera_p create_camera(scene::Camera_c _c);
508 
516  static scene::Light_p create_light();
546  static scene::Light_p create_light(const std::string &_id,const Eigen::Vector4d &_a,const Eigen::Vector4d &_d,
547  const Eigen::Vector4d &_s,const Eigen::Vector4d &_p,const Eigen::Vector4d &_sd,
548  float64 _se,float64 _sc,float64 _ca,float64 _la,float64 _qa,uint8 _l);
558  static scene::Light_p create_light(scene::Light_c _l);
559 
567  static scene::Material_p create_material();
585  static scene::Material_p create_material(const std::string &_id,const Eigen::Vector4d &_a,const Eigen::Vector4d &_d,
586  const Eigen::Vector4d &_s,const Eigen::Vector4d &_e,float64 _sh);
596  static scene::Material_p create_material(scene::Material_c _m);
597 
605  static scene::Mesh_p create_mesh();
621  static scene::Mesh_p create_mesh(const std::string &_id,const std::vector<Eigen::Vector4d> &_v,
622  const std::vector<Eigen::Vector3d> &_vn,const std::vector<Eigen::Vector4d> &_vt,
623  const std::vector<Eigen::Vector4d> &_vc);
633  static scene::Mesh_p create_mesh(scene::Mesh_c _m);
634 
642  static scene::Pose_p create_pose();
652  static scene::Pose_p create_pose(const std::string &_id,const Eigen::Matrix4d &_m);
662  static scene::Pose_p create_pose(scene::Pose_c _p);
663 
671  static scene::Texture_p create_texture();
695  static scene::Texture_p create_texture(const std::string &_id,uint32 _w,uint32 _h,uint8 _bpp,bool _f,
696  texture_filtering_e _tf,uint8 _af,uint8 _tu,const std::vector<uint8> &_d);
706  static scene::Texture_p create_texture(scene::Texture_c _t);
707 
715  static scene::Scenegraph_p create_scenegraph();
727  static scene::Scenegraph_p create_scenegraph(const std::vector<scene::Scenenode_c> &_n,const boost::adjacency_list<> &_e);
737  static scene::Scenegraph_p create_scenegraph(scene::Scenegraph_c _sg);
738 
746  static math::Beziercurve_p create_beziercurve();
756  static math::Beziercurve_p create_beziercurve(const std::vector<Eigen::Vector4d> &_p);
766  static math::Beziercurve_p create_beziercurve(math::Beziercurve_c _c);
767 
775  static math::Bezierpatch_p create_bezierpatch();
785  static math::Bezierpatch_p create_bezierpatch(const std::vector<math::Beziercurve_p> &_c);
795  static math::Bezierpatch_p create_bezierpatch(math::Bezierpatch_c _p);
796 
805  static math::NURBScurve_p create_nurbscurve();
820  static math::NURBScurve_p create_nurbscurve(uint32 _d,const std::vector<Eigen::Vector4d> &_p, bool _cl);
830  static math::NURBScurve_p create_nurbscurve(math::NURBScurve_c _c);
831 
840  static math::NURBSpatch_p create_nurbspatch();
855  static math::NURBSpatch_p create_nurbspatch(uint32 _d,const std::vector<math::NURBScurve_p> &_c,bool _cl);
865  static math::NURBSpatch_p create_nurbspatch(math::NURBSpatch_c _p);
866  };
867 
874  class Engine
875  {
876  private:
877  static boost::weak_ptr<display::Visual> current_visual;
878  static boost::weak_ptr<renderer::Context> current_context;
879 
880  Engine();
881  ~Engine();
882 
883  public:
892  static void init(display::Visual_p _v);
901  static void init(renderer::Context_p _c);
911  static void make_current(display::Visual_p _v,renderer::Context_p _c);
923  static bool is_current(display::Visual_p _v);
935  static bool is_current(renderer::Context_p _c);
936  };
937 }
938 
939 #endif // TYPES_H_INCLUDED