From a853e102eb33fc1abb3a36f7d44f5f8b169a00ab Mon Sep 17 00:00:00 2001 From: seba Date: Sun, 13 Apr 2008 03:17:27 +0200 Subject: [PATCH] cooles update, funtzt mehr, materialien noch nich ganz --- models/load3ds.cpp | 117 ++++++++++++++++++++++++++++++++++++++++----- models/load3ds.h | 3 ++ models/model.cpp | 71 ++++++++++++++++++++++----- models/model.h | 28 ++++++++++- 4 files changed, 196 insertions(+), 23 deletions(-) diff --git a/models/load3ds.cpp b/models/load3ds.cpp index 2da72db..08aa5bf 100644 --- a/models/load3ds.cpp +++ b/models/load3ds.cpp @@ -22,35 +22,35 @@ bool Load3ds::parse(Model *m) { unsigned short ident; unsigned int len; + bool modelloaded = false; + bool materialloaded = false; + + Material mat; ModelObject modobj; m->clear(); - std::cout << "size: " << sizeof(int) << std::endl; while(!mfile.eof()) { ident = len = 0; mfile.read((char *)&ident, 2); mfile.read((char *)&len, 4); -// mfile.read((char *)&m, 1); -// mfile.read((char *)&n, 1); + std::cout << "Chunk: 0x" << std::hex << ident << " (" << std::dec << len << ")" << std::endl; switch(ident) { case 0x4d4d: // Main Chunk - std::cout << "yeah!" << std::endl; break; case 0x3d3d: // 3D Editor Chunk - std::cout << "editorchunk" << std::endl; break; case 0x4000: { if(modelloaded) { - modobj.calcNormales(); + modobj.calcNormales(); m->addObject(modobj); modobj.clear(); } modelloaded = true; - std::cout << "namechunk" << std::endl; +// std::cout << "namechunk" << std::endl; std::string name; char c; do { @@ -67,6 +67,7 @@ bool Load3ds::parse(Model *m) { unsigned short panz; Punkt3D p; mfile.read((char*)&panz, 2); +// std::cout << "\t\t\tVertexanz: " << panz << std::endl; glBegin(GL_LINE_LOOP); for(unsigned int i=0; ifindMaterial(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 " << tp << std::endl; +// } + } + break; case 0x4140: { unsigned short panz; Punkt2D p; mfile.read((char*)&panz, 2); +// std::cout << "\t\t\tUV-Anz: " << panz << std::endl; for(unsigned int i=0; iaddMaterial(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; default: //Switch Chunk - std::cout << "einfach ignorieren..." << std::endl; +// std::cout << "einfach ignorieren..." << std::endl; mfile.ignore(len-6); +// std::cout << "moep" << std::endl; break; } } + if(materialloaded) + m->addMaterial(mat); if(modelloaded) { modobj.calcNormales(); + modobj.mat = m->findMaterial(modobj.getName()); m->addObject(modobj); - modobj.clear(); } - + std::cout << "Ende Load" << std::endl; mfile.close(); 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; +} + void Load3ds::unload() { parsed = false; error = false; diff --git a/models/load3ds.h b/models/load3ds.h index 4dd5b90..ee46745 100644 --- a/models/load3ds.h +++ b/models/load3ds.h @@ -6,6 +6,7 @@ #include #include #include "model.h" +#include "../glcolor.h" class Chunk { private: @@ -24,6 +25,8 @@ class Load3ds { bool parsed; bool error; + + GLColor readColorChunk(std::ifstream *file); public: Load3ds(std::string _fname, bool parse=true); diff --git a/models/model.cpp b/models/model.cpp index 3c826ff..6027cab 100644 --- a/models/model.cpp +++ b/models/model.cpp @@ -13,35 +13,70 @@ void ModelObject::clear() { void ModelObject::calcNormales() { normal.clear(); - Punkt3D a, b; + Punkt3D a, b, c; for(unsigned int i=0; i polygon; void calcNormales(); std::string name; + + Material mat; public: ModelObject(); + std::string getName(); void render(); void clear(); @@ -28,13 +51,16 @@ class ModelObject { class Model { private: std::vector objects; - + std::vector materials; public: Model(); void addObject(ModelObject c); + void addMaterial(Material m); void clear(); void render(); + + Material findMaterial(std::string name); }; #endif