GeometryDataControlFileOBJ.cc 12.3 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, 2012 Computer Graphics Group RWTH Aachen University    //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#include <ACGL/OpenGL/Controller/GeometryDataControlFileOBJ.hh>
#include <ACGL/OpenGL/Controller/DataControlFileFactory.hh>

#include <ACGL/Base/StringOperations.hh>

#include <vector>
#include <iostream>
#include <fstream>
Robert Menzel's avatar
Robert Menzel committed
14
#include <cstring>
Robert Menzel's avatar
Robert Menzel committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;
using namespace ACGL::Base;

bool GeometryDataControlFileOBJ::load(SharedGeometryData& geometry) const
{
    std::string fullFileName = getFullFilePath();

    std::string line = "";

    std::ifstream fileStream(fullFileName.c_str(), std::ifstream::in);

    std::vector<GLfloat> sharedVertexVector;
    std::vector<GLfloat> sharedTexCoordVector;
    std::vector<GLfloat> sharedNormalVector;
    uint_t texCoordElements = 0;
    uint_t texCoordDimension = 0;

    sharedVertexVector.resize(4);
    sharedNormalVector.resize(3);

    bool elementOK = true;
    bool lineOK = true;

    uint_t lineNumber = 0;

    if (!fileStream.is_open())
    {
        error() << "Failed to open file: " << fullFileName << std::endl;
        return false;
    }

    //Read the first line
    if (fileStream.good()) std::getline(fileStream, line);

    //
    // read in vertex, texture and normal data:
    //
    while (fileStream.good())
    {
        if (StringOperations::startsWith(line, "vt"))
        {
            std::vector<std::string> elements = StringOperations::split(line, ' ');
            //The first occurence of vt gives us the number of texture coordinates per vertex.
            if (texCoordElements == 0)
            {
                texCoordElements = elements.size();
                texCoordDimension = texCoordElements - 1;
                sharedTexCoordVector.resize(texCoordDimension);
            }
            if ((uint_t)elements.size() == texCoordElements)
            {
                for(uint_t i = 1; i < texCoordElements; ++i)
                    sharedTexCoordVector.push_back(StringOperations::to<GLfloat>(elements[i], &elementOK));
            }
            else
            {
                //If a later texture coordinate is defined wrong, we enter zeros, because
                //otherwise the indices would be intermixed.
                for(uint_t i = 1; i < texCoordElements; ++i)
                    sharedTexCoordVector.push_back(0.0f);
                lineOK = false;
            }
        }
        else if (StringOperations::startsWith(line, "vn"))
        {
            std::vector<std::string> elements = StringOperations::split(line, ' ');
            if (elements.size() == 4)
            {
                sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK));
                sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK));
                sharedNormalVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK));
            }
            else
            {
                //If a later texture coordinate is defined wrong, we enter zeros, because
                //otherwise the indices would be intermixed.
                sharedNormalVector.push_back(0.0f);
                sharedNormalVector.push_back(0.0f);
                sharedNormalVector.push_back(0.0f);
                lineOK = false;
            }
        }
        else if (StringOperations::startsWith(line, "v"))
        {
            std::vector<std::string> elements = StringOperations::split(line, ' ');
            if (elements.size() == 4)
            {
                sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[1], &elementOK));
                sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[2], &elementOK));
                sharedVertexVector.push_back(StringOperations::to<GLfloat>(elements[3], &elementOK));
                sharedVertexVector.push_back(1.0f);
            }
            else
            {
                lineOK = false;
            }
        }
        else if (StringOperations::startsWith(line, "f"))
        {
            //As soon as the first f occurs, we assume that all vertex
            //positions, normals and tex coords have been defined!
            break;
        }

        if (!lineOK)
        {
            warning() << "Format of line " << lineNumber << " is wrong!" << std::endl;
            debug() << line << std::endl;
            lineOK = true;
        }
        if (!elementOK)
        {
            warning() << "Element in line " << lineNumber << " is wrong!" << std::endl;
            debug() << line << std::endl;
            elementOK = true;
        }

        ++lineNumber;
        getline(fileStream, line);
    }

    bool hasNormals = sharedNormalVector.size() != 3;
    if (!hasNormals) {
        message() << "The file " << fullFileName << " has no normals!" << std::endl;
    }

    //By default the number of attributes is 1 (only vertices)
    uint_t attributes = 1;
    //Then we check for other attributes like normals...
    if (hasNormals)
        attributes++;
    //...or tex coords
    if (texCoordDimension > 0)
        attributes++;

    //By default components is the size of 4, because vertices have 3 components (x,y,z,w)
    uint_t components = 4;
    //If there are normals we have to add another 3 elements
    if (hasNormals)
        components += 3;
    //We then add the number of tex coord components. This number is 0, if there are none
    components += texCoordDimension;

    std::vector<GLfloat> interleavedDataVector;
    typedef GLuint IndexType;
    std::vector<IndexType> indexVector;

    //
    // read faces:
    //
    uint_t index = 0;
    while (fileStream.good())
    {
        if (StringOperations::startsWith(line, "f"))
        {
            std::vector<std::string> elements = StringOperations::split(line, ' ');
            uint_t uiPolygonSize = elements.size() - 1;
            if (uiPolygonSize > 2)
            {
                for(size_t i = 1; i < elements.size(); ++i)
                {
                    if (i > 3)
                    {
                        //If we have more than 3 vertices, we have to triangulate in a simple way.
                        //E.g. for a quad 1234 we should get 123 134.
                        indexVector.push_back(index - i + 1);
                        indexVector.push_back(index - 1);
                    }
                    indexVector.push_back(index++);
                    std::vector<std::string> indices = StringOperations::split(elements[i], '/', false);
                    if (indices.size() == 3 && hasNormals && texCoordDimension > 0)
                    {
                        uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]);
                        sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]);
                        sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK);
                        for(uint_t i = 0; i < texCoordDimension; ++i)
                            interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]);
                    }
                    else if (indices.size() == 3 && hasNormals)
                    {
                        uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]);
                        sharedIndex = StringOperations::to<IndexType>(indices[2], &elementOK);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex]);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedNormalVector[3 * sharedIndex + 2]);
                    }
                    else if (indices.size() == 2 && texCoordDimension > 0)
                    {
                        uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]);
                        sharedIndex = StringOperations::to<IndexType>(indices[1], &elementOK);
                        for(uint_t i = 0; i < texCoordDimension; ++i)
                            interleavedDataVector.push_back(sharedTexCoordVector[texCoordDimension * sharedIndex + i]);
                    }
                    else if (indices.size() == 1)
                    {
                        uint_t sharedIndex = StringOperations::to<IndexType>(indices[0], &elementOK);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 1]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 2]);
                        interleavedDataVector.push_back(sharedVertexVector[4 * sharedIndex + 3]);
                    }
                    else
                    {
                        for(uint_t j = 0; j < components; ++j)
                            interleavedDataVector.push_back(0.0f);
                        lineOK = false;
                    }
                }
            }
            else
            {
                lineOK = false;
            }
        }

        if (!lineOK)
        {
            warning() << "Format of line " << lineNumber << " is wrong!" << std::endl;
            debug() << line << std::endl;
            lineOK = true;
        }
        if (!elementOK)
        {
            warning() << "Element in line " << lineNumber << " is wrong!" << std::endl;
            debug() << line << std::endl;
            elementOK = true;
        }
        ++lineNumber;
        getline(fileStream, line);
    }

    fileStream.close();

266
267
268
    // flatten interleavedDataVector -> data
    uint_t totalIndices = indexVector.size();
    float *data = new float[ totalIndices * components ];
Robert Menzel's avatar
Robert Menzel committed
269

270
271
272
273
274
275
276
    for(uint_t ind = 0; ind < totalIndices; ++ind)
    {
        for(uint_t comp = 0; comp < components; ++comp)
        {
            data[ind * components + comp] = interleavedDataVector[indexVector[ind] * components + comp];
        }
    }
Robert Menzel's avatar
Robert Menzel committed
277

278
    geometry->setSize( totalIndices * components * sizeof(float) );
Robert Menzel's avatar
Robert Menzel committed
279
    geometry->setData( (GLubyte*)data );
280
    geometry->setStrideSize( components * sizeof(float) );
Robert Menzel's avatar
Robert Menzel committed
281

282
    ArrayBuffer::Attribute apos  = { "aPosition", GL_FLOAT, 4, 0,               GL_FALSE, 0 };
Robert Menzel's avatar
Robert Menzel committed
283
284
285
    geometry->mAttributes.push_back( apos );

    if (hasNormals) {
286
        ArrayBuffer::Attribute anorm = { "aNormal",   GL_FLOAT, 3, 4*sizeof(float), GL_FALSE, 0 };
Robert Menzel's avatar
Robert Menzel committed
287
288
289
290
291
292
        geometry->mAttributes.push_back( anorm );
    }

    if (texCoordDimension > 0) {
        int offset = (hasNormals?7:4);
        offset *= sizeof(float);
293
        ArrayBuffer::Attribute atex  = { "aTexCoord", GL_FLOAT, texCoordDimension, offset, GL_FALSE, 0 };
Robert Menzel's avatar
Robert Menzel committed
294
295
296
297
298
299
        geometry->mAttributes.push_back( atex );
    }

    return true;
}