00001 #include "3dsshape.hh"
00002 #include "dlc_register.h"
00003
00004 #include "stringhash.hh"
00005 #include "3ds.hh"
00006
00007 struct 3dsData {
00008 DtShapeRef shape;
00009 GLuint display;
00010 };
00011
00012 string_map<3dsData *> loaded;
00013
00014 void o3dsShape::init(oConfiguration &c) {
00015 if (c.hasElement("filename")) {
00016 string s = c.getString("filename")
00017
00018 3dsData* data;
00019 data = loaded[s.c_str()];
00020 if (data == NULL) {
00021 data = new 3dsData;
00022 data->display = glGenLists(1);
00023
00024 3ds loader(s);
00025
00026 glNewList(data->display, GL_COMPILE);
00027 glBegin(GL_TRIANGLES);
00028
00029 data->shape = dtNewComplexShape();
00030
00031 cout << "number of objects: " << loader.numOfObjects << endl;
00032 for(int i=0; i<loader.numOfObjects; i++) {
00033 if(loader.pObject.size() <= 0) break;
00034
00035 t3DObject *pObject = &loader.pObject[i];
00036 if(pObject->bHasTexture) {
00037 glEnable(GL_TEXTURE_2D);
00038 glColor3ub(255, 255, 255);
00039 glBindTexture(GL_TEXTURE_2D, texture[pObject->materialID]);
00040 } else {
00041 glDisable(GL_TEXTURE_2D);
00042 }
00043
00044 for(int j = 0; j < pObject->numOfFaces; j++) {
00045
00046 dtBegin(DT_SIMPLEX);
00047 for(int whichVertex = 0; whichVertex < 3; whichVertex++) {
00048 int index = pObject->pFaces[j].vertIndex[whichVertex];
00049 glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
00050 if(pObject->bHasTexture) {
00051 if(pObject->pTexVerts) {
00052 glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
00053 }
00054 } else {
00055 BYTE *pColor = loader.pMaterials[pObject->materialID].color;
00056 glColor3ub(pColor[0], pColor[1], pColor[2]);
00057 }
00058 glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
00059 dtVertex(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
00060 }
00061 dtEnd();
00062 }
00063 glEnd();
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 dtEndComplexShape();
00086
00087 glEnd();
00088 glEndList();
00089
00090 loaded[s.c_str()] = data;
00091 }
00092
00093 dtCreateObject(parent, data->shape);
00094 display = data->display;
00095 } else {
00096 cerr << "No filename given for 3dshape" << endl;
00098 }
00099
00100 Shape::init(xml);
00101 }
00102
00103 void o3dsShape::redraw() {
00104 glCallList(display);
00105 Shape::redraw();
00106 }
00107
00108 DLC_Register_Macro(o3dsShape)