Revision: 20788
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20788
Author:   chingachgook
Date:     2009-06-10 20:02:54 +0200 (Wed, 10 Jun 2009)

Log Message:
-----------
Added uvs export. Textures and materials are not exported yet.

Modified Paths:
--------------
    branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp

Modified: 
branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp
===================================================================
--- branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp  
2009-06-10 16:34:36 UTC (rev 20787)
+++ branches/soc-2009-chingachgook/source/blender/collada/DocumentExporter.cpp  
2009-06-10 18:02:54 UTC (rev 20788)
@@ -1,6 +1,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_mesh_types.h"
 extern "C" 
 {
 #include "BKE_DerivedMesh.h"
@@ -48,6 +49,7 @@
                                MFace *mfaces = dm->getFaceArray(dm);
                                int totfaces = dm->getNumFaces(dm);
                                int totverts = dm->getNumVerts(dm);
+                               bool checkTexcoords = false;
 
                                std::string geom_name(ob->id.name);
 
@@ -57,6 +59,7 @@
                                //<source>
                                createVertsSource(sce, mSW, geom_name, dm);
                                createNormalsSource(sce, mSW, geom_name, dm);
+                               checkTexcoords = createTexcoordsSource(sce, 
mSW, geom_name, dm, (Mesh*)ob->data);
 
                                //<vertices>    
                                COLLADASW::Vertices verts(mSW);
@@ -80,24 +83,63 @@
                                                                                
getUrlBySemantics(geom_name, COLLADASW::VERTEX), 0);
                                COLLADASW::Input input3(COLLADASW::NORMAL,
                                                                                
getUrlBySemantics(geom_name, COLLADASW::NORMAL), 0);
+
                                til.push_back(input2);
                                til.push_back(input3);
-                                                       
+                               
+                               //if mesh has uv coords writes input for 
TEXCOORD
+                               if (checkTexcoords == true)
+                                       {
+                                               COLLADASW::Input 
input4(COLLADASW::TEXCOORD,
+                                                                               
                getUrlBySemantics(geom_name, COLLADASW::TEXCOORD), 1);
+                                               til.push_back(input4);
+                                       }
+                               
                                tris.prepareToAppendValues();
-
+                               
                                int i;
+                               int texindex = 0;
                                for (i = 0; i < totfaces; i++) {
                                        MFace *f = &mfaces[i];
-
-                                       // if triangle
-                                       if (f->v4 == 0) {
-                                               tris.appendValues(f->v1, f->v2, 
f->v3);
+                                       
+                                       if (checkTexcoords == true)     {
+                                               // if triangle
+                                               if (f->v4 == 0) {
+                                                       
tris.appendValues(f->v1);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v2);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v3);
+                                                       
tris.appendValues(texindex++);
+                                               }
+                                               // quad
+                                               else {
+                                                       
tris.appendValues(f->v1);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v2);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v3);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v3);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v4);
+                                                       
tris.appendValues(texindex++);
+                                                       
tris.appendValues(f->v1);
+                                                       
tris.appendValues(texindex++);
+                                               }
                                        }
-                                       // quad
                                        else {
-                                               tris.appendValues(f->v1, f->v2, 
f->v3);
-                                               tris.appendValues(f->v3, f->v4, 
f->v1);
-                                       }
+                                               // if triangle
+                                               if (f->v4 == 0) {
+                                                       
tris.appendValues(f->v1, f->v2, f->v3); 
+                                               }
+                                               // quad
+                                               else {
+                                                       
tris.appendValues(f->v1, f->v2, f->v3);
+                                                       
tris.appendValues(f->v3, f->v4, f->v1);
+                                               }
+                                               
+                                       } 
                                }
 
                                tris.closeElement();
@@ -113,36 +155,6 @@
                        base= base->next;
                }
 
-               /*      //openMesh(geoId, geoName, meshId)
-               void openMesh("", "", "");
-               
-               //<source>
-               void sourceCreator(&sce);
-
-               //<vertices>    
-               Vertices verts(&sw);
-               verts.setId();
-               Input input(POSITION, source);
-               InputList inputL(&sw);
-               inputL.push_back(input);
-               verts.add();
-
-               //triangles
-               PrimitivesBase pBase(&sw, CSWC::CSW_ELEMENT_TRIANGLES);
-               Primitive<CSWC::CSW_ELEMENT_TRIANGLES> prim(&sw);
-               prim.setCount();
-               prim.setMaterial();
-               InputList &til = pBase.getInputList();
-               til.push_back(input);
-
-               prim.prepareToAppendValues();
-               prim.appendValues();
-               prim.closeElement();
-               prim.finish();
-               
-               closeMesh();
-               closeGeometry();
-               */
                closeLibrary();
        }
 
@@ -173,7 +185,7 @@
                int i = 0;
                for (i = 0; i < totverts; i++) {
                        source.appendValues(verts[i].co[0], verts[i].co[1], 
verts[i].co[2]);
-                       //      source.appendValues((float)(v->no[i] / 
32767.0));
+                       
                }
                /*closes <float_array>, adds
                  <technique_common>
@@ -185,27 +197,85 @@
 
        /*----------------------------------------------------------*/
        
-       /*      //creates <source> for texcoords
-       void createTexcoordsSource(Scene *sce, COLLADASW::StreamWriter *sw,
-                                                          std::string 
geom_name, DerivedMesh *dm)
+       //creates <source> for texcoords
+       // returns true if mesh has uv data
+       bool createTexcoordsSource(Scene *sce, COLLADASW::StreamWriter *sw,
+                                                          std::string 
geom_name, DerivedMesh *dm, Mesh *me)
        {
-               COLLADASW::FloatSourceF source(sw);
-               source.setId(getIdBySemantics(geom_name, COLLADASW::TEXCOORD));
-               source.setArrayId(geom_name + COLLADA::TEXCOORD + 
ARRAY_ID_SUFFIX);
-               //TODO: replace totverts to totuvs
-               source.setAccessorCount(totverts);
-               source.setAccessorStride(2);
-               COLLADASW::SourceBase::ParameterNameList &param = 
source.getParameterNameList();
-               param.push_back("X");
-               param.push_back("Y");
-       
-               source.prepareToAppendValues();
-               //appends data to <float_array> 
                
+               int totfaces = dm->getNumFaces(dm);
+               MTFace *tface = me->mtface;
+               MFace *mfaces = dm->getFaceArray(dm);
+               if(tface != NULL)
+                       {
+                               
+                               COLLADASW::FloatSourceF source(sw);
+                               source.setId(getIdBySemantics(geom_name, 
COLLADASW::TEXCOORD));
+                               source.setArrayId(getIdBySemantics(geom_name, 
COLLADASW::TEXCOORD) +
+                                                                 
ARRAY_ID_SUFFIX);
+                               source.setAccessorCount(countTris(dm) * 3);
+                               source.setAccessorStride(2);
+                               COLLADASW::SourceBase::ParameterNameList &param 
= source.getParameterNameList();
+                               param.push_back("X");
+                               param.push_back("Y");
+                               
+                               source.prepareToAppendValues();
+                               
+                               int i;
+                               for (i = 0; i < totfaces; i++) {
+                                       MFace *f = &mfaces[i];
+                                       
+                                       // if triangle
+                                       if (f->v4 == 0) {
+                                               
+                                               // get uv1's X coordinate
+                                               
source.appendValues(tface[i].uv[0][0]);
+                                               // get uv1's Y coordinate
+                                               
source.appendValues(tface[i].uv[0][1]);
+                                               // get uv2's X coordinate
+                                               
source.appendValues(tface[i].uv[1][0]);
+                                               // etc...
+                                               
source.appendValues(tface[i].uv[1][1]);
+                                               //uv3
+                                               
source.appendValues(tface[i].uv[2][0]);
+                                               
source.appendValues(tface[i].uv[2][1]);
+                                               
+                                               
+                                       }
+                                       // quad
+                                       else {
+                                               
+                                               // get uv1's X coordinate
+                                               
source.appendValues(tface[i].uv[0][0]);
+                                               // get uv1's Y coordinate
+                                               
source.appendValues(tface[i].uv[0][1]);
+                                               //uv2
+                                               
source.appendValues(tface[i].uv[1][0]);
+                                               
source.appendValues(tface[i].uv[1][1]);
+                                               //uv3
+                                               
source.appendValues(tface[i].uv[2][0]);
+                                               
source.appendValues(tface[i].uv[2][1]);
+                                               //uv3
+                                               
source.appendValues(tface[i].uv[2][0]);
+                                               
source.appendValues(tface[i].uv[2][1]);
+                                               //uv4
+                                               
source.appendValues(tface[i].uv[3][0]);
+                                               
source.appendValues(tface[i].uv[3][1]);
+                                               //uv1
+                                               
source.appendValues(tface[i].uv[0][0]);
+                                               
source.appendValues(tface[i].uv[0][1]);
+                                               
+                                       }
+                               }
+                               
+                               source.finish();
+                               return true;
+                       }
+               return false;
        }
 
-       */
 
+
        //creates <source> for normals
        void createNormalsSource(Scene *sce, COLLADASW::StreamWriter *sw,
                                                         std::string geom_name, 
DerivedMesh *dm)
@@ -239,7 +309,31 @@
                
 
        }
-
+       
+       int countTris(DerivedMesh *dm)
+       {
+               
+               MFace *mfaces = dm->getFaceArray(dm);
+               int totfaces = dm->getNumFaces(dm);
+               
+               int i;
+               int tottri = 0;
+               for (i = 0; i < totfaces; i++) {
+                       MFace *f = &mfaces[i];
+                       
+                       // if triangle
+                       if (f->v4 == 0) {
+                               tottri++;
+                       }
+                       // quad
+                       else {
+                               tottri += 2;
+                       }
+               }
+               return tottri;
+               
+       }
+       
        std::string getIdBySemantics(std::string geom_name, 
COLLADASW::Semantics type) {
                return geom_name +
                        getSuffixBySemantic(type);
@@ -305,43 +399,7 @@
        }
 };
 
-/*
 
-  <library_visual_scenes>
-   <visual_scene>
-    <node>
-        <translate>
-        <rotate>
-        <instance_geometry>
-       </node>
-    ...
-   </visual_scene>
-  </library_visual_scenes>
-
-  <library_geometries>
-   <geometry id="">
-    <mesh>
-        <source id="source_id">
-         <float_array id="" count="">
-          0.0 0.0 0.0
-         </float_array>
-        </source>
-
-        <vertices>
-         <input source="#source_id" ...>
-        </vertices>
-
-        <triangles>
-         <input>
-         <p>
-        </triangles>
-
-       </mesh>
-   </geometry>
-  </library_geometries>
-
- */
-
 void DocumentExporter::exportCurrentScene(Scene *sce, const char* filename)
 {
        COLLADABU::NativeString native_filename =
@@ -363,6 +421,7 @@
        GeometryExporter ge(&sw);
        ge.exportGeom(sce);
 
+
        //<scene>
        std::string scene_name(sce->id.name);
        COLLADASW::Scene scene(&sw, 
COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING,


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to