Commit c72474f0 authored by Robert Menzel's avatar Robert Menzel
Browse files

obj loading works again

parent 0abdc61d
......@@ -6,6 +6,8 @@
#ifndef ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROL_HH
#define ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROL_HH
/* *************** DEPRECATED *********************
#include <ACGL/ACGL.hh>
#include <ACGL/Resource/BasicCreateController.hh>
......@@ -86,4 +88,6 @@ protected:
} // OpenGL
} // ACGL
*/
#endif // ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROL_HH
......@@ -6,6 +6,8 @@
#ifndef ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROLFILEATB_HH
#define ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROLFILEATB_HH
/* *************** DEPRECATED *********************
#include <ACGL/ACGL.hh>
#include <ACGL/Resource/FileController.hh>
......@@ -55,4 +57,6 @@ protected:
} // OpenGL
} // ACGL
*/
#endif // ACGL_OPENGL_CONTROLLER_ARRAYBUFFERCONTROLFILEATB_HH
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, 2012 Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#ifndef ACGL_OPENGL_CONTROLLER_GEOMETRYDATACONTROLFILEOBJ_HH
#define ACGL_OPENGL_CONTROLLER_GEOMETRYDATACONTROLFILEOBJ_HH
/**
* A minimal obj loader.
*/
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Controller/DataControlFile.hh>
#include <ACGL/OpenGL/InitStaticFileTypes.hh>
#include <ACGL/OpenGL/Controller/DataControlFileFactory.hh>
namespace ACGL{
namespace OpenGL{
class GeometryDataControlFileOBJ : public ACGL::OpenGL::GeometryDataControlFile
{
friend void initStaticFileTypes(void);
public:
GeometryDataControlFileOBJ(const std::string& _filename) : GeometryDataControlFile(_filename) {}
virtual ~GeometryDataControlFileOBJ(void) {}
// the entry point that gets registered in registerType()
static SharedGeometryDataControlFile creator(const std::string& _filename) { return SharedGeometryDataControlFile(new GeometryDataControlFileOBJ(_filename)); }
private:
virtual bool load(SharedGeometryData& geometry) const;
private:
// register the file extension 'obj' for this loader
static ACGL::int_t registerType(void)
{
sTypeID = GeometryDataControlFileFactory::the()->registerType("obj", &GeometryDataControlFileOBJ::creator);
return sTypeID;
}
};
ACGL_SMARTPOINTER_TYPEDEFS(GeometryDataControlFileOBJ)
} // OpenGL
} // ACGL
#endif // ACGL_OPENGL_CONTROLLER_GEOMETRYDATACONTROLFILEOBJ_HH
......@@ -105,6 +105,7 @@ public:
inline const AttributeVec& getAttributes(void) const { return mAttributes; }
inline GLenum getMode (void) const { return mMode; }
inline SharedElementArrayBuffer getElementArrayBuffer (void) const { return mpElementArrayBuffer; }
inline GLuint getElements (void) const { return mAttributes[0].arrayBuffer->getElements(); }
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
......
......@@ -3,6 +3,8 @@
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
/* *************** DEPRECATED *********************
#include <ACGL/OpenGL/Controller/ArrayBufferControl.hh>
using namespace ACGL::OpenGL;
......@@ -25,3 +27,5 @@ SharedArrayBuffer ArrayBufferControl::create(void)
}
return arrayBuffer;
}
*/
......@@ -3,6 +3,8 @@
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
/* *************** DEPRECATED *********************
#include <ACGL/OpenGL/Controller/ArrayBufferControlFileATB.hh>
#include <ACGL/OpenGL/Controller/ArrayBufferControl.hh>
#include <ACGL/OpenGL/Controller/ElementArrayBufferControl.hh>
......@@ -172,3 +174,5 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
error() << "Failed to open file: " << getFullFilePath() << std::endl;
return false;
}
*/
////////////////////////////////////////////////////////////////////////////////
// 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>
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();
geometry->setSize( index * components * sizeof(float) );
float *data = new float[ index * components ];
for (unsigned int i = 0; i < index * components; ++i) {
data[i] = interleavedDataVector[i];
}
geometry->setData( (GLubyte*)data );
geometry->setStrideSize( components*sizeof(float) );
ArrayBuffer::Attribute apos = { "aPosition", GL_FLOAT, 4, 0, GL_FALSE };
geometry->mAttributes.push_back( apos );
if (hasNormals) {
ArrayBuffer::Attribute anorm = { "aNormal", GL_FLOAT, 3, 4*sizeof(float), GL_FALSE };
geometry->mAttributes.push_back( anorm );
}
if (texCoordDimension > 0) {
int offset = (hasNormals?7:4);
offset *= sizeof(float);
ArrayBuffer::Attribute atex = { "aTexCoord", GL_FLOAT, texCoordDimension, offset, GL_FALSE };
geometry->mAttributes.push_back( atex );
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University //
// Copyright (c) 2011, 2012 Computer Graphics Group RWTH Aachen University //
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
......@@ -9,14 +9,26 @@
#include <ACGL/OpenGL/Controller/TextureDataControlFilePNG.hh>
#include <ACGL/OpenGL/Controller/TextureDataControlFilePPM.hh>
#include <ACGL/OpenGL/Controller/GeometryDataControlFileOBJ.hh>
#include <ACGL/OpenGL/Controller/GeometryDataControlFileATB.hh>
using namespace ACGL::OpenGL;
void ACGL::OpenGL::initStaticFileTypes(void)
{
//
// Textures
//
#ifdef ACGL_COMPILE_WITH_QT
TextureDataControlFileJPG::registerType();
TextureDataControlFilePNG::registerType();
#endif
TextureDataControlFilePPM::registerType();
//
// Geometry, Meshes, Vertex data
//
GeometryDataControlFileOBJ::registerType();
//GeometryDataControlFileATB::registerType();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment