LightWrap++
AC++wrapperfortheLightWave3DSDK
displacement_handler.h
Go to the documentation of this file.
1 #ifndef DISPLACEMENT_HANDLER_H
2 #define DISPLACEMENT_HANDLER_H
3 
4 #include <lwpp/plugin_handler.h>
5 #include <lwpp/point3d.h>
6 #include <lwpp/vector3d.h>
7 #include <lwdisplce.h>
8 #include <lwmeshes.h>
9 
10 namespace lwpp
11 {
12  /*
13  * The DisplacementAccess passed to the evaluation function contains two point positions,
14  * the point ID, and a mesh info for the object the point belongs to.
15  */
17  {
18  protected:
19  LWDisplacementAccess *access;
20  public:
21  DisplacementAccess(LWDisplacementAccess *acc) : access(acc) { ;}
25  const Point3d getPointPosition( void ) const { return Point3d(access->oPos); }
33  const Vector3d getSource( void ) const { return Vector3d(access->source); }
37  const LWPntID getPointID( void ) const {return access->point;}
41  const LWMeshInfo *getMeshInfo( void ) const { return access->info; }
46  const Vector3d getAvgNormal( void ) const {return Vector3d(access->wNorm);}
47 
51  void setAvgNormal(Vector3d &normal)
52  {
53  access->wNorm[0] = normal.x;
54  access->wNorm[1] = normal.y;
55  access->wNorm[2] = normal.z;
56 
57  }
61  void setSource(Point3d &sspt)
62  {
63  access->source[0] = sspt.x;
64  access->source[1] = sspt.y;
65  access->source[2] = sspt.z;
66  }
70  void setSource(Point3f &sspt)
71  {
72  access->source[0] = (double) sspt.x;
73  access->source[1] = (double) sspt.y;
74  access->source[2] = (double) sspt.z;
75  }
76 
77 
78  };
79 
81 
86 {
87  protected:
90  public:
91  DisplacementHandler(void *g, void *context, LWError *err) : InstanceHandler(g, context, err, LWDISPLACEMENT_HCLASS )
92  {
93  Context.SetID( (LWItemID) context);
94  }
95  virtual ~DisplacementHandler() {;}
96  virtual void Evaluate(LWDisplacementAccess *da)
97  {
98  ;
99  }
100  virtual unsigned int Flags() {return 0;}
101 };
102 
104 
107 template <class T, int maxVersion, int minVersion>
108 class DisplacementAdaptor : public InstanceAdaptor <T>, public ItemAdaptor <T>, public RenderAdaptor <T>
109 {
110  public:
111  DisplacementAdaptor(const char *name, ServerTagInfo *tags)
112  {
113  LWServer::AddPlugin(LWDISPLACEMENT_HCLASS, name, Activate, tags);
114  }
116  static int Activate (int version, GlobalFunc *global, LWInstance inst, void *serverData)
117  {
118  if ( version > maxVersion ) return AFUNC_BADVERSION;
119  if ( version < minVersion ) return AFUNC_BADVERSION;
120  try
121  {
122  UNUSED(serverData);
123  lwpp::SetSuperGlobal(global);
124 
125  LWDisplacementHandler *plugin = static_cast<LWDisplacementHandler *>(inst);
126 
127  InstanceAdaptor<T>::Activate(plugin->inst);
128  ItemAdaptor<T>::Activate(plugin->item);
129  RenderAdaptor<T>::Activate(plugin->rend);
130  plugin->evaluate = DisplacementAdaptor::Evaluate;
131  plugin->flags = DisplacementAdaptor::Flags;
132 
133  return AFUNC_OK;
134  }
135  catch (std::exception &e)
136  {
137  lwpp::LWMessage::Error("An exception occured in Displacement::Activate():", e.what());
138  return AFUNC_BADAPP;
139  }
140  }
141  private:
142 
143  static void Evaluate (LWInstance instance, LWDisplacementAccess *da)
144  {
145  try
146  {
147  T *plugin = (T *) instance;
148  plugin->Evaluate(DisplacementAccess(da));
149  }
150  catch (std::exception &e)
151  {
152  lwpp::LWMessage::Error("An exception occured in Displacement::Evaluate():", e.what());
153  }
154  }
155 
156  static unsigned int Flags (LWInstance instance)
157  {
158  try
159  {
160  T *plugin = (T *) instance;
161  return plugin->Flags();
162  }
163  catch (std::exception &e)
164  {
165  lwpp::LWMessage::Error("An exception occured in Displacement::Flags():", e.what());
166  return 0;
167  }
168  }
169 };
170 
176 
181 
182 } // end namespace
183 
184 #endif // DISPLACEMENT_HANDLER_H