source: GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp @ 3195

Revision 3195, 6.3 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[3107]1#include "VboFormatConverter.h"
2#include "SimpleTri.h"
3#include "SimpleVec.h"
4#include "gzstream.h"
5#include <iostream>
6
7
8using namespace std;
9
10
11VboFormatConverter::VboFormatConverter()
12{}
13
14
15VboFormatConverter::~VboFormatConverter()
16{
17        for (size_t i = 0; i < mGeometry.size(); ++ i)
18        {
19                delete [] mGeometry[i]->mVertices;
20                delete [] mGeometry[i]->mNormals;
[3127]21                delete [] mGeometry[i]->mTangents;
[3107]22                delete [] mGeometry[i]->mTexcoords;
23
24                delete mGeometry[i];
25        }
26       
27        mGeometry.clear();
28}
29
30
[3119]31bool VboFormatConverter::Convert(const string &filename,
32                                                                 const std::string &outputFilename)
[3107]33{
34        mNumShapes = 0;
35       
36        for (size_t i = 0; i < mGeometry.size(); ++ i)
37        {
38                delete [] mGeometry[i]->mVertices;
39                delete [] mGeometry[i]->mNormals;
40                delete [] mGeometry[i]->mTexcoords;
41
42                delete mGeometry[i];
43        }
44       
45        mGeometry.clear();
46
47        if (!ReadFile(filename))
48        {
49                cerr << "could not read file" << endl;
50                return false;
51        }
52
53        if (!WriteFile(outputFilename))
54        {
55                cerr << "could not write file" << endl;
56                return false;
57        }
58
59
60        return true;
61}
62
63
64bool VboFormatConverter::ReadFile(const string &filename)
65{
66        FILE *file;
[3119]67        if ((file = fopen(filename.c_str(), "r")) == NULL) return false;
[3107]68       
69        int line = 0;
70
71        const int len = 10000;
72        char str[len];
73
[3119]74        int numElements;
[3107]75
[3119]76        // first line only holds number of vertices
77        fgets(str, len, file);
78        sscanf(str, "%d", &numElements);
[3107]79
[3119]80        Geometry *geom = new Geometry();
[3107]81
[3119]82        // convert the triangles to geometry
83        geom->mVertexCount = numElements;
[3153]84        geom->mTexcoordCount = numElements;
85
[3119]86        geom->mVertices = new SimpleVec[numElements];
87        geom->mNormals = new SimpleVec[numElements];
[3127]88        geom->mTangents = new SimpleVec[numElements];
[3119]89        geom->mTexcoords = new Texcoord[numElements];
[3107]90
[3119]91        SimpleVec vtx, normal, tangent;
92        Texcoord tex;
[3107]93
[3119]94        cout << "elements: " << numElements << endl;
95        while (fgets(str, len, file) != NULL)
96        {
97                sscanf(str, "%f %f %f %f %f %f %f %f %f %f %f",
[3153]98                       &vtx.x, &vtx.y, &vtx.z,
99                           &normal.x, &normal.y, &normal.z,
[3119]100                           &tex.first, &tex.second,
101                           &tangent.x, &tangent.y, &tangent.z);
[3107]102
[3120]103                const float scale = 0.05f;
[3153]104               
105                geom->mVertices[line].x = vtx.x * scale;
106                geom->mVertices[line].y = vtx.z * scale;
107                geom->mVertices[line].z = vtx.y * scale;
108       
109                geom->mNormals[line].x = normal.x;
110                geom->mNormals[line].y = normal.z;
111                geom->mNormals[line].z = normal.y;
[3107]112
[3153]113                geom->mTangents[line].x = tangent.x;
114                geom->mTangents[line].y = tangent.z;
115                geom->mTangents[line].z = tangent.y;
116
[3154]117                geom->mTexcoords[line].first = tex.first;
118                geom->mTexcoords[line].second = 1.0f - tex.second;
[3107]119
120                ++ line;
121        }
122
[3119]123        mGeometry.push_back(geom);
[3107]124
[3119]125        mNumShapes = 1;
[3107]126        fclose(file);
127       
128        return true;
129}
130
131
132void VboFormatConverter::WriteGeometry(ogzstream &str, Geometry *geom)
133{
134        int vertexCount = geom->mVertexCount;
135        str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
136 
137        str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
138        str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
[3127]139        str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
[3107]140
141        int texCoordCount = geom->mTexcoordCount;
142        str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
143
144        if (texCoordCount)
[3153]145        {
[3107]146                str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
[3153]147        }
[3107]148
[3153]149
[3107]150        ///////
151        //-- texture
152
[3128]153        //int texId = -1;
154        int texId = 0;
155       
[3107]156        str.write(reinterpret_cast<char *>(&texId), sizeof(int));
[3128]157        //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
[3107]158
159        bool alphaTestEnabled = false;
[3146]160        //bool cullFaceEnabled = false;
161        bool cullFaceEnabled = true;
[3107]162
163        str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
164        str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
165
166        // material
167        bool hasMaterial = true;
168        //bool hasMaterial = false;
169        str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
170       
171        if (hasMaterial)
172        {
[3128]173                SimpleVec ambient, diffuse, spec, emm;
[3107]174
175                ambient.x = ambient.y = ambient.z = 0.2f;
[3193]176                //diffuse.x = 0.7f; diffuse.y = 0.4f; diffuse.z = 0.2f;
[3195]177                diffuse.x = 0.4f; diffuse.y = 0.6f; diffuse.z = 0.6f;
[3155]178                //diffuse.x = diffuse.y = diffuse.z = 1.0f;
[3154]179                spec.x = spec.y = spec.z = .0f;
[3128]180                emm = spec;
[3107]181
182                // only write rgb part of the material
183                str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
184                str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
[3128]185                str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
186                str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
[3107]187        }
188}
189
190
191bool VboFormatConverter::WriteFile(const string &filename)
192{
193        ogzstream ofile(filename.c_str());
194
195        if (!ofile.is_open())
196                return false;
197       
198
199        /////////
200        //-- write textures
201
[3128]202        int textureCount = 1;
203        //int textureCount = 0;
[3107]204
205        ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
206
207        if (textureCount > 0)
208        {
209                // hack
[3152]210                const string texName("FischiNormalmap.png");
[3128]211                //const string texName("wood.jpg");
[3107]212
213                int texnameSize = (int)texName.length() + 1;
214                ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
215
216                ofile.write(texName.c_str(), sizeof(char) * texnameSize);
217
[3152]218                // set boundary to repeat
[3154]219                int boundS, boundT;
220                boundS = boundT = 1;
[3152]221               
[3107]222                ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
223                ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
224        }
225
226
227        ///////////
228        //-- write shapes
229
230        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
231
232        vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
233
234        for (it = mGeometry.begin(); it != it_end; ++ it)
235        {
236                WriteGeometry(ofile, *it);
237        }
238
239
240        int entityCount = 1;
241        ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
242
243
244        //////////
245        //-- write single scene entity
246
247        // no transformation
248        bool hasTrafo = false;
249        ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
250
251        // a dummy lod
252        int numLODs = 1;
253        ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
254
255        float dist = 0;
256        ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
257
258        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
259
260        // all shapes belong to this scene entity
261        for (int i = 0; i < mNumShapes; ++ i)
262        {
263                int shapeId = i;
264                ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
265        }
266
267        return true;
268}
Note: See TracBrowser for help on using the repository browser.