Chápu, že je to strašně dlouhé, ale opravdu nevím, jak dál.. Je to jeden z takových těch problémů, kdy čím déle nad ním přemýšlím, tím menší mám šanci na něj přijít, takže kdyby se tu někde našla nějaká dobrá duše, která by zdrojáv v rychlosti prolétla a upozornila mě na tu určitě naprosto začátečnickou a snadno objevitelnou chybu, byl bych mooc rád =) TIA
b3DS.h
#ifndef _B3DS_H_
#define _B3DS_H_
#include <iostream>
#include <vector>
using namespace std;
#define BUILD
// CHUNKS
#define PRIMARY 0x4D4D
#define EDIT 0x3D3D
#define OBJECT 0x4000
#define MESH 0x4100
#define VERTS 0x4110
#define FACES 0x4120
#define FACES_MAT 0x4130
#define COORDS 0x4140
#define LIGHT 0x4600
#define CAMERA 0x4700
#define MATERIAL 0xAFFF
#define KEYFRAME 0xB000
class b3DSVect3
{
public:
float x, y, z;
};
class b3DSVect2
{
public:
float u, v;
};
class b3DSObject
{
public:
uint nVerts;
b3DSVect3 *vert;
char name[255];
};
class b3DSModel
{
public:
uint nObjects;
vector<b3DSObject> object;
};
class b3DSChunk
{
public:
uint id;
uint size;
unsigned long int read;
};
class b3DS
{
public:
b3DS(char *filename);
void b3DShowModel();
private:
FILE *file;
b3DSModel model;
void b3DSProcessChunk(b3DSChunk *tChunk);
void b3DSProcessObjectChunk(b3DSChunk *tChunk);
void b3DSReadChunk(b3DSChunk *chunk);
uint b3DSGetString(char *str);
void b3DSReadVerts(b3DSChunk *tChunk);
};
#endif
/*******************************************************************************
MAIN CHUNK 0x4D4D
3D EDITOR CHUNK 0x3D3D
OBJECT BLOCK 0x4000
TRIANGULAR MESH 0x4100
VERTICES LIST 0x4110
FACES DESCRIPTION 0x4120
FACES MATERIAL 0x4130
MAPPING COORDINATES LIST 0x4140
SMOOTHING GROUP LIST 0x4150
LOCAL COORDINATES SYSTEM 0x4160
LIGHT 0x4600
SPOTLIGHT 0x4610
CAMERA 0x4700
MATERIAL BLOCK 0xAFFF
MATERIAL NAME 0xA000
AMBIENT COLOR 0xA010
DIFFUSE COLOR 0xA020
SPECULAR COLOR 0xA030
TEXTURE MAP 1 0xA200
BUMP MAP 0xA230
REFLECTION MAP 0xA220
[SUB CHUNKS FOR EACH MAP]
MAPPING FILENAME 0xA300
MAPPING PARAMETERS 0xA351
KEYFRAMER CHUNK 0xB000
MESH INFORMATION BLOCK 0xB002
SPOT LIGHT INFORMATION BLOCK 0xB007
FRAMES (START AND END) 0xB008
OBJECT NAME 0xB010
OBJECT PIVOT POINT 0xB013
POSITION TRACK 0xB020
ROTATION TRACK 0xB021
SCALE TRACK 0xB022
HIERARCHY POSITION 0xB030
********************************************************************************/
b3DS.cc
b3DS::b3DS(char *filename)
{
#ifdef BUILD
cout << "b3DS" << endl;
#endif
b3DSChunk aChunk;
model.nObjects = 0;
file = fopen(filename, "rb");
if(!file){
cout << "File not found!" << endl;
exit(1);
}
b3DSReadChunk(&aChunk);
if(aChunk.id != PRIMARY){
cout << "Unknown format!" << endl;
exit(1);
}
b3DSProcessChunk(&aChunk);
// tady program spadne se segmentation fault, nebo (v pripade ze fclose odstranim, spadne s aborted :'-(
fclose(file);
#ifdef BUILD
cout << "END" << endl;
#endif
}
void b3DS::b3DShowModel()
{
}
void b3DS::b3DSProcessChunk(b3DSChunk *tChunk)
{
#ifdef BUILD
cout << "ProcessChunk" << endl;
#endif
b3DSChunk aChunk;
uint buffer[100000];
while(tChunk->read < tChunk->size){
b3DSReadChunk(&aChunk);
switch(aChunk.id){
case EDIT:
{
#ifdef BUILD
cout << "EDIT" << endl;
#endif
b3DSProcessChunk(&aChunk);
break;
}
case OBJECT:
{
#ifdef BUILD
cout << "OBJECT" << endl;
#endif
model.nObjects += 1;
model.object.resize(model.nObjects);
aChunk.read += b3DSGetString(model.object[model.nObjects].name);
b3DSProcessChunk(&aChunk);
break;
}
case MATERIAL:
{
#ifdef BUILD
cout << "MATERIAL" << endl;
#endif
b3DSProcessChunk(&aChunk);
break;
}
case KEYFRAME:
{
#ifdef BUILD
cout << "KEYFRAME" << endl;
#endif
b3DSProcessChunk(&aChunk);
break;
}
case MESH:
{
#ifdef BUILD
cout << "MESH" << endl;
#endif
b3DSProcessChunk(&aChunk);
break;
}
case VERTS:
{
#ifdef BUILD
cout << "VERTS" << endl;
#endif
b3DSReadVerts(&aChunk);
break;
}
case FACES:
{
#ifdef BUILD
cout << "FACES" << endl;
#endif
aChunk.read += fread(buffer, 1, aChunk.size - aChunk.read, file);
break;
}
case FACES_MAT:
{
#ifdef BUILD
cout << "FACES_MAT" << endl;
#endif
aChunk.read += fread(buffer, 1, aChunk.size - aChunk.read, file);
break;
}
case COORDS:
{
#ifdef BUILD
cout << "COORDS" << endl;
#endif
aChunk.read += fread(buffer, 1, aChunk.size - aChunk.read, file);
break;
}
default:
{
#ifdef BUILD
cout << "UNKNOWN" << endl;
#endif
aChunk.read += fread(buffer, 1, aChunk.size - aChunk.read, file);
break;
}
}
tChunk->read += aChunk.read;
}
#ifdef BUILD
cout << "OK" << endl;
#endif
}
void b3DS::b3DSReadChunk(b3DSChunk *chunk)
{
#ifdef BUILD
cout << "ReadChunk" << endl;
#endif
chunk->id = 0;
chunk->read = 0;
chunk->size = 0;
chunk->read += fread(&chunk->id, 1, 2, file);
chunk->read += fread(&chunk->size, 1, 4, file);
#ifdef BUILD
cout << "OK" << endl;
#endif
}
void b3DS::b3DSReadVerts(b3DSChunk *tChunk)
{
#ifdef BUILD
cout << "ReadVerts" << endl;
#endif
tChunk->read += fread(&model.object[model.nObjects].nVerts, 1, 2, file);
model.object[model.nObjects].vert = new b3DSVect3[sizeof(b3DSVect3) * model.object[model.nObjects].nVerts];
tChunk->read += fread(model.object[model.nObjects].vert, 1, tChunk->size - tChunk->read, file);
for(uint i=0; i < model.object[model.nObjects].nVerts; i++){
float ty = model.object[model.nObjects].vert[i].y;
model.object[model.nObjects].vert[i].y = model.object[model.nObjects].vert[i].z;
model.object[model.nObjects].vert[i].z = ty * -1;
}
#ifdef BUILD
cout << "OK" << endl;
#endif
}
uint b3DS::b3DSGetString(char *str)
{
#ifdef BUILD
cout << "GetStrig" << endl;
#endif
uint k=0;
fread(str, 1, 1, file);
while(*(str+(k++)) != 0){
fread(str+k, 1, 1, file);
}
#ifdef BUILD
cout << "OK" << endl;
#endif
return k;
}