source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreDemos/src/Common/src/SceneSerializer.cpp @ 3255

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