00001 /* 00002 File: Scene.h 00003 00004 Function: Basic definitions for a scene and its constituent objects: 00005 attributes (for setting state variables such as colour), 00006 primitives (drawing) and groups (aggregates of primitives). 00007 00008 Author(s): Andrew Willmott 00009 00010 Copyright: (c) 1995-2000, Andrew Willmott 00011 */ 00012 00013 #ifndef __Scene__ 00014 #define __Scene__ 00015 00016 #include "cl/ObjArray.h" 00017 #include "gcl/Renderer.h" 00018 #include "gcl/SLContext.h" 00019 00020 // --- Basic Object types ----------------------------------------------------- 00021 00022 00023 enum scAttributeID 00024 { 00025 // -- attributes -------// 00026 aState = 1, // general state variables 00027 aColour, // a colour/reflectance value 00028 aEmittance, // an emittance value 00029 00030 aPoints, // a list of points 00031 aColours, // a list of colours 00032 aNormals, // a list of normals 00033 aCoords, // a list of texture coordinates 00034 00035 aPointIndexes, // indexes into a point list. 00036 aNormalIndexes, // indexes into a normal list. 00037 aCoordIndexes, // indexes into a coord list. 00038 aColourIndexes, // indexes of vertex colour attributes. 00039 00040 aFaceIndexes, // list of index ranges per face 00041 aMeshType, // are our faces polygons, triangle lists, tri strips... 00042 00043 aTransform, // a transformation 00044 aCamera, // scene camera (i.e., projection kind) 00045 aTexture, // scene texture map 00046 aObjHide, // set if objects should be off 00047 aAvarList, // a list of avar values 00048 // -- end of list ------// 00049 aNumAttributes 00050 }; 00051 00052 enum scPrimitiveID 00053 { 00054 // -- primitives -------// 00055 pGroup = 1, 00056 pPoly, 00057 pSphere, 00058 pCylinder, 00059 pCone, 00060 pTetrahedron, 00061 pMRModel, 00062 pExtension, 00063 // -- end of list ------// 00064 pNumPrimitives 00065 }; 00066 00067 class Avar; 00068 00069 // --- Scene decimation ------------------------------------------------------- 00070 00071 enum DecFlags 00072 { 00073 DecTris = 0x0001, // triangulate polys 00074 DecQuads = 0x0002, // pass through parallelograms 00075 DecUseMaster = 0x0004, // generate a single, master point list 00076 DecIgnoreMRM = 0x0008, // ignore multiresolution models 00077 DecIsMRM = 0x0010 // this is a mr model triangle 00078 }; 00079 00080 enum DecStatus 00081 { 00082 DEC_Start = -1, 00083 DEC_End = -2 00084 }; 00085 00086 struct Decimator 00087 { 00088 virtual Void HandlePoly( 00089 Int numVertices, // # vertices in this polygon 00090 Int vertices[], // vertex indicies 00091 Int materialChanged // has the material changed 00092 ) = 0; 00093 00094 Int decNum; // increases by 1 with each DecCallback 00095 Int pointsAccNum; // points offset 00096 Transform transAcc; // current model transform 00097 PointList *pointsAcc; // master points list if requested 00098 SLContext *context; // current context for this polygon 00099 Flags32 flags; 00100 }; 00101 00102 struct FindDecInfo : public Decimator 00103 { 00104 Void HandlePoly(Int numVerts, Int vertices[], Int materialChanged); 00105 00106 Int numTris; 00107 Int numQuads; 00108 Int numPolys; 00109 Int numProps; 00110 }; 00111 00112 00113 // --- Fundamental scene objects ---------------------------------------------- 00114 00115 #define SO_GET(X) ((sc ## X *) Get(a ## X)) 00116 // shorthand for fetch & cast attribute... 00117 00118 class scObject; 00119 class scGroup; 00120 class scSceneAction; 00121 typedef scObject *scObjectPtr; 00122 00123 class scObject : public Object 00124 { 00125 public: 00126 00127 scObject() : Object(), itsType(0) {}; 00128 scObject(Int itsType) : Object(), itsType(itsType) {}; 00129 scObject(const scObject &so) : Object(), itsType(so.itsType) {}; 00130 virtual ~scObject(); 00131 00132 virtual Void Apply(const Transform &m); 00133 // Transform the object 00134 Void ApplyAction(scSceneAction &a); 00135 // Apply action to object 00136 virtual Void ApplyActionSelf(scSceneAction &a); 00137 // Internal routine for the above. 00138 Void Print(ostream &s) const; 00139 virtual Void HierPrint(ostream &s, Int indent) const; 00140 00141 Void Decimate(Decimator &dec, UInt32 flags); 00142 virtual Void DecimateSelf(Decimator &dec) {}; 00143 00144 Object *Clone() const { return new scObject(SELF); }; 00145 Bool IsPrim() const { return(itsType > 0); }; 00146 Bool IsAttr() const { return(itsType < 0); }; 00147 Bool IsGroup() const { return(itsType == pGroup); }; 00148 scPrimitiveID PrimID() const { return(scPrimitiveID) itsType; }; 00149 scAttributeID AttrID() const { return(scAttributeID) -itsType; }; 00150 00151 protected: 00152 Int itsType; 00153 // -ve == attribute, +ve == primitive. 00154 static Int prIndent; 00155 // indent in printing 00156 }; 00157 00158 #define PrimCast(x) ((scPrimitive *) (scObject *) x) 00159 #define AttrCast(x) ((scAttribute *) (scObject *) x) 00160 #define GroupCast(x) ((scGroup *) x) 00161 00162 class scAttribute : public scObject 00163 { 00164 public: 00165 scAttribute(AttrType attrType) : scObject(-attrType) {}; 00166 scAttribute(const scAttribute &sa) : scObject(sa) {}; 00167 00168 virtual Void AddToContext(SLContext *context); 00169 // Add this attribute to the context 00170 virtual Bool HasAvar(Avar &avar, Int slot); 00171 // returns true (and sets avar) if the attribute 00172 // is avar-controlled. 00173 Void Print(ostream &s) const; 00174 00175 Object *Clone() const { return new scAttribute(SELF); }; 00176 }; 00177 00178 class scClearAttr : public scAttribute 00179 // Signals end of influence of a particular attribute. 00180 { 00181 public: 00182 scClearAttr(AttrType attrType) : scAttribute(attrType) {}; 00183 00184 Void AddToContext(SLContext *context); 00185 00186 Void Print(ostream &s) const; 00187 00188 Object *Clone() const { return new scClearAttr(SELF); }; 00189 }; 00190 00191 00192 class scPrimitive : public scObject, public Renderable 00193 { 00194 public: 00195 00196 scPrimitive(scPrimitiveID primType) : scObject(primType), parent(0) {}; 00197 scPrimitive(const scPrimitive &sp) : scObject(sp), parent(sp.parent) {}; 00198 00199 Void Draw(Renderer &r); 00200 virtual Void Draw(Renderer &r, SLContext *context); 00201 Void Print(ostream &s) const; 00202 00203 Void Normalise(); 00204 // re-scale scene to fit in unit cube. 00205 Void FindBounds(Point &min, Point &max); 00206 virtual Void UpdateBounds(Point &min, Point &max, 00207 const Transform &t); 00208 00209 Void Apply(const Transform &m); 00210 virtual Void Set(scAttribute *sa); 00211 virtual scAttribute *Get(scAttributeID id); 00212 virtual scAttribute *FindFirst(scAttributeID id); 00213 00214 virtual StrConst Label() const { return "unknown primitive"; }; 00215 Object *Clone() const { return new scPrimitive(SELF); }; 00216 scGroup *parent; 00217 00218 Void ExtDispatch(Int method, Void *data); 00219 // for later extensions 00220 00221 static RenderStyle sRenderStyle; 00222 static Colour sDefaultColour; 00223 00224 private: 00225 scPrimitive() {}; 00226 Void *extData; 00227 }; 00228 00229 00230 // --- Group object ----------------------------------------------------------- 00231 00232 00233 class scGroup : public scPrimitive 00234 { 00235 public: 00236 scGroup() : scPrimitive(pGroup), children(), 00237 name("unknown") {}; 00238 scGroup(StrConst name) : scPrimitive(pGroup), children(), 00239 name(name) {}; 00240 scGroup(const scGroup &sg); 00241 00242 Void Apply(const Transform &m); 00243 Void ApplyActionSelf(scSceneAction &a); 00244 Void UpdateBounds(Point &min, Point &max, 00245 const Transform &t); 00246 00247 Void Draw(Renderer &r, SLContext *context); 00248 Void Print(ostream &s) const; 00249 Void HierPrint(ostream &s, Int indent) const; 00250 Void Parse(istream &s); 00251 Void Set(scAttribute *sa); 00252 virtual scAttribute *FindFirst(scAttributeID id); 00253 00254 Void DecimateSelf(Decimator &dec); 00255 00256 scObject *Child(Int i) const 00257 { return((scObject *) children[i]); }; 00258 Int NumChildren() const 00259 { return(children.NumItems()); }; 00260 00261 Void SetName(StrConst name); 00262 00263 Void Add(scObject *so); 00264 scObject *Last(); 00265 StrConst Label() const; 00266 Object *Clone() const; 00267 00268 String name; 00269 ObjArray children; 00270 }; 00271 00272 00273 // --- Typedefs --------------------------------------------------------------- 00274 00275 00276 typedef scGroup scScene; 00277 typedef scScene *scScenePtr; 00278 typedef ObjArray scObjectList; 00279 00280 ostream &operator << (ostream &s, const scScenePtr scene); 00281 00282 // --- Actions ---------------------------------------------------------------- 00283 00284 class scSceneAction 00285 { 00286 public: 00287 scSceneAction() : context(0) {}; 00288 00289 virtual Void Start(); 00290 virtual Void Stop(); 00291 00292 virtual Void Primitive(scPrimitive *sp); 00293 virtual Void Attribute(scAttribute *sa); 00294 00295 SLContext *context; 00296 Transform transAcc; // XXX should be part of context one of these 00297 // days 00298 }; 00299 00300 #endif