[2957] | 1 | #include "ObjConverter.h"
|
---|
| 2 | #include "SceneEntity.h"
|
---|
| 3 | #include "Transform3.h"
|
---|
| 4 | #include "Geometry.h"
|
---|
| 5 | #include "Shape.h"
|
---|
| 6 | #include "Material.h"
|
---|
| 7 | #include "Texture.h"
|
---|
| 8 | #include <map>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | using namespace CHCDemoEngine;
|
---|
| 12 | using namespace std;
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | static void LoadIndices(char *str,
|
---|
| 16 | const VertexArray &vertices,
|
---|
| 17 | const VertexArray &normals,
|
---|
| 18 | const vector<pair<float, float> > &texcoords,
|
---|
| 19 | VertexArray &faceVertices,
|
---|
| 20 | VertexArray &faceNormals,
|
---|
| 21 | vector<pair<float, float> > &faceTexcoords
|
---|
| 22 | )
|
---|
| 23 | {
|
---|
| 24 | vector<char *> substrings;
|
---|
| 25 |
|
---|
| 26 | // extract the triples of the form v/t/n v/t/n ...
|
---|
| 27 | char *pch = strtok(str + 1, " ");
|
---|
| 28 |
|
---|
| 29 | while (pch != NULL)
|
---|
| 30 | {
|
---|
| 31 | substrings.push_back(pch);
|
---|
| 32 | pch = strtok(NULL, " ");
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | vector<int> indices;
|
---|
| 36 | vector<int> nIndices;
|
---|
| 37 | vector<int> tIndices;
|
---|
| 38 |
|
---|
| 39 | for (size_t i = 0; i < substrings.size(); ++ i)
|
---|
| 40 | {
|
---|
| 41 | // vertex, normal and texture indices
|
---|
| 42 | char *str = strtok(substrings[i], "/");
|
---|
| 43 | int index = (int)strtol(str, NULL, 10) - 1;
|
---|
| 44 |
|
---|
| 45 | str = strtok(substrings[i], "/");
|
---|
| 46 | int tIndex = (int)strtol(str, NULL, 10) - 1;
|
---|
| 47 |
|
---|
| 48 | str = strtok(substrings[i], "/");
|
---|
| 49 | int nIndex = (int)strtol(str, NULL, 10) - 1;
|
---|
| 50 |
|
---|
| 51 | // store indices
|
---|
| 52 | if (index >= 0)
|
---|
| 53 | {
|
---|
| 54 | indices.push_back(index);
|
---|
| 55 | nIndices.push_back(nIndex);
|
---|
| 56 | tIndices.push_back(tIndex);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | // new triangle found
|
---|
| 60 | if (indices.size() > 2)
|
---|
| 61 | {
|
---|
| 62 | int idx2 = (int)indices.size() - 2;
|
---|
| 63 | int idx3 = (int)indices.size() - 1;
|
---|
| 64 |
|
---|
| 65 | faceVertices.push_back(vertices[indices[0]]);
|
---|
| 66 | faceVertices.push_back(vertices[indices[idx2]]);
|
---|
| 67 | faceVertices.push_back(vertices[indices[idx3]]);
|
---|
| 68 |
|
---|
| 69 | faceNormals.push_back(normals[nIndices[0]]);
|
---|
| 70 | faceNormals.push_back(normals[nIndices[idx2]]);
|
---|
| 71 | faceNormals.push_back(normals[nIndices[idx3]]);
|
---|
| 72 |
|
---|
| 73 | faceTexcoords.push_back(texcoords[tIndices[0]]);
|
---|
| 74 | faceTexcoords.push_back(texcoords[tIndices[idx2]]);
|
---|
| 75 | faceTexcoords.push_back(texcoords[tIndices[idx3]]);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | static Shape *LoadShape(const VertexArray &faceVertices,
|
---|
| 82 | const VertexArray &faceNormals,
|
---|
| 83 | const vector<pair<float, float> > &faceTexcoords,
|
---|
| 84 | SceneEntity *parent)
|
---|
| 85 | {
|
---|
| 86 | int numElements = (int)faceVertices.size();
|
---|
| 87 |
|
---|
| 88 | // convert the triangles to geometry
|
---|
| 89 | Vector3 *_vertices = new Vector3[numElements];
|
---|
| 90 | Vector3 *_normals = new Vector3[numElements];
|
---|
| 91 | float *_texcoords = new float[numElements * 2];
|
---|
| 92 |
|
---|
| 93 | for (int i = 0; i < numElements; ++ i)
|
---|
| 94 | {
|
---|
| 95 | _vertices[i].x = faceVertices[i].x;
|
---|
| 96 | _vertices[i].y = -faceVertices[i].z;
|
---|
| 97 | _vertices[i].z = faceVertices[i].y;
|
---|
| 98 |
|
---|
| 99 | _normals[i].x = faceNormals[i].x;
|
---|
| 100 | _normals[i].y = -faceNormals[i].z;
|
---|
| 101 | _normals[i].z = faceNormals[i].y;
|
---|
| 102 |
|
---|
| 103 | //_texcoords[i * 2 + 0] = faceTexcoords[i].second;
|
---|
| 104 | //_texcoords[i * 2 + 1] = faceTexcoords[i].first;
|
---|
| 105 |
|
---|
| 106 | _texcoords[i * 2 + 0] = faceTexcoords[i].first;
|
---|
| 107 | _texcoords[i * 2 + 1] = faceTexcoords[i].second;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | cout<<"number of triangles in geometry: " << numElements / 3 << endl;
|
---|
| 111 |
|
---|
| 112 | // Material *mat = new Material(RgbaColor(0, 1, 0, 1));
|
---|
| 113 | Material *mat = new Material();
|
---|
| 114 | Geometry *geom = new Geometry(_vertices, _normals, _texcoords, numElements, false);
|
---|
| 115 |
|
---|
| 116 | Shape *shape = new Shape(geom, mat, parent);
|
---|
| 117 | return shape;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | SceneEntity * ObjConverter::Load(const string &filename) const
|
---|
| 122 | {
|
---|
| 123 | FILE *file;
|
---|
| 124 |
|
---|
| 125 | if ((file = fopen(filename.c_str(), "rt")) == NULL)
|
---|
| 126 | {
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | Vector3 v(470.398f, 240.364f, 182.5f);
|
---|
| 131 | Matrix4x4 m = TranslationMatrix(v);
|
---|
| 132 | Transform3 *trafo = new Transform3(m);
|
---|
| 133 |
|
---|
| 134 | SceneEntity *sceneEntity = new SceneEntity(trafo);
|
---|
| 135 | LODLevel *lodLevel = new LODLevel(0);
|
---|
| 136 |
|
---|
| 137 | VertexArray faceVertices;
|
---|
| 138 | VertexArray faceNormals;
|
---|
| 139 | vector<pair<float, float> > faceTexcoords;
|
---|
| 140 |
|
---|
| 141 | VertexArray vertices;
|
---|
| 142 | VertexArray normals;
|
---|
| 143 | vector<pair<float, float> > texcoords;
|
---|
| 144 |
|
---|
| 145 | vector<int> indices;
|
---|
| 146 |
|
---|
| 147 | int line = 0;
|
---|
| 148 |
|
---|
| 149 | char str[100000];
|
---|
| 150 |
|
---|
| 151 | // hack
|
---|
| 152 | Texture *tex = new Texture(model_path + "wood.jpg");
|
---|
| 153 | tex->Create();
|
---|
| 154 |
|
---|
| 155 | while (fgets(str, 100000, file) != NULL)
|
---|
| 156 | {
|
---|
| 157 | switch (str[0])
|
---|
| 158 | {
|
---|
| 159 | case 'v': // vertex or normal
|
---|
| 160 | {
|
---|
| 161 | float x, y, z;
|
---|
| 162 |
|
---|
| 163 | switch (str[1])
|
---|
| 164 | {
|
---|
| 165 | case 'n' :
|
---|
| 166 | sscanf(str + 2, "%f %f %f", &x, &y, &z);
|
---|
| 167 | normals.push_back(Vector3(x, y, z));
|
---|
| 168 | break;
|
---|
| 169 | case 't':
|
---|
| 170 | sscanf(str + 2, "%f %f", &x, &y);
|
---|
| 171 | texcoords.push_back(pair<float, float>(x, y));
|
---|
| 172 | break;
|
---|
| 173 | default:
|
---|
| 174 | sscanf(str + 1, "%f %f %f", &x, &y, &z);
|
---|
| 175 | vertices.push_back(Vector3(x, y, z));
|
---|
| 176 | //cout <<"v " << x << " " << y << " "<< z << " ";
|
---|
| 177 | }
|
---|
| 178 | break;
|
---|
| 179 | }
|
---|
| 180 | case 'f':
|
---|
| 181 | {
|
---|
| 182 | //////////
|
---|
| 183 | //-- indices in the current line
|
---|
| 184 |
|
---|
| 185 | LoadIndices(str,
|
---|
| 186 | vertices, normals, texcoords,
|
---|
| 187 | faceVertices, faceNormals, faceTexcoords);
|
---|
| 188 |
|
---|
| 189 | break;
|
---|
| 190 | } // end face
|
---|
| 191 | case 'g':
|
---|
| 192 | #if 0
|
---|
| 193 | {
|
---|
| 194 | Shape *shape = LoadShape(faceVertices, faceNormals, faceTexcoords, sceneEntity);
|
---|
| 195 |
|
---|
| 196 | shape->GetMaterial()->SetTexture(tex);
|
---|
| 197 | sceneEntity->AddShape(shape);
|
---|
| 198 | lodLevel->AddShape(shape);
|
---|
| 199 |
|
---|
| 200 | faceVertices.clear();
|
---|
| 201 | faceNormals.clear();
|
---|
| 202 | faceTexcoords.clear();
|
---|
| 203 | }
|
---|
| 204 | #endif
|
---|
| 205 | break;
|
---|
| 206 | default:
|
---|
| 207 | break;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | ++ line;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | fclose(file);
|
---|
| 214 |
|
---|
| 215 | if (!faceVertices.empty())
|
---|
| 216 | {
|
---|
| 217 | Shape *shape = LoadShape(faceVertices, faceNormals, faceTexcoords, sceneEntity);
|
---|
| 218 |
|
---|
| 219 | //shape->GetMaterial()->SetTexture(tex);
|
---|
| 220 |
|
---|
| 221 | sceneEntity->AddShape(shape);
|
---|
| 222 | lodLevel->AddShape(shape);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | sceneEntity->AddLODLevel(lodLevel);
|
---|
| 226 |
|
---|
| 227 | return sceneEntity;
|
---|
| 228 | }
|
---|