# OgreMaterialScriptCompiler.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.stevestreeting.com/ogre/
    00006
    00007 Copyright (c) 2000-2005 The OGRE Team
    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 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 General Public License for more details.
    00018
    00019 You should have received a copy of the GNU 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/gpl.html.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025
    00026 #ifndef __MaterialScriptScompiler_H__
    00027 #define __MaterialScriptScompiler_H__
    00028
    00029 #include "OgreCompiler2Pass.h"
    00030 #include "OgrePrerequisites.h"
    00031 #include "OgreTextureUnitState.h"
    00032 #include "OgreMaterial.h"
    00033 //#include "OgreBlendMode.h"
    00034 //#include "OgreTextureUnitState.h"
    00035 #include "OgreGpuProgram.h"
    00036 #include "OgreStringVector.h"
    00037
    00038 namespace Ogre {
    00039
    00040     class _OgreExport MaterialScriptCompiler : public Compiler2Pass
    00041     {
    00042
    00043     public:
    00044         MaterialScriptCompiler(void);
    00045         ~MaterialScriptCompiler(void);
    00048         virtual const String& getClientBNFGrammer(void) { return materialScript_BNF; }
    00049
    00052         virtual const String& getClientGrammerName(void) const { static const String grammerName("Material Script"); return grammerName; }
    00060         void parseScript(DataStreamPtr& stream, const String& groupName)
    00061         {
    00062             mScriptContext.groupName = groupName;
    00063             Compiler2Pass::compile(stream->getAsString(),  stream->getName());
    00064         }
    00065
    00066     protected:
    00067         // Token ID enumeration
    00068         enum TokenID {
    00069             // Terminal Tokens section
    00070             ID_UNKOWN = 0, ID_OPENBRACE, ID_CLOSEBRACE,
    00071             // GPU Program
    00072             ID_VERTEX_PROGRAM, ID_FRAGMENT_PROGRAM, ID_SOURCE, ID_SYNTAX, ID_CUSTOM_PARAMETER,
    00073             ID_DEFAULT_PARAMS,
    00074             ID_INCLUDES_SKELETAL_ANIMATION, ID_INCLUDES_MORPH_ANIMATION, ID_INCLUDES_POSE_ANIMATION,
    00075             // material
    00076             ID_MATERIAL, ID_CLONE, ID_LOD_DISTANCES, ID_RECEIVE_SHADOWS,
    00077             ID_TRANSPARENCY_CASTS_SHADOWS, ID_SET_TEXTURE_ALIAS,
    00078             // technique
    00079             ID_TECHNIQUE, ID_SCHEME, ID_LOD_INDEX,
    00080             // pass
    00081             ID_PASS, ID_AMBIENT, ID_DIFFUSE, ID_SPECULAR, ID_EMISSIVE,
    00082             ID_VERTEXCOLOUR,
    00083             // scene blend
    00084             ID_SCENE_BLEND, ID_COLOUR_BLEND, ID_DEST_COLOUR,
    00085             ID_SRC_COLOUR, ID_ONE_MINUS_DEST_COLOUR, ID_ONE_MINUS_SRC_COLOUR,
    00086             ID_DEST_ALPHA, ID_SRC_ALPHA, ID_ONE_MINUS_DEST_ALPHA, ID_ONE_MINUS_SRC_ALPHA,
    00087             // Depth
    00088             ID_DEPTH_CHECK, ID_DEPTH_WRITE, ID_DEPTH_FUNC, ID_DEPTH_BIAS, ID_ALWAYS_FAIL, ID_ALWAYS_PASS,
    00089             ID_LESS_EQUAL, ID_LESS, ID_EQUAL, ID_NOT_EQUAL, ID_GREATER_EQUAL, ID_GREATER,
    00090
    00091             ID_ALPHA_REJECTION, ID_CULL_HARDWARE, ID_CLOCKWISE, ID_ANTICLOCKWISE,
    00092             ID_CULL_SOFTWARE, ID_CULL_BACK, ID_CULL_FRONT,
    00093             ID_LIGHTING, ID_SHADING, ID_FLAT, ID_GOURAUD, ID_PHONG,
    00094             ID_POLYGON_MODE, ID_SOLID, ID_WIREFRAME, ID_POINTS,
    00095             ID_FOG_OVERRIDE, ID_EXP, ID_EXP2,
    00096             ID_COLOUR_WRITE, ID_MAX_LIGHTS,
    00097             ID_ITERATION, ID_ONCE, ID_ONCE_PER_LIGHT, ID_PER_LIGHT, ID_DIRECTIONAL, ID_SPOT,
    00098             ID_POINT_SIZE, ID_POINT_SPRITES, ID_POINT_SIZE_ATTENUATION,
    00099             ID_POINT_SIZE_MIN, ID_POINT_SIZE_MAX,
    00100
    00101             // texture unit state
    00102             ID_TEXTURE_UNIT, ID_TEXTURE_ALIAS, ID_TEXTURE, ID_1D, ID_2D, ID_3D, ID_CUBIC, ID_UNLIMITED,
    00103             ID_ALPHA, ID_ANIM_TEXTURE, ID_CUBIC_TEXTURE, ID_SEPARATE_UV, ID_COMBINED_UVW,
    00104             ID_TEX_COORD_SET, ID_TEX_ADDRESS_MODE, ID_WRAP, ID_CLAMP, ID_MIRROR, ID_BORDER, ID_TEX_BORDER_COLOUR,
    00105             ID_FILTERING, ID_BILINEAR, ID_TRILINEAR, ID_ANISOTROPIC,
    00106             ID_MAX_ANISOTROPY, ID_COLOUR_OP, ID_REPLACE,
    00107             ID_COLOUR_OP_EX, ID_SOURCE1, ID_SOURCE2, ID_MODULATE_X2, ID_MODULATE_X4, ID_ADD_SIGNED,
    00108             ID_ADD_SMOOTH, ID_SUBTRACT, ID_BLEND_DIFFUSE_COLOUR, ID_BLEND_DIFFUSE_ALPHA,
    00109             ID_BLEND_TEXTURE_ALPHA, ID_BLEND_CURRENT_ALPHA, ID_BLEND_MANUAL, ID_DOTPRODUCT,
    00110             ID_SRC_CURRENT, ID_SRC_TEXTURE, ID_SRC_DIFFUSE, ID_SRC_SPECULAR, ID_SRC_MANUAL,
    00111             ID_COLOUR_OP_MULTIPASS_FALLBACK, ID_ALPHA_OP_EX,
    00112             ID_ENV_MAP, ID_SPHERICAL, ID_PLANAR, ID_CUBIC_REFLECTION, ID_CUBIC_NORMAL,
    00113             ID_SCROLL, ID_SCROLL_ANIM, ID_ROTATE, ID_ROTATE_ANIM, ID_SCALE, ID_WAVE_XFORM,
    00114             ID_SCROLL_X, ID_SCROLL_Y, ID_SCALE_X, ID_SCALE_Y, ID_SINE, ID_TRIANGLE,
    00115             ID_SQUARE, ID_SAWTOOTH, ID_INVERSE_SAWTOOTH,
    00116             ID_TRANSFORM,
    00117             // GPU program references
    00118             ID_VERTEX_PROGRAM_REF, ID_FRAGMENT_PROGRAM_REF, ID_SHADOW_CASTER_VERTEX_PROGRAM_REF,
    00119             ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF, ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF,
    00120             // GPU Parameters
    00121             ID_PARAM_INDEXED_AUTO, ID_PARAM_INDEXED, ID_PARAM_NAMED_AUTO, ID_PARAM_NAMED,
    00122
    00123             // general
    00124             ID_ON, ID_OFF, ID_TRUE, ID_FALSE, ID_NONE, ID_POINT, ID_LINEAR, ID_ADD, ID_MODULATE, ID_ALPHA_BLEND,
    00125             ID_ONE, ID_ZERO
    00126         };
    00127
    00129         enum MaterialScriptSection
    00130         {
    00131             MSS_NONE,
    00132             MSS_MATERIAL,
    00133             MSS_TECHNIQUE,
    00134             MSS_PASS,
    00135             MSS_TEXTUREUNIT,
    00136             MSS_PROGRAM_REF,
    00137             MSS_PROGRAM,
    00138             MSS_DEFAULT_PARAMETERS,
    00139             MSS_TEXTURESOURCE
    00140         };
    00142         struct MaterialScriptProgramDefinition
    00143         {
    00144             String name;
    00145             GpuProgramType progType;
    00146             String language;
    00147             String source;
    00148             String syntax;
    00149             bool supportsSkeletalAnimation;
    00150             bool supportsMorphAnimation;
    00151             ushort supportsPoseAnimation; // number of simultaneous poses supported
    00152             std::map<String, String> customParameters;
    00153         };
    00155         struct MaterialScriptContext
    00156         {
    00157             MaterialScriptSection section;
    00158             String groupName;
    00159             MaterialPtr material;
    00160             Technique* technique;
    00161             Pass* pass;
    00162             TextureUnitState* textureUnit;
    00163             GpuProgramPtr program; // used when referencing a program, not when defining it
    00164             bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
    00165             bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00166             bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00167             GpuProgramParametersSharedPtr programParams;
    00168             ushort numAnimationParametrics;
    00169             MaterialScriptProgramDefinition* programDef; // this is used while defining a program
    00170
    00171             int techLev,    //Keep track of what tech, pass, and state level we are in
    00172                 passLev,
    00173                 stateLev;
    00174             // container of token que positions for default params that require pass 2 processing
    00175             std::vector<size_t> pendingDefaultParams;
    00176
    00177             AliasTextureNamePairList textureAliases;
    00178         };
    00179
    00180         MaterialScriptContext mScriptContext;
    00181
    00182         // static library database for tokens and BNF rules
    00183         static TokenRule materialScript_RulePath[];
    00184         // simplified Backus - Naur Form (BNF) grammer for material scripts
    00185         static String materialScript_BNF;
    00186
    00187         typedef void (MaterialScriptCompiler::* MSC_Action)(void);
    00188         typedef std::map<size_t, MSC_Action> TokenActionMap;
    00189         typedef TokenActionMap::iterator TokenActionIterator;
    00194         static TokenActionMap mTokenActionMap;
    00195
    00199         virtual void executeTokenAction(const size_t tokenID);
    00202         virtual void setupTokenDefinitions(void);
    00203         void addLexemeTokenAction(const String& lexeme, const size_t token, const MSC_Action action = 0);
    00204
    00205         void logParseError(const String& error);
    00206
    00207         // support methods that convert tokens
    00208         ColourValue _parseColourValue(void);
    00209         CompareFunction convertCompareFunction(void);
    00210
    00211         // Token Actions which get called when tokens are created during parsing.
    00212         void parseOpenBrace(void);
    00213         void parseCloseBrace(void);
    00214         // material section Actions
    00215         void parseMaterial(void);
    00216         void parseLodDistances(void);
    00217         void parseReceiveShadows(void);
    00218         void parseTransparencyCastsShadows(void);
    00219         void parseSetTextureAlias(void);
    00220         // Technique related actions
    00221         void parseTechnique(void);
    00222         void parseScheme(void);
    00223         void parseLodIndex(void);
    00224         // Pass related Actions
    00225         void parsePass(void);
    00226         void parseAmbient(void);
    00227         void parseDiffuse(void);
    00228         void parseSpecular(void);
    00229         void parseEmissive(void);
    00230         void parseSceneBlend(void);
    00231         SceneBlendFactor convertBlendFactor(void);
    00232         void parseDepthCheck(void);
    00233         void parseDepthWrite(void);
    00234         void parseDepthFunc(void);
    00235         void parseDepthBias(void);
    00236         void parseAlphaRejection(void);
    00237         void parseCullHardware(void);
    00238         void parseCullSoftware(void);
    00239         void parseLighting(void);
    00240         void parseShading(void);
    00241         void parsePolygonMode(void);
    00242         void parseFogOverride(void);
    00243         void parseMaxLights(void);
    00244         void parseIteration(void);
    00245         void parseIterationLightTypes(void);
    00246         void parseColourWrite(void);
    00247         void parsePointSize(void);
    00248         void parsePointSprites(void);
    00249         void parsePointSizeMin(void);
    00250         void parsePointSizeMax(void);
    00251         void parsePointSizeAttenuation(void);
    00252         // Texture Unit related Actions
    00253         void parseTextureUnit(void);
    00254         void parseTextureAlias(void);
    00255         void parseTexture(void);
    00256         void parseAnimTexture(void);
    00257         void parseCubicTexture(void);
    00258         void parseTexCoord(void);
    00259         TextureUnitState::TextureAddressingMode convTexAddressMode(void);
    00260         void parseTexAddressMode(void);
    00261         void parseTexBorderColour(void);
    00262         void parseFiltering(void);
    00263         FilterOptions convertFiltering();
    00264         void parseMaxAnisotropy(void);
    00265         void parseColourOp(void);
    00266         void parseColourOpEx(void);
    00267         LayerBlendOperationEx convertBlendOpEx(void);
    00268         LayerBlendSource convertBlendSource(void);
    00269         void parseColourOpMultipassFallback(void);
    00270         void parseAlphaOpEx(void);
    00271         void parseEnvMap(void);
    00272         void parseScroll(void);
    00273         void parseScrollAnim(void);
    00274         void parseRotate(void);
    00275         void parseRotateAnim(void);
    00276         void parseScale(void);
    00277         void parseWaveXform(void);
    00278         void parseTransform(void);
    00279         void parseTextureCustomParameter(void);
    00280         // GPU Program
    00281         void parseGPUProgram(void);
    00282         void parseProgramSource(void);
    00283         void parseProgramSyntax(void);
    00284         void parseProgramCustomParameter(void);
    00285         void parseDefaultParams(void);
    00286         void parseProgramSkeletalAnimation(void);
    00287         void parseProgramMorphAnimation(void);
    00288         void parseProgramPoseAnimation(void);
    00289         void parseVertexProgramRef(void);
    00290         void parseFragmentProgramRef(void);
    00291         void parseShadowCasterVertexProgramRef(void);
    00292         void parseShadowReceiverVertexProgramRef(void);
    00293         void parseShadowReceiverFragmentProgramRef(void);
    00294         void parseParamIndexed(void);
    00295         void parseParamIndexedAuto(void);
    00296         void parseParamNamed(void);
    00297         void parseParamNamedAuto(void);
    00298         void processManualProgramParam(size_t index, const String& commandname, const String& paramName = "");
    00299         void processAutoProgramParam(size_t index, const String& commandname, const String& paramName = "");
    00300
    00301
    00302         void finishProgramDefinition(void);
    00303
    00304     };
    00305 }
    00306
    00307 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 17 15:39:09 2006