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

Revision 3146, 5.8 KB checked in by mattausch, 16 years ago (diff)

normal mapping hack not working yet. found problems with ssao if the geometry is not tesselated enough (especially with smoothed
normals: one can see the underlying tesselation!

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->mVertices = new SimpleVec[numElements];
85        geom->mNormals = new SimpleVec[numElements];
86        geom->mTangents = new SimpleVec[numElements];
87        geom->mTexcoords = new Texcoord[numElements];
88
89        SimpleVec vtx, normal, tangent;
90        Texcoord tex;
91
92        cout << "elements: " << numElements << endl;
93        while (fgets(str, len, file) != NULL)
94        {
95                sscanf(str, "%f %f %f %f %f %f %f %f %f %f %f",
96                       &vtx.x, &vtx.z, &vtx.y,
97                           &normal.x, &normal.z, &normal.y,
98                           &tex.first, &tex.second,
99                           &tangent.x, &tangent.y, &tangent.z);
100
101                const float scale = 0.05f;
102                vtx.x *= scale;
103                vtx.y *= scale;
104                vtx.z *= scale;
105
106                geom->mVertices[line] = vtx;
107                geom->mNormals[line] = normal;
108                geom->mTexcoords[line] = tex;
109                geom->mTangents[line] = tangent;
110
111                ++ line;
112        }
113
114        mGeometry.push_back(geom);
115
116        mNumShapes = 1;
117        fclose(file);
118       
119        return true;
120}
121
122
123void VboFormatConverter::WriteGeometry(ogzstream &str, Geometry *geom)
124{
125        int vertexCount = geom->mVertexCount;
126        str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
127 
128        str.write(reinterpret_cast<char *>(geom->mVertices), sizeof(SimpleVec) * vertexCount);
129        str.write(reinterpret_cast<char *>(geom->mNormals), sizeof(SimpleVec) * vertexCount);
130        str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
131
132        int texCoordCount = geom->mTexcoordCount;
133        str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
134
135        if (texCoordCount)
136                str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
137       
138
139        ///////
140        //-- texture
141
142        //int texId = -1;
143        int texId = 0;
144       
145        str.write(reinterpret_cast<char *>(&texId), sizeof(int));
146        //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
147
148        bool alphaTestEnabled = false;
149        //bool cullFaceEnabled = false;
150        bool cullFaceEnabled = true;
151
152        str.write(reinterpret_cast<char *>(&alphaTestEnabled), sizeof(bool));
153        str.write(reinterpret_cast<char *>(&cullFaceEnabled), sizeof(bool));
154
155        // material
156        bool hasMaterial = true;
157        //bool hasMaterial = false;
158        str.write(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
159       
160        if (hasMaterial)
161        {
162                SimpleVec ambient, diffuse, spec, emm;
163
164                ambient.x = ambient.y = ambient.z = 0.2f;
165                diffuse.x = diffuse.y = diffuse.z = 1.0f;
166                spec.x   = spec.y   = spec.z   =  .0f;
167                emm = spec;
168
169                // only write rgb part of the material
170                str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
171                str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
172                str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
173                str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
174        }
175}
176
177
178bool VboFormatConverter::WriteFile(const string &filename)
179{
180        ogzstream ofile(filename.c_str());
181
182        if (!ofile.is_open())
183                return false;
184       
185
186        /////////
187        //-- write textures
188
189        int textureCount = 1;
190        //int textureCount = 0;
191
192        ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
193
194        if (textureCount > 0)
195        {
196                // hack
197                const string texName("fischi_u1_v1.png");
198                //const string texName("wood.jpg");
199
200                int texnameSize = (int)texName.length() + 1;
201                ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
202
203                ofile.write(texName.c_str(), sizeof(char) * texnameSize);
204
205                int boundS = 1, boundT = 1;
206
207                ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
208                ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
209        }
210
211
212        ///////////
213        //-- write shapes
214
215        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
216
217        vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
218
219        for (it = mGeometry.begin(); it != it_end; ++ it)
220        {
221                WriteGeometry(ofile, *it);
222        }
223
224
225        int entityCount = 1;
226        ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
227
228
229        //////////
230        //-- write single scene entity
231
232        // no transformation
233        bool hasTrafo = false;
234        ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
235
236        // a dummy lod
237        int numLODs = 1;
238        ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
239
240        float dist = 0;
241        ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
242
243        ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
244
245        // all shapes belong to this scene entity
246        for (int i = 0; i < mNumShapes; ++ i)
247        {
248                int shapeId = i;
249                ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
250        }
251
252        return true;
253}
Note: See TracBrowser for help on using the repository browser.