//---------------------------------------------------------------------- void game::exit(){ sgDeleteObject(sgBunny); sgDeleteObject(sgTetrahedron); sgDeleteObject(sgDodecahedron); sgDeleteObject(sgIcosahedron); sgDeleteObject(sgOctahedron); //sgDeleteObject(sgTeapot); }
//-------------------------------------------------------------- //////////////////////////// Intersection/////////////////////// void slicer::intersectCubes(sgCObject *obj){ //it uses intersection of 27 cubes, on the object, to get all the pieces for each cubie in one oeration //all pieces are left in pieces[] for(int i =0; i<27; i ++){ sgCObject *tempObj = obj->Clone(); sgCObject *tempCutter = myCutter->cubes[i]->Clone(); //do intersecton at origin pieces[i] = sgBoolean::Intersection(*(sgC3DObject*)tempObj,*(sgC3DObject*)tempCutter); //now we have the whole piece that goes into a cubie for that slicning cube //clean up sgDeleteObject(tempObj); sgDeleteObject(tempCutter); } }
//---------------------------------------------------------------------- bool game::extrudeObject(ofPolyline *drawing){ //this functino extrudes the input ofPolylne vector< ofPoint > points = drawing->getVertices(); sgCObject* cont_objcts[2000];///////////////////////////////////////////////////////////////////////////////clean this memeory!!! for (int i =0; i < points.size() ; i ++) { if(i!= points.size()-1){ cont_objcts[i] = sgCreateLine(points[i].x, 0, points[i].y,points[i+1].x, 0, points[i+1].y); }else{ //its the last point.. make the line with the first point cont_objcts[i] = sgCreateLine(points[i].x, 0, points[i].y,points[0].x, 0, points[0].y); } } sgCContour* win_cont = sgCContour::CreateContour(cont_objcts, points.size()-1); //check if its self intersecting if(win_cont->IsSelfIntersecting()){ //its self intersecting //abort!!! //for now extrudedB = false; //clear ofpolylines!!! //myCanvas->myPolyline->clear(); //free(myCanvas->myPolyline); //myCanvas->myPolyline2->clear(); free(drawing); sgDeleteObject(win_cont); return false; }else{ ////extrude along vector SG_VECTOR extVec = {0,-300,0}; if (objectID == -1){ extrudedObject = (sgC3DObject*)sgKinematic::Extrude(*win_cont,NULL,0,extVec,true); }else{ free(extrudedObject); extrudedObject = (sgC3DObject*)sgKinematic::Extrude(*win_cont,NULL,0,extVec,true); } extrudedB = true; //we have the sg3DObjcect to load loadObject(200,slicingPos,posP);//using id=200 free(drawing); sgDeleteObject(win_cont); return true; } ////////free(cont_objcts); }
void AddFloorInScene(double size1, double size2, double x_shift, double y_shift, double z_shift) { sgCObject* objects_buffer[4]; objects_buffer[0] = sgCreateLine(size1/2.0, size2/2.0, 0.0, -size1/2.0, size2/2.0, 0.0); objects_buffer[1] = sgCreateLine(-size1/2.0, size2/2.0, 0.0, -size1/2.0, -size2/2.0, 0.0); objects_buffer[2] = sgCreateLine(-size1/2.0, -size2/2.0, 0.0, size1/2.0, -size2/2.0, 0.0); objects_buffer[3] = sgCreateLine(size1/2.0, -size2/2.0, 0.0, size1/2.0, size2/2.0, 0.0); sgCContour* cnt = sgCContour::CreateContour(objects_buffer,4); sgCObject* floor = sgSurfaces::Face((const sgC2DObject&)(*cnt),NULL,0); sgDeleteObject(cnt); sgGetScene()->AttachObject(floor); floor->SetAttribute(SG_OA_COLOR,25); SG_VECTOR transV = {x_shift, y_shift, z_shift}; floor->InitTempMatrix()->Translate(transV); floor->ApplyTempMatrix(); floor->DestroyTempMatrix(); }
//---------------------------------------------------------------------- void game::extrudeObject(){ //create and use circle SG_POINT crCen = {0,0,0.0}; SG_VECTOR crNor; crNor.x = 0.0; crNor.y = 3.0; crNor.z = 0.0; sgSpaceMath::NormalVector(crNor); SG_CIRCLE crGeo; crGeo.FromCenterRadiusNormal( crCen, 150, crNor); sgCCircle* cr = sgCreateCircle(crGeo); ////extrude along vector SG_VECTOR extVec = {0,-300,0}; if (objectID == -1){ extrudedObject = (sgC3DObject*)sgKinematic::Extrude((const sgC2DObject&)(*cr),NULL,0,extVec,true); }else{ free(extrudedObject); extrudedObject = (sgC3DObject*)sgKinematic::Extrude((const sgC2DObject&)(*cr),NULL,0,extVec,true); } extrudedObject->SetAttribute(SG_OA_COLOR,30); extrudedB = true; sgDeleteObject(cr); //we have the sg3DObjcect to load loadObject(200,slicingPos,posP);//using id=200 }
//---------------------------------------------------------------------- void game::loadPuzzle(puzzle *inputPuzzle){ ///////////////////////////////////////////////////////// ///////////do game reset..because loading a puzzle can happen at anytime if(step == 6){ if(canvasB){ myCanvas->exit(); delete myCanvas; canvasB = false; } step = 0; objectID = -1; }else if(step==4 || step==5){ myPuzzle->exit(); myCutter->exit(); mySlicer->exit(); objectDisplayed->exit(); objectID = -1; step = 0; armID = -1; }else if (step==3){ objectDisplayed->exit(); //clean displayed object after puzzle is created, so we dont keep it until the exit or restart step = 0; objectID = -1; armID = -1; }else if (step==1 || step==2){ objectDisplayed->exit(); step = 0; objectID = -1; if(canvasB){ myCanvas->exit(); delete myCanvas; canvasB = false; } } offsetSlicer.x = 0; offsetSlicer.y = 0; offsetSlicer.z = 0; rotateSlicer.x = 0; rotateSlicer.y = 0; rotateSlicer.z = 0; curRot.set (ofVec4f(0,0,0,0)); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } ////////////////////////////////////////////////////////// //load a puzzle from the puzzle menu on the center myPuzzle = inputPuzzle; myPuzzle->pos.x = posP.x; myPuzzle->pos.y = posP.y; myPuzzle->pos.z = posP.z; step = 7; objectID = -1; }
//---------------------------------------------------------------- void puzzle::loadPieces(sgCGroup **pcs,int selObjId, ofVec3f v){ //it loads the pieces that the slicer made, the pieces are in a sgCGroup** pieces[], //this function receives a copy of that sgCGroup** made by mySlicer->getPieces() //it loads them into its own cubies //create cubies //so each time there is a new boolean operation, whole new cubies get created with variables in zero or blank for(int i=0;i<numPieces;i++){ cubie *auxCubie = new cubie(pos.x,pos.y,pos.z,i+1,selObjId,cubiesOffset); //auxCubie->setup(); //add this cubie to mycubies[] myCubies[i] = auxCubie; } for(int i=0;i<numPieces;i++){ //get group from pieces[] copy: pcs[] sgCGroup *part = pcs[i]; if(part != NULL){ const int ChCnt = part->GetChildrenList()->GetCount(); sgCObject** allParts = (sgCObject**)malloc(ChCnt*sizeof(sgCObject*)); part->BreakGroup(allParts); sgCObject **obj = (sgCObject**)malloc(50*sizeof(sgCObject*)); int realNumPieces=0; for (int j=0; j < ChCnt; j++){ //clone each part obj[j] =allParts[j]; realNumPieces ++; } //make them a group cubieGroup = sgCGroup::CreateGroup(obj,realNumPieces); myCubies[i]->setObjects(cubieGroup,i,v);//here goes the group of clones from the iriginal slicing pieces[] //i is the cubie ID //put that cubie on the cubies[] //cleanup free(obj); if(allParts != NULL){ for (int j=0; j < ChCnt; j++){ if(allParts[j] != NULL){ sgCObject::DeleteObject(allParts[j]); } } free(allParts); } sgDeleteObject(part); }else{ myCubies[i]->setObjects(NULL,i,v); } } //create the meshes from the sgCore objects //so the objects can be renderes by openFrameworks for(int i=0;i<numPieces;i++){ myCubies[i]->crateOfMeshs(); } }
//---------------------------------------------------------------- void myobject3D::loadObject(sgC3DObject *obj, int ID) { //it will load a sgCore lib object: torus, box if(object==NULL) { object = obj; } else { sgDeleteObject(object); object = obj; } objectId = ID; if(objectId == 1) { //torus SG_VECTOR offset = {0,0,0}; //for torus to be in place, the object->InitTempMatrix()->Translate(offset);//this translates the object to be cut!! //SG_POINT rotP = {0,0,0}; //SG_VECTOR rotV = {1,0,0}; //object->GetTempMatrix()->Rotate(rotP,rotV,ofDegToRad(45)); object->ApplyTempMatrix(); object->DestroyTempMatrix(); } else if(objectId == 2) { //cube SG_VECTOR offset = {-150,-150,-150}; //for the cube to be in center place, it has sides of 300 object->InitTempMatrix()->Translate(offset);//this translates the object to be cut!! object->ApplyTempMatrix(); object->DestroyTempMatrix(); } else if(objectId == 3) { //cone..pyramid /*SG_POINT rotP = {0,0,0}; SG_VECTOR rotV = {1,0,0}; object->InitTempMatrix()->Rotate(rotP,rotV,ofDegToRad(90)); SG_VECTOR offset = {0,100,0}; object->GetTempMatrix()->Translate(offset); object->ApplyTempMatrix(); object->DestroyTempMatrix();*/ } else if(objectId == 4) { //rabbit SG_POINT rotP = {0,0,0}; SG_VECTOR rotV = {1,0,0}; object->InitTempMatrix()->Rotate(rotP,rotV,ofDegToRad(180)); /*SG_VECTOR offset = {0,800,200}; object->GetTempMatrix()->Translate(offset);*/ object->ApplyTempMatrix(); object->DestroyTempMatrix(); } else if(objectId == 200) { //extruded object SG_VECTOR offset = {0,150,0}; object->InitTempMatrix()->Translate(offset); object->ApplyTempMatrix(); object->DestroyTempMatrix(); } }
//---------------------------------------------------------------------- void game::restart(){ if(step == 7){ myPuzzle->exit(); step = 0; objectID = -1; }else if(step == 6){ if(canvasB){ myCanvas->exit(); delete myCanvas; canvasB = false; } step = 0; objectID = -1; }else if(step==4 || step==5){ myPuzzle->exit(); myCutter->exit(); mySlicer->exit(); objectDisplayed->exit(); objectID = -1; step = 0; armID = -1; }else if (step==3){ objectDisplayed->exit(); //clean displayed object after puzzle is created, so we dont keep it until the exit or restart step = 0; objectID = -1; armID = -1; }else if (step==1 || step==2){ objectDisplayed->exit(); step = 0; objectID = -1; if(canvasB){ myCanvas->exit(); delete myCanvas; canvasB = false; } } offsetSlicer.x = 0; offsetSlicer.y = 0; offsetSlicer.z = 0; rotateSlicer.x = 0; rotateSlicer.y = 0; rotateSlicer.z = 0; curRot.set (ofVec4f(0,0,0,0)); curRotA.set (ofVec4f(0,0,0,0)); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } }
sgCObject* CDrillsScene::CreateDrill(double rad, double dH) { sgC2DObject* drill_1_sections[10]; double params[10]; memset(params,0,sizeof(double)*10); drill_1_sections[0] = CreateDrillSection(rad); SG_POINT rotAxeP = {0.0, 0.0, 0.0}; SG_VECTOR rotAxeDir = {0.0, 0.0, 1.0}; SG_VECTOR transVec = {0.0, 0.0, 1.0}; for (int i=1;i<7;i++) { drill_1_sections[i] = (sgCContour*)(drill_1_sections[0]->Clone()); drill_1_sections[i]->InitTempMatrix()->Rotate(rotAxeP,rotAxeDir,i*5.0*3.14159265/6.0); transVec.z = dH*i; drill_1_sections[i]->GetTempMatrix()->Translate(transVec); drill_1_sections[i]->ApplyTempMatrix(); drill_1_sections[i]->DestroyTempMatrix(); } SG_CIRCLE cirGeo; cirGeo.center.x = cirGeo.center.y = 0.0; cirGeo.center.z = 7*dH+5; cirGeo.normal.x = cirGeo.normal.y = 0.0; cirGeo.normal.z = 1.0; cirGeo.radius = rad-0.1; drill_1_sections[7] = sgCreateCircle(cirGeo); drill_1_sections[7]->ChangeOrient(); cirGeo.center.z = 7*dH+10; drill_1_sections[8] = sgCreateCircle(cirGeo); drill_1_sections[8]->ChangeOrient(); cirGeo.center.z =7*dH+15; drill_1_sections[9] = sgCreateCircle(cirGeo); drill_1_sections[9]->ChangeOrient(); sgCObject* resul = sgSurfaces::SplineSurfaceFromSections((const sgC2DObject**)drill_1_sections, params,10,true); for (int i=0;i<10;i++) sgDeleteObject(drill_1_sections[i]); return resul; }
//---------------------------------------------------------------------- void game::loadObject(int objID, SG_VECTOR p, SG_VECTOR t){ if (objectID == -1){ objectDisplayed = new myobject3D(p,t); }else{ objectDisplayed->exit(); delete objectDisplayed; objectDisplayed = new myobject3D(p,t); } objectID = objID; if(step == 0 || step==1 || step == 6){ if(objID == 1){ //torus objectDisplayed->loadObject(sgCreateTorus(100,70,50,50),1);//(radius,thickness,meridiansDonut,meridiansDonutCut) if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } if(objID == 2){ //cube objectDisplayed->loadObject(sgCreateBox(300,300,300),2);//(tamX,tamY,tamZ) if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } }if(objID == 3){ //cone //objectDisplayed->loadObject(sgCreateCone(250,1,250,3),3); objectDisplayed->loadObject((sgC3DObject *)sgTetrahedron->Clone(),3); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } if(objID == 4){ //try to load the bunny objectDisplayed->loadObject((sgC3DObject *)sgBunny->Clone(),4); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } if(objID == 5){ //try to load the dodecahedron objectDisplayed->loadObject((sgC3DObject *)sgDodecahedron->Clone(),5); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } if(objID == 6){ //try to load the Icosahedron objectDisplayed->loadObject((sgC3DObject *)sgIcosahedron->Clone(),6); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } if(objID == 7){ //try to load the Octahedron objectDisplayed->loadObject((sgC3DObject *)sgOctahedron->Clone(),7); if(extrudedB){ sgDeleteObject(extrudedObject); extrudedB = false; } } //if(objID == 8){ // //try to load the Teapot // objectDisplayed->loadObject((sgC3DObject *)sgTeapot->Clone(),8); //} if(objID == 200){ //load extruded object objectDisplayed->loadObject((sgC3DObject *)extrudedObject->Clone(),200); } objectDisplayed->setup(); step = 1; } ////////////////////// from STL file /*const char* nel = ofToDataPath("cube.stl",false).c_str(); objectDisplayed.loadObjectFromFile(nel);*/ }
CDrillsScene::CDrillsScene() { sgGetScene()->Clear(); m_drills[0] = CreateDrill(1.0,5.0); //SetMaterialToObject(m_drills[0],MATERIAL_METALL_GOLD_4); m_drills[1] = m_drills[0]->Clone(); //SetMaterialToObject(m_drills[1],MATERIAL_METALL_GOLD_4); m_drills[2] = m_drills[0]->Clone(); //SetMaterialToObject(m_drills[2],MATERIAL_METALL_GOLD_4); m_drills[3] = m_drills[0]->Clone(); //SetMaterialToObject(m_drills[3],MATERIAL_METALL_GOLD_4); sgCCylinder* holes[5]; for (int i=0;i<4;i++) holes[i] = sgCCylinder::Create(1.0, 20.0, 24); holes[4] = sgCCylinder::Create(2.5, 20.0, 24); SG_VECTOR transVec = {15.0, 15.0, 0.0}; m_drills[0]->InitTempMatrix()->Translate(transVec); m_drills[0]->ApplyTempMatrix(); m_drills[0]->DestroyTempMatrix(); transVec.z = -10.0; holes[0]->InitTempMatrix()->Translate(transVec); holes[0]->ApplyTempMatrix(); holes[0]->DestroyTempMatrix(); sgGetScene()->AttachObject(m_drills[0]); m_drills[0]->SetAttribute(SG_OA_COLOR,2); m_drills[0]->SetAttribute(SG_OA_LINE_THICKNESS, 1); transVec.x = -15.0; transVec.y = 15.0; transVec.z = 0.0; m_drills[1]->InitTempMatrix()->Translate(transVec); m_drills[1]->ApplyTempMatrix(); m_drills[1]->DestroyTempMatrix(); transVec.z = -10.0; holes[1]->InitTempMatrix()->Translate(transVec); holes[1]->ApplyTempMatrix(); holes[1]->DestroyTempMatrix(); sgGetScene()->AttachObject(m_drills[1]); m_drills[1]->SetAttribute(SG_OA_COLOR,3); m_drills[1]->SetAttribute(SG_OA_LINE_THICKNESS, 1); transVec.x = -15.0; transVec.y = -15.0; transVec.z = 0.0; m_drills[2]->InitTempMatrix()->Translate(transVec); m_drills[2]->ApplyTempMatrix(); m_drills[2]->DestroyTempMatrix(); transVec.z = -10.0; holes[2]->InitTempMatrix()->Translate(transVec); holes[2]->ApplyTempMatrix(); holes[2]->DestroyTempMatrix(); sgGetScene()->AttachObject(m_drills[2]); m_drills[2]->SetAttribute(SG_OA_COLOR,4); m_drills[2]->SetAttribute(SG_OA_LINE_THICKNESS, 1); transVec.x = 15.0; transVec.y = -15.0; transVec.z = 0.0; m_drills[3]->InitTempMatrix()->Translate(transVec); m_drills[3]->ApplyTempMatrix(); m_drills[3]->DestroyTempMatrix(); transVec.z = -10.0; holes[3]->InitTempMatrix()->Translate(transVec); holes[3]->ApplyTempMatrix(); holes[3]->DestroyTempMatrix(); sgGetScene()->AttachObject(m_drills[3]); m_drills[3]->SetAttribute(SG_OA_COLOR,5); m_drills[3]->SetAttribute(SG_OA_LINE_THICKNESS, 1); m_drills[4] = CreateDrill(2.5,8.0); sgGetScene()->AttachObject(m_drills[4]); m_drills[4]->SetAttribute(SG_OA_COLOR,10); m_drills[4]->SetAttribute(SG_OA_LINE_THICKNESS, 1); //SetMaterialToObject(m_drills[4],MATERIAL_METALL_GOLD_4); transVec.x = 0.0; transVec.y = 0.0; transVec.z = -10.0; holes[4]->InitTempMatrix()->Translate(transVec); holes[4]->ApplyTempMatrix(); holes[4]->DestroyTempMatrix(); sgCObject* tmpOb1; sgCGroup* tmpGroup; tmpOb1 = sgCreateBox(40.0,40.0, 1.0); transVec.x = -20.0; transVec.y = -20.0; transVec.z = -4.0; tmpOb1->InitTempMatrix()->Translate(transVec); tmpOb1->ApplyTempMatrix(); tmpOb1->DestroyTempMatrix(); for (int i=0;i<5;i++) { tmpGroup = sgBoolean::Sub((const sgC3DObject&)(*tmpOb1), (const sgC3DObject&)(*holes[i])); sgDeleteObject(tmpOb1); tmpGroup->BreakGroup(&tmpOb1); sgDeleteObject(tmpGroup); sgDeleteObject(holes[i]); } m_box = tmpOb1; //SetMaterialToObject(m_box,MATERIAL_WOOD_7); sgGetScene()->AttachObject(m_box); m_box->SetAttribute(SG_OA_COLOR,10); m_box->SetAttribute(SG_OA_LINE_THICKNESS, 1); }