Commit dc68434d authored by Janis Born's avatar Janis Born
Browse files

reuse old implementation from ArrayBufferControlFileATB in GeometryDataControlFileATB

parent cdbd9d2f
......@@ -7,7 +7,14 @@
#define ACGL_OPENGL_CONTROLLER_GEOMETRYDATACONTROLFILEATB_HH
/**
* A minimal obj loader.
* A minimal ATB loader.
*
* Creates a GeometryData object with one attribute.
*
* By default, the attribute's name is the name of the provided file (without
* the file extension), prepended with an 'a', i.e. loading a file named
* Tangent.atb would result in an attribute named 'aTangent'.
*
*/
#include <ACGL/ACGL.hh>
......@@ -19,35 +26,42 @@
namespace ACGL{
namespace OpenGL{
class GeometryDataControlFileOBJ;
class GeometryDataControlFileATB : public ACGL::OpenGL::GeometryDataControlFile
{
friend void initStaticFileTypes(void);
public:
GeometryDataControlFileATB(const std::string& _filename)
: GeometryDataControlFile(_filename,Base::Settings::the()->getFullGeometryPath()),
mOBJLoader(_filename)
{}
GeometryDataControlFileATB(const std::string& _filename);
virtual ~GeometryDataControlFileATB(void) {}
// the entry point that gets registered in registerType()
static SharedGeometryDataControlFile creator(const std::string& _filename) { return SharedGeometryDataControlFile(new GeometryDataControlFileATB(_filename)); }
static SharedGeometryDataControlFile creator(const std::string& _filename)
{
return SharedGeometryDataControlFile(new GeometryDataControlFileATB(_filename));
}
//! Sets the name of the only attribute
inline GeometryDataControlFileATB& attributeName(const std::string& _attributeName)
{
mAttributeName = _attributeName;
return *this;
}
private:
virtual bool load(SharedGeometryData& geometry) const;
private:
// register the file extension 'obj' for this loader
// register the file extension 'atb' for this loader
static ACGL::int_t registerType(void)
{
sTypeID = GeometryDataControlFileFactory::the()->registerType("atb", &GeometryDataControlFileATB::creator);
return sTypeID;
}
GeometryDataControlFileOBJ mOBJLoader;
std::string mAttributeName;
};
ACGL_SMARTPOINTER_TYPEDEFS(GeometryDataControlFileATB)
} // OpenGL
......
......@@ -3,31 +3,158 @@
// All rights reserved. //
////////////////////////////////////////////////////////////////////////////////
#include <ACGL/OpenGL/Controller/GeometryDataControlFileOBJ.hh>
#include <ACGL/OpenGL/Controller/GeometryDataControlFileATB.hh>
#include <ACGL/OpenGL/Controller/DataControlFileFactory.hh>
#include <sstream>
#include <cstring>
using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;
using namespace ACGL::Base;
bool GeometryDataControlFileATB::load(SharedGeometryData& geometry) const
GeometryDataControlFileATB::GeometryDataControlFileATB(const std::string& _filename)
: GeometryDataControlFile(_filename, Base::Settings::the()->getFullGeometryPath())
{
// 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);
bool ok = mOBJLoader.load( geometry );
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;
}
if (ok) {
if ( geometry->mAttributes.size() == 1 ) geometry->mAttributes[0].name = mFilename;
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);
for (unsigned int i = 0; i < geometry->mAttributes.size(); ++i) {
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
sstream << i;
geometry->mAttributes[i].name = mFilename+sstream.str();
}
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);
for(uint_t i = 0; i < attributeDimension; ++i)
dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndex + i]);
}
}
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
ArrayBuffer::Attribute attr = { mAttributeName, GL_FLOAT, attributeDimension, 0, GL_FALSE };
_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;
}
return ok;
error() << "Failed to open file: " << getFullFilePath() << std::endl;
return false;
}
......@@ -29,6 +29,6 @@ void ACGL::OpenGL::initStaticFileTypes(void)
// Geometry, Meshes, Vertex data
//
GeometryDataControlFileOBJ::registerType();
//GeometryDataControlFileATB::registerType();
GeometryDataControlFileATB::registerType();
}
Supports Markdown
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