libsegl/models/load3ds.cpp

229 lines
4.8 KiB
C++
Raw Normal View History

2008-04-10 00:59:47 +02:00
#include "load3ds.h"
2008-04-10 22:03:45 +02:00
#include "../punkt3d.h"
2008-04-10 00:59:47 +02:00
Load3ds::Load3ds(std::string _fname, bool _parse) {
filename = _fname;
parsed = false;
error = false;
2008-04-11 15:57:56 +02:00
// if(_parse)
// parse();
2008-04-10 00:59:47 +02:00
}
2008-04-11 15:57:56 +02:00
bool Load3ds::parse(Model *m) {
2008-04-10 00:59:47 +02:00
if(parsed)
unload();
std::ifstream mfile(filename.c_str(), std::ios::binary);
if(!mfile) {
return false;
}
unsigned short ident;
2008-04-10 18:55:31 +02:00
unsigned int len;
2008-04-11 15:57:56 +02:00
bool modelloaded = false;
bool materialloaded = false;
Material mat;
2008-04-11 15:57:56 +02:00
ModelObject modobj;
m->clear();
2008-04-10 00:59:47 +02:00
while(!mfile.eof()) {
2008-04-10 18:55:31 +02:00
ident = len = 0;
2008-04-10 00:59:47 +02:00
mfile.read((char *)&ident, 2);
2008-04-10 18:55:31 +02:00
mfile.read((char *)&len, 4);
2008-04-10 18:55:31 +02:00
std::cout << "Chunk: 0x" << std::hex << ident << " (" << std::dec << len << ")" << std::endl;
2008-04-10 00:59:47 +02:00
switch(ident) {
case 0x4d4d:
2008-04-10 18:55:31 +02:00
// Main Chunk
2008-04-10 00:59:47 +02:00
break;
2008-04-10 18:55:31 +02:00
case 0x3d3d:
// 3D Editor Chunk
break;
case 0x4000:
2008-04-10 22:03:45 +02:00
{
2008-04-11 15:57:56 +02:00
if(modelloaded) {
modobj.calcNormales();
2008-04-11 15:57:56 +02:00
m->addObject(modobj);
modobj.clear();
}
modelloaded = true;
// std::cout << "namechunk" << std::endl;
2008-04-10 22:03:45 +02:00
std::string name;
char c;
do {
mfile.read(&c, 1);
name += c;
} while(c!=0);
std::cout << "Name: " << name << std::endl;
2008-04-11 15:57:56 +02:00
modobj.name = name;
2008-04-10 22:03:45 +02:00
}
break;
case 0x4100:
break;
case 0x4110:{
unsigned short panz;
Punkt3D p;
mfile.read((char*)&panz, 2);
// std::cout << "\t\t\tVertexanz: " << panz << std::endl;
2008-04-10 22:03:45 +02:00
glBegin(GL_LINE_LOOP);
for(unsigned int i=0; i<panz; i++) {
mfile.read((char*)&p.x, sizeof(float));
mfile.read((char*)&p.y, sizeof(float));
mfile.read((char*)&p.z, sizeof(float));
// p.print("Punkt");
2008-04-11 15:57:56 +02:00
modobj.vertex.push_back(p);
2008-04-10 22:03:45 +02:00
glVertex3f(p);
}
glEnd();
}
2008-04-10 18:55:31 +02:00
break;
2008-04-11 15:57:56 +02:00
case 0x4120:
{
unsigned short panz, tp;
Punkt3D p;
mfile.read((char*)&panz, 2);
// std::cout << "\t\t\tPolygonanz: " << panz << std::endl;
2008-04-11 15:57:56 +02:00
for(unsigned int i=0; i<panz; i++) {
mfile.read((char*)&tp, 2);
p.x = tp;
mfile.read((char*)&tp, 2);
p.y = tp;
mfile.read((char*)&tp, 2);
p.z = tp;
mfile.read((char*)&tp, 2);
tp; // flags
// p.print("Polygon");
2008-04-11 15:57:56 +02:00
modobj.polygon.push_back(p);
}
}
break;
// Komischer Chunk, ein haufen Zahlen sind sich selber zugewiesen
case 0x4130:
{
std::string name;
char c;
do {
mfile.read(&c, 1);
name += c;
} while(c!=0);
modobj.mat = m->findMaterial(name);
std::cout << "Mat Name: " << name << std::endl;
unsigned short panz, tp;
mfile.read((char*)&panz, 2);
mfile.ignore(panz*2);
// for(unsigned int i=0; i<panz; i++) {
// mfile.read((char*)&tp, 2);
// std::cout << i << " ==> " << tp << std::endl;
// }
}
break;
2008-04-11 15:57:56 +02:00
case 0x4140:
{
unsigned short panz;
Punkt2D p;
mfile.read((char*)&panz, 2);
// std::cout << "\t\t\tUV-Anz: " << panz << std::endl;
2008-04-11 15:57:56 +02:00
for(unsigned int i=0; i<panz; i++) {
mfile.read((char*)&p.x, sizeof(float));
mfile.read((char*)&p.y, sizeof(float));
modobj.mapcoord.push_back(p);
}
}
break;
case 0xafff:
// Materialchunk
break;
case 0xa000:
{
if(materialloaded) {
m->addMaterial(mat);
mat.clear();
}
materialloaded = true;
std::string name;
char c;
do {
mfile.read(&c, 1);
name += c;
} while(c!=0);
mat.name = name;
std::cout << "Mat Name: " << name << std::endl;
}
break;
case 0xa010:
{
GLColor col;
col = readColorChunk(&mfile);
mat.ambient = col;
std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
}
break;
case 0xa020:
{
GLColor col;
col = readColorChunk(&mfile);
mat.diffuse = col;
std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
}
break;
case 0xa030:
{
GLColor col;
col = readColorChunk(&mfile);
mat.specular = col;
std::cout << col.r << ", " << col.g << ", " << col.b << std::endl;
}
break;
2008-04-10 00:59:47 +02:00
default:
//Switch Chunk
// std::cout << "einfach ignorieren..." << std::endl;
2008-04-10 18:55:31 +02:00
mfile.ignore(len-6);
// std::cout << "moep" << std::endl;
2008-04-10 00:59:47 +02:00
break;
}
}
if(materialloaded)
m->addMaterial(mat);
2008-04-11 15:57:56 +02:00
if(modelloaded) {
modobj.calcNormales();
modobj.mat = m->findMaterial(modobj.getName());
2008-04-11 15:57:56 +02:00
m->addObject(modobj);
}
std::cout << "Ende Load" << std::endl;
2008-04-10 18:55:31 +02:00
mfile.close();
2008-04-10 00:59:47 +02:00
return true;
}
GLColor Load3ds::readColorChunk(std::ifstream *file) {
GLColor col;
unsigned short id, tp;
unsigned int clen;
file->read((char*)&id, 2); // Color, warscheinlich nur bytes, wenn nich implementiere funktion
file->read((char*)&clen, 4); // len, gleiches wie oben
switch(id) {
case 0x11:
file->read((char*)&tp, 1);
col.r = tp/255.0f;
file->read((char*)&tp, 1);
col.g = tp/255.0f;
file->read((char*)&tp, 1);
col.b = tp/255.0f;
break;
default:
file->ignore(clen-6);
break;
}
return col;
}
2008-04-10 00:59:47 +02:00
void Load3ds::unload() {
parsed = false;
error = false;
}