//data loading bool ModelData::LoadIn(QString filepath) { bool loaderReady; bool abort; STLTri* pLoadedTri = NULL; Triangle3D newtri; this->filepath = filepath; if(filepath.isEmpty()) return false; //extract filename from path! filename = QFileInfo(filepath).fileName(); B9ModelLoader mLoader(filepath,loaderReady,NULL); if(loaderReady == false)//error opening model data { //display Loader Error QMessageBox msgBox; msgBox.setText(mLoader.GetError()); msgBox.exec(); return false; } //make a progress bar and connect it to LoadingBar loadbar(0,100); loadbar.useCancelButton(false); loadbar.setDescription("Importing: " + filename); QObject::connect(&mLoader,SIGNAL(PercentCompletedUpdate(qint64,qint64)), &loadbar,SLOT(setProgress(qint64,qint64))); //now we are ready to walk the loader through reading each triangle //and copying it into the this model data. while(mLoader.LoadNextTri(pLoadedTri,abort)) { if(abort) { //display Loader abort error QMessageBox msgBox; msgBox.setText(mLoader.GetError()); msgBox.exec(); return false; } else { //newtri.normal.setX(pLoadedTri->nx); //newtri.normal.setY(pLoadedTri->ny); //newtri.normal.setZ(pLoadedTri->nz); newtri.vertex[0].setX(pLoadedTri->x0); newtri.vertex[0].setY(pLoadedTri->y0); newtri.vertex[0].setZ(pLoadedTri->z0); newtri.vertex[1].setX(pLoadedTri->x1); newtri.vertex[1].setY(pLoadedTri->y1); newtri.vertex[1].setZ(pLoadedTri->z1); newtri.vertex[2].setX(pLoadedTri->x2); newtri.vertex[2].setY(pLoadedTri->y2); newtri.vertex[2].setZ(pLoadedTri->z2); //use right hand rule for importing normals - ignore file normals.. newtri.UpdateNormalFromGeom(); delete pLoadedTri; newtri.UpdateBounds(); triList.push_back(newtri); } } qDebug() << "Loaded triangles: " << triList.size(); //now center it! CenterModel(); //generate a normal display lists. int displaySuccess = FormNormalDisplayLists(); if(displaySuccess) return true; else return false; }
//Baking //using all rotations scales positions etc, generates the list of triangles that //is represented by the rendered shapes. //similar to instance baking. //remember that this function needed to always reflect what render() does but inverted. void B9SupportStructure::BakeToInstanceGeometry() { unsigned int t; unsigned short int v; Triangle3D* pNewTri; QVector3D topScale; QVector3D topPos; QVector3D midScale; QVector3D midPos; QVector3D bottomScale; QVector3D bottomPos; //first bake the top topScale = QVector3D(topRadius*2.0,topRadius*2.0,topLength + topPenetration); topPos = QVector3D((topPenetrationPoint.x() + topPivot.x())*0.5 ,(topPenetrationPoint.y() + topPivot.y())*0.5 ,(topPenetrationPoint.z() + topPivot.z())*0.5); if(topAttachShape != NULL) for(t = 0; t < topAttachShape->GetTriangles()->size(); t++) { pNewTri = new Triangle3D(topAttachShape->GetTriangles()->at(t)); for(v=0;v<3;v++) { //scale triangle vertices 1st pNewTri->vertex[v] *= topScale; //Rotate 2nd //dont deal with y - not needed in rendering or baking RotateVector(pNewTri->vertex[v], topThetaX, QVector3D(1,0,0));//x RotateVector(pNewTri->vertex[v], topThetaZ, QVector3D(0,0,1));//z //Translate 3rd into global space pNewTri->vertex[v] += topPos; pNewTri->vertex[v] += instanceParent->GetPos(); } //Update the triangle bounds and normal pNewTri->UpdateBounds(); pNewTri->UpdateNormalFromGeom(); //Add To List instanceParent->triList.push_back(pNewTri); } //second bake the middle midScale = QVector3D(midRadius*2.0,midRadius*2.0,midLength); midPos = QVector3D((topMidExtensionPoint.x() + bottomMidExtensionPoint.x())*0.5 ,(topMidExtensionPoint.y() + bottomMidExtensionPoint.y())*0.5 ,(topMidExtensionPoint.z() + bottomMidExtensionPoint.z())*0.5); if(midAttachShape != NULL) for(t = 0; t < midAttachShape->GetTriangles()->size(); t++) { pNewTri = new Triangle3D(midAttachShape->GetTriangles()->at(t)); for(v=0;v<3;v++) { //scale triangle vertices 1st pNewTri->vertex[v] *= midScale; //Rotate 2nd //dont deal with y - not needed in rendering or baking RotateVector(pNewTri->vertex[v], midThetaX, QVector3D(1,0,0));//x RotateVector(pNewTri->vertex[v], midThetaZ, QVector3D(0,0,1));//z //Translate 3rd into global space pNewTri->vertex[v] += midPos; pNewTri->vertex[v] += instanceParent->GetPos(); } //Update the triangle bounds and normal pNewTri->UpdateBounds(); pNewTri->UpdateNormalFromGeom(); //Add To List instanceParent->triList.push_back(pNewTri); } //third bake the bottom bottomScale = QVector3D(bottomRadius*2.0,bottomRadius*2.0,bottomLength + bottomPenetration); bottomPos = QVector3D((bottomPenetrationPoint.x() + bottomPivot.x())*0.5 ,(bottomPenetrationPoint.y() + bottomPivot.y())*0.5 ,(bottomPenetrationPoint.z() + bottomPivot.z())*0.5); if(bottomAttachShape != NULL) for(t = 0; t < bottomAttachShape->GetTriangles()->size(); t++) { pNewTri = new Triangle3D(bottomAttachShape->GetTriangles()->at(t)); for(v=0;v<3;v++) { //scale triangle vertices 1st pNewTri->vertex[v] *= bottomScale; //Rotate 2nd //dont deal with y - not needed in rendering or baking RotateVector(pNewTri->vertex[v], bottomThetaX+180, QVector3D(1,0,0));//x RotateVector(pNewTri->vertex[v], bottomThetaZ, QVector3D(0,0,1));//z //Translate 3rd into global space pNewTri->vertex[v] += bottomPos; pNewTri->vertex[v] += instanceParent->GetPos(); } //Update the triangle bounds and normal pNewTri->UpdateBounds(); pNewTri->UpdateNormalFromGeom(); //Add To List instanceParent->triList.push_back(pNewTri); } }