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

Revision 2208, 9.1 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                stream->readLine(buffer, 500);
160                line = buffer;
161                tokens = StringUtil::split(line);
162                ep.prob = StringConverter::parseReal(tokens[1]);
163
164                OgreIlluminationManager::getSingleton().addPathMapEntryPoint(ep);
165
166                stream->readLine(buffer, 500);
167        }
168        stream->readLine(buffer, 500);
169        line = buffer;
170        tokens = StringUtil::split(line);
171    unsigned int clusterCnt = StringConverter::parseUnsignedInt(tokens[1]);
172        for(int i= 0; i < clusterCnt; i++)
173        {
174                stream->readLine(buffer, 500);
175                unsigned int clusterlength = StringConverter::parseUnsignedInt(buffer);
176                OgreIlluminationManager::getSingleton().addPathMapClusterLength(clusterlength);         
177        }
178}
179
180 void SceneSerializer::parseScript(DataStreamPtr& stream, const String& groupName)
181 {
182    //String line;
183    bool nextIsOpenBrace = false;
184
185    mScriptContext.section = SSS_NONE;       
186    mScriptContext.lineNo = 0;         
187    mScriptContext.filename = stream->getName();
188       
189        char buffer[500];
190        //String summ = stream->getAsString();
191
192    while(!stream->eof())
193    {
194
195                stream->readLine(buffer, 500);
196                               
197        String line = buffer;
198                StringUtil::trim(line,true, true);
199               
200        mScriptContext.lineNo++;
201       
202        // DEBUG LINE
203        // LogManager::getSingleton().logMessage("About to attempt line(#" +
204        //    StringConverter::toString(mScriptContext.lineNo) + "): " + line);
205
206        // Ignore comments & blanks
207        if (!(line.length() == 0 || line.substr(0,2) == "//"))
208        {
209            if (nextIsOpenBrace)
210            {
211                // NB, parser will have changed context already
212                if (line != "{")
213                {
214                    logParseError("Expecting '{' but got " +
215                        line + " instead.", mScriptContext);
216                }
217                nextIsOpenBrace = false;
218            }
219            else
220            {
221                nextIsOpenBrace = parseScriptLine(line);
222            }
223
224        }
225    }
226
227    // Check all braces were closed
228    if (mScriptContext.section != SSS_NONE)
229    {
230        logParseError("Unexpected end of file.", mScriptContext);
231    }   
232
233  }
234
235 bool SceneSerializer::parseScriptLine(String& line)
236{
237    switch(mScriptContext.section)
238    {
239                case SSS_NONE:
240                        if (line == "}")
241                        {
242                                logParseError("Unexpected terminating brace.", mScriptContext);
243                                return false;
244                        }
245                        else
246                        {
247                                // find & invoke a parser
248                                return invokeParser(line, mRootAttribParsers);
249                        }
250                        break;
251                case SSS_MESH:
252                        if (line == "}")
253                        {
254                                mScriptContext.section = SSS_NONE;
255                                mScriptContext.meshName = "";
256                                return false;
257                        }
258                        else
259                        {
260                                // find & invoke a parser
261                                return invokeParser(line, mMeshAttribParsers);
262                        }
263                        break;
264                case SSS_ENTITY:
265                        if (line == "}")
266                        {
267                                // End of technique
268                                mScriptContext.section = SSS_NONE;
269                                mScriptContext.entity = 0;
270                                mScriptContext.entityName = "";
271                                mScriptContext.sceneNode = 0;
272                                return false;
273                        }
274                        else
275                        {
276                                // find & invoke a parser
277                                return invokeParser(line, mEntityAttribParsers);
278                        }
279                        break;
280        }
281}
282
283bool SceneSerializer::invokeParser(String& line, AttribParserList& parsers)
284{
285    // First, split line on first divisor only
286    StringVector splitCmd(StringUtil::split(line, " \t", 1));
287
288    // Find attribute parser
289    AttribParserList::iterator iparser = parsers.find(splitCmd[0]);
290    if (iparser == parsers.end())
291    {
292        // BAD command. BAD!
293        logParseError("Unrecognised command: " + splitCmd[0], mScriptContext);
294        return false;
295    }
296    else
297    {
298        String cmd;
299        if(splitCmd.size() >= 2)
300            cmd = splitCmd[1];
301        // Use parser, make sure we have 2 params before using splitCmd[1]
302        return iparser->second( cmd, mScriptContext );
303    }
304}
Note: See TracBrowser for help on using the repository browser.