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

Revision 3120, 5.5 KB checked in by mattausch, 16 years ago (diff)

worked on filtering now trying to reduce flickering (have to reorder ssao function quite much

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