source: GTP/trunk/App/Demos/Illum/Ogre/src/Common/src/SceneSerializer.cpp @ 2388

Revision 2388, 13.7 KB checked in by szirmay, 17 years ago (diff)
Line 
1#include "OgreIlluminationManager.h"
2#include "SceneSerializer.h"
3
4void logParseError(const String& error, const SceneScriptContext& context)
5{   
6        LogManager::getSingleton().logMessage(
7                    "Error at line " + StringConverter::toString(context.lineNo) +
8                    " of " + context.filename + ": " + error);       
9}
10
11bool parseMeshFileName(String& params, SceneScriptContext& context)
12{
13        if(params == "")
14                logParseError("Error reading mesh filename!", context);
15
16        StringUtil::trim(params);
17        context.meshFiles[context.meshName] = params;
18
19        return false;
20}
21
22#ifdef NXOGRE
23ShapeDescription* createShape(String params)
24{
25        std::vector<String> parameters = StringUtil::split(params);
26       
27        ShapeDescription* s = 0;
28        if(parameters[0] == "cube")
29                s = new CubeShape(StringConverter::parseReal(parameters[1]),//width
30                                                        StringConverter::parseReal(parameters[2]),//height
31                                                        StringConverter::parseReal(parameters[3]));//depth
32        else if(parameters[0] == "sphere")
33                s = new SphereShape(StringConverter::parseReal(parameters[1]));//radius
34        else if(parameters[0] == "capsule")
35                s = new CapsuleShape(StringConverter::parseReal(parameters[1]),//radius
36                                                                StringConverter::parseReal(parameters[2]));//height
37        else if(parameters[0] == "convex_mesh")
38                s = new ConvexShape(parameters[1]);//mesh name
39        else if(parameters[0] == "free_mesh")
40                s = new MeshShape(parameters[1]);//mesh name
41
42        return s;
43}
44#endif
45
46bool parseMeshName(String& params, SceneScriptContext& context)
47{
48        if(params == "")
49                logParseError("No mesh name given!", context);
50
51        //create the Entity with the given mesh, attach to a scene node
52        StringUtil::trim(params);
53        String meshfilename = "";
54        if(context.meshFiles.count(params) != 0)
55                meshfilename = context.meshFiles[params];
56        context.meshName = params;
57
58#ifdef NXOGRE   
59        if(context.meshPhysXShapes.count(context.meshName) != 0)
60        {
61                ShapeDescription *newShape = createShape(context.meshPhysXShapes[context.meshName]);
62                //LogManager::getSingleton().logMessage("meshname: " + meshfilename);
63                context.physXBody = context.scene->createBody(context.entityName, meshfilename, newShape, Vector3(0,0,0), context.meshPhysXShapeParams[context.meshName]);
64                context.entity = context.physXBody->getEntity();
65                context.sceneNode = context.physXBody->getNode();
66        }
67        else
68        {
69                context.entity = context.sceneManager->createEntity(context.entityName, meshfilename);
70                SceneNode* root = context.sceneManager->getRootSceneNode();
71                context.sceneNode = root->createChildSceneNode(context.entityName);
72                context.sceneNode->attachObject(context.entity);       
73        }
74#else
75        context.entity = context.sceneManager->createEntity(context.entityName, meshfilename);
76        SceneNode* root = context.sceneManager->getRootSceneNode();
77        context.sceneNode = root->createChildSceneNode(context.entityName);
78        context.sceneNode->attachObject(context.entity);       
79#endif 
80    return false;
81}
82
83bool parseClusters(String& params, SceneScriptContext& context)
84{
85        if(params == "")
86                logParseError("No clusters given!", context);
87
88        std::vector<String> parameters = StringUtil::split(params);
89        String subentityname = context.entityName + "_SE_" + parameters[0];
90        PathMapClusters clasters;
91        clasters.count = parameters.size() - 2;
92        clasters.clusters = new unsigned int[clasters.count];
93        for(int i = 2; i < parameters.size(); i++)
94                clasters.clusters[i-2] = StringConverter::parseUnsignedInt(parameters.at(i));
95        clasters.pathMapTextureFilename = context.pathMapTextureName + "_" + parameters[0]+ ".dds";
96        clasters.pathMapResolution = context.pathMapResolutions[context.meshName];
97        OgreIlluminationManager::getSingleton().addPathMapClusters(subentityname, clasters);
98       
99    return false;
100}
101
102bool parseCastShadows(String& params, SceneScriptContext& context)
103{
104        context.entity->setCastShadows(StringConverter::parseBool(params));
105
106        return false;
107}
108
109#ifdef NXOGRE
110bool parsePhysXShape(String& params, SceneScriptContext& context)
111{
112        context.meshPhysXShapes[context.meshName] = params;
113
114        return false;
115}
116
117bool parsePhysXMass(String& params, SceneScriptContext& context)
118{
119        if(context.meshPhysXShapeParams.count(context.meshName) == 0)
120                context.meshPhysXShapeParams[context.meshName] = "";
121
122        context.meshPhysXShapeParams[context.meshName] += "mass: " + params + ",";
123
124        return false;
125}
126
127bool parsePhysXDensity(String& params, SceneScriptContext& context)
128{
129        if(context.meshPhysXShapeParams.count(context.meshName) == 0)
130                context.meshPhysXShapeParams[context.meshName] = "";
131
132        context.meshPhysXShapeParams[context.meshName] += "density: " + params + ",";
133       
134        return false;
135}
136
137bool parsePhysXKinematic(String& params, SceneScriptContext& context)
138{
139        if(context.meshPhysXShapeParams.count(context.meshName) == 0)
140                context.meshPhysXShapeParams[context.meshName] = "";
141
142        context.meshPhysXShapeParams[context.meshName] += "kinematic: " + params + ",";
143       
144        return false;
145}
146
147bool parsePhysXStatic(String& params, SceneScriptContext& context)
148{
149        if(context.meshPhysXShapeParams.count(context.meshName) == 0)
150                context.meshPhysXShapeParams[context.meshName] = "";
151
152        context.meshPhysXShapeParams[context.meshName] += "static: " + params + ",";
153       
154        return false;
155}
156
157#endif
158
159bool parseTransform(String& params, SceneScriptContext& context)
160{
161        Matrix4 transformMatrix = StringConverter::parseMatrix4(params);
162       
163        Vector4 x(1,0,0,0);
164        x = transformMatrix * x;
165        Vector4 y(0,1,0,0);
166        y = transformMatrix * y;
167        Vector4 z(0,0,1,0);
168        z = transformMatrix * z;
169
170#ifdef NXOGRE
171        if(context.meshPhysXShapes.count(context.meshName) != 0)
172        {
173                context.sceneNode->scale((Vector3(x.x,x.y,x.z)).length(),
174                                                                (Vector3(y.x,y.y,y.z)).length(),
175                                                                (Vector3(z.x,z.y,z.z)).length());
176                context.physXBody->setGlobalOrientation(transformMatrix.extractQuaternion());
177                context.physXBody->setGlobalPosition(transformMatrix.getTrans());       
178        }
179        else
180        {
181                context.sceneNode->setOrientation(transformMatrix.extractQuaternion());
182                context.sceneNode->scale((Vector3(x.x,x.y,x.z)).length(),
183                                                                (Vector3(y.x,y.y,y.z)).length(),
184                                                                (Vector3(z.x,z.y,z.z)).length());
185                context.sceneNode->translate(transformMatrix.getTrans());
186        }
187#else
188        context.sceneNode->setOrientation(transformMatrix.extractQuaternion());
189        context.sceneNode->scale((Vector3(x.x,x.y,x.z)).length(),
190                                                                (Vector3(y.x,y.y,y.z)).length(),
191                                                                (Vector3(z.x,z.y,z.z)).length());
192        context.sceneNode->translate(transformMatrix.getTrans());
193#endif
194       
195        return false;
196}
197
198bool parseMesh(String& params, SceneScriptContext& context)
199{
200        if(params == "")
201                logParseError("No mesh name given!", context);
202       
203        StringUtil::trim(params);
204        context.meshName = params;
205        context.section = SSS_MESH;
206        return true;
207}
208
209bool parseEntity(String& params, SceneScriptContext& context)
210{
211        if(params == "")
212                logParseError("No entity name given!", context);
213       
214        StringUtil::trim(params);
215        context.entityName = params;
216        context.section = SSS_ENTITY;
217        return true;
218}
219
220bool parsePMRes(String& params, SceneScriptContext& context)
221{
222        context.pathMapResolutions[context.meshName] = StringConverter::parseUnsignedInt(params);
223        return false;
224}
225
226bool parsePRMMapName(String& params, SceneScriptContext& context)
227{
228        context.pathMapTextureName = params;
229        return false;
230}
231
232SceneSerializer::SceneSerializer(SceneManager* sm)
233{
234        mScriptContext.sceneManager = sm;
235        mScriptContext.section = SSS_NONE;
236
237        mRootAttribParsers.insert(AttribParserList::value_type("mesh", (SCENE_ATTRIBUTE_PARSER)parseMesh));
238        mRootAttribParsers.insert(AttribParserList::value_type("entity", (SCENE_ATTRIBUTE_PARSER)parseEntity));
239       
240        mMeshAttribParsers.insert(AttribParserList::value_type("ogrefile", (SCENE_ATTRIBUTE_PARSER)parseMeshFileName));
241        mMeshAttribParsers.insert(AttribParserList::value_type("pathmapresolution", (SCENE_ATTRIBUTE_PARSER)parsePMRes));
242
243        mEntityAttribParsers.insert(AttribParserList::value_type("mesh", (SCENE_ATTRIBUTE_PARSER)parseMeshName));
244        mEntityAttribParsers.insert(AttribParserList::value_type("transformation", (SCENE_ATTRIBUTE_PARSER)parseTransform));
245        mEntityAttribParsers.insert(AttribParserList::value_type("subentity", (SCENE_ATTRIBUTE_PARSER)parseClusters));
246        mEntityAttribParsers.insert(AttribParserList::value_type("pathmapfile", (SCENE_ATTRIBUTE_PARSER)parsePRMMapName));
247        mEntityAttribParsers.insert(AttribParserList::value_type("castShadows", (SCENE_ATTRIBUTE_PARSER)parseCastShadows));
248
249        #ifdef NXOGRE
250        mMeshAttribParsers.insert(AttribParserList::value_type("physXShape", (SCENE_ATTRIBUTE_PARSER)parsePhysXShape));
251        mMeshAttribParsers.insert(AttribParserList::value_type("physXMass", (SCENE_ATTRIBUTE_PARSER)parsePhysXMass));
252        mMeshAttribParsers.insert(AttribParserList::value_type("physXDensity", (SCENE_ATTRIBUTE_PARSER)parsePhysXDensity));
253        mMeshAttribParsers.insert(AttribParserList::value_type("physXKinematic", (SCENE_ATTRIBUTE_PARSER)parsePhysXKinematic));
254        mMeshAttribParsers.insert(AttribParserList::value_type("physXStatic", (SCENE_ATTRIBUTE_PARSER)parsePhysXStatic));
255        #endif
256}
257
258void SceneSerializer::parseEntryPoints(String filename)
259{
260        DataStreamPtr stream;
261        stream = ResourceGroupManager::getSingleton().openResource(filename);
262
263        char buffer[500];
264   
265        stream->readLine(buffer, 500);
266        String line = buffer;
267
268        std::vector<String> tokens = StringUtil::split(line);
269        unsigned int entryPointCnt = StringConverter::parseUnsignedInt(tokens[1]);
270        for(int i= 0; i < entryPointCnt; i++)
271        {
272                stream->readLine(buffer, 500);
273                line = buffer;
274                tokens = StringUtil::split(line);
275                PathMapEntryPoint ep;
276                ep.position = Vector3(StringConverter::parseReal(tokens[1]),
277                                          StringConverter::parseReal(tokens[2]),
278                                                          StringConverter::parseReal(tokens[3]));
279
280                stream->readLine(buffer, 500);
281                line = buffer;
282                tokens = StringUtil::split(line);
283                ep.normal = Vector3(StringConverter::parseReal(tokens[1]),
284                                        StringConverter::parseReal(tokens[2]),
285                                                        StringConverter::parseReal(tokens[3]));
286               
287                stream->readLine(buffer, 500);
288                line = buffer;
289                tokens = StringUtil::split(line);
290                ep.prob = StringConverter::parseReal(tokens[1]);
291
292                OgreIlluminationManager::getSingleton().addPathMapEntryPoint(ep);
293
294                stream->readLine(buffer, 500);
295        }
296        stream->readLine(buffer, 500);
297        line = buffer;
298        tokens = StringUtil::split(line);
299    unsigned int clusterCnt = StringConverter::parseUnsignedInt(tokens[1]);
300        for(int i= 0; i < clusterCnt; i++)
301        {
302                stream->readLine(buffer, 500);
303                unsigned int clusterlength = StringConverter::parseUnsignedInt(buffer);
304                OgreIlluminationManager::getSingleton().addPathMapClusterLength(clusterlength);         
305        }
306}
307
308 void SceneSerializer::parseScript(DataStreamPtr& stream, const String& groupName)
309 {
310    //String line;
311    bool nextIsOpenBrace = false;
312
313    mScriptContext.section = SSS_NONE;       
314    mScriptContext.lineNo = 0;         
315    mScriptContext.filename = stream->getName();
316       
317        char buffer[500];
318        //String summ = stream->getAsString();
319
320    while(!stream->eof())
321    {
322
323                stream->readLine(buffer, 500);
324                               
325        String line = buffer;
326                StringUtil::trim(line,true, true);
327               
328        mScriptContext.lineNo++;
329       
330        // DEBUG LINE
331        // LogManager::getSingleton().logMessage("About to attempt line(#" +
332        //    StringConverter::toString(mScriptContext.lineNo) + "): " + line);
333
334        // Ignore comments & blanks
335        if (!(line.length() == 0 || line.substr(0,2) == "//"))
336        {
337            if (nextIsOpenBrace)
338            {
339                // NB, parser will have changed context already
340                if (line != "{")
341                {
342                    logParseError("Expecting '{' but got " +
343                        line + " instead.", mScriptContext);
344                }
345                nextIsOpenBrace = false;
346            }
347            else
348            {
349                nextIsOpenBrace = parseScriptLine(line);
350            }
351
352        }
353    }
354
355    // Check all braces were closed
356    if (mScriptContext.section != SSS_NONE)
357    {
358        logParseError("Unexpected end of file.", mScriptContext);
359    }   
360
361  }
362
363 bool SceneSerializer::parseScriptLine(String& line)
364{
365    switch(mScriptContext.section)
366    {
367                case SSS_NONE:
368                        if (line == "}")
369                        {
370                                logParseError("Unexpected terminating brace.", mScriptContext);
371                                return false;
372                        }
373                        else
374                        {
375                                // find & invoke a parser
376                                return invokeParser(line, mRootAttribParsers);
377                        }
378                        break;
379                case SSS_MESH:
380                        if (line == "}")
381                        {
382                                mScriptContext.section = SSS_NONE;
383                                mScriptContext.meshName = "";
384                                return false;
385                        }
386                        else
387                        {
388                                // find & invoke a parser
389                                return invokeParser(line, mMeshAttribParsers);
390                        }
391                        break;
392                case SSS_ENTITY:
393                        if (line == "}")
394                        {
395                                // End of technique
396                                mScriptContext.section = SSS_NONE;
397                                mScriptContext.entity = 0;
398                                mScriptContext.entityName = "";
399                                mScriptContext.sceneNode = 0;
400                                return false;
401                        }
402                        else
403                        {
404                                // find & invoke a parser
405                                return invokeParser(line, mEntityAttribParsers);
406                        }
407                        break;
408        }
409}
410
411bool SceneSerializer::invokeParser(String& line, AttribParserList& parsers)
412{
413    // First, split line on first divisor only
414    StringVector splitCmd(StringUtil::split(line, " \t", 1));
415
416    // Find attribute parser
417    AttribParserList::iterator iparser = parsers.find(splitCmd[0]);
418    if (iparser == parsers.end())
419    {
420        // BAD command. BAD!
421        logParseError("Unrecognised command: " + splitCmd[0], mScriptContext);
422        return false;
423    }
424    else
425    {
426        String cmd;
427        if(splitCmd.size() >= 2)
428            cmd = splitCmd[1];
429        // Use parser, make sure we have 2 params before using splitCmd[1]
430        return iparser->second( cmd, mScriptContext );
431    }
432}
Note: See TracBrowser for help on using the repository browser.