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

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