SparrowRenderer/utils.cpp
2015-07-06 21:46:17 +02:00

194 lines
4.5 KiB
C++

#include "utils.h"
#include "mesh.h"
#include <QFile>
#include <QTextStream>
#include <QImage>
#include <vector>
std::string Utils::fileToString(const std::string &filename)
{
QFile f(QString(filename.c_str()));
if(!f.open(QFile::ReadOnly | QFile::Text))
return NULL;
QTextStream in(&f);
return in.readAll().toStdString();
}
/*void Utils::loadOBJ(const std::string &filename)
{
// loadOBJ
// mtllib -> loadMTL
// g -> resourcebase.addMesh(new Mesh())
// -> resourcebase.addEntity(new Entity())
// end -> resourcebase.addEntity(new Entity())
std::vector<glm::vec3> pos;
std::vector<glm::vec3> norm;
std::vector<glm::vec2> tex;
QString line;
QStringList list;
QFile f(QString(filename.c_str()));
if(!f.open(QFile::ReadOnly | QFile::Text))
return NULL;
QTextStream in(&f);
line = in.readLine();
while(!line.isNull())
{
if(line.isEmpty())
{
line = in.readLine();
continue;
}
switch(line.at(0).toLatin1())
{
case 'v':
//vertex attribute
list = line.split(QChar(' '));
switch(line.at(1).toLatin1())
{
case ' ':
// vertex position
pos.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
break;
case 't':
// texCoord
tex.push_back(glm::vec2(list[1].toFloat(), list[2].toFloat()));
break;
case 'n':
// normal
norm.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
break;
}
break;
case 'f':
// face
break;
case 'u':
// usemtl
break;
default:
case 'm':
// mtllib
break;
default:
case '#':
// comment
break;
}
line = in.readLine();
}
f.close();
}*/
void Utils::loadMTL(const std::string &filename)
{
// loadMTL
// newmtl -> resourcebase.addMaterial(new Material())
}
Mesh* Utils::loadOBJ(const std::string &filename)
{
Mesh* mesh = new Mesh();
std::vector<glm::vec3> pos;
std::vector<glm::vec3> norm;
std::vector<glm::vec2> tex;
QString line;
QStringList list;
QStringList faceList;
Mesh::Vertex v;
int nb_vertices = 0;
QFile f(QString(filename.c_str()));
if(!f.open(QFile::ReadOnly | QFile::Text))
return NULL;
QTextStream in(&f);
line = in.readLine();
while(!line.isNull())
{
if(line.isEmpty())
{
line = in.readLine();
continue;
}
switch(line.at(0).toLatin1())
{
case 'v':
//vertex attribute
list = line.split(QChar(' '));
switch(line.at(1).toLatin1())
{
case ' ':
// vertex position
pos.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
break;
case 't':
// texCoord
tex.push_back(glm::vec2(list[1].toFloat(), list[2].toFloat()));
break;
case 'n':
// normal
norm.push_back(glm::vec3(list[1].toFloat(), list[2].toFloat(), list[3].toFloat()));
break;
}
break;
case 'f':
// face
list = line.split(QChar(' '));
mesh->addFace(nb_vertices, nb_vertices+1, nb_vertices+2);
for(int i=0; i<3; ++i)
{
faceList = list[i+1].split(QChar('/'));
v.position = pos[faceList[0].toInt()];
v.texCoord= tex[faceList[1].toInt()];
v.normal = norm[faceList[2].toInt()];
mesh->addVertex(v);
}
nb_vertices += 3;
break;
case 'u':
// usemtl
break;
default:
case '#':
// comment
break;
}
line = in.readLine();
}
return mesh;
}
Utils::Image::Image(std::string filename)
{
img = new QImage(QString(filename.c_str()));
}
Utils::Image::~Image()
{
delete(img);
}
int Utils::Image::depth()
{
return img->depth();
}
int Utils::Image::width()
{
return img->width();
}
int Utils::Image::height()
{
return img->height();
}
void* Utils::Image::pixels()
{
return (void*)img->bits();
}