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

Revision 2201, 9.2 KB checked in by szirmay, 17 years ago (diff)
Line 
1#include "SceneSerializer.h"
2#include "OgreIlluminationManager.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
22bool parseMeshName(String& params, SceneScriptContext& context)
23{
24        if(params == "")
25                logParseError("No mesh name given!", context);
26
27        //create the Entity with the given mesh, attach to a scene node
28        StringUtil::trim(params);
29        String meshfilename = context.meshFiles[params];
30        context.meshName = params;
31        context.entity = context.sceneManager->createEntity(context.entityName, meshfilename);
32        SceneNode* root = context.sceneManager->getRootSceneNode();
33        context.sceneNode = root->createChildSceneNode(context.entityName);
34        context.sceneNode->attachObject(context.entity);
35
36    return false;
37}
38
39bool parseClusters(String& params, SceneScriptContext& context)
40{
41        if(params == "")
42                logParseError("No clusters given!", context);
43
44        //create the Entity with the given mesh, attach to a scene node
45        std::vector<String> parameters = StringUtil::split(params);
46        String subentityname = context.entityName + "_SE_" + parameters[0];
47        PathMapClusters clasters;
48        clasters.count = parameters.size() - 2;
49        clasters.clusters = new unsigned int[clasters.count];
50        for(int i = 2; i < parameters.size(); i++)
51                clasters.clusters[i-2] = StringConverter::parseUnsignedInt(parameters.at(i));
52        clasters.pathMapTextureFilename = context.pathMapTextureName + "_" + parameters[0]+ ".dds";
53        clasters.pathMapResolution = context.pathMapResolutions[context.meshName];
54        OgreIlluminationManager::getSingleton().addPathMapClusters(subentityname, clasters);
55       
56    return false;
57}
58
59bool parseTransform(String& params, SceneScriptContext& context)
60{
61        Matrix4 transformMatrix = StringConverter::parseMatrix4(params);
62        context.sceneNode->setOrientation(transformMatrix.extractQuaternion());
63       
64        Vector4 x(1,0,0,0);
65        x = transformMatrix * x;
66        Vector4 y(0,1,0,0);
67        y = transformMatrix * y;
68        Vector4 z(0,0,1,0);
69        z = transformMatrix * z;
70        context.sceneNode->scale((Vector3(x.x,x.y,x.z)).length(),
71                                                                (Vector3(y.x,y.y,y.z)).length(),
72                                                                (Vector3(z.x,z.y,z.z)).length());
73        context.sceneNode->translate(transformMatrix.getTrans());
74       
75        return false;
76}
77
78bool parseMesh(String& params, SceneScriptContext& context)
79{
80        if(params == "")
81                logParseError("No mesh name given!", context);
82       
83        StringUtil::trim(params);
84        context.meshName = params;
85        context.section = SSS_MESH;
86        return true;
87}
88
89bool parseEntity(String& params, SceneScriptContext& context)
90{
91        if(params == "")
92                logParseError("No entity name given!", context);
93       
94        StringUtil::trim(params);
95        context.entityName = params;
96        context.section = SSS_ENTITY;
97        return true;
98}
99
100bool parsePMRes(String& params, SceneScriptContext& context)
101{
102        context.pathMapResolutions[context.meshName] = StringConverter::parseUnsignedInt(params);
103        return false;
104}
105
106bool parsePRMMapName(String& params, SceneScriptContext& context)
107{
108        context.pathMapTextureName = params;
109        return false;
110}
111
112SceneSerializer::SceneSerializer(SceneManager* sm)
113{
114        mScriptContext.sceneManager = sm;
115        mScriptContext.section = SSS_NONE;
116
117        mRootAttribParsers.insert(AttribParserList::value_type("mesh", (SCENE_ATTRIBUTE_PARSER)parseMesh));
118        mRootAttribParsers.insert(AttribParserList::value_type("entity", (SCENE_ATTRIBUTE_PARSER)parseEntity));
119       
120        mMeshAttribParsers.insert(AttribParserList::value_type("ogrefile", (SCENE_ATTRIBUTE_PARSER)parseMeshFileName));
121        mMeshAttribParsers.insert(AttribParserList::value_type("pathmapresolution", (SCENE_ATTRIBUTE_PARSER)parsePMRes));
122
123        mEntityAttribParsers.insert(AttribParserList::value_type("mesh", (SCENE_ATTRIBUTE_PARSER)parseMeshName));
124        mEntityAttribParsers.insert(AttribParserList::value_type("transformation", (SCENE_ATTRIBUTE_PARSER)parseTransform));
125        mEntityAttribParsers.insert(AttribParserList::value_type("subentity", (SCENE_ATTRIBUTE_PARSER)parseClusters));
126        mEntityAttribParsers.insert(AttribParserList::value_type("pathmapfile", (SCENE_ATTRIBUTE_PARSER)parsePRMMapName));
127
128}
129
130void SceneSerializer::parseEntryPoints(String filename)
131{
132        DataStreamPtr stream;
133        stream = ResourceGroupManager::getSingleton().openResource(filename);
134
135        char buffer[500];
136   
137        stream->readLine(buffer, 500);
138        String line = buffer;
139
140        std::vector<String> tokens = StringUtil::split(line);
141        unsigned int entryPointCnt = StringConverter::parseUnsignedInt(tokens[1]);
142        for(int i= 0; i < entryPointCnt; i++)
143        {
144                stream->readLine(buffer, 500);
145                line = buffer;
146                tokens = StringUtil::split(line);
147                PathMapEntryPoint ep;
148                ep.position = Vector3(StringConverter::parseReal(tokens[1]),
149                                          StringConverter::parseReal(tokens[2]),
150                                                          StringConverter::parseReal(tokens[3]));
151
152                stream->readLine(buffer, 500);
153                line = buffer;
154                tokens = StringUtil::split(line);
155                ep.normal = Vector3(StringConverter::parseReal(tokens[1]),
156                                        StringConverter::parseReal(tokens[2]),
157                                                        StringConverter::parseReal(tokens[3]));
158
159                OgreIlluminationManager::getSingleton().addPathMapEntryPoint(ep);
160
161                stream->readLine(buffer, 500);
162                line = buffer;
163                tokens = StringUtil::split(line);
164                ep.position = Vector3(StringConverter::parseReal(tokens[1]));
165
166                OgreIlluminationManager::getSingleton().addPathMapEntryPoint(ep);
167
168                stream->readLine(buffer, 500);
169        }
170        stream->readLine(buffer, 500);
171        line = buffer;
172        tokens = StringUtil::split(line);
173    unsigned int clusterCnt = StringConverter::parseUnsignedInt(tokens[1]);
174        for(int i= 0; i < clusterCnt; i++)
175        {
176                stream->readLine(buffer, 500);
177                unsigned int clusterlength = StringConverter::parseUnsignedInt(buffer);
178                OgreIlluminationManager::getSingleton().addPathMapClusterLength(clusterlength);         
179        }
180}
181
182 void SceneSerializer::parseScript(DataStreamPtr& stream, const String& groupName)
183 {
184    //String line;
185    bool nextIsOpenBrace = false;
186
187    mScriptContext.section = SSS_NONE;       
188    mScriptContext.lineNo = 0;         
189    mScriptContext.filename = stream->getName();
190       
191        char buffer[500];
192        //String summ = stream->getAsString();
193
194    while(!stream->eof())
195    {
196
197                stream->readLine(buffer, 500);
198                               
199        String line = buffer;
200                StringUtil::trim(line,true, true);
201               
202        mScriptContext.lineNo++;
203       
204        // DEBUG LINE
205        // LogManager::getSingleton().logMessage("About to attempt line(#" +
206        //    StringConverter::toString(mScriptContext.lineNo) + "): " + line);
207
208        // Ignore comments & blanks
209        if (!(line.length() == 0 || line.substr(0,2) == "//"))
210        {
211            if (nextIsOpenBrace)
212            {
213                // NB, parser will have changed context already
214                if (line != "{")
215                {
216                    logParseError("Expecting '{' but got " +
217                        line + " instead.", mScriptContext);
218                }
219                nextIsOpenBrace = false;
220            }
221            else
222            {
223                nextIsOpenBrace = parseScriptLine(line);
224            }
225
226        }
227    }
228
229    // Check all braces were closed
230    if (mScriptContext.section != SSS_NONE)
231    {
232        logParseError("Unexpected end of file.", mScriptContext);
233    }   
234
235  }
236
237 bool SceneSerializer::parseScriptLine(String& line)
238{
239    switch(mScriptContext.section)
240    {
241                case SSS_NONE:
242                        if (line == "}")
243                        {
244                                logParseError("Unexpected terminating brace.", mScriptContext);
245                                return false;
246                        }
247                        else
248                        {
249                                // find & invoke a parser
250                                return invokeParser(line, mRootAttribParsers);
251                        }
252                        break;
253                case SSS_MESH:
254                        if (line == "}")
255                        {
256                                mScriptContext.section = SSS_NONE;
257                                mScriptContext.meshName = "";
258                                return false;
259                        }
260                        else
261                        {
262                                // find & invoke a parser
263                                return invokeParser(line, mMeshAttribParsers);
264                        }
265                        break;
266                case SSS_ENTITY:
267                        if (line == "}")
268                        {
269                                // End of technique
270                                mScriptContext.section = SSS_NONE;
271                                mScriptContext.entity = 0;
272                                mScriptContext.entityName = "";
273                                mScriptContext.sceneNode = 0;
274                                return false;
275                        }
276                        else
277                        {
278                                // find & invoke a parser
279                                return invokeParser(line, mEntityAttribParsers);
280                        }
281                        break;
282        }
283}
284
285bool SceneSerializer::invokeParser(String& line, AttribParserList& parsers)
286{
287    // First, split line on first divisor only
288    StringVector splitCmd(StringUtil::split(line, " \t", 1));
289
290    // Find attribute parser
291    AttribParserList::iterator iparser = parsers.find(splitCmd[0]);
292    if (iparser == parsers.end())
293    {
294        // BAD command. BAD!
295        logParseError("Unrecognised command: " + splitCmd[0], mScriptContext);
296        return false;
297    }
298    else
299    {
300        String cmd;
301        if(splitCmd.size() >= 2)
302            cmd = splitCmd[1];
303        // Use parser, make sure we have 2 params before using splitCmd[1]
304        return iparser->second( cmd, mScriptContext );
305    }
306}
Note: See TracBrowser for help on using the repository browser.