GeometryDataControlFileATB.cc 6.52 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6
7
8
9
10

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

#include <sstream>
11
#include <cstring>
Robert Menzel's avatar
Robert Menzel committed
12
13
14
15
16
17

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

18
19
GeometryDataControlFileATB::GeometryDataControlFileATB(const std::string& _filename)
:   GeometryDataControlFile(_filename, Base::Settings::the()->getFullGeometryPath())
Robert Menzel's avatar
Robert Menzel committed
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
    // Set the default attribute name
    std::string filenameBase, filenameExtension;
    ACGL::Base::StringOperations::splitFileExtension(mFilename, filenameBase, filenameExtension);
    mAttributeName = "a" + filenameBase;
}

bool GeometryDataControlFileATB::load(SharedGeometryData& _geometry) const
{
    std::string full = getFullFilePath();

    std::string line = "";

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

    std::vector<GLfloat> sharedAttributeVector;
    uint_t attributeElements = 0;
    uint_t attributeDimension = 0;

    bool elementOK = true;
    bool lineOK = true;

    uint_t lineNumber = 0;

    if(fileStream.is_open())
    {
        //Read the first line
        if(fileStream.good())
            std::getline(fileStream, line);
Robert Menzel's avatar
Robert Menzel committed
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
        while(fileStream.good())
        {
            if(StringOperations::startsWith(line, "a"))
            {
                std::vector<std::string> elements = StringOperations::split(line, ' ');
                //The first occurence of vt gives us the number of texture coordinates per vertex.
                if(attributeElements == 0)
                {
                    attributeElements = elements.size();
                    attributeDimension = attributeElements - 1;
                    sharedAttributeVector.resize(attributeDimension);
                }
                if((uint_t)elements.size() == attributeElements)
                {
                    for(uint_t i = 1; i < attributeElements; ++i)
                        sharedAttributeVector.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 < attributeElements; ++i)
                        sharedAttributeVector.push_back(0.0f);
                    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;
            }
Robert Menzel's avatar
Robert Menzel committed
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
            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);
Robert Menzel's avatar
Robert Menzel committed
97
98

        }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

        std::vector<GLfloat> dataVector;

        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)
                    {
                        index++;
                        uint_t sharedIndex = StringOperations::to<uint_t>(elements[i], &elementOK);
115
116
117
118
119
120
121
122
123
124
125
126
127
128

                        if(i > 3)
                        {
                            // triangulate polygons with number of vertices > 3
                            uint_t sharedIndexFirst = StringOperations::to<uint_t>(elements[1], &elementOK);
                            for(uint_t d = 0; d < attributeDimension; ++d)
                                dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndexFirst + d]);

                            uint_t sharedIndexPrev = StringOperations::to<uint_t>(elements[i-1], &elementOK);
                            for(uint_t d = 0; d < attributeDimension; ++d)
                                dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndexPrev + d]);
                        }
                        for(uint_t d = 0; d < attributeDimension; ++d)
                            dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndex + d]);
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
                    }
                }
                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();

        // Copy the bare data so the _geometry object can reference it
        GLfloat *data = new GLfloat[dataVector.size()];
        std::memcpy(data, &dataVector[0], dataVector.size() * sizeof(GLfloat));

        // Name the attribute according to the filename
160
        ArrayBuffer::Attribute attr = { mAttributeName, GL_FLOAT, attributeDimension, 0, GL_FALSE, 0, GL_FALSE };
161
162
163
164
165
166
167
168
        _geometry->mAttributes.push_back(attr);

        // Tell the _geometry object where it finds the data
        _geometry->setStrideSize(attributeDimension * sizeof(GLfloat));
        _geometry->setSize(dataVector.size() * sizeof(GLfloat));
        _geometry->setData((GLubyte*)data);

        return true;
Robert Menzel's avatar
Robert Menzel committed
169
170
    }

171
172
    error() << "Failed to open file: " << getFullFilePath() << std::endl;
    return false;
Robert Menzel's avatar
Robert Menzel committed
173
}