00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VRS_SG_STATEHANDLER_H
00019 #define VRS_SG_STATEHANDLER_H
00020
00021 #include <map>
00022 #include <functional>
00023
00024 #include <vrs/texture.h>
00025 #include <vrs/container/dictionary.h>
00026 #include <vrs/opengl/textureunitselectorgl.h>
00027 #include <vrs/opengl/textureunitsettinggl.h>
00028 #include <vrs/opengl/shapematerialgl.h>
00029
00030 namespace std {
00031 using VRS::SO;
00032 using VRS::ShapeMaterialGL;
00033
00034 template<> struct less<SO<ShapeMaterialGL> > : public binary_function<SO<ShapeMaterialGL>, SO<ShapeMaterialGL>, bool> {
00035 bool operator()(const SO<ShapeMaterialGL> left, const SO<ShapeMaterialGL> right) const {
00036 if( (left->getPerVertexColor() < right->getPerVertexColor()) ||
00037 (left->getSpecular() < right->getSpecular()) ||
00038 (left->getShininess() < right->getShininess()) ||
00039 (left->getVertexColorMapping() < right->getVertexColorMapping()) ||
00040 (left->getAmbient() < right->getAmbient()) ||
00041 (left->getDiffuse() < right->getDiffuse()) ||
00042 (left->getEmission() < right->getEmission()))
00043 return true;
00044 else
00045 return false;
00046 }
00047 };
00048 }
00049
00050 namespace VRS {
00051
00061 class AbstractStateHandler : public SharedObj{
00062 public:
00064 virtual AbstractStateHandler* create(void) = 0;
00066 virtual void reset(void) = 0;
00068 virtual void setSuccessor(AbstractStateHandler* successor) {
00069 successor_ = successor;
00070 }
00071
00079 virtual void setState(RenderObj* state) = 0;
00080
00085 virtual void pushState(void) = 0;
00086
00091 virtual void popState(void) = 0;
00092
00101 virtual void collectShape(unsigned int index) = 0;
00102
00116 virtual void createSortedScene(SO<SceneThing> root, SO<Array<unsigned int> > shapeIndices, SO<Array<SO<SharedObj> > > shapes) = 0;
00117
00118 protected:
00120 SO<AbstractStateHandler> successor_;
00121 };
00122
00132 template<class T>
00133 class StateHandler : public AbstractStateHandler {
00134 public:
00136 StateHandler(void) : stateStack_(new Stack<SO<T> >) {
00137 stateStack_->push(new T);
00138 }
00139
00141 virtual AbstractStateHandler* create(void) {
00142 return new StateHandler<T>;
00143 }
00144
00146 virtual void reset(void) {
00147 stateStack_->clear();
00148 stateShapes_.clear();
00149 }
00150
00152 virtual void setState(RenderObj* state) {
00153 if(state->isA(T::ClassNameVRS())) {
00154 stateStack_->pop();
00155 SO<T> type = VRS_GuardedCast(T, state);
00156 stateStack_->push(type);
00157 }
00158 }
00159
00161 virtual void pushState(void) {
00162 stateStack_->push(stateStack_->top());
00163 }
00164
00166 virtual void popState(void) {
00167 stateStack_->pop();
00168 }
00169
00171 virtual void collectShape(unsigned int index) {
00172 typename std::map<SO<T>, SO<Array<unsigned int> > >::iterator iter = stateShapes_.find(stateStack_->top());
00173
00174 if(iter == stateShapes_.end()) {
00175 stateShapes_[stateStack_->top()] = new Array<unsigned int>;
00176 stateShapes_[stateStack_->top()]->append(index);
00177 }
00178 else
00179 iter->second->append(index);
00180 }
00181
00183 virtual void createSortedScene(SO<SceneThing> root, SO<Array<unsigned int> > shapeIndices, SO<Array<SO<SharedObj> > > shapes) {
00184 typename std::map<SO<T>, SO<Array<unsigned int> > >::iterator state = stateShapes_.begin();
00185
00186
00187 while(state != stateShapes_.end()) {
00188 SO<Array<unsigned int> > intersection(new IndicesArray);
00189 SO<Iterator<unsigned int> > iter = state->second->newIterator();
00190
00191
00192 for(unsigned int i = 0; i < iter->size(); ++i) {
00193 if(shapeIndices->contains(iter->get(i)))
00194 intersection->append(iter->get(i));
00195 }
00196
00197
00198 if(!successor_) {
00199 if(0 < intersection->size()) {
00200 root->append(state->first);
00201
00202 SO<Iterator<unsigned int> > indices = intersection->newIterator();
00203
00204 for(unsigned int i = 0; i < indices->size(); ++i) {
00205 root->append(shapes->getElement(indices->get(i)));
00206 }
00207 }
00208 }
00209
00210 else {
00211 root->append(state->first);
00212
00213 successor_->createSortedScene(root, intersection, shapes);
00214 }
00215
00216 ++state;
00217 }
00218 }
00219
00220 private:
00221 typedef Array<unsigned int> IndicesArray;
00222 typedef std::map<SO<T>, SO<IndicesArray> > StateShapesMap;
00223
00225 SO<Stack<SO<T> > > stateStack_;
00227 StateShapesMap stateShapes_;
00228 };
00229
00230 }
00231
00232 #endif // VRS_SG_STATEHANDLER_H