LightWrap++
AC++wrapperfortheLightWave3DSDK
light_handler.h
Go to the documentation of this file.
1 #ifndef LWPP_LIGHT_HANDLER
2 #define LWPP_LIGHT_HANDLER
3 
4 #if (LW_SDK >= 95)
5 
6 #include "lwpp/plugin_handler.h"
7 #include <lwlight.h>
8 
9 namespace lwpp
10 {
12 
15 class LightHandler : public InstanceHandler, public ItemHandler, public RenderHandler
16 {
17  protected:
18  LWItemID Context;
19  public:
20  LightHandler(void *g, void *context, LWError *err) : InstanceHandler(g, context, err, LWLIGHT_HCLASS)
21  {
22  Context = static_cast<LWItemID>(context);
23  }
24  virtual ~LightHandler() {;}
25  virtual unsigned int Flags ()
26  {
27  return (LWLIGHTTYPEF_NO_PHOTONS | LWLIGHTTYPEF_NO_RAYILLUM);
28  }
29  virtual LWError NewFrame (const LWFrameInfo* frameinfo, unsigned int* maxilluminations, const LWLightAccess* lightaccess) = 0;
30  virtual int Evaluate (const LWDVector spot, double fractime, LWIllumination illumination[], const LWLightAccess* lightaccess) = 0;
31  virtual unsigned int GetPhotons (unsigned int maxphotons, LWPhoton photons[], const LWLightAccess* lightaccess) {return 0;}
32  virtual unsigned int GetRayIlluminations (LWDVector raystart, LWDVector raydir, double farclip, unsigned int maxrayilluminations, LWRayIllumination rayilluminations[], const LWLightAccess* lightaccess) {return 0;}
33  virtual const LWPreviewLight* Preview() {return 0;}
34 };
35 
37 
40  template <class T, int maxVersion, int minVersion>
41  class LightAdaptor : public InstanceAdaptor <T>, public ItemAdaptor <T>, public RenderAdaptor <T>
42  {
43  public:
44  LightAdaptor(const char *name, ServerTagInfo *tags)
45  {
46  LWServer::AddPlugin(LWLIGHT_HCLASS, name, Activate, tags);
47  }
49  static int Activate (int version, GlobalFunc *global, LWInstance inst, void *serverData)
50  {
51  UNUSED(serverData);
52  try
53  {
54  if ( version > maxVersion ) return AFUNC_BADVERSION;
55  if ( version < minVersion ) return AFUNC_BADVERSION;
56  lwpp::SetSuperGlobal(global);
57  LWLightHandler *plugin = static_cast<LWLightHandler *>(inst);
58 
59  InstanceAdaptor<T>::Activate(plugin->inst);
60  ItemAdaptor<T>::Activate(plugin->item);
61  RenderAdaptor<T>::Activate(plugin->rend);
62  plugin->evaluate = LightAdaptor::Evaluate;
63  plugin->flags = LightAdaptor::Flags;
64  plugin->newFrame = LightAdaptor::NewFrame;
65  plugin->getPhotons = LightAdaptor::GetPhotons;
66  plugin->getRayIlluminations = LightAdaptor::GetRayIlluminations;
67  plugin->preview = LightAdaptor::Preview;
68  return AFUNC_OK;
69  }
70  catch (std::exception &e)
71  {
72  lwpp::LWMessage::Error("An exception occured in LightAdaptor::Activate():", e.what());
73  return AFUNC_BADAPP;
74  }
75  }
76  private:
77  static LWError NewFrame (LWInstance instance, const LWFrameInfo* frameinfo, unsigned int* maxilluminations, const LWLightAccess* lightaccess)
78  {
79  try
80  {
81  T *plugin = (T *) instance;
82  return plugin->NewFrame(frameinfo, maxilluminations, lightaccess);
83  }
84  catch (std::exception &e)
85  {
86  lwpp::LWMessage::Error("An exception occured in LightHandler::NewFrame():", e.what());
87  return 0;
88  }
89  }
90  static int Evaluate (LWInstance instance, const LWDVector spot, double fractime, LWIllumination illumination[], const LWLightAccess* lightaccess)
91  {
92  try
93  {
94  T *plugin = (T *) instance;
95  return plugin->Evaluate(spot, fractime, illumination, lightaccess);
96  }
97  catch (std::exception &e)
98  {
99  lwpp::LWMessage::Error("An exception occured in LightHandler::Evaluate():", e.what());
100  return 0;
101  }
102  }
103  static unsigned int GetPhotons (LWInstance instance, unsigned int maxphotons, LWPhoton photons[], const LWLightAccess* lightaccess)
104  {
105  try
106  {
107  T *plugin = (T *) instance;
108  return plugin->GetPhotons(maxphotons, photons, lightaccess);
109  }
110  catch (std::exception &e)
111  {
112  lwpp::LWMessage::Error("An exception occured in LightHandler::GetPhotons():", e.what());
113  return 0;
114  }
115  }
116 
117  static unsigned int GetRayIlluminations (LWInstance instance, LWDVector raystart, LWDVector raydir, double farclip, unsigned int maxrayilluminations, LWRayIllumination rayilluminations[], const LWLightAccess* lightaccess)
118  {
119  try
120  {
121  T *plugin = (T *) instance;
122  return plugin->GetRayIlluminations(raystart, raydir, farclip, maxrayilluminations, rayilluminations, lightaccess);
123  }
124  catch (std::exception &e)
125  {
126  lwpp::LWMessage::Error("An exception occured in LightHandler::GetRayIlluminations():", e.what());
127  return 0;
128  }
129  }
130 
131  static const LWPreviewLight* Preview (LWInstance instance)
132  {
133  try
134  {
135  T *plugin = (T *) instance;
136  return plugin->Preview();
137  }
138  catch (std::exception &e)
139  {
140  lwpp::LWMessage::Error("An exception occured in LightHandler::Preview():", e.what());
141  return 0;
142  }
143  }
144 
145 
146  static unsigned int Flags (LWInstance instance)
147  {
148  try
149  {
150  T *plugin = (T *) instance;
151  return plugin->Flags();
152  }
153  catch (std::exception &e)
154  {
155  lwpp::LWMessage::Error("An exception occured in LightHandler::Evaluate():", e.what());
156  return 0;
157  }
158  }
159  };
160 
166 }
167 
168 #endif // LWSDK version check
169 #endif // LWPP_LIGHT_HANDLER