[3107] | 1 | #include "VboFormatConverter.h"
|
---|
| 2 | #include "SimpleTri.h"
|
---|
| 3 | #include "SimpleVec.h"
|
---|
| 4 | #include "gzstream.h"
|
---|
| 5 | #include <iostream>
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | using namespace std;
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | VboFormatConverter::VboFormatConverter()
|
---|
| 12 | {}
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | VboFormatConverter::~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] | 31 | bool 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 |
|
---|
| 64 | bool 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;
|
---|
| 84 | geom->mVertices = new SimpleVec[numElements];
|
---|
| 85 | geom->mNormals = new SimpleVec[numElements];
|
---|
[3127] | 86 | geom->mTangents = new SimpleVec[numElements];
|
---|
[3119] | 87 | geom->mTexcoords = new Texcoord[numElements];
|
---|
[3107] | 88 |
|
---|
[3119] | 89 | SimpleVec vtx, normal, tangent;
|
---|
| 90 | Texcoord tex;
|
---|
[3107] | 91 |
|
---|
[3119] | 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);
|
---|
[3107] | 100 |
|
---|
[3120] | 101 | const float scale = 0.05f;
|
---|
| 102 | vtx.x *= scale;
|
---|
| 103 | vtx.y *= scale;
|
---|
| 104 | vtx.z *= scale;
|
---|
[3107] | 105 |
|
---|
[3119] | 106 | geom->mVertices[line] = vtx;
|
---|
| 107 | geom->mNormals[line] = normal;
|
---|
| 108 | geom->mTexcoords[line] = tex;
|
---|
[3127] | 109 | geom->mTangents[line] = tangent;
|
---|
[3107] | 110 |
|
---|
| 111 | ++ line;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3119] | 114 | mGeometry.push_back(geom);
|
---|
[3107] | 115 |
|
---|
[3119] | 116 | mNumShapes = 1;
|
---|
[3107] | 117 | fclose(file);
|
---|
| 118 |
|
---|
| 119 | return true;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | void 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);
|
---|
[3127] | 130 | str.write(reinterpret_cast<char *>(geom->mTangents), sizeof(SimpleVec) * vertexCount);
|
---|
[3107] | 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 |
|
---|
[3128] | 142 | //int texId = -1;
|
---|
| 143 | int texId = 0;
|
---|
| 144 |
|
---|
[3107] | 145 | str.write(reinterpret_cast<char *>(&texId), sizeof(int));
|
---|
[3128] | 146 | //str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
|
---|
[3107] | 147 |
|
---|
| 148 | bool alphaTestEnabled = false;
|
---|
[3146] | 149 | //bool cullFaceEnabled = false;
|
---|
| 150 | bool cullFaceEnabled = true;
|
---|
[3107] | 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 | {
|
---|
[3128] | 162 | SimpleVec ambient, diffuse, spec, emm;
|
---|
[3107] | 163 |
|
---|
| 164 | ambient.x = ambient.y = ambient.z = 0.2f;
|
---|
| 165 | diffuse.x = diffuse.y = diffuse.z = 1.0f;
|
---|
[3128] | 166 | spec.x = spec.y = spec.z = .0f;
|
---|
| 167 | emm = spec;
|
---|
[3107] | 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));
|
---|
[3128] | 172 | str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
|
---|
| 173 | str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
|
---|
[3107] | 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | bool 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 |
|
---|
[3128] | 189 | int textureCount = 1;
|
---|
| 190 | //int textureCount = 0;
|
---|
[3107] | 191 |
|
---|
| 192 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
| 193 |
|
---|
| 194 | if (textureCount > 0)
|
---|
| 195 | {
|
---|
| 196 | // hack
|
---|
[3128] | 197 | const string texName("fischi_u1_v1.png");
|
---|
| 198 | //const string texName("wood.jpg");
|
---|
[3107] | 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 | } |
---|