OgreScriptCompiler.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2006 Torus Knot Software Ltd
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 
00024 You may alternatively use this source under the terms of a specific version of
00025 the OGRE Unrestricted License provided you have obtained such a license from
00026 Torus Knot Software Ltd.
00027 -----------------------------------------------------------------------------
00028 */
00029 
00030 #ifndef __SCRIPTCOMPILER_H_
00031 #define __SCRIPTCOMPILER_H_
00032 
00033 #include "OgreSharedPtr.h"
00034 #include "OgreMaterial.h"
00035 #include "OgreHighLevelGpuProgram.h"
00036 #include "OgreCompositor.h"
00037 #include "OgreCompositionPass.h"
00038 #include "OgreAny.h"
00039 
00040 namespace Ogre
00041 {
00043     enum ConcreteNodeType
00044     {
00045         CNT_VARIABLE,
00046         CNT_VARIABLE_ASSIGN,
00047         CNT_WORD,
00048         CNT_IMPORT,
00049         CNT_QUOTE,
00050         CNT_LBRACE,
00051         CNT_RBRACE,
00052         CNT_COLON
00053     };
00054 
00056     struct ConcreteNode;
00057     typedef SharedPtr<ConcreteNode> ConcreteNodePtr;
00058     typedef std::list<ConcreteNodePtr> ConcreteNodeList;
00059     typedef SharedPtr<ConcreteNodeList> ConcreteNodeListPtr;
00060     struct ConcreteNode : public ScriptCompilerAlloc
00061     {
00062         String token, file;
00063         unsigned int line;
00064         ConcreteNodeType type;
00065         ConcreteNodeList children;
00066         ConcreteNode *parent;
00067     };
00068 
00070     enum AbstractNodeType
00071     {
00072         ANT_UNKNOWN,
00073         ANT_ATOM,
00074         ANT_OBJECT,
00075         ANT_PROPERTY,
00076         ANT_IMPORT,
00077         ANT_VARIABLE_SET,
00078         ANT_VARIABLE_ACCESS
00079     };
00080     class AbstractNode;
00081     typedef SharedPtr<AbstractNode> AbstractNodePtr;
00082     typedef std::list<AbstractNodePtr> AbstractNodeList;
00083     typedef SharedPtr<AbstractNodeList> AbstractNodeListPtr;
00084 
00085     class _OgreExport AbstractNode : public AbstractNodeAlloc
00086     {
00087     public:
00088         String file;
00089         unsigned int line;
00090         AbstractNodeType type;
00091         AbstractNode *parent;
00092         Any context; // A holder for translation context data
00093     public:
00094         AbstractNode(AbstractNode *ptr);
00095         virtual ~AbstractNode(){}
00097         virtual AbstractNode *clone() const = 0;
00099         virtual String getValue() const = 0;
00100     };
00101 
00103     class _OgreExport AtomAbstractNode : public AbstractNode
00104     {
00105     public:
00106         String value;
00107         uint32 id;
00108     public:
00109         AtomAbstractNode(AbstractNode *ptr);
00110         AbstractNode *clone() const;
00111         String getValue() const;
00112     private:
00113         void parseNumber() const;
00114     };
00115 
00117     class _OgreExport ObjectAbstractNode : public AbstractNode
00118     {
00119     private:
00120         std::map<String,String> mEnv;
00121     public:
00122         String name, cls, base;
00123         uint32 id;
00124         bool abstract;
00125         AbstractNodeList children;
00126         AbstractNodeList values;
00127         AbstractNodeList overrides; // For use when processing object inheritance and overriding
00128     public:
00129         ObjectAbstractNode(AbstractNode *ptr);
00130         AbstractNode *clone() const;
00131         String getValue() const;
00132 
00133         void addVariable(const String &name);
00134         void setVariable(const String &name, const String &value);
00135         std::pair<bool,String> getVariable(const String &name) const;
00136         const std::map<String,String> &getVariables() const;
00137     };
00138 
00140     class _OgreExport PropertyAbstractNode : public AbstractNode
00141     {
00142     public:
00143         String name;
00144         uint32 id;
00145         AbstractNodeList values;
00146     public:
00147         PropertyAbstractNode(AbstractNode *ptr);
00148         AbstractNode *clone() const;
00149         String getValue() const;
00150     };
00151 
00153     class _OgreExport ImportAbstractNode : public AbstractNode
00154     {
00155     public:
00156         String target, source;
00157     public:
00158         ImportAbstractNode();
00159         AbstractNode *clone() const;
00160         String getValue() const;
00161     };
00162 
00164     class _OgreExport VariableAccessAbstractNode : public AbstractNode
00165     {
00166     public:
00167         String name;
00168     public:
00169         VariableAccessAbstractNode(AbstractNode *ptr);
00170         AbstractNode *clone() const;
00171         String getValue() const;
00172     };
00173 
00174     class ScriptCompilerListener;
00175 
00180     class _OgreExport ScriptCompiler : public ScriptCompilerAlloc
00181     {
00182     public: // Externally accessible types
00183         typedef std::map<String,uint32> IdMap;
00184 
00185         // The container for errors
00186         struct Error : public ScriptCompilerAlloc
00187         {
00188             String file, message;
00189             int line;
00190             uint32 code;
00191         };
00192         typedef SharedPtr<Error> ErrorPtr;
00193         typedef std::list<ErrorPtr> ErrorList;
00194 
00195         // These are the built-in error codes
00196         enum{
00197             CE_STRINGEXPECTED,
00198             CE_NUMBEREXPECTED,
00199             CE_FEWERPARAMETERSEXPECTED,
00200             CE_VARIABLEEXPECTED,
00201             CE_UNDEFINEDVARIABLE,
00202             CE_OBJECTNAMEEXPECTED,
00203             CE_OBJECTALLOCATIONERROR,
00204             CE_INVALIDPARAMETERS,
00205             CE_DUPLICATEOVERRIDE,
00206             CE_UNEXPECTEDTOKEN,
00207             CE_OBJECTBASENOTFOUND,
00208             CE_UNSUPPORTEDBYRENDERSYSTEM,
00209             CE_REFERENCETOANONEXISTINGOBJECT
00210         };
00211         static String formatErrorCode(uint32 code);
00212     public:
00213         ScriptCompiler();
00214         virtual ~ScriptCompiler() {}
00215 
00217 
00222         bool compile(const String &str, const String &source, const String &group);
00224         bool compile(const ConcreteNodeListPtr &nodes, const String &group);
00226         bool _compile(AbstractNodeListPtr nodes, const String &group);
00228         void addError(uint32 code, const String &file, int line, const String &msg = "");
00230         void setListener(ScriptCompilerListener *listener);
00232         ScriptCompilerListener *getListener();
00234         const String &getResourceGroup() const;
00236 
00241         void addNameExclusion(const String &type);
00243         void removeNameExclusion(const String &type);
00245         bool _fireEvent(const String &name, const std::vector<Any> &args, Any *retval);
00247         Any _fireCreateObject(const String &type, const std::vector<Any> &args);
00248     private: // Tree processing
00249         AbstractNodeListPtr convertToAST(const ConcreteNodeListPtr &nodes);
00251         void processImports(AbstractNodeListPtr &nodes);
00253         AbstractNodeListPtr loadImportPath(const String &name);
00255         AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target);
00257         void processObjects(AbstractNodeList *nodes, const AbstractNodeListPtr &top);
00259         void processVariables(AbstractNodeList *nodes);
00261         void overlayObject(const AbstractNodePtr &source, ObjectAbstractNode *dest);
00263         bool isNameExcluded(const String &cls, AbstractNode *parent);
00265         void initWordMap();
00266     private:
00267         // Resource group
00268         String mGroup;
00269         // The word -> id conversion table
00270         IdMap mIds;
00271         // This is an environment map
00272         typedef std::map<String,String> Environment;
00273         Environment mEnv;
00274 
00275         typedef std::map<String,AbstractNodeListPtr> ImportCacheMap;
00276         ImportCacheMap mImports; // The set of imported scripts to avoid circular dependencies
00277         typedef std::multimap<String,String> ImportRequestMap;
00278         ImportRequestMap mImportRequests; // This holds the target objects for each script to be imported
00279 
00280         // This stores the imports of the scripts, so they are separated and can be treated specially
00281         AbstractNodeList mImportTable;
00282 
00283         // Error list
00284         ErrorList mErrors;
00285 
00286         // The listener
00287         ScriptCompilerListener *mListener;
00288     private: // Internal helper classes and processors
00289         class AbstractTreeBuilder
00290         {
00291         private:
00292             AbstractNodeListPtr mNodes;
00293             AbstractNode *mCurrent;
00294             ScriptCompiler *mCompiler;
00295         public:
00296             AbstractTreeBuilder(ScriptCompiler *compiler);
00297             const AbstractNodeListPtr &getResult() const;
00298             void visit(ConcreteNode *node);
00299             static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes);
00300         };
00301         friend class AbstractTreeBuilder;
00302     public: // Public translator definitions
00303         // This enum are built-in word id values
00304         enum
00305         {
00306             ID_ON = 1,
00307             ID_OFF = 2,
00308             ID_TRUE = 1,
00309             ID_FALSE = 2,
00310             ID_YES = 1,
00311             ID_NO = 2
00312         };  
00313     };
00314 
00319     class _OgreExport ScriptCompilerListener
00320     {
00321     public:
00322         ScriptCompilerListener();
00323         virtual ~ScriptCompilerListener() {}
00324 
00326         virtual ConcreteNodeListPtr importFile(ScriptCompiler *compiler, const String &name);
00328         virtual void preConversion(ScriptCompiler *compiler, ConcreteNodeListPtr nodes);
00330 
00336         virtual bool postConversion(ScriptCompiler *compiler, const AbstractNodeListPtr&);
00338         virtual void handleError(ScriptCompiler *compiler, uint32 code, const String &file, int line, const String &msg);
00340 
00366         virtual bool handleEvent(ScriptCompiler *compiler, const String &name, const std::vector<Ogre::Any> &args, Ogre::Any *retval);
00368 
00409         virtual Ogre::Any createObject(ScriptCompiler *compiler, const String &type, const std::vector<Ogre::Any> &args);
00410     };
00411 
00412     class ScriptTranslator;
00413     class ScriptTranslatorManager;
00414 
00418     class _OgreExport ScriptCompilerManager : public Singleton<ScriptCompilerManager>, public ScriptLoader, public ScriptCompilerAlloc
00419     {
00420     private:
00421         OGRE_AUTO_MUTEX
00422 
00423         // A list of patterns loaded by this compiler manager
00424         StringVector mScriptPatterns;
00425 
00426         // A pointer to the listener used for compiling scripts
00427         ScriptCompilerListener *mListener;
00428 
00429         // Stores a map from object types to the translators that handle them
00430         std::vector<ScriptTranslatorManager*> mManagers;
00431 
00432         // A pointer to the built-in ScriptTranslatorManager
00433         ScriptTranslatorManager *mBuiltinTranslatorManager;
00434 
00435         // A pointer to the specific compiler instance used
00436         OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler);
00437     public:
00438         ScriptCompilerManager();
00439         virtual ~ScriptCompilerManager();
00440 
00442         void setListener(ScriptCompilerListener *listener);
00444         ScriptCompilerListener *getListener();
00445 
00447         void addTranslatorManager(ScriptTranslatorManager *man);
00449         void removeTranslatorManager(ScriptTranslatorManager *man);
00451         void clearTranslatorManagers();
00453         ScriptTranslator *getTranslator(const AbstractNodePtr &node);
00454 
00456         const StringVector& getScriptPatterns(void) const;
00458         void parseScript(DataStreamPtr& stream, const String& groupName);
00460         Real getLoadingOrder(void) const;
00461 
00477         static ScriptCompilerManager& getSingleton(void);
00493         static ScriptCompilerManager* getSingletonPtr(void);
00494     };
00495 
00497     enum
00498     {
00499         ID_MATERIAL = 3,
00500         ID_VERTEX_PROGRAM,
00501         ID_GEOMETRY_PROGRAM,
00502         ID_FRAGMENT_PROGRAM,
00503         ID_TECHNIQUE,
00504         ID_PASS,
00505         ID_TEXTURE_UNIT,
00506         ID_VERTEX_PROGRAM_REF,
00507         ID_GEOMETRY_PROGRAM_REF,
00508         ID_FRAGMENT_PROGRAM_REF,
00509         ID_SHADOW_CASTER_VERTEX_PROGRAM_REF,
00510         ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF,
00511         ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF,
00512         ID_SHADOW_CASTER_MATERIAL,
00513         ID_SHADOW_RECEIVER_MATERIAL,
00514         
00515         ID_LOD_DISTANCES,
00516         ID_RECEIVE_SHADOWS,
00517         ID_TRANSPARENCY_CASTS_SHADOWS,
00518         ID_SET_TEXTURE_ALIAS,
00519 
00520         ID_SOURCE,
00521         ID_SYNTAX,
00522         ID_DEFAULT_PARAMS,
00523         ID_PARAM_INDEXED,
00524         ID_PARAM_NAMED,
00525         ID_PARAM_INDEXED_AUTO,
00526         ID_PARAM_NAMED_AUTO,
00527 
00528         ID_SCHEME,
00529         ID_LOD_INDEX,
00530         ID_GPU_VENDOR_RULE,
00531         ID_GPU_DEVICE_RULE,
00532         ID_INCLUDE, 
00533         ID_EXCLUDE, 
00534 
00535         ID_AMBIENT,
00536         ID_DIFFUSE,
00537         ID_SPECULAR,
00538         ID_EMISSIVE,
00539             ID_VERTEXCOLOUR,
00540         ID_SCENE_BLEND,
00541             ID_COLOUR_BLEND,
00542             ID_ONE,
00543             ID_ZERO,
00544             ID_DEST_COLOUR,
00545             ID_SRC_COLOUR,
00546             ID_ONE_MINUS_DEST_COLOUR,
00547             ID_ONE_MINUS_SRC_COLOUR,
00548             ID_DEST_ALPHA,
00549             ID_SRC_ALPHA,
00550             ID_ONE_MINUS_DEST_ALPHA,
00551             ID_ONE_MINUS_SRC_ALPHA,
00552         ID_SEPARATE_SCENE_BLEND,
00553         ID_DEPTH_CHECK,
00554         ID_DEPTH_WRITE,
00555         ID_DEPTH_FUNC,
00556         ID_DEPTH_BIAS,
00557         ID_ITERATION_DEPTH_BIAS,
00558             ID_ALWAYS_FAIL,
00559             ID_ALWAYS_PASS,
00560             ID_LESS_EQUAL,
00561             ID_LESS,
00562             ID_EQUAL,
00563             ID_NOT_EQUAL,
00564             ID_GREATER_EQUAL,
00565             ID_GREATER,
00566         ID_ALPHA_REJECTION,
00567         ID_ALPHA_TO_COVERAGE,
00568         ID_LIGHT_SCISSOR,
00569         ID_LIGHT_CLIP_PLANES,
00570         ID_TRANSPARENT_SORTING,
00571         ID_ILLUMINATION_STAGE,
00572             ID_DECAL,
00573         ID_CULL_HARDWARE,
00574             ID_CLOCKWISE,
00575             ID_ANTICLOCKWISE,
00576         ID_CULL_SOFTWARE,
00577             ID_BACK,
00578             ID_FRONT,
00579         ID_NORMALISE_NORMALS,
00580         ID_LIGHTING,
00581         ID_SHADING,
00582             ID_FLAT, 
00583             ID_GOURAUD,
00584             ID_PHONG,
00585         ID_POLYGON_MODE,
00586             ID_SOLID,
00587             ID_WIREFRAME,
00588             ID_POINTS,
00589         ID_POLYGON_MODE_OVERRIDEABLE,
00590         ID_FOG_OVERRIDE,
00591             ID_NONE,
00592             ID_LINEAR,
00593             ID_EXP,
00594             ID_EXP2,
00595         ID_COLOUR_WRITE,
00596         ID_MAX_LIGHTS,
00597         ID_START_LIGHT,
00598         ID_ITERATION,
00599             ID_ONCE,
00600             ID_ONCE_PER_LIGHT,
00601             ID_PER_LIGHT,
00602             ID_PER_N_LIGHTS,
00603             ID_POINT,
00604             ID_SPOT,
00605             ID_DIRECTIONAL,
00606         ID_POINT_SIZE,
00607         ID_POINT_SPRITES,
00608         ID_POINT_SIZE_ATTENUATION,
00609         ID_POINT_SIZE_MIN,
00610         ID_POINT_SIZE_MAX,
00611 
00612         ID_TEXTURE_ALIAS,
00613         ID_TEXTURE,
00614             ID_1D,
00615             ID_2D,
00616             ID_3D,
00617             ID_CUBIC,
00618             ID_UNLIMITED,
00619             ID_ALPHA,
00620             ID_GAMMA,
00621         ID_ANIM_TEXTURE,
00622         ID_CUBIC_TEXTURE,
00623             ID_SEPARATE_UV,
00624             ID_COMBINED_UVW,
00625         ID_TEX_COORD_SET,
00626         ID_TEX_ADDRESS_MODE,
00627             ID_WRAP,
00628             ID_CLAMP,
00629             ID_BORDER,
00630             ID_MIRROR,
00631         ID_TEX_BORDER_COLOUR,
00632         ID_FILTERING,
00633             ID_BILINEAR,
00634             ID_TRILINEAR,
00635             ID_ANISOTROPIC,
00636         ID_MAX_ANISOTROPY,
00637         ID_MIPMAP_BIAS,
00638         ID_COLOUR_OP,
00639             ID_REPLACE,
00640             ID_ADD,
00641             ID_MODULATE,
00642             ID_ALPHA_BLEND,
00643         ID_COLOUR_OP_EX,
00644             ID_SOURCE1,
00645             ID_SOURCE2,
00646             ID_MODULATE_X2,
00647             ID_MODULATE_X4,
00648             ID_ADD_SIGNED,
00649             ID_ADD_SMOOTH,
00650             ID_SUBTRACT,
00651             ID_BLEND_DIFFUSE_COLOUR,
00652             ID_BLEND_DIFFUSE_ALPHA,
00653             ID_BLEND_TEXTURE_ALPHA,
00654             ID_BLEND_CURRENT_ALPHA,
00655             ID_BLEND_MANUAL,
00656             ID_DOT_PRODUCT,
00657             ID_SRC_CURRENT,
00658             ID_SRC_TEXTURE,
00659             ID_SRC_DIFFUSE,
00660             ID_SRC_SPECULAR,
00661             ID_SRC_MANUAL,
00662         ID_COLOUR_OP_MULTIPASS_FALLBACK,
00663         ID_ALPHA_OP_EX,
00664         ID_ENV_MAP,
00665             ID_SPHERICAL,
00666             ID_PLANAR,
00667             ID_CUBIC_REFLECTION,
00668             ID_CUBIC_NORMAL,
00669         ID_SCROLL,
00670         ID_SCROLL_ANIM,
00671         ID_ROTATE,
00672         ID_ROTATE_ANIM,
00673         ID_SCALE,
00674         ID_WAVE_XFORM,
00675             ID_SCROLL_X,
00676             ID_SCROLL_Y,
00677             ID_SCALE_X,
00678             ID_SCALE_Y,
00679             ID_SINE,
00680             ID_TRIANGLE,
00681             ID_SQUARE,
00682             ID_SAWTOOTH,
00683             ID_INVERSE_SAWTOOTH,
00684         ID_TRANSFORM,
00685         ID_BINDING_TYPE,
00686             ID_VERTEX,
00687             ID_FRAGMENT,
00688         ID_CONTENT_TYPE,
00689             ID_NAMED,
00690             ID_SHADOW,
00691         ID_TEXTURE_SOURCE,
00692 
00693         ID_PARTICLE_SYSTEM,
00694         ID_EMITTER,
00695         ID_AFFECTOR,
00696 
00697         ID_COMPOSITOR,
00698             ID_TARGET,
00699             ID_TARGET_OUTPUT,
00700 
00701             ID_INPUT,
00702                 ID_PREVIOUS,
00703                 ID_TARGET_WIDTH,
00704                 ID_TARGET_HEIGHT,
00705                 ID_TARGET_WIDTH_SCALED,
00706                 ID_TARGET_HEIGHT_SCALED,
00707             ID_ONLY_INITIAL,
00708             ID_VISIBILITY_MASK,
00709             ID_LOD_BIAS,
00710             ID_MATERIAL_SCHEME,
00711             ID_SHADOWS_ENABLED,
00712 
00713             ID_CLEAR,
00714             ID_STENCIL,
00715             ID_RENDER_SCENE,
00716             ID_RENDER_QUAD,
00717             ID_IDENTIFIER,
00718             ID_FIRST_RENDER_QUEUE,
00719             ID_LAST_RENDER_QUEUE,
00720 
00721             ID_BUFFERS,
00722                 ID_COLOUR,
00723                 ID_DEPTH,
00724             ID_COLOUR_VALUE,
00725             ID_DEPTH_VALUE,
00726             ID_STENCIL_VALUE,
00727 
00728             ID_CHECK,
00729             ID_COMP_FUNC,
00730             ID_REF_VALUE,
00731             ID_MASK,
00732             ID_FAIL_OP,
00733                 ID_KEEP,
00734                 ID_INCREMENT,
00735                 ID_DECREMENT,
00736                 ID_INCREMENT_WRAP,
00737                 ID_DECREMENT_WRAP,
00738                 ID_INVERT,
00739             ID_DEPTH_FAIL_OP,
00740             ID_PASS_OP,
00741             ID_TWO_SIDED,
00742         ID_END_BUILTIN_IDS
00743     };
00744 }
00745 
00746 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 27 22:02:25 2009