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