source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ObjConverter.cpp @ 2958

Revision 2958, 5.4 KB checked in by mattausch, 16 years ago (diff)

preetham not working for deferred

Line 
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
11using namespace CHCDemoEngine;
12using namespace std;
13
14
15static 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
81static 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        mat->SetAmbient(RgbaColor(0, 0, 0, 0));
115        mat->SetDiffuse(RgbaColor(0, 0, 0, 0));
116
117        Geometry *geom = new Geometry(_vertices, _normals, _texcoords, numElements, false);
118
119        Shape *shape = new Shape(geom, mat, parent);
120        return shape;
121}
122
123
124SceneEntity * ObjConverter::Load(const string &filename) const
125{
126        FILE *file;
127
128        if ((file = fopen(filename.c_str(), "rt")) == NULL)
129        {       
130                return NULL;
131        }
132       
133        Vector3 v(470.398f, 240.364f, 182.5f);
134        Matrix4x4 m = TranslationMatrix(v);
135        Transform3 *trafo = new Transform3(m);
136
137        SceneEntity *sceneEntity = new SceneEntity(trafo);
138        LODLevel *lodLevel = new LODLevel(0);
139
140        VertexArray faceVertices;
141        VertexArray faceNormals;
142        vector<pair<float, float> > faceTexcoords;
143
144        VertexArray vertices;
145        VertexArray normals;
146        vector<pair<float, float> > texcoords;
147
148        vector<int> indices;
149
150        int line = 0;
151
152        char str[100000];
153
154        // hack
155        Texture *tex = new Texture(model_path + "wood.jpg");
156        tex->Create();
157
158        while (fgets(str, 100000, file) != NULL)
159        {
160                switch (str[0])
161                {
162                case 'v': // vertex or normal
163                        {
164                                float x, y, z;
165
166                                switch (str[1])
167                                {
168                                case 'n' :
169                                        sscanf(str + 2, "%f %f %f", &x, &y, &z);
170                                        normals.push_back(Vector3(x, y, z));
171                                        break;
172                                case 't':
173                                        sscanf(str + 2, "%f %f", &x, &y);
174                                        texcoords.push_back(pair<float, float>(x, y));
175                                        break;
176                                default:
177                                        sscanf(str + 1, "%f %f %f", &x, &y, &z);
178                                        vertices.push_back(Vector3(x, y, z));
179                                        //cout <<"v " << x << " " << y << " "<< z << " ";
180                                }
181                                break;
182                        }
183                case 'f':
184                        {
185                                //////////
186                                //-- indices in the current line
187
188                                LoadIndices(str,
189                                                vertices, normals, texcoords,
190                                                faceVertices, faceNormals, faceTexcoords);
191
192                                break;
193                        }   // end face
194                case 'g':
195#if 0
196                        {
197                                Shape *shape = LoadShape(faceVertices, faceNormals, faceTexcoords, sceneEntity);
198
199                                shape->GetMaterial()->SetTexture(tex);
200                                sceneEntity->AddShape(shape);
201                                lodLevel->AddShape(shape);
202
203                                faceVertices.clear();
204                                faceNormals.clear();
205                                faceTexcoords.clear();
206                        }
207#endif
208                        break;
209                default:
210                        break;
211                }
212
213                ++ line;
214        }
215
216        fclose(file);
217
218        if (!faceVertices.empty())
219        {
220                Shape *shape = LoadShape(faceVertices, faceNormals, faceTexcoords, sceneEntity);
221
222                //shape->GetMaterial()->SetTexture(tex);
223
224                sceneEntity->AddShape(shape);
225                lodLevel->AddShape(shape);
226        }
227
228        sceneEntity->AddLODLevel(lodLevel);
229
230        return sceneEntity;
231}
Note: See TracBrowser for help on using the repository browser.