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 
00216 
00221         bool compile(const String &str, const String &source, const String &group);
00223         bool compile(const ConcreteNodeListPtr &nodes, const String &group);
00225         bool _compile(AbstractNodeListPtr nodes, const String &group);
00227         void addError(uint32 code, const String &file, int line, const String &msg = "");
00229         void setListener(ScriptCompilerListener *listener);
00231         ScriptCompilerListener *getListener();
00233         const String &getResourceGroup() const;
00235 
00240         void addNameExclusion(const String &type);
00242         void removeNameExclusion(const String &type);
00244         bool _fireEvent(const String &name, const std::vector<Any> &args, Any *retval);
00246         Any _fireCreateObject(const String &type, const std::vector<Any> &args);
00247     private: // Tree processing
00248         AbstractNodeListPtr convertToAST(const ConcreteNodeListPtr &nodes);
00250         void processImports(AbstractNodeListPtr &nodes);
00252         AbstractNodeListPtr loadImportPath(const String &name);
00254         AbstractNodeListPtr locateTarget(AbstractNodeList *nodes, const String &target);
00256         void processObjects(AbstractNodeList *nodes, const AbstractNodeListPtr &top);
00258         void processVariables(AbstractNodeList *nodes);
00260         void overlayObject(const AbstractNodePtr &source, ObjectAbstractNode *dest);
00262         bool isNameExcluded(const String &cls, AbstractNode *parent);
00264         void initWordMap();
00265     private:
00266         // Resource group
00267         String mGroup;
00268         // The word -> id conversion table
00269         IdMap mIds;
00270         // This is an environment map
00271         typedef std::map<String,String> Environment;
00272         Environment mEnv;
00273 
00274         typedef std::map<String,AbstractNodeListPtr> ImportCacheMap;
00275         ImportCacheMap mImports; // The set of imported scripts to avoid circular dependencies
00276         typedef std::multimap<String,String> ImportRequestMap;
00277         ImportRequestMap mImportRequests; // This holds the target objects for each script to be imported
00278 
00279         // This stores the imports of the scripts, so they are separated and can be treated specially
00280         AbstractNodeList mImportTable;
00281 
00282         // Error list
00283         ErrorList mErrors;
00284 
00285         // The listener
00286         ScriptCompilerListener *mListener;
00287     private: // Internal helper classes and processors
00288         class AbstractTreeBuilder
00289         {
00290         private:
00291             AbstractNodeListPtr mNodes;
00292             AbstractNode *mCurrent;
00293             ScriptCompiler *mCompiler;
00294         public:
00295             AbstractTreeBuilder(ScriptCompiler *compiler);
00296             const AbstractNodeListPtr &getResult() const;
00297             void visit(ConcreteNode *node);
00298             static void visit(AbstractTreeBuilder *visitor, const ConcreteNodeList &nodes);
00299         };
00300         friend class AbstractTreeBuilder;
00301     public: // Public translator definitions
00302         // This enum are built-in word id values
00303         enum
00304         {
00305             ID_ON = 1,
00306             ID_OFF = 2,
00307             ID_TRUE = 1,
00308             ID_FALSE = 2,
00309             ID_YES = 1,
00310             ID_NO = 2
00311         };  
00312     };
00313 
00318     class _OgreExport ScriptCompilerListener
00319     {
00320     public:
00321         ScriptCompilerListener();
00322 
00324         virtual ConcreteNodeListPtr importFile(ScriptCompiler *compiler, const String &name);
00326         virtual void preConversion(ScriptCompiler *compiler, ConcreteNodeListPtr nodes);
00328 
00334         virtual bool postConversion(ScriptCompiler *compiler, const AbstractNodeListPtr&);
00336         virtual void handleError(ScriptCompiler *compiler, uint32 code, const String &file, int line, const String &msg);
00338 
00364         virtual bool handleEvent(ScriptCompiler *compiler, const String &name, const std::vector<Ogre::Any> &args, Ogre::Any *retval);
00366 
00407         virtual Ogre::Any createObject(ScriptCompiler *compiler, const String &type, const std::vector<Ogre::Any> &args);
00408     };
00409 
00410     class ScriptTranslator;
00411     class ScriptTranslatorManager;
00412 
00416     class _OgreExport ScriptCompilerManager : public Singleton<ScriptCompilerManager>, public ScriptLoader, public ScriptCompilerAlloc
00417     {
00418     private:
00419         OGRE_AUTO_MUTEX
00420 
00421         // A list of patterns loaded by this compiler manager
00422         StringVector mScriptPatterns;
00423 
00424         // A pointer to the listener used for compiling scripts
00425         ScriptCompilerListener *mListener;
00426 
00427         // Stores a map from object types to the translators that handle them
00428         std::vector<ScriptTranslatorManager*> mManagers;
00429 
00430         // A pointer to the built-in ScriptTranslatorManager
00431         ScriptTranslatorManager *mBuiltinTranslatorManager;
00432 
00433         // A pointer to the specific compiler instance used
00434         OGRE_THREAD_POINTER(ScriptCompiler, mScriptCompiler);
00435     public:
00436         ScriptCompilerManager();
00437         virtual ~ScriptCompilerManager();
00438 
00440         void setListener(ScriptCompilerListener *listener);
00442         ScriptCompilerListener *getListener();
00443 
00445         void addTranslatorManager(ScriptTranslatorManager *man);
00447         void removeTranslatorManager(ScriptTranslatorManager *man);
00449         void clearTranslatorManagers();
00451         ScriptTranslator *getTranslator(const AbstractNodePtr &node);
00452 
00454         const StringVector& getScriptPatterns(void) const;
00456         void parseScript(DataStreamPtr& stream, const String& groupName);
00458         Real getLoadingOrder(void) const;
00459 
00475         static ScriptCompilerManager& getSingleton(void);
00491         static ScriptCompilerManager* getSingletonPtr(void);
00492     };
00493 
00495     enum
00496     {
00497         ID_MATERIAL = 3,
00498         ID_VERTEX_PROGRAM,
00499         ID_GEOMETRY_PROGRAM,
00500         ID_FRAGMENT_PROGRAM,
00501         ID_TECHNIQUE,
00502         ID_PASS,
00503         ID_TEXTURE_UNIT,
00504         ID_VERTEX_PROGRAM_REF,
00505         ID_GEOMETRY_PROGRAM_REF,
00506         ID_FRAGMENT_PROGRAM_REF,
00507         ID_SHADOW_CASTER_VERTEX_PROGRAM_REF,
00508         ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF,
00509         ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF,
00510         ID_SHADOW_CASTER_MATERIAL,
00511         ID_SHADOW_RECEIVER_MATERIAL,
00512         
00513         ID_LOD_DISTANCES,
00514         ID_RECEIVE_SHADOWS,
00515         ID_TRANSPARENCY_CASTS_SHADOWS,
00516         ID_SET_TEXTURE_ALIAS,
00517 
00518         ID_SOURCE,
00519         ID_SYNTAX,
00520         ID_DEFAULT_PARAMS,
00521         ID_PARAM_INDEXED,
00522         ID_PARAM_NAMED,
00523         ID_PARAM_INDEXED_AUTO,
00524         ID_PARAM_NAMED_AUTO,
00525 
00526         ID_SCHEME,
00527         ID_LOD_INDEX,
00528         ID_GPU_VENDOR_RULE,
00529         ID_GPU_DEVICE_RULE,
00530         ID_INCLUDE, 
00531         ID_EXCLUDE, 
00532 
00533         ID_AMBIENT,
00534         ID_DIFFUSE,
00535         ID_SPECULAR,
00536         ID_EMISSIVE,
00537             ID_VERTEXCOLOUR,
00538         ID_SCENE_BLEND,
00539             ID_COLOUR_BLEND,
00540             ID_ONE,
00541             ID_ZERO,
00542             ID_DEST_COLOUR,
00543             ID_SRC_COLOUR,
00544             ID_ONE_MINUS_DEST_COLOUR,
00545             ID_ONE_MINUS_SRC_COLOUR,
00546             ID_DEST_ALPHA,
00547             ID_SRC_ALPHA,
00548             ID_ONE_MINUS_DEST_ALPHA,
00549             ID_ONE_MINUS_SRC_ALPHA,
00550         ID_SEPARATE_SCENE_BLEND,
00551         ID_DEPTH_CHECK,
00552         ID_DEPTH_WRITE,
00553         ID_DEPTH_FUNC,
00554         ID_DEPTH_BIAS,
00555         ID_ITERATION_DEPTH_BIAS,
00556             ID_ALWAYS_FAIL,
00557             ID_ALWAYS_PASS,
00558             ID_LESS_EQUAL,
00559             ID_LESS,
00560             ID_EQUAL,
00561             ID_NOT_EQUAL,
00562             ID_GREATER_EQUAL,
00563             ID_GREATER,
00564         ID_ALPHA_REJECTION,
00565         ID_ALPHA_TO_COVERAGE,
00566         ID_LIGHT_SCISSOR,
00567         ID_LIGHT_CLIP_PLANES,
00568         ID_TRANSPARENT_SORTING,
00569         ID_ILLUMINATION_STAGE,
00570             ID_DECAL,
00571         ID_CULL_HARDWARE,
00572             ID_CLOCKWISE,
00573             ID_ANTICLOCKWISE,
00574         ID_CULL_SOFTWARE,
00575             ID_BACK,
00576             ID_FRONT,
00577         ID_NORMALISE_NORMALS,
00578         ID_LIGHTING,
00579         ID_SHADING,
00580             ID_FLAT, 
00581             ID_GOURAUD,
00582             ID_PHONG,
00583         ID_POLYGON_MODE,
00584             ID_SOLID,
00585             ID_WIREFRAME,
00586             ID_POINTS,
00587         ID_POLYGON_MODE_OVERRIDEABLE,
00588         ID_FOG_OVERRIDE,
00589             ID_NONE,
00590             ID_LINEAR,
00591             ID_EXP,
00592             ID_EXP2,
00593         ID_COLOUR_WRITE,
00594         ID_MAX_LIGHTS,
00595         ID_START_LIGHT,
00596         ID_ITERATION,
00597             ID_ONCE,
00598             ID_ONCE_PER_LIGHT,
00599             ID_PER_LIGHT,
00600             ID_PER_N_LIGHTS,
00601             ID_POINT,
00602             ID_SPOT,
00603             ID_DIRECTIONAL,
00604         ID_POINT_SIZE,
00605         ID_POINT_SPRITES,
00606         ID_POINT_SIZE_ATTENUATION,
00607         ID_POINT_SIZE_MIN,
00608         ID_POINT_SIZE_MAX,
00609 
00610         ID_TEXTURE_ALIAS,
00611         ID_TEXTURE,
00612             ID_1D,
00613             ID_2D,
00614             ID_3D,
00615             ID_CUBIC,
00616             ID_UNLIMITED,
00617             ID_ALPHA,
00618         ID_ANIM_TEXTURE,
00619         ID_CUBIC_TEXTURE,
00620             ID_SEPARATE_UV,
00621             ID_COMBINED_UVW,
00622         ID_TEX_COORD_SET,
00623         ID_TEX_ADDRESS_MODE,
00624             ID_WRAP,
00625             ID_CLAMP,
00626             ID_BORDER,
00627             ID_MIRROR,
00628         ID_TEX_BORDER_COLOUR,
00629         ID_FILTERING,
00630             ID_BILINEAR,
00631             ID_TRILINEAR,
00632             ID_ANISOTROPIC,
00633         ID_MAX_ANISOTROPY,
00634         ID_MIPMAP_BIAS,
00635         ID_COLOUR_OP,
00636             ID_REPLACE,
00637             ID_ADD,
00638             ID_MODULATE,
00639             ID_ALPHA_BLEND,
00640         ID_COLOUR_OP_EX,
00641             ID_SOURCE1,
00642             ID_SOURCE2,
00643             ID_MODULATE_X2,
00644             ID_MODULATE_X4,
00645             ID_ADD_SIGNED,
00646             ID_ADD_SMOOTH,
00647             ID_SUBTRACT,
00648             ID_BLEND_DIFFUSE_COLOUR,
00649             ID_BLEND_DIFFUSE_ALPHA,
00650             ID_BLEND_TEXTURE_ALPHA,
00651             ID_BLEND_CURRENT_ALPHA,
00652             ID_BLEND_MANUAL,
00653             ID_DOT_PRODUCT,
00654             ID_SRC_CURRENT,
00655             ID_SRC_TEXTURE,
00656             ID_SRC_DIFFUSE,
00657             ID_SRC_SPECULAR,
00658             ID_SRC_MANUAL,
00659         ID_COLOUR_OP_MULTIPASS_FALLBACK,
00660         ID_ALPHA_OP_EX,
00661         ID_ENV_MAP,
00662             ID_SPHERICAL,
00663             ID_PLANAR,
00664             ID_CUBIC_REFLECTION,
00665             ID_CUBIC_NORMAL,
00666         ID_SCROLL,
00667         ID_SCROLL_ANIM,
00668         ID_ROTATE,
00669         ID_ROTATE_ANIM,
00670         ID_SCALE,
00671         ID_WAVE_XFORM,
00672             ID_SCROLL_X,
00673             ID_SCROLL_Y,
00674             ID_SCALE_X,
00675             ID_SCALE_Y,
00676             ID_SINE,
00677             ID_TRIANGLE,
00678             ID_SQUARE,
00679             ID_SAWTOOTH,
00680             ID_INVERSE_SAWTOOTH,
00681         ID_TRANSFORM,
00682         ID_BINDING_TYPE,
00683             ID_VERTEX,
00684             ID_FRAGMENT,
00685         ID_CONTENT_TYPE,
00686             ID_NAMED,
00687             ID_SHADOW,
00688 
00689         ID_PARTICLE_SYSTEM,
00690         ID_EMITTER,
00691         ID_AFFECTOR,
00692 
00693         ID_COMPOSITOR,
00694             ID_TARGET,
00695             ID_TARGET_OUTPUT,
00696 
00697             ID_INPUT,
00698                 ID_PREVIOUS,
00699                 ID_TARGET_WIDTH,
00700                 ID_TARGET_HEIGHT,
00701                 ID_TARGET_WIDTH_SCALED,
00702                 ID_TARGET_HEIGHT_SCALED,
00703             ID_ONLY_INITIAL,
00704             ID_VISIBILITY_MASK,
00705             ID_LOD_BIAS,
00706             ID_MATERIAL_SCHEME,
00707 
00708             ID_CLEAR,
00709             ID_STENCIL,
00710             ID_RENDER_SCENE,
00711             ID_RENDER_QUAD,
00712             ID_IDENTIFIER,
00713             ID_FIRST_RENDER_QUEUE,
00714             ID_LAST_RENDER_QUEUE,
00715 
00716             ID_BUFFERS,
00717                 ID_COLOUR,
00718                 ID_DEPTH,
00719             ID_COLOUR_VALUE,
00720             ID_DEPTH_VALUE,
00721             ID_STENCIL_VALUE,
00722 
00723             ID_CHECK,
00724             ID_COMP_FUNC,
00725             ID_REF_VALUE,
00726             ID_MASK,
00727             ID_FAIL_OP,
00728                 ID_KEEP,
00729                 ID_INCREMENT,
00730                 ID_DECREMENT,
00731                 ID_INCREMENT_WRAP,
00732                 ID_DECREMENT_WRAP,
00733                 ID_INVERT,
00734             ID_DEPTH_FAIL_OP,
00735             ID_PASS_OP,
00736             ID_TWO_SIDED,
00737         ID_END_BUILTIN_IDS
00738     };
00739 }
00740 
00741 #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 Thu Aug 28 20:53:52 2008