00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef VRS_ENGINE_H
00021 #define VRS_ENGINE_H
00022
00023 #include <vrs/matrix.h>
00024 #include <vrs/sharedobj.h>
00025 #include <vector>
00026
00027 namespace VRS {
00028
00029 class Attribute;
00030 class CameraInfo;
00031 class Canvas;
00032 class Handler;
00033 class MonoAttribute;
00034 class Painter;
00035 class PolyAttribute;
00036 class Technique;
00037 class TechniqueProcessor;
00038 class Shader;
00039 class Shape;
00040 class Simplifier;
00041 class Switch;
00042 class ViewInfo;
00043 class ViewInfoFactory;
00044 template<typename T> class Callback1;
00045 template<typename T> class DataIterator;
00046 template<typename T> class NonPersistentDeque;
00047 template<typename T> class IDHashTable;
00048 template<typename T> class NonPersistentArray;
00049 template<typename T, typename V> class NonPersistentDictionary;
00050 template<typename T> class NonPersistentHashTable;
00051 template<typename T> class Stack;
00052
00069 class VRS_CORE_API Engine : public SharedObj {
00070 public:
00071 VRS_TYPEINFO(Engine, SharedObj);
00072
00073 Engine();
00074 virtual ~Engine();
00075
00076 virtual void push(MonoAttribute* attr);
00078
00082 virtual void push(PolyAttribute* attr);
00084
00088 virtual void push(Switch* sw);
00090
00096 virtual void push(Handler* hdl);
00098 virtual void push(Technique* tech);
00100
00101 virtual void pop(MonoAttribute* attr);
00103
00106 virtual void pop(PolyAttribute* attr);
00108
00112 virtual void pop(Switch* sw);
00114
00117 virtual void pop(Handler* hdl);
00119 virtual void pop(Technique* tech);
00121
00122 virtual void eval(const Shape* shape);
00133 virtual void eval(const MonoAttribute* attr, bool pushHint);
00135 virtual void eval(const PolyAttribute* attr, unsigned int index, bool pushHint);
00137
00138 virtual bool isHandledByShader(const MonoAttribute* attr);
00140
00154 virtual bool demandsShadingPass(const MonoAttribute* attr);
00156 virtual unsigned int shadingPass() const;
00158
00159 virtual void push(Attribute* attr);
00163 virtual void pop(Attribute* attr);
00167 virtual void pop(const ID& id);
00169
00174 virtual void setProcessor(TechniqueProcessor*);
00176 virtual TechniqueProcessor* getProcessor() const;
00178
00179 enum TFType { modelviewMatrix = 0, projectionMatrix = 1,
00180 textureMatrix = 2, colorMatrix = 3 };
00181 VRS_SERIALIZABLE_CLASS_ENUM(TFType);
00182 virtual void pushTf(TFType category = Engine::modelviewMatrix);
00184 virtual void popTf(TFType category = Engine::modelviewMatrix);
00186 virtual void multTf(const Matrix& matrix, TFType category = Engine::modelviewMatrix);
00188 virtual void loadTf(const Matrix& matrix, TFType category = Engine::modelviewMatrix);
00190 virtual Matrix currentTf(TFType category = Engine::modelviewMatrix) const;
00192 virtual Matrix currentTfInv(TFType category = Engine::modelviewMatrix) const;
00194 virtual Matrix orientationTf() const;
00196 virtual Matrix orientationTfInv() const;
00198 virtual void setOrientationTf(const Matrix& matrix);
00199 virtual void setOrientationTf();
00201
00202 virtual Matrix currentNormalizedDeviceCoordsTf() const;
00204 virtual Matrix currentPureProjectionTf() const;
00205 virtual Matrix currentPureProjectionTfInv() const;
00207
00208 virtual void pushViewInfoFactory(SO<ViewInfoFactory> newViewInfoFactory);
00210
00213 virtual void popViewInfoFactory();
00215 virtual SO<ViewInfo> viewInfo() const;
00217
00220 virtual SO<CameraInfo> cameraInfo() const;
00222
00223 virtual Handler* handler(const ID& service, const ID& target) const;
00227 virtual Painter* painter(const ID& target) const;
00231 virtual Simplifier* simplifier(const ID& target) const;
00236 virtual void pushMonoAttribute(const ID& category, MonoAttribute* attr);
00238
00242 virtual void popMonoAttribute(const ID& category);
00244 virtual MonoAttribute* getMonoAttribute(const ID& category) const;
00246
00247 enum SwitchType { ON = 1, OFF = 2, ALL = 3 };
00248 VRS_SERIALIZABLE_CLASS_ENUM(SwitchType);
00250
00259 virtual unsigned int appendPolyAttribute(const ID& category, PolyAttribute* attr, SwitchType switchSetting = Engine::ON);
00261
00265 virtual void removePolyAttribute(const ID& category, PolyAttribute* attr);
00267
00268 virtual void applyGlobalPolyAttributes();
00270
00272 virtual void unapplyGlobalPolyAttributes();
00274
00276 virtual unsigned int polyAttributes(const ID& category, SwitchType switchQuery = Engine::ALL) const;
00278
00282 virtual PolyAttribute* getPolyAttribute(const ID& category, unsigned int i, SwitchType switchQuery = Engine::ALL) const;
00284
00288 virtual int containsPolyAttribute(PolyAttribute* attr, SwitchType switchQuery = Engine::ALL) const;
00290
00292 virtual unsigned int findPolyAttribute(PolyAttribute* attr, SwitchType switchQuery = Engine::ALL) const;
00294
00296 virtual Iterator<SO<PolyAttribute> >* newPolyAttributeIterator(const ID& category, SwitchType switchQuery = Engine::ON) const;
00298
00301 virtual bool getSwitch(const ID& category, unsigned int index) const;
00303 virtual bool getSwitch(PolyAttribute*) const;
00305
00306 void disregard(const ID& category);
00308
00311 void regard(const ID& category);
00313 bool isRegarded(const ID& category) const;
00315
00316 virtual void pushShader(Shader*);
00318
00321 virtual void popShader();
00323
00326 virtual Shader* getShader() const;
00328
00331 virtual MonoAttribute* getShaderAttribute() const;
00333
00337 void storeTransform(const Attribute*);
00339 void deleteTransform(const Attribute*);
00341
00342 void setTransform(const SharedObj*, const Matrix&);
00344 void eraseTransform(const SharedObj*);
00346 Matrix getTransform(const SharedObj*) const;
00348 bool containsTransform(const SharedObj*) const;
00350
00351 virtual void pushHandler(const ID& service, const ID& target, Handler* hdl);
00353
00357 virtual void popHandler(const ID& service, const ID& target);
00359
00360 virtual Iterator<SO<Technique> >* getTechniques() const;
00362
00363 const Stack< SO<MonoAttribute> >* monoTable(const ID& category) const;
00365
00368 struct PolyAttributeEntry {
00369 PolyAttributeEntry(PolyAttribute* p, SwitchType s) : attribute(p), setting(s) { }
00370 SO<PolyAttribute> attribute;
00371 SwitchType setting;
00372 };
00373 const NonPersistentArray<PolyAttributeEntry>* polyTable(const ID& category) const;
00375
00377 const IDHashTable< Stack< SO<Handler> > >* serviceTable(const ID& service) const;
00379
00382 virtual void initialize();
00384
00385 static bool addInitializer(Callback1<Engine*>* init);
00393 void callInitializers();
00395
00396 Canvas* getCanvas() const;
00398 void pushCanvas(Canvas* canvas);
00400 void popCanvas();
00402
00403 static bool registerHandler(Handler* handler);
00405
00406
00409 typedef Callback1<SO<Engine> > RenderCallback;
00410
00411 static void addPreRenderCallback(SO<RenderCallback> cb);
00412 static bool removePreRenderCallback(SO<RenderCallback> cb);
00413 static SO<Iterator<SO<RenderCallback> > > preRenderCallbacks();
00414
00415 static void addPostRenderCallback(SO<RenderCallback> cb);
00416 static bool removePostRenderCallback(SO<RenderCallback> cb);
00417 static SO<Iterator<SO<RenderCallback> > > postRenderCallbacks();
00418
00419 protected:
00420 virtual void finalEval(const Shape* shape);
00423 virtual bool isFinalEvalDone() const;
00425
00426 private:
00427
00428 SO<IDHashTable< Stack< SO<MonoAttribute> > > > monoAttrTable_;
00429
00430
00431 SO<IDHashTable< NonPersistentArray<PolyAttributeEntry> > > polyAttrTable_;
00432
00433
00434 SO<NonPersistentDeque<SO<PolyAttribute> > > globalPolyAttrStack_;
00435
00436 unsigned int globalPolyAttrCounter_;
00437
00438
00439 SO<NonPersistentHashTable<ID> > disregardTable_;
00440
00441
00442 SO<IDHashTable< IDHashTable< Stack< SO< Handler> > > > > handlerTable_;
00443
00444
00445 SO<NonPersistentDictionary<SO<const SharedObj>, Matrix> > attrTransform_;
00446
00447 std::vector<Matrix> matrixStacks_[4];
00448 mutable std::vector<Matrix> invMatrixStacks_[4];
00449 Matrix orientationTf_;
00450 mutable Matrix invOrientationTf_;
00451
00452 SO<NonPersistentArray<SO<Technique> > > techniques_;
00453
00454 struct ShaderStackEntry {
00455 SO<MonoAttribute> attribute;
00456 SO<Shader> shader;
00457 unsigned int pass;
00458 bool prepared;
00459 };
00460 SO<Stack<ShaderStackEntry> > shaderStack_;
00461 unsigned int shadingPass_;
00462 bool passPrepared_;
00463
00464 bool finalEvalDone_;
00465
00466 SO<Stack<SO<Canvas> > > renderingCanvases_;
00467 SO<TechniqueProcessor> processor_;
00468
00469 mutable SO<CameraInfo> m_cameraInfo;
00470 mutable SO<Stack<SO<ViewInfoFactory> > > viewInfoFactoryStack_;
00471 mutable SO<ViewInfo> viewInfo_;
00472
00473 #ifndef NDEBUG
00474 SO<Stack<SO<MonoAttribute> > > unorderedAttributeStack_;
00475 #endif // NDEBUG
00476 };
00477
00478 inline unsigned int Engine::shadingPass() const {
00479 return shadingPass_;
00480 }
00481
00482 #define VRS_GetMonoAttribute(TARGET_TYPE, ENGINE) \
00483 VRS_Cast(TARGET_TYPE, (ENGINE)->getMonoAttribute(TARGET_TYPE::ClassNameVRS()))
00484
00485 #define VRS_GuardedGetMonoAttribute(TARGET_TYPE, ENGINE) \
00486 VRS_GuardedCast(TARGET_TYPE, (ENGINE)->getMonoAttribute(TARGET_TYPE::ClassNameVRS()))
00487
00488
00489
00490
00491
00492
00493
00494
00495 #define VRS_REGISTER_HANDLER(HANDLER, ENGINE) \
00496 namespace { \
00497 bool VRS_ANONYMOUS_IDENTIFIER(registration) = ENGINE::registerHandler(new HANDLER); \
00498 }
00499
00500 }
00501
00502 #endif // VRS_ENGINE_H