source: OGRE/trunk/ogrenew/Tools/MayaExport/src/submesh.cpp @ 692

Revision 692, 8.7 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1#include "submesh.h"
2
3namespace OgreMayaExporter
4{
5        /***** Class Submesh *****/
6        // constructor
7        Submesh::Submesh(const MString& name)
8        {
9                clear();
10                m_name = name;
11        }
12
13        // destructor
14        Submesh::~Submesh()
15        {
16                clear();
17        }
18
19        // clear data
20        void Submesh::clear()
21        {
22                m_name = "";
23                m_numTriangles = 0;
24                m_pMaterial = NULL;
25                m_vertices.clear();
26                m_faces.clear();
27                m_uvsets.clear();
28                m_use32bitIndexes = false;
29        }
30
31        // return number of triangles composing the mesh
32        long Submesh::numTriangles()
33        {
34                return m_numTriangles;
35        }
36
37        // return number of vertices composing the mesh
38        long Submesh::numVertices()
39        {
40                return m_numVertices;
41        }
42
43        // return submesh name
44        MString& Submesh::name()
45        {
46                return m_name;
47        }
48
49/***** load data *****/
50        MStatus Submesh::loadMaterial(MObject& shader,MStringArray& uvsets,ParamList& params)
51        {
52                MPlug plug;
53                MPlugArray srcplugarray;
54                bool foundShader = false;
55                MStatus stat;
56                MFnLambertShader* pShader;
57                //get shader from shading group
58                MFnDependencyNode shadingGroup(shader);
59                plug = shadingGroup.findPlug("surfaceShader");
60                plug.connectedTo(srcplugarray,true,false,&stat);
61                for (int i=0; i<srcplugarray.length() && !foundShader; i++)
62                {
63                        if (srcplugarray[i].node().hasFn(MFn::kLambert))
64                        {
65                                pShader = new MFnLambertShader(srcplugarray[i].node());
66                                foundShader = true;
67                        }
68                }
69                std::cout << "Found material: " << pShader->name().asChar() << "\n";
70                //check if this material has already been created
71                Material* pMaterial = MaterialSet::getSingleton().getMaterial(pShader->name());
72                //if the material has already been created, update the pointer
73                if (pMaterial)
74                        m_pMaterial = pMaterial;
75                //else create it and add it to the material set
76                else
77                {
78                        pMaterial = new Material();
79                        pMaterial->load(pShader,uvsets,params);
80                        m_pMaterial = pMaterial;
81                        MaterialSet::getSingleton().addMaterial(pMaterial);
82                }
83                //delete temporary shader
84                delete pShader;
85                //loading complete
86                return MS::kSuccess;
87        }
88
89        MStatus Submesh::load(std::vector<face>& faces, std::vector<vertexInfo>& vertInfo, MFloatPointArray& points,
90                MFloatVectorArray& normals, MStringArray& texcoordsets,ParamList& params,bool opposite)
91        {
92                //save uvsets info
93                for (int i=m_uvsets.size(); i<texcoordsets.length(); i++)
94                {
95                        uvset uv;
96                        uv.size = 2;
97                        m_uvsets.push_back(uv);
98                }
99                //iterate over faces array, to retrieve vertices info
100                for (i=0; i<faces.size(); i++)
101                {
102                        face newFace;
103                        // if we are using shared geometry, indexes refer to the vertex buffer of the whole mesh
104                        if (params.useSharedGeom)
105                        {
106                                if(opposite)
107                                {       // reverse order of face vertices for correct culling
108                                        newFace.v[0] = faces[i].v[2];
109                                        newFace.v[1] = faces[i].v[1];
110                                        newFace.v[2] = faces[i].v[0];
111                                }
112                                else
113                                {
114                                        newFace.v[0] = faces[i].v[0];
115                                        newFace.v[1] = faces[i].v[1];
116                                        newFace.v[2] = faces[i].v[2];
117                                }
118                        }
119                        // otherwise we create a vertex buffer for this submesh
120                        else
121                        {       // faces are triangles, so retrieve index of the three vertices
122                                for (int j=0; j<3; j++)
123                                {
124                                        vertex v;
125                                        vertexInfo vInfo = vertInfo[faces[i].v[j]];
126                                        // save vertex coordinates
127                                        v.x = points[vInfo.pointIdx].x;
128                                        v.y = points[vInfo.pointIdx].y;
129                                        v.z = points[vInfo.pointIdx].z;
130                                        // save vertex normal
131                                        if (opposite)
132                                        {
133                                                v.n.x = -normals[vInfo.normalIdx].x;
134                                                v.n.y = -normals[vInfo.normalIdx].y;
135                                                v.n.z = -normals[vInfo.normalIdx].z;
136                                        }
137                                        else
138                                        {
139                                                v.n.x = normals[vInfo.normalIdx].x;
140                                                v.n.y = normals[vInfo.normalIdx].y;
141                                                v.n.z = normals[vInfo.normalIdx].z;
142                                        }
143                                        v.n.normalize();
144                                        // save vertex color
145                                        v.r = vInfo.r;
146                                        v.g = vInfo.g;
147                                        v.b = vInfo.b;
148                                        v.a = vInfo.a;
149                                        // save vertex bone assignements
150                                        for (int k=0; k<vInfo.vba.size(); k++)
151                                        {
152                                                vba newVba;
153                                                newVba.jointIdx = vInfo.jointIds[k];
154                                                newVba.weight = vInfo.vba[k];
155                                                v.vbas.push_back(newVba);
156                                        }
157                                        // save texture coordinates
158                                        for (k=0; k<vInfo.u.size(); k++)
159                                        {
160                                                texcoords newTexCoords;
161                                                newTexCoords.u = vInfo.u[k];
162                                                newTexCoords.v = vInfo.v[k];
163                                                newTexCoords.w = 0;
164                                                v.texcoords.push_back(newTexCoords);
165                                        }
166                                        // add newly created vertex to vertex list
167                                        m_vertices.push_back(v);
168                                        if (opposite)   // reverse order of face vertices to get correct culling
169                                                newFace.v[2-j] = m_vertices.size() - 1;
170                                        else
171                                                newFace.v[j] = m_vertices.size() - 1;
172                                }
173                        }
174                        m_faces.push_back(newFace);
175                }
176                // set use32bitIndexes flag
177                if (params.useSharedGeom || (m_vertices.size() > 65535) || (m_faces.size() > 65535))
178                        m_use32bitIndexes = true;
179                else
180                        m_use32bitIndexes = false;
181                return MS::kSuccess;
182        }
183
184/***** write data *****/
185        MStatus Submesh::writeXML(ParamList &params)
186        {
187                // Start submesh description
188                params.outMesh << "\t\t<submesh ";
189                // Write material name
190                params.outMesh << "material=\"" << m_pMaterial->name().asChar() << "\" ";
191                // Write use32bitIndexes flag
192                params.outMesh << "use32bitindexes=\"";
193                if (m_use32bitIndexes)
194                        params.outMesh << "true";
195                else
196                        params.outMesh << "false";
197                params.outMesh << "\" ";
198                // Write use32bitIndexes flag
199                params.outMesh << "usesharedvertices=\"";
200                if (params.useSharedGeom)
201                        params.outMesh << "true";
202                else
203                        params.outMesh << "false";
204                params.outMesh << "\" ";
205                // Write operation type flag
206                params.outMesh << "operationtype=\"triangle_list\">\n";
207
208                // Write submesh polygons
209                params.outMesh << "\t\t\t<faces count=\"" << m_faces.size() << "\">\n";
210                for (int i=0; i<m_faces.size(); i++)
211                {
212                        params.outMesh << "\t\t\t\t<face v1=\"" << m_faces[i].v[0] << "\" v2=\"" << m_faces[i].v[1] << "\" "
213                                << "v3=\"" << m_faces[i].v[2] << "\"/>\n";
214                }
215                params.outMesh << "\t\t\t</faces>\n";
216
217                // Write mesh geometry
218                if (!params.useSharedGeom)
219                {
220                        params.outMesh << "\t\t\t<geometry vertexcount=\"" << m_vertices.size() << "\">\n";
221                        params.outMesh << "\t\t\t\t<vertexbuffer positions=\"true\" normals=";
222                        if (params.exportVertNorm)
223                                params.outMesh << "\"true\"";
224                        else
225                                params.outMesh << "\"false\"";
226                        params.outMesh << " colours_diffuse=";
227                        if (params.exportVertCol)
228                                params.outMesh << "\"true\"";
229                        else
230                                params.outMesh << "\"false\"";
231                        params.outMesh << " colours_specular=\"false\" texture_coords=\"";
232                        if (params.exportTexCoord)
233                                params.outMesh << m_uvsets.size() << "\">\n";
234                        else
235                                params.outMesh << 0 << "\">\n";
236                        //write vertex data
237                        for (i=0; i<m_vertices.size(); i++)
238                        {
239                                params.outMesh << "\t\t\t\t\t<vertex>\n";
240                                //write vertex position
241                                params.outMesh << "\t\t\t\t\t\t<position x=\"" << m_vertices[i].x << "\" y=\""
242                                        << m_vertices[i].y << "\" " << "z=\"" << m_vertices[i].z << "\"/>\n";
243                                //write vertex normal
244                                if (params.exportVertNorm)
245                                {
246                                        params.outMesh << "\t\t\t\t\t\t<normal x=\"" << m_vertices[i].n.x << "\" y=\""
247                                                << m_vertices[i].n.y << "\" " << "z=\"" << m_vertices[i].n.z << "\"/>\n";
248                                }
249                                //write vertex colour
250                                if (params.exportVertCol)
251                                {
252                                        float r,g,b,a;
253                                        if (params.exportVertColWhite)
254                                        {
255                                                r = g = b = a = 1.0f;
256                                        }
257                                        else
258                                        {
259                                                r = m_vertices[i].r;
260                                                g = m_vertices[i].g;
261                                                b = m_vertices[i].b;
262                                                a = m_vertices[i].a;
263                                        }
264
265                                        params.outMesh << "\t\t\t\t\t\t<colour_diffuse value=\"" << r << " " << g
266                                                << " " << b << " " << a << "\"/>\n";
267                                }//write vertex texture coordinates
268                                if (params.exportTexCoord)
269                                {
270                                        for (int j=0; j<m_uvsets.size(); j++)
271                                        {
272                                                params.outMesh << "\t\t\t\t\t\t<texcoord u=\"" << m_vertices[i].texcoords[j].u << "\" v=\"" <<
273                                                        m_vertices[i].texcoords[j].v << "\"/>\n";
274                                        }
275                                }
276                                params.outMesh << "\t\t\t\t\t</vertex>\n";
277                        }
278                        //end vertex data
279                        params.outMesh << "\t\t\t\t</vertexbuffer>\n";
280                        //end geometry description
281                        params.outMesh << "\t\t\t</geometry>\n";
282
283                        // Write bone assignments
284                        if (params.exportVBA)
285                        {
286                                params.outMesh << "\t\t\t<boneassignments>\n";
287                                for (i=0; i<m_vertices.size(); i++)
288                                {
289                                        for (int j=0; j<m_vertices[i].vbas.size(); j++)
290                                        {
291                                                if (m_vertices[i].vbas[j].weight > 0.001)
292                                                {
293                                                        params.outMesh << "\t\t\t\t<vertexboneassignment vertexindex=\"" << i
294                                                                << "\" boneindex=\"" << m_vertices[i].vbas[j].jointIdx << "\" weight=\""
295                                                                << m_vertices[i].vbas[j].weight <<"\"/>\n";
296                                                }
297                                        }
298                                }
299                                params.outMesh << "\t\t\t</boneassignments>\n";
300                        }
301                }
302                // End submesh description
303                params.outMesh << "\t\t</submesh>\n";
304
305                return MS::kSuccess;
306        }
307
308
309}; //end of namespace
Note: See TracBrowser for help on using the repository browser.