00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef LUX_CONTEXT_H
00024 #define LUX_CONTEXT_H
00025
00026 #include "lux.h"
00027 #include "paramset.h"
00028
00029 #include <boost/thread/mutex.hpp>
00030 #include <map>
00031 using std::map;
00032
00033
00034 #define STATE_UNINITIALIZED 0
00035 #define STATE_OPTIONS_BLOCK 1
00036 #define STATE_WORLD_BLOCK 2
00037
00038 namespace lux {
00039
00040 class FlexImageFilm;
00041
00042 class Context {
00043 public:
00044
00045 Context(std::string n="Lux default context") : name(n) {
00046 init();
00047 }
00048
00049 ~Context() {
00050 free();
00051 }
00052
00053
00054
00055
00056
00057
00058 static Context* getActive() {
00059 return activeContext;
00060 }
00061 static void setActive(Context *c) {
00062 activeContext=c;
00063 }
00064
00065 static map<string, boost::shared_ptr<Texture<float> > > *getActiveFloatTextures() {
00066 return &(activeContext->graphicsState->floatTextures);
00067 }
00068 static map<string, boost::shared_ptr<Texture<SWCSpectrum> > > *getActiveSWCSpectrumTextures() {
00069 return &(activeContext->graphicsState->colorTextures);
00070 }
00071 static u_int getActiveLightGroup() {
00072 return activeContext->GetActiveLightGroup();
00073 }
00074
00075
00076
00077 static void luxIdentity() { activeContext->identity(); }
00078 static void luxTranslate(float dx, float dy, float dz) { activeContext->translate(dx, dy, dz); }
00079 static void luxRotate(float angle, float ax, float ay, float az) { activeContext->rotate(angle, ax, ay, az); }
00080 static void luxScale(float sx, float sy, float sz) { activeContext->scale(sx, sy, sz); }
00081 static void luxLookAt(float ex, float ey, float ez, float lx, float ly, float lz, float ux, float uy, float uz) { activeContext->lookAt(ex, ey, ez, lx, ly, lz, ux, uy, uz) ; }
00082 static void luxConcatTransform(float transform[16]) { activeContext->concatTransform(transform); }
00083 static void luxTransform(float transform[16]) { activeContext->transform(transform); }
00084 static void luxCoordinateSystem(const string &s) { activeContext->coordinateSystem(s) ; }
00085 static void luxCoordSysTransform(const string &s) { activeContext->coordSysTransform(s); }
00086 static void luxPixelFilter(const string &name, const ParamSet ¶ms) { activeContext->pixelFilter(name, params); }
00087 static void luxFilm(const string &type, const ParamSet ¶ms) { activeContext->film(type, params); }
00088 static void luxSampler(const string &name, const ParamSet ¶ms) { activeContext->sampler(name, params); }
00089 static void luxAccelerator(const string &name, const ParamSet ¶ms) { activeContext->accelerator(name, params); }
00090 static void luxSurfaceIntegrator(const string &name, const ParamSet ¶ms) { activeContext->surfaceIntegrator(name, params); }
00091 static void luxVolumeIntegrator(const string &name, const ParamSet ¶ms) { activeContext->volumeIntegrator(name, params); }
00092 static void luxCamera(const string &s, const ParamSet &cameraParams) { activeContext->camera(s, cameraParams); }
00093 static void luxWorldBegin() { activeContext->worldBegin(); }
00094 static void luxAttributeBegin() { activeContext->attributeBegin(); }
00095 static void luxAttributeEnd() { activeContext->attributeEnd(); }
00096 static void luxTransformBegin() { activeContext->transformBegin(); }
00097 static void luxTransformEnd() { activeContext->transformEnd(); }
00098 static void luxTexture(const string &name, const string &type, const string &texname, const ParamSet ¶ms) { activeContext->texture(name, type, texname, params); }
00099 static void luxMaterial(const string &name, const ParamSet ¶ms) { activeContext->material(name, params); }
00100 static void luxMakeNamedMaterial(const string &name, const ParamSet ¶ms) { activeContext->makenamedmaterial(name, params); }
00101 static void luxNamedMaterial(const string &name, const ParamSet ¶ms) { activeContext->namedmaterial(name, params); }
00102 static void luxLightGroup(const string &name, const ParamSet ¶ms) { activeContext->lightGroup(name, params); }
00103 static void luxLightSource(const string &name, const ParamSet ¶ms) { activeContext->lightSource(name, params); }
00104 static void luxAreaLightSource(const string &name, const ParamSet ¶ms) { activeContext->areaLightSource(name, params); }
00105 static void luxPortalShape(const string &name, const ParamSet ¶ms) { activeContext->portalShape(name, params); }
00106 static void luxShape(const string &name, const ParamSet ¶ms) { activeContext->shape(name, params); }
00107 static void luxReverseOrientation() { activeContext->reverseOrientation(); }
00108 static void luxVolume(const string &name, const ParamSet ¶ms) { activeContext->volume(name, params); }
00109 static void luxObjectBegin(const string &name) { activeContext->objectBegin(name); }
00110 static void luxObjectEnd() { activeContext->objectEnd(); }
00111 static void luxObjectInstance(const string &name) { activeContext->objectInstance(name); }
00112 static void luxMotionInstance(const string &name, float startTime, float endTime, const string &toTransform) { activeContext->motionInstance(name, startTime, endTime, toTransform); }
00113 static void luxWorldEnd() { activeContext->worldEnd(); }
00114
00121 boost::shared_ptr<Material> makematerial(const ParamSet& shapeparams, bool force);
00122 void makemixmaterial(const ParamSet& shapeparams, const ParamSet& materialparams, boost::shared_ptr<Material> mtl);
00123
00124
00125 static void luxCleanup() { activeContext->cleanup(); }
00126
00127
00128 static void luxLoadFLM(const string &name) { activeContext->loadFLM(name); }
00129 static void luxSaveFLM(const string &name) { activeContext->saveFLM(name); }
00130 static void luxOverrideResumeFLM(const string &name) { activeContext->overrideResumeFLM(name); }
00131
00132
00133
00134 static void luxStart() { activeContext->start(); }
00135 static void luxPause() { activeContext->pause(); }
00136 static void luxExit() { activeContext->exit(); }
00137
00138 static void luxWait() { activeContext->wait(); }
00139
00140 static void luxSetHaltSamplePerPixel(int haltspp, bool haveEnoughSamplePerPixel, bool suspendThreadsWhenDone) {
00141 activeContext->setHaltSamplePerPixel(haltspp, haveEnoughSamplePerPixel,suspendThreadsWhenDone);
00142 }
00143
00144
00145 static int luxAddThread() { return activeContext->addThread(); }
00146 static void luxRemoveThread() { activeContext->removeThread(); }
00147 static int luxGetRenderingThreadsStatus(RenderingThreadInfo *info, int maxInfoCount) { return activeContext->getRenderingThreadsStatus(info, maxInfoCount); }
00148
00149
00150 static void luxUpdateFramebuffer() { activeContext->updateFramebuffer(); }
00151 static unsigned char* luxFramebuffer() { return activeContext->framebuffer(); }
00152
00153
00154 static void luxGetHistogramImage(unsigned char *outPixels, int width, int height, int options){ activeContext->getHistogramImage(outPixels, width, height, options); }
00155
00156
00157 static void luxSetParameterValue(luxComponent comp, luxComponentParameters param, double value, int index) {
00158 activeContext->SetParameterValue(comp, param, value, index);
00159 }
00160 static double luxGetParameterValue(luxComponent comp, luxComponentParameters param, int index) {
00161 return activeContext->GetParameterValue(comp, param, index);
00162 }
00163 static double luxGetDefaultParameterValue(luxComponent comp, luxComponentParameters param, int index) {
00164 return activeContext->GetDefaultParameterValue(comp, param, index);
00165 }
00166 static void luxSetStringParameterValue(luxComponent comp, luxComponentParameters param, const string& value, int index) {
00167 activeContext->SetStringParameterValue(comp, param, value, index);
00168 }
00169 static string luxGetStringParameterValue(luxComponent comp, luxComponentParameters param, int index) {
00170 return activeContext->GetStringParameterValue(comp, param, index);
00171 }
00172 static string luxGetDefaultStringParameterValue(luxComponent comp, luxComponentParameters param, int index) {
00173 return activeContext->GetDefaultStringParameterValue(comp, param, index);
00174 }
00175
00176
00177 static void luxUpdateFilmFromNetwork() { activeContext->updateFilmFromNetwork(); }
00178 static void luxSetNetworkServerUpdateInterval(int updateInterval) { activeContext->setNetworkServerUpdateInterval(updateInterval); }
00179 static int luxGetNetworkServerUpdateInterval() { return activeContext->getNetworkServerUpdateInterval(); }
00180 static void luxAddServer(const string &name) { activeContext->addServer(name); }
00181 static void luxRemoveServer(const string &name) { activeContext->removeServer(name); }
00182 static int luxGetServerCount() { return activeContext->getServerCount(); }
00183 static int luxGetRenderingServersStatus(RenderingServerInfo *info, int maxInfoCount) { return activeContext->getRenderingServersStatus(info, maxInfoCount); }
00184
00185
00186 static void luxSceneReady() { activeContext->sceneReady(); }
00187 static double luxStatistics(const string &statName) { return activeContext->statistics(statName); }
00188
00189
00190 static void luxTransmitFilm(std::basic_ostream<char> &stream) { activeContext->transmitFilm(stream); }
00191
00192
00193 static void luxEnableDebugMode() { activeContext->enableDebugMode(); }
00194
00195
00196 static void luxDisableRandomMode() { activeContext->disableRandomMode(); }
00197
00198
00199 static void luxSetEpsilon(const float minValue, const float maxValue) { activeContext->setEpsilon(minValue, maxValue); }
00200
00201 private:
00202 static Context *activeContext;
00203 string name;
00204 Scene *luxCurrentScene;
00205 bool luxCurrentSceneReady;
00206
00207 void init();
00208 void free();
00209
00210
00211 void identity();
00212 void translate(float dx, float dy, float dz);
00213 void rotate(float angle, float ax, float ay, float az);
00214 void scale(float sx, float sy, float sz);
00215 void lookAt(float ex, float ey, float ez, float lx, float ly, float lz,
00216 float ux, float uy, float uz);
00217 void concatTransform(float transform[16]);
00218 void transform(float transform[16]);
00219 void coordinateSystem(const string &);
00220 void coordSysTransform(const string &);
00221 void pixelFilter(const string &name, const ParamSet ¶ms);
00222 void film(const string &type, const ParamSet ¶ms);
00223 void sampler(const string &name, const ParamSet ¶ms);
00224 void accelerator(const string &name, const ParamSet ¶ms);
00225 void surfaceIntegrator(const string &name, const ParamSet ¶ms);
00226 void volumeIntegrator(const string &name, const ParamSet ¶ms);
00227 void camera(const string &, const ParamSet &cameraParams);
00228 void worldBegin();
00229 void attributeBegin();
00230 void attributeEnd();
00231 void transformBegin();
00232 void transformEnd();
00233 void texture(const string &name, const string &type, const string &texname,
00234 const ParamSet ¶ms);
00235 void material(const string &name, const ParamSet ¶ms);
00236 void makenamedmaterial(const string &name, const ParamSet ¶ms);
00237 void namedmaterial(const string &name, const ParamSet ¶ms);
00238 void lightGroup(const string &name, const ParamSet ¶ms);
00239 void lightSource(const string &name, const ParamSet ¶ms);
00240 void areaLightSource(const string &name, const ParamSet ¶ms);
00241 void portalShape(const string &name, const ParamSet ¶ms);
00242 void shape(const string &name, const ParamSet ¶ms);
00243 void reverseOrientation();
00244 void volume(const string &name, const ParamSet ¶ms);
00245 void objectBegin(const string &name);
00246 void objectEnd();
00247 void objectInstance(const string &name);
00248 void motionInstance(const string &name, float startTime, float endTime, const string &toTransform);
00249 void worldEnd();
00250
00251
00252 void cleanup();
00253
00254
00255 void loadFLM(const string &name);
00256 void saveFLM(const string &name);
00257 void overrideResumeFLM(const string &name);
00258
00259
00260
00261 void start();
00262 void pause();
00263 void exit();
00264 void wait();
00265
00266 void setHaltSamplePerPixel(int haltspp, bool haveEnoughSamplePerPixel,
00267 bool suspendThreadsWhenDone);
00268
00269
00270 int addThread();
00271 void removeThread();
00272 int getRenderingThreadsStatus(RenderingThreadInfo *info, int maxInfoCount);
00273
00274
00275 void updateFramebuffer();
00276 unsigned char* framebuffer();
00277
00278
00279 void getHistogramImage(unsigned char *outPixels, int width, int height, int options);
00280
00281
00282 void SetParameterValue(luxComponent comp, luxComponentParameters param, double value, int index);
00283 double GetParameterValue(luxComponent comp, luxComponentParameters param, int index);
00284 double GetDefaultParameterValue(luxComponent comp, luxComponentParameters param, int index);
00285 void SetStringParameterValue(luxComponent comp, luxComponentParameters param, const string& value, int index);
00286 string GetStringParameterValue(luxComponent comp, luxComponentParameters param, int index);
00287 string GetDefaultStringParameterValue(luxComponent comp, luxComponentParameters param, int index);
00288
00289 u_int GetActiveLightGroup();
00290
00291
00292
00293
00294
00295
00296 void updateFilmFromNetwork();
00297 void setNetworkServerUpdateInterval(int updateInterval);
00298 int getNetworkServerUpdateInterval();
00299 void transmitFilm(std::basic_ostream<char> &stream);
00300
00301
00302 double statistics(const string &statName);
00303 void sceneReady();
00304 void addServer(const string &name);
00305 void removeServer(const string &name);
00306 int getServerCount();
00307 int getRenderingServersStatus(RenderingServerInfo *info, int maxInfoCount);
00308
00309 void enableDebugMode();
00310 void disableRandomMode();
00311
00312 void setEpsilon(const float minValue, const float maxValue);
00313
00314
00315 struct RenderOptions {
00316
00317 RenderOptions() {
00318
00319 FilterName = "mitchell";
00320 FilmName = "multiimage";
00321 SamplerName = "random";
00322 AcceleratorName = "kdtree";
00323 SurfIntegratorName = "path";
00324 VolIntegratorName = "emission";
00325 CameraName = "perspective";
00326 currentInstance = NULL;
00327 debugMode = false;
00328 randomMode = true;
00329 }
00330
00331 Scene *MakeScene() const;
00332
00333 string FilterName;
00334 ParamSet FilterParams;
00335 string FilmName;
00336 ParamSet FilmParams;
00337 string SamplerName;
00338 ParamSet SamplerParams;
00339 string AcceleratorName;
00340 ParamSet AcceleratorParams;
00341 string SurfIntegratorName, VolIntegratorName;
00342 ParamSet SurfIntegratorParams, VolIntegratorParams;
00343 string CameraName;
00344 ParamSet CameraParams;
00345 Transform WorldToCamera;
00346 Transform WorldToCameraEnd;
00347 bool gotSearchPath;
00348 mutable vector<Light *> lights;
00349 mutable vector<boost::shared_ptr<Primitive> > primitives;
00350 mutable vector<VolumeRegion *> volumeRegions;
00351 mutable map<string, vector<boost::shared_ptr<Primitive> > > instances;
00352 mutable vector<boost::shared_ptr<Primitive> > *currentInstance;
00353 bool debugMode;
00354 bool randomMode;
00355 mutable vector<string> lightGroups;
00356 };
00357
00358 struct NamedMaterial {
00359 NamedMaterial() {};
00360
00361 ParamSet materialParams;
00362 string material;
00363 };
00364
00365 struct GraphicsState {
00366
00367 GraphicsState() {
00368
00369 material = "";
00370 currentLightGroup = "";
00371 reverseOrientation = false;
00372 }
00373
00374 map<string, boost::shared_ptr<Texture<float> > > floatTextures;
00375 map<string, boost::shared_ptr<Texture<SWCSpectrum> > > colorTextures;
00376 ParamSet materialParams;
00377 string material;
00378 ParamSet areaLightParams;
00379 string areaLight;
00380 string currentLight;
00381 string currentLightGroup;
00382
00383
00384
00385 Light* currentLightPtr0;
00386 Light* currentLightPtr1;
00387 bool reverseOrientation;
00388 };
00389
00390 bool terminated;
00391 int currentApiState;
00392 Transform curTransform;
00393 map<string, Transform> namedCoordinateSystems;
00394 RenderOptions *renderOptions;
00395 GraphicsState *graphicsState;
00396 vector<NamedMaterial> namedmaterials;
00397 vector<GraphicsState> pushedGraphicsStates;
00398 vector<Transform> pushedTransforms;
00399 RenderFarm *renderFarm;
00400
00401 ParamSet *filmOverrideParams;
00402
00403
00404 mutable boost::mutex renderingMutex;
00405 };
00406
00407 }
00408
00409 #endif //LUX_CONTEXT_H