source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreMaterialScriptCompiler.h @ 1812

Revision 1812, 13.3 KB checked in by gumbau, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.stevestreeting.com/ogre/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/gpl.html.
23-----------------------------------------------------------------------------
24*/
25
26#ifndef __MaterialScriptScompiler_H__
27#define __MaterialScriptScompiler_H__
28
29#include "OgreCompiler2Pass.h"
30#include "OgrePrerequisites.h"
31#include "OgreTextureUnitState.h"
32#include "OgreMaterial.h"
33//#include "OgreBlendMode.h"
34//#include "OgreTextureUnitState.h"
35#include "OgreGpuProgram.h"
36#include "OgreStringVector.h"
37
38namespace Ogre {
39
40    class _OgreExport MaterialScriptCompiler : public Compiler2Pass
41    {
42
43    public:
44        MaterialScriptCompiler(void);
45        ~MaterialScriptCompiler(void);
46        /** gets BNF Grammer for Compositor script.
47        */
48        virtual const String& getClientBNFGrammer(void) { return materialScript_BNF; }
49
50        /** get the name of the BNF grammer.
51        */
52        virtual const String& getClientGrammerName(void) const { static const String grammerName("Material Script"); return grammerName; }
53        /** Compile a material script from a data stream using a specific resource group name.
54        @param stream Weak reference to a data stream which is the source of the material script
55        @param groupName The name of the resource group that resources which are
56                        parsed are to become a member of. If this group is loaded or unloaded,
57                        then the resources discovered in this script will be loaded / unloaded
58                        with it.
59        */
60        void parseScript(DataStreamPtr& stream, const String& groupName)
61        {
62            mScriptContext.groupName = groupName;
63            Compiler2Pass::compile(stream->getAsString(),  stream->getName());
64        }
65
66    protected:
67            // Token ID enumeration
68            enum TokenID {
69                    // Terminal Tokens section
70            ID_UNKOWN = 0, ID_OPENBRACE, ID_CLOSEBRACE,
71            // GPU Program
72            ID_VERTEX_PROGRAM, ID_FRAGMENT_PROGRAM, ID_SOURCE, ID_SYNTAX, ID_CUSTOM_PARAMETER,
73            ID_DEFAULT_PARAMS,
74            ID_INCLUDES_SKELETAL_ANIMATION, ID_INCLUDES_MORPH_ANIMATION, ID_INCLUDES_POSE_ANIMATION,
75            // material
76            ID_MATERIAL, ID_CLONE, ID_LOD_DISTANCES, ID_RECEIVE_SHADOWS,
77            ID_TRANSPARENCY_CASTS_SHADOWS, ID_SET_TEXTURE_ALIAS,
78            // technique
79            ID_TECHNIQUE, ID_SCHEME, ID_LOD_INDEX,
80            // pass
81            ID_PASS, ID_AMBIENT, ID_DIFFUSE, ID_SPECULAR, ID_EMISSIVE,
82            ID_VERTEXCOLOUR,
83            // scene blend
84            ID_SCENE_BLEND, ID_COLOUR_BLEND, ID_DEST_COLOUR,
85            ID_SRC_COLOUR, ID_ONE_MINUS_DEST_COLOUR, ID_ONE_MINUS_SRC_COLOUR,
86            ID_DEST_ALPHA, ID_SRC_ALPHA, ID_ONE_MINUS_DEST_ALPHA, ID_ONE_MINUS_SRC_ALPHA,
87            // Depth
88            ID_DEPTH_CHECK, ID_DEPTH_WRITE, ID_DEPTH_FUNC, ID_DEPTH_BIAS, ID_ALWAYS_FAIL, ID_ALWAYS_PASS,
89            ID_LESS_EQUAL, ID_LESS, ID_EQUAL, ID_NOT_EQUAL, ID_GREATER_EQUAL, ID_GREATER,
90
91            ID_ALPHA_REJECTION, ID_CULL_HARDWARE, ID_CLOCKWISE, ID_ANTICLOCKWISE,
92            ID_CULL_SOFTWARE, ID_CULL_BACK, ID_CULL_FRONT,
93            ID_LIGHTING, ID_SHADING, ID_FLAT, ID_GOURAUD, ID_PHONG,
94            ID_POLYGON_MODE, ID_SOLID, ID_WIREFRAME, ID_POINTS,
95            ID_FOG_OVERRIDE, ID_EXP, ID_EXP2,
96            ID_COLOUR_WRITE, ID_MAX_LIGHTS,
97            ID_ITERATION, ID_ONCE, ID_ONCE_PER_LIGHT, ID_PER_LIGHT, ID_DIRECTIONAL, ID_SPOT,
98                        ID_POINT_SIZE, ID_POINT_SPRITES, ID_POINT_SIZE_ATTENUATION,
99                        ID_POINT_SIZE_MIN, ID_POINT_SIZE_MAX,
100
101            // texture unit state
102            ID_TEXTURE_UNIT, ID_TEXTURE_ALIAS, ID_TEXTURE, ID_1D, ID_2D, ID_3D, ID_CUBIC, ID_UNLIMITED,
103            ID_ALPHA, ID_ANIM_TEXTURE, ID_CUBIC_TEXTURE, ID_SEPARATE_UV, ID_COMBINED_UVW,
104            ID_TEX_COORD_SET, ID_TEX_ADDRESS_MODE, ID_WRAP, ID_CLAMP, ID_MIRROR, ID_BORDER, ID_TEX_BORDER_COLOUR,
105            ID_FILTERING, ID_BILINEAR, ID_TRILINEAR, ID_ANISOTROPIC,
106            ID_MAX_ANISOTROPY, ID_COLOUR_OP, ID_REPLACE,
107            ID_COLOUR_OP_EX, ID_SOURCE1, ID_SOURCE2, ID_MODULATE_X2, ID_MODULATE_X4, ID_ADD_SIGNED,
108            ID_ADD_SMOOTH, ID_SUBTRACT, ID_BLEND_DIFFUSE_COLOUR, ID_BLEND_DIFFUSE_ALPHA,
109            ID_BLEND_TEXTURE_ALPHA, ID_BLEND_CURRENT_ALPHA, ID_BLEND_MANUAL, ID_DOTPRODUCT,
110            ID_SRC_CURRENT, ID_SRC_TEXTURE, ID_SRC_DIFFUSE, ID_SRC_SPECULAR, ID_SRC_MANUAL,
111            ID_COLOUR_OP_MULTIPASS_FALLBACK, ID_ALPHA_OP_EX,
112            ID_ENV_MAP, ID_SPHERICAL, ID_PLANAR, ID_CUBIC_REFLECTION, ID_CUBIC_NORMAL,
113            ID_SCROLL, ID_SCROLL_ANIM, ID_ROTATE, ID_ROTATE_ANIM, ID_SCALE, ID_WAVE_XFORM,
114            ID_SCROLL_X, ID_SCROLL_Y, ID_SCALE_X, ID_SCALE_Y, ID_SINE, ID_TRIANGLE,
115            ID_SQUARE, ID_SAWTOOTH, ID_INVERSE_SAWTOOTH,
116            ID_TRANSFORM,
117            // GPU program references
118            ID_VERTEX_PROGRAM_REF, ID_FRAGMENT_PROGRAM_REF, ID_SHADOW_CASTER_VERTEX_PROGRAM_REF,
119            ID_SHADOW_RECEIVER_VERTEX_PROGRAM_REF, ID_SHADOW_RECEIVER_FRAGMENT_PROGRAM_REF,
120            // GPU Parameters
121            ID_PARAM_INDEXED_AUTO, ID_PARAM_INDEXED, ID_PARAM_NAMED_AUTO, ID_PARAM_NAMED,
122
123            // general
124            ID_ON, ID_OFF, ID_TRUE, ID_FALSE, ID_NONE, ID_POINT, ID_LINEAR, ID_ADD, ID_MODULATE, ID_ALPHA_BLEND,
125            ID_ONE, ID_ZERO
126        };
127
128        /** Enum to identify material sections. */
129        enum MaterialScriptSection
130        {
131            MSS_NONE,
132            MSS_MATERIAL,
133            MSS_TECHNIQUE,
134            MSS_PASS,
135            MSS_TEXTUREUNIT,
136            MSS_PROGRAM_REF,
137                    MSS_PROGRAM,
138            MSS_DEFAULT_PARAMETERS,
139                    MSS_TEXTURESOURCE
140        };
141            /** Struct for holding a program definition which is in progress. */
142            struct MaterialScriptProgramDefinition
143            {
144                    String name;
145                    GpuProgramType progType;
146            String language;
147                    String source;
148                    String syntax;
149            bool supportsSkeletalAnimation;
150                    bool supportsMorphAnimation;
151                    ushort supportsPoseAnimation; // number of simultaneous poses supported
152                    std::map<String, String> customParameters;
153            };
154        /** Struct for holding the script context while parsing. */
155        struct MaterialScriptContext
156        {
157            MaterialScriptSection section;
158                    String groupName;
159            MaterialPtr material;
160            Technique* technique;
161            Pass* pass;
162            TextureUnitState* textureUnit;
163            GpuProgramPtr program; // used when referencing a program, not when defining it
164            bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
165            bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
166            bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
167            GpuProgramParametersSharedPtr programParams;
168                        ushort numAnimationParametrics;
169                    MaterialScriptProgramDefinition* programDef; // this is used while defining a program
170
171                    int techLev,        //Keep track of what tech, pass, and state level we are in
172                            passLev,
173                            stateLev;
174            // container of token que positions for default params that require pass 2 processing
175            std::vector<size_t> pendingDefaultParams;
176
177            AliasTextureNamePairList textureAliases;
178        };
179
180        MaterialScriptContext mScriptContext;
181
182            // static library database for tokens and BNF rules
183            static TokenRule materialScript_RulePath[];
184        // simplified Backus - Naur Form (BNF) grammer for material scripts
185        static String materialScript_BNF;
186
187        typedef void (MaterialScriptCompiler::* MSC_Action)(void);
188        typedef std::map<size_t, MSC_Action> TokenActionMap;
189        typedef TokenActionMap::iterator TokenActionIterator;
190        /** Map of Token value as key to an Action.  An Action converts tokens into
191            the final format.
192            All instances use the same Token Action Map.
193        */
194        static TokenActionMap mTokenActionMap;
195
196        /** Execute an Action associated with a token.  Gets called when the compiler finishes tokenizing a
197            section of the source that has been parsed.
198        **/
199        virtual void executeTokenAction(const size_t tokenID);
200        /** Associate all the lexemes used in a material script with their corresponding tokens and actions.
201        **/
202        virtual void setupTokenDefinitions(void);
203        void addLexemeTokenAction(const String& lexeme, const size_t token, const MSC_Action action = 0);
204
205        void logParseError(const String& error);
206
207        // support methods that convert tokens
208        ColourValue _parseColourValue(void);
209        CompareFunction convertCompareFunction(void);
210
211        // Token Actions which get called when tokens are created during parsing.
212        void parseOpenBrace(void);
213        void parseCloseBrace(void);
214        // material section Actions
215        void parseMaterial(void);
216        void parseLodDistances(void);
217        void parseReceiveShadows(void);
218        void parseTransparencyCastsShadows(void);
219        void parseSetTextureAlias(void);
220        // Technique related actions
221        void parseTechnique(void);
222        void parseScheme(void);
223        void parseLodIndex(void);
224        // Pass related Actions
225        void parsePass(void);
226        void parseAmbient(void);
227        void parseDiffuse(void);
228        void parseSpecular(void);
229        void parseEmissive(void);
230        void parseSceneBlend(void);
231        SceneBlendFactor convertBlendFactor(void);
232        void parseDepthCheck(void);
233        void parseDepthWrite(void);
234        void parseDepthFunc(void);
235        void parseDepthBias(void);
236        void parseAlphaRejection(void);
237        void parseCullHardware(void);
238        void parseCullSoftware(void);
239        void parseLighting(void);
240        void parseShading(void);
241        void parsePolygonMode(void);
242        void parseFogOverride(void);
243        void parseMaxLights(void);
244        void parseIteration(void);
245        void parseIterationLightTypes(void);
246        void parseColourWrite(void);
247        void parsePointSize(void);
248        void parsePointSprites(void);
249        void parsePointSizeMin(void);
250        void parsePointSizeMax(void);
251        void parsePointSizeAttenuation(void);
252        // Texture Unit related Actions
253        void parseTextureUnit(void);
254        void parseTextureAlias(void);
255        void parseTexture(void);
256        void parseAnimTexture(void);
257        void parseCubicTexture(void);
258        void parseTexCoord(void);
259        TextureUnitState::TextureAddressingMode convTexAddressMode(void);
260        void parseTexAddressMode(void);
261        void parseTexBorderColour(void);
262        void parseFiltering(void);
263        FilterOptions convertFiltering();
264        void parseMaxAnisotropy(void);
265        void parseColourOp(void);
266        void parseColourOpEx(void);
267        LayerBlendOperationEx convertBlendOpEx(void);
268        LayerBlendSource convertBlendSource(void);
269        void parseColourOpMultipassFallback(void);
270        void parseAlphaOpEx(void);
271        void parseEnvMap(void);
272        void parseScroll(void);
273        void parseScrollAnim(void);
274        void parseRotate(void);
275        void parseRotateAnim(void);
276        void parseScale(void);
277        void parseWaveXform(void);
278        void parseTransform(void);
279        void parseTextureCustomParameter(void);
280        // GPU Program
281        void parseGPUProgram(void);
282        void parseProgramSource(void);
283        void parseProgramSyntax(void);
284        void parseProgramCustomParameter(void);
285        void parseDefaultParams(void);
286        void parseProgramSkeletalAnimation(void);
287        void parseProgramMorphAnimation(void);
288        void parseProgramPoseAnimation(void);
289        void parseVertexProgramRef(void);
290        void parseFragmentProgramRef(void);
291        void parseShadowCasterVertexProgramRef(void);
292        void parseShadowReceiverVertexProgramRef(void);
293        void parseShadowReceiverFragmentProgramRef(void);
294        void parseParamIndexed(void);
295        void parseParamIndexedAuto(void);
296        void parseParamNamed(void);
297        void parseParamNamedAuto(void);
298        void processManualProgramParam(size_t index, const String& commandname, const String& paramName = "");
299        void processAutoProgramParam(size_t index, const String& commandname, const String& paramName = "");
300
301
302        void finishProgramDefinition(void);
303
304    };
305}
306
307#endif
Note: See TracBrowser for help on using the repository browser.