VertexBufferControlFileOBJ.cc 13.9 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

Robert Menzel's avatar
Robert Menzel committed
6 7 8 9 10
#include <ACGL/OpenGL/Controller/VertexBufferControlFileOBJ.hh>
#include <ACGL/OpenGL/Controller/ArrayBufferControl.hh>
#include <ACGL/OpenGL/Controller/ElementArrayBufferControl.hh>
#include <ACGL/OpenGL/Controller/VertexBufferControl.hh>
#include <ACGL/Base/StringOperations.hh>
Robert Menzel's avatar
Robert Menzel committed
11 12 13 14 15 16

#include <vector>
#include <iostream>
#include <fstream>

using namespace ACGL;
Robert Menzel's avatar
Robert Menzel committed
17
using namespace ACGL::Base;
Robert Menzel's avatar
Robert Menzel committed
18
using namespace ACGL::Utils;
Robert Menzel's avatar
Robert Menzel committed
19
using namespace ACGL::OpenGL;
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 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

SharedVertexBuffer VertexBufferControlFileOBJ::create(void)
{
    updateFileModificationTime();

    SharedVertexBuffer vertexBuffer = VertexBufferControl().
        index(ElementArrayBufferControl().
            mode(GL_TRIANGLES).
            usage(GL_STATIC_DRAW).
            create()).
        data(ArrayBufferControl().
            mode(GL_TRIANGLES).
            usage(GL_STATIC_DRAW).
            create()).
        create();

    if(loadOBJ(vertexBuffer))
        return vertexBuffer;

    return SharedVertexBuffer();
}

bool VertexBufferControlFileOBJ::update(SharedVertexBuffer& vertexBuffer)
{
    if(fileIsUpToDate())
        return false;

    if(!loadOBJ(vertexBuffer))
        return false;

    updateFileModificationTime();
    return true;
}

bool VertexBufferControlFileOBJ::loadOBJ(SharedVertexBuffer& _vertexBuffer)
{
    std::string full = getFullFilePath();

    std::string line = "";

    std::ifstream fileStream(full.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())
    {
        //Read the first line
        if(fileStream.good())
            std::getline(fileStream, line);

        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 " << full << " 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 3, because vertices have 3 components (x,y,z)
        uint_t components = 3;
        //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;
        GLenum indexType = GL_UNSIGNED_INT;
        std::vector<IndexType> indexVector;

        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();

        SharedElementArrayBuffer elementArrayBuffer = _vertexBuffer->getElementArrayBuffer();
        SharedArrayBuffer arrayBuffer = _vertexBuffer->getArrayBuffers()[0];

        arrayBuffer->removeAttributes();
        _vertexBuffer->removeAttributes();

297 298
        arrayBuffer->attachAttribute("iPosition", GL_FLOAT, 4, GL_FALSE);
        _vertexBuffer->attachAttribute("iPosition", 0, "iPosition");
Robert Menzel's avatar
Robert Menzel committed
299 300
        if(hasNormals)
        {
301 302
            arrayBuffer->attachAttribute("iNormal", GL_FLOAT, 3, GL_FALSE);
            _vertexBuffer->attachAttribute("iNormal", 0, "iNormal");
Robert Menzel's avatar
Robert Menzel committed
303 304 305
        }
        if(texCoordDimension > 0)
        {
306 307
            arrayBuffer->attachAttribute("iTexCoord", GL_FLOAT, texCoordDimension, GL_FALSE);
            _vertexBuffer->attachAttribute("iTexCoord", 0, "iTexCoord");
Robert Menzel's avatar
Robert Menzel committed
308 309 310 311 312
        }

        elementArrayBuffer->setType(indexType);
        elementArrayBuffer->bind();
        elementArrayBuffer->setData(&indexVector[0], indexVector.size());
313
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Robert Menzel's avatar
Robert Menzel committed
314 315
        arrayBuffer->bind();
        arrayBuffer->setData(&interleavedDataVector[0], index);
316
        //glBindBuffer(GL_ARRAY_BUFFER, 0);
317
        //_vertexBuffer->setElements(indexVector.size());
Robert Menzel's avatar
Robert Menzel committed
318 319 320 321 322 323 324

        return true;
    }

    error() << "Failed to open file: " << getFullFilePath() << std::endl;
    return false;
}