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

Revision 3161, 6.3 KB checked in by mattausch, 16 years ago (diff)

not working ....

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]->mTangents;
22                delete [] mGeometry[i]->mTexcoords;
23
24                delete mGeometry[i];
25        }
26       
27        mGeometry.clear();
28}
29
30
31bool VboFormatConverter::Convert(const string &filename,
32                                                                 const std::string &outputFilename)
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;
67        if ((file = fopen(filename.c_str(), "r")) == NULL) return false;
68       
69        int line = 0;
70
71        const int len = 10000;
72        char str[len];
73
74        int numElements;
75
76        // first line only holds number of vertices
77        fgets(str, len, file);
78        sscanf(str, "%d", &numElements);
79
80        Geometry *geom = new Geometry();
81
82        // convert the triangles to geometry
83        geom->mVertexCount = numElements;
84        geom->mTexcoordCount = numElements;
85
86        geom->mVertices = new SimpleVec[numElements];
87        geom->mNormals = new SimpleVec[numElements];
88        geom->mTangents = new SimpleVec[numElements];
89        geom->mTexcoords = new Texcoord[numElements];
90
91        SimpleVec vtx, normal, tangent;
92        Texcoord tex;
93
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",
98                       &vtx.x, &vtx.y, &vtx.z,
99                           &normal.x, &normal.y, &normal.z,
100                           &tex.first, &tex.second,
101                           &tangent.x, &tangent.y, &tangent.z);
102
103                const float scale = 0.05f;
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;
112
113                geom->mTangents[line].x = tangent.x;
114                geom->mTangents[line].y = tangent.z;
115                geom->mTangents[line].z = tangent.y;
116
117                geom->mTexcoords[line].first = tex.first;
118                geom->mTexcoords[line].second = 1.0f - tex.second;
119
120                ++ line;
121        }
122
123        mGeometry.push_back(geom);
124
125        mNumShapes = 1;
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);
139        str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
140
141        int texCoordCount = geom->mTexcoordCount;
142        str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
143
144        if (texCoordCount)
145        {
146                str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
147        }
148
149
150        ///////
151        //-- texture
152
153        //int texId = -1;
154        int texId = 0;
155       
156        str.write(reinterpret_cast<char *>(&texId), sizeof(int));
157        //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
158
159        bool alphaTestEnabled = false;
160        //bool cullFaceEnabled = false;
161        bool cullFaceEnabled = true;
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        {
173                SimpleVec ambient, diffuse, spec, emm;
174
175                ambient.x = ambient.y = ambient.z = 0.2f;
176                diffuse.x = 0.7f; diffuse.y = 0.4f; diffuse.z = 0.2f;
177                //diffuse.x = diffuse.y = diffuse.z = 1.0f;
178                spec.x = spec.y = spec.z = .0f;
179                emm = spec;
180
181                // only write rgb part of the material
182                str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
183                str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
184                str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
185                str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
186        }
187}
188
189
190bool VboFormatConverter::WriteFile(const string &filename)
191{
192        ogzstream ofile(filename.c_str());
193
194        if (!ofile.is_open())
195                return false;
196       
197
198        /////////
199        //-- write textures
200
201        int textureCount = 1;
202        //int textureCount = 0;
203
204        ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
205
206        if (textureCount > 0)
207        {
208                // hack
209                const string texName("FischiNormalmap.png");
210                //const string texName("wood.jpg");
211
212                int texnameSize = (int)texName.length() + 1;
213                ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
214
215                ofile.write(texName.c_str(), sizeof(char) * texnameSize);
216
217                // set boundary to repeat
218                int boundS, boundT;
219                boundS = boundT = 1;
220               
221                ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
222                ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
223        }
224
225
226        ///////////
227        //-- write shapes
228
229        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
230
231        vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
232
233        for (it = mGeometry.begin(); it != it_end; ++ it)
234        {
235                WriteGeometry(ofile, *it);
236        }
237
238
239        int entityCount = 1;
240        ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
241
242
243        //////////
244        //-- write single scene entity
245
246        // no transformation
247        bool hasTrafo = false;
248        ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
249
250        // a dummy lod
251        int numLODs = 1;
252        ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
253
254        float dist = 0;
255        ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
256
257        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
258
259        // all shapes belong to this scene entity
260        for (int i = 0; i < mNumShapes; ++ i)
261        {
262                int shapeId = i;
263                ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
264        }
265
266        return true;
267}
Note: See TracBrowser for help on using the repository browser.