OSG_USING_NAMESPACE int main (int argc, char *argv[]) { osgInit(argc, argv); FieldContainerFactory::the()->setThrowInvalidPointerException(true); #ifdef OSG_INVALID_PTR_CHECK try { NodePtr planeNode1 = makePlane(2,2, 1,1); NodePtr planeNode2 = planeNode1; addRefCP(planeNode1); subRefCP(planeNode1); if(planeNode1 == NullFC) { printf("planeNode1 == NullFC\n"); } // now call subRefCP on a NullFC pointer! subRefCP(planeNode1); // now call subRefCP on a invalid pointer! subRefCP(planeNode2); NodePtr planeNode3 = makePlane(2,2, 1,1); NodePtr planeNode4 = planeNode3; addRefCP(planeNode3); subRefCP(planeNode3); NodeCorePtr core1 = planeNode4->getCore(); NodeCorePtr core2 = planeNode3->getCore(); } catch(InvalidPointerException &e) { printf("error: '%s'\n", e.what()); } #endif return 0; }
//====================================================================== // STEP 3: Replace initGround(), initCube(), and initSphere() functions // with the following defintion. This ensures VertexPNTBX and // SimpleIndexedGeometryPNTBX are used, which provides extra // vertex attributes used for Bump Mapping later //======================================================================= static void initGround() { int ibLen, vbLen; getPlaneVbIbLen(vbLen, ibLen); // Temporary storage for cube Geometry vector<VertexPNTBX> vtx(vbLen); vector<unsigned short> idx(ibLen); makePlane(g_groundSize*2, vtx.begin(), idx.begin()); g_ground.reset(new SimpleIndexedGeometryPNTBX(&vtx[0], &idx[0], vbLen, ibLen)); }
artemis::Entity& EntityFactory::makePlaneEntity(){ artemis::Entity& planeEntity = em->create(); vector<vertex> planeVerts; vector<GLuint> planeVertIndex; makePlane(5,5, planeVerts, planeVertIndex); calcFaceNormals(planeVerts, planeVertIndex); planeEntity.addComponent(geoManager.create(planeVerts, planeVertIndex)); return planeEntity; }
// // Constructor // //InvPlaneMover::InvPlaneMover(InvViewer *vv): InvPlaneMover::InvPlaneMover() : show_(0) , distOffset_(0, 0, 0) , feedbackInfo_(NULL) , planeNormal_(0, 1, 0) , iNorm_(0, 1, 0) , motionMode_(InvPlaneMover::FREE) { handle_ = new SoSeparator; handle_->ref(); // our move-handle is a SoJackDragger with appropriate decoration // i.e. a square with a vector in its center handleSwitch_ = new SoSwitch(); handleSwitch_->whichChild.setValue(SO_SWITCH_NONE); handle_->addChild(handleSwitch_); jDrag_ = new SoJackDragger; jDrag_->addFinishCallback(InvPlaneMover::dragFinishCB, this); handleDrawStyle_ = new SoDrawStyle; handleDrawStyle_->style.setValue(SoDrawStyle::FILLED); transl_ = new SoTranslation; handleSwitch_->addChild(transl_); fullRot_ = new SoRotation; handleSwitch_->addChild(fullRot_); scale_ = new SoScale; handleSwitch_->addChild(scale_); int ii; SoSeparator *plane[6]; for (ii = 0; ii < 6; ii++) { plane[ii] = new SoSeparator; plane[ii]->addChild(handleDrawStyle_); plane[ii]->addChild(makePlane()); } SoSeparator *empty[6]; for (ii = 0; ii < 6; ii++) { empty[ii] = new SoSeparator; empty[ii]->addChild(handleDrawStyle_); } SoSeparator *scale[2]; for (ii = 0; ii < 2; ii++) { scale[ii] = new SoSeparator; scale[ii]->addChild(handleDrawStyle_); //scale[ii]->addChild(makeArrow()); } SoSeparator *arrow[2]; for (ii = 0; ii < 2; ii++) { arrow[ii] = new SoSeparator; arrow[ii]->addChild(handleDrawStyle_); arrow[ii]->addChild(makeArrow()); } transl_ = new SoTranslation; handleSwitch_->addChild(transl_); fullRot_ = new SoRotation; handleSwitch_->addChild(fullRot_); scale_ = new SoScale; handleSwitch_->addChild(scale_); handleSwitch_->addChild(jDrag_); jDrag_->setPart("rotator.rotator", arrow[0]); jDrag_->setPart("rotator.rotatorActive", arrow[1]); jDrag_->setPart("translator.yTranslator.translator", plane[0]); jDrag_->setPart("translator.xTranslator.translator", plane[2]); jDrag_->setPart("translator.zTranslator.translator", plane[4]); jDrag_->setPart("translator.yTranslator.translatorActive", plane[1]); jDrag_->setPart("translator.xTranslator.translatorActive", plane[3]); jDrag_->setPart("translator.zTranslator.translatorActive", plane[5]); jDrag_->setPart("translator.yzTranslator.translatorActive", empty[0]); jDrag_->setPart("translator.xzTranslator.translatorActive", empty[1]); jDrag_->setPart("translator.xyTranslator.translatorActive", empty[2]); jDrag_->setPart("translator.yzTranslator.translator", empty[3]); jDrag_->setPart("translator.xzTranslator.translator", empty[4]); jDrag_->setPart("translator.xyTranslator.translator", empty[5]); }
int main (int argc, char **argv) { osgInit(argc,argv); // GLUT init glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); int id=glutCreateWindow("OpenSG"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); GLUTWindowPtr gwin=GLUTWindow::create(); gwin->setId(id); gwin->init(); // create the scene NodePtr scene = Node::create(); beginEditCP(scene); scene->setCore(Group::create()); endEditCP(scene); // create the SimpleSceneManager helper _mgr = new SimpleSceneManager; // tell the manager what to manage _mgr->setWindow(gwin ); _mgr->setRoot (scene); // create the geometry. NodePtr plane = makePlane( 1, 1, 2, 2 ); NodePtr torus = makeTorus( .2, 1, 16, 8 ); GeometryPtr plane_geo, torus_geo; plane_geo = GeometryPtr::dcast(plane->getCore()); torus_geo = GeometryPtr::dcast(torus->getCore()); PolygonChunkPtr pchunk = PolygonChunk::create(); beginEditCP(pchunk); pchunk->setFrontMode(GL_LINE); pchunk->setBackMode(GL_LINE); pchunk->setOffsetFactor(-1.0); pchunk->setOffsetLine(true); endEditCP(pchunk); // create materials for the plane. SimpleMaterialPtr pm1 = SimpleMaterial::create(); beginEditCP(pm1); pm1->setDiffuse( Color3f( 0,1,0 ) ); pm1->setAmbient( Color3f( 0,1,0 ) ); pm1->setSpecular( Color3f( 0,0,0 ) ); endEditCP(pm1); SimpleMaterialPtr pm2 = SimpleMaterial::create(); beginEditCP(pm2); pm2->setDiffuse( Color3f( 1,0,0 ) ); pm2->setAmbient( Color3f( 1,0,0 ) ); pm2->setSpecular( Color3f( 0,0,0 ) ); pm2->addChunk(pchunk); endEditCP(pm2); MultiPassMaterialPtr mppm = MultiPassMaterial::create(); beginEditCP(mppm); mppm->addMaterial(pm1); mppm->addMaterial(pm2); endEditCP(mppm); plane_geo->setMaterial(mppm); // create materials for the torus. SimpleMaterialPtr tm1 = SimpleMaterial::create(); beginEditCP(tm1); tm1->setDiffuse( Color3f( 0,0,1 ) ); tm1->setAmbient( Color3f( 0,0,1 ) ); tm1->setTransparency(0.6); endEditCP(tm1); SimpleMaterialPtr tm2 = SimpleMaterial::create(); beginEditCP(tm2); tm2->setDiffuse( Color3f( 1,0,0 ) ); tm2->setAmbient( Color3f( 1,0,0 ) ); tm2->setSpecular( Color3f( 0,0,0 ) ); tm2->addChunk(pchunk); endEditCP(tm2); MultiPassMaterialPtr mptm = MultiPassMaterial::create(); beginEditCP(mptm); mptm->addMaterial(tm1); mptm->addMaterial(tm2); endEditCP(mptm); torus_geo->setMaterial( mptm ); beginEditCP(scene); scene->addChild(plane); scene->addChild(torus); endEditCP(scene); // show the whole scene _mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect, const AATriangle& triangle, const Vector& addl_ground_movement) { if(!intersects(rect, (const Rectf&) triangle)) return false; Vector normal; float c = 0.0; Vector p1; Rectf area; switch(triangle.dir & AATriangle::DEFORM_MASK) { case 0: area.p1 = triangle.bbox.p1; area.p2 = triangle.bbox.p2; break; case AATriangle::DEFORM_BOTTOM: area.p1 = Vector(triangle.bbox.p1.x, triangle.bbox.p1.y + triangle.bbox.get_height()/2); area.p2 = triangle.bbox.p2; break; case AATriangle::DEFORM_TOP: area.p1 = triangle.bbox.p1; area.p2 = Vector(triangle.bbox.p2.x, triangle.bbox.p1.y + triangle.bbox.get_height()/2); break; case AATriangle::DEFORM_LEFT: area.p1 = triangle.bbox.p1; area.p2 = Vector(triangle.bbox.p1.x + triangle.bbox.get_width()/2, triangle.bbox.p2.y); break; case AATriangle::DEFORM_RIGHT: area.p1 = Vector(triangle.bbox.p1.x + triangle.bbox.get_width()/2, triangle.bbox.p1.y); area.p2 = triangle.bbox.p2; break; default: assert(false); } switch(triangle.dir & AATriangle::DIRECTION_MASK) { case AATriangle::SOUTHWEST: p1 = Vector(rect.p1.x, rect.p2.y); makePlane(area.p1, area.p2, normal, c); break; case AATriangle::NORTHEAST: p1 = Vector(rect.p2.x, rect.p1.y); makePlane(area.p2, area.p1, normal, c); break; case AATriangle::SOUTHEAST: p1 = rect.p2; makePlane(Vector(area.p1.x, area.p2.y), Vector(area.p2.x, area.p1.y), normal, c); break; case AATriangle::NORTHWEST: p1 = rect.p1; makePlane(Vector(area.p2.x, area.p1.y), Vector(area.p1.x, area.p2.y), normal, c); break; default: assert(false); } float n_p1 = -(normal * p1); float depth = n_p1 - c; if(depth < 0) return false; #if 0 std::cout << "R: " << rect << " Tri: " << triangle << "\n"; std::cout << "Norm: " << normal << " Depth: " << depth << "\n"; #endif Vector outvec = normal * (depth + 0.2f); const float RDELTA = 3; if(p1.x < area.p1.x - RDELTA || p1.x > area.p2.x + RDELTA || p1.y < area.p1.y - RDELTA || p1.y > area.p2.y + RDELTA) { set_rectangle_rectangle_constraints(constraints, rect, area); } else { if(outvec.x < 0) { constraints->constrain_right(rect.get_right() + outvec.x, addl_ground_movement.x); constraints->hit.right = true; } else { constraints->constrain_left(rect.get_left() + outvec.x, addl_ground_movement.x); constraints->hit.left = true; } if(outvec.y < 0) { constraints->constrain_bottom(rect.get_bottom() + outvec.y, addl_ground_movement.y); constraints->hit.bottom = true; constraints->ground_movement += addl_ground_movement; } else { constraints->constrain_top(rect.get_top() + outvec.y, addl_ground_movement.y); constraints->hit.top = true; } constraints->hit.slope_normal = normal; } return true; }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); // Create the SimpleSceneManager helper SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); //Attach to events TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager)); TutorialWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager)); TutorialWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager)); TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager)); //Particle System Material TextureObjChunkRefPtr QuadTextureChunk = TextureObjChunk::create(); ImageRefPtr LoadedImage = ImageFileHandler::the()->read("Data/Cloud.png"); QuadTextureChunk->setImage(LoadedImage); TextureEnvChunkRefPtr QuadTextureEnvChunk = TextureEnvChunk::create(); QuadTextureEnvChunk->setEnvMode(GL_MODULATE); BlendChunkRefPtr PSBlendChunk = BlendChunk::create(); PSBlendChunk->setSrcFactor(GL_SRC_ALPHA); PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkRefPtr PSMaterialChunk = MaterialChunk::create(); PSMaterialChunk->setAmbient(Color4f(0.3f,0.3f,0.3f,1.0f)); PSMaterialChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f)); PSMaterialChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f)); PSMaterialChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE); ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create(); PSMaterial->addChunk(QuadTextureChunk); PSMaterial->addChunk(QuadTextureEnvChunk); PSMaterial->addChunk(PSMaterialChunk); PSMaterial->addChunk(PSBlendChunk); //Particle System ParticleSystemRecPtr ExampleParticleSystem = ParticleSystem::create(); ExampleParticleSystem->attachUpdateProducer(TutorialWindow); //Particle System Drawer QuadParticleSystemDrawerRefPtr ExampleParticleSystemDrawer = QuadParticleSystemDrawer::create(); BurstParticleGeneratorRecPtr ExampleBurstGenerator = BurstParticleGenerator::create(); //Attach the function objects to the Generator ExampleBurstGenerator->setPositionDistribution(createPositionDistribution()); ExampleBurstGenerator->setLifespanDistribution(createLifespanDistribution()); ExampleBurstGenerator->setBurstAmount(50.0); ExampleBurstGenerator->setVelocityDistribution(createVelocityDistribution()); ExampleBurstGenerator->setAccelerationDistribution(createAccelerationDistribution()); ExampleBurstGenerator->setSizeDistribution(createSizeDistribution()); //Particle System Node ParticleSystemCoreRefPtr ParticleNodeCore = ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = Node::create(); ParticleNode->setCore(ParticleNodeCore); //Ground Node NodeRefPtr GoundNode = makePlane(30.0,30.0,10,10); Matrix GroundTransformation; GroundTransformation.setRotate(Quaternion(Vec3f(1.0f,0.0,0.0), -3.14195f)); TransformRefPtr GroundTransformCore = Transform::create(); GroundTransformCore->setMatrix(GroundTransformation); NodeRefPtr GroundTransformNode = Node::create(); GroundTransformNode->setCore(GroundTransformCore); GroundTransformNode->addChild(GoundNode); // Make Main Scene Node and add the Torus NodeRefPtr scene = Node::create(); scene->setCore(Group::create()); scene->addChild(ParticleNode); scene->addChild(GroundTransformNode); TutorialWindow->connectKeyTyped(boost::bind(keyTyped, _1, &sceneManager, ExampleParticleSystem.get(), ExampleBurstGenerator.get(), ExampleParticleSystemDrawer.get())); sceneManager.setRoot(scene); //Create the Documentation SimpleScreenDoc TheSimpleScreenDoc(&sceneManager, TutorialWindow); // Show the whole Scene sceneManager.showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "05QuadParticleDrawer"); //Enter main Loop TutorialWindow->mainLoop(); } osgExit(); return 0; }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); gwin = GLUTWindow::create(); // create root rootNode = makeCoredNode<Group>(); NodePtr scene = makeCoredNode<Group>(); // create lights TransformPtr point1_trans; NodePtr point1 = makeCoredNode<PointLight>(&_point1_core); NodePtr point1_beacon = makeCoredNode<Transform>(&point1_trans); beginEditCP(point1_trans); point1_trans->editMatrix().setTranslate(0.0, 0.0, 25.0); endEditCP(point1_trans); beginEditCP(_point1_core); _point1_core->setAmbient(0.15,0.15,0.15,1); _point1_core->setDiffuse(0.4,0.4,0.4,1); _point1_core->setSpecular(0.0,0.0,0.0,1); _point1_core->setBeacon(point1_beacon); _point1_core->setOn(true); endEditCP(_point1_core); TransformPtr point2_trans; NodePtr point2 = makeCoredNode<PointLight>(&_point2_core); NodePtr point2_beacon = makeCoredNode<Transform>(&point2_trans); beginEditCP(point2_trans); point2_trans->editMatrix().setTranslate(5.0, 5.0, 20.0); endEditCP(point2_trans); beginEditCP(_point2_core); _point2_core->setAmbient(0.15,0.15,0.15,1); _point2_core->setDiffuse(0.4,0.4,0.4,1); _point2_core->setSpecular(0.0,0.0,0.0,1); _point2_core->setBeacon(point2_beacon); _point2_core->setOn(true); endEditCP(_point2_core); beginEditCP(point1); point1->addChild(point2); endEditCP(point1); beginEditCP(point2); point2->addChild(scene); endEditCP(point2); // create scene // bottom NodePtr plane = makePlane(25.0, 25.0, 128, 128); int size = imageWinWidth*imageWinHeight*256; ImagePtr plane_img = Image::create(); beginEditCP(plane_img); plane_img->set(Image::OSG_RGBA_PF, imageWinWidth, imageWinHeight, 1, 1, 1, 0, NULL); endEditCP(plane_img); TextureChunkPtr plane_tex = TextureChunk::create(); beginEditCP(plane_tex); plane_tex->setImage(plane_img); plane_tex->setMinFilter(GL_LINEAR); plane_tex->setMagFilter(GL_LINEAR); plane_tex->setTarget(GL_TEXTURE_2D); plane_tex->setInternalFormat(GL_RGBA16F_ARB); endEditCP(plane_tex); SHLChunkPtr shl = SHLChunk::create(); beginEditCP(shl); shl->setVertexProgram(_vp_program); shl->setFragmentProgram(_fp_program); shl->setUniformParameter("tex0", 0); endEditCP(shl); SimpleMaterialPtr plane_mat = SimpleMaterial::create(); beginEditCP(plane_mat); plane_mat->setAmbient(Color3f(0.3,0.3,0.3)); plane_mat->setDiffuse(Color3f(1.0,1.0,1.0)); plane_mat->addChunk(plane_tex); plane_mat->addChunk(shl); endEditCP(plane_mat); GeometryPtr plane_geo = GeometryPtr::dcast(plane->getCore()); beginEditCP(plane_geo); plane_geo->setMaterial(plane_mat); beginEditCP(plane_geo); // box box_trans_node = makeCoredNode<Transform>(&_box_trans); beginEditCP(_box_trans); _box_trans->editMatrix().setTranslate(0.0, 0.0, 12.0); endEditCP(_box_trans); NodePtr box = makeBox(4.0, 4.0, 0.8, 10, 10 , 10); beginEditCP(box_trans_node); box_trans_node->addChild(box); endEditCP(box_trans_node); PolygonChunkPtr pchunk = osg::PolygonChunk::create(); beginEditCP(pchunk); pchunk->setCullFace(GL_BACK); endEditCP(pchunk); SimpleMaterialPtr box_mat = SimpleMaterial::create(); beginEditCP(box_mat); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,0.0,1.0)); box_mat->addChunk(pchunk); endEditCP(box_mat); GeometryPtr box_geo = GeometryPtr::dcast(box->getCore()); beginEditCP(box_geo); box_geo->setMaterial(box_mat); beginEditCP(box_geo); // cylinder1 NodePtr cylinder1_trans_node = makeCoredNode<Transform>(&_cylinder1_trans); beginEditCP(_cylinder1_trans); _cylinder1_trans->editMatrix().setTranslate(0.0, 0.0, 5.0); endEditCP(_cylinder1_trans); NodePtr cylinder1 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true); beginEditCP(cylinder1_trans_node); cylinder1_trans_node->addChild(cylinder1); endEditCP(cylinder1_trans_node); SimpleMaterialPtr cylinder1_mat = SimpleMaterial::create(); beginEditCP(cylinder1_mat); cylinder1_mat->setAmbient(Color3f(0.0,0.0,0.0)); cylinder1_mat->setDiffuse(Color3f(1.0,0.0,0.0)); cylinder1_mat->addChunk(pchunk); endEditCP(cylinder1_mat); GeometryPtr cylinder1_geo = GeometryPtr::dcast(cylinder1->getCore()); beginEditCP(cylinder1_geo); cylinder1_geo->setMaterial(cylinder1_mat); beginEditCP(cylinder1_geo); // cylinder2 NodePtr cylinder2_trans_node = makeCoredNode<Transform>(&_cylinder2_trans); beginEditCP(_cylinder2_trans); _cylinder2_trans->editMatrix().setTranslate(0.0, 0.0, 8.0); endEditCP(_cylinder2_trans); NodePtr cylinder2 = OSG::makeCylinder(10.0, 0.4, 32, true, true ,true); beginEditCP(cylinder2_trans_node); cylinder2_trans_node->addChild(cylinder2); endEditCP(cylinder2_trans_node); SimpleMaterialPtr cylinder2_mat = SimpleMaterial::create(); beginEditCP(cylinder2_mat); cylinder2_mat->setAmbient(Color3f(0.0,0.0,0.0)); cylinder2_mat->setDiffuse(Color3f(0.0,1.0,0.0)); cylinder2_mat->addChunk(pchunk); endEditCP(cylinder2_mat); GeometryPtr cylinder2_geo = GeometryPtr::dcast(cylinder2->getCore()); beginEditCP(cylinder2_geo); cylinder2_geo->setMaterial(cylinder2_mat); beginEditCP(cylinder2_geo); // scene beginEditCP(scene); scene->addChild(plane); scene->addChild(box_trans_node); scene->addChild(cylinder1_trans_node); scene->addChild(cylinder2_trans_node); endEditCP(scene); vp = ShadowViewport::create(); GradientBackgroundPtr gbg = GradientBackground::create(); SolidBackgroundPtr sbg = SolidBackground::create(); UChar8 imgdata[] = { 255,0,0, 0,255,0, 0,0,255, 255,255,0 }; ImagePtr img1 = Image::create(); img1->set(Image::OSG_RGB_PF, 2, 2, 1, 1, 1, 0, imgdata); TextureChunkPtr tcPtr = TextureChunk::create(); beginEditCP(tcPtr); tcPtr->setImage(img1); endEditCP(tcPtr); TextureBackgroundPtr bkg = TextureBackground::create(); beginEditCP(bkg); bkg->setTexture(tcPtr); endEditCP(bkg); beginEditCP(sbg); sbg->setColor(Color3f(0.2,0.4,0.6)); endEditCP(sbg); beginEditCP(gbg); gbg->addLine(Color3f(0.7, 0.7, 0.8), 0); gbg->addLine(Color3f(0.1, 0.1, 0.3), 1); endEditCP(gbg); beginEditCP(rootNode); rootNode->addChild(point1); rootNode->addChild(point1_beacon); rootNode->addChild(point2_beacon); endEditCP(rootNode); //FBOViewportPtr fbo_vp = FBOViewport::create(); addRefCP(fbo_vp); // the Camera for the map cam = PerspectiveCamera::create(); beginEditCP(cam); cam->setFov(osgdegree2rad(90)); cam->setAspect(1); cam->setNear(0.001); cam->setFar(10000); cam->setBeacon(box_trans_node); endEditCP(cam); beginEditCP(fbo_vp); fbo_vp->setBackground(bkg); fbo_vp->setRoot(rootNode); fbo_vp->setCamera(cam); fbo_vp->setSize(0,0,imageWinWidth-1, imageWinHeight-1); fbo_vp->setStorageWidth(imageWinWidth); fbo_vp->setStorageHeight(imageWinHeight); fbo_vp->setDirty(true); fbo_vp->editMFTextures ()->push_back(plane_tex); fbo_vp->editMFExcludeNodes()->push_back(plane); fbo_vp->setFboOn(true); endEditCP(fbo_vp); // normal shadow viewport beginEditCP(vp); vp->setBackground(gbg); vp->setRoot(rootNode); vp->setSize(0,0,1,1); endEditCP(vp); beginEditCP(gwin); //Window gwin->setId(winid); gwin->addPort(vp); gwin->init(); endEditCP(gwin); Vec3f min,max; rootNode->updateVolume(); rootNode->getVolume().getBounds( min, max ); // create the SimpleSceneManager helper mgr = new SimpleSceneManager; mgr->setWindow(gwin); mgr->setRoot(rootNode); //Viewport beginEditCP(vp); vp->setCamera(mgr->getCamera()); endEditCP(vp); mgr->turnHeadlightOff(); mgr->showAll(); // GLUT main loop glutMainLoop(); return 0; }
NodePtr createScoreBoards() { GeometryPtr geo; NodePtr bg; SimpleMaterialPtr m; ScoreBoard1 = new TextStuff() ; ScoreBoard2 = new TextStuff() ; // First get the global group node OSG::NodePtr scoreBoardsNodePtr = OSG::Node::create(); scoreBoardsNodePtr->setCore(OSG::Group::create()); // Setup text 1 ScoreBoard1->initialize(); ScoreBoard1->updateFace(); ScoreBoard1->updateScore(0,0); // Setup text 2 ScoreBoard2->initialize(); ScoreBoard2->updateFace(); ScoreBoard2->updateScore(0,0); ////////// 1 ///////// // make its transform TransformPtr trans1; NodePtr trans_node1 = makeCoredNode<Transform>(&trans1); beginEditCP(trans1); trans1->getMatrix().setTransform(Vec3f(0,4,-10.5),Quaternion( Vec3f(0,1,0),deg2rad(0))); endEditCP(trans1); // make geometry bg = makePlane(9.3, 1, 8,2); m= SimpleMaterial::create(); beginEditCP(m); { m->setAmbient (Color3f(0,0,0)); m->setDiffuse (Color3f(0.0,0.0,0.0)); } endEditCP (m); geo = GeometryPtr::dcast(bg->getCore()); beginEditCP(geo); geo->setMaterial(m); beginEditCP(geo); beginEditCP(bg); bg->addChild(ScoreBoard1->mRootNode); endEditCP(bg); beginEditCP(trans_node1); trans_node1->addChild(bg); endEditCP(trans_node1); ////////// 2 ///////// // make its transform TransformPtr trans2; NodePtr trans_node2 = makeCoredNode<Transform>(&trans2); beginEditCP(trans2); trans2->getMatrix().setTransform(Vec3f(0,4,10.5),Quaternion( Vec3f(0,1,0),deg2rad(180))); endEditCP(trans2); // make geometry bg = makePlane(9.3, 1, 8,2); m = SimpleMaterial::create(); beginEditCP(m); { m->setAmbient (Color3f(0,0,0)); m->setDiffuse (Color3f(0.0,0.0,0.0)); } endEditCP (m); geo = GeometryPtr::dcast(bg->getCore()); beginEditCP(geo); geo->setMaterial(m); beginEditCP(geo); beginEditCP(bg); bg->addChild(ScoreBoard2->mRootNode); endEditCP(bg); beginEditCP(trans_node2); trans_node2->addChild(bg); endEditCP(trans_node2); beginEditCP(scoreBoardsNodePtr); scoreBoardsNodePtr->addChild(trans_node1); scoreBoardsNodePtr->addChild(trans_node2); endEditCP(scoreBoardsNodePtr); return scoreBoardsNodePtr; }
// Call this varying palign and salign. void DepthComplexity3D::processMeshAlign(const PlaneAlign &palign, const PlaneAlign &salign) { assert(palign != salign); BoundingBox aabb = _mesh->aabb; aabb.merge(aabb.min - aabb.extents()/10.0); aabb.merge(aabb.max + aabb.extents()/10.0); vec3d c0 = vec3d(aabb.min.x, aabb.min.y, aabb.min.z); vec3d c1 = vec3d(aabb.max.x, aabb.min.y, aabb.min.z); vec3d c2 = vec3d(aabb.min.x, aabb.max.y, aabb.min.z); vec3d c3 = vec3d(aabb.max.x, aabb.max.y, aabb.min.z); vec3d c4 = vec3d(aabb.min.x, aabb.min.y, aabb.max.z); vec3d c5 = vec3d(aabb.max.x, aabb.min.y, aabb.max.z); vec3d c6 = vec3d(aabb.min.x, aabb.max.y, aabb.max.z); vec3d c7 = vec3d(aabb.max.x, aabb.max.y, aabb.max.z); // generate all planes varying on z const unsigned steps = _discretSteps; for (unsigned az = 0; az<steps; ++az) { // double t = 3*az / (steps - 1.0) - 1; // [-1, 2] double t = az / (steps - 1.0); for (unsigned bz = 0; bz<steps; ++bz) { // double u = 3*bz / (steps - 1.0) - 1; // [-1, 2] double u = bz / (steps - 1.0); Segment sa, sb; switch (palign) { case AlignZ: { Point a = mix(c0, c4, t); // along Z Point b = mix(c7, c3, u); // along Z sa.a = sa.b = a; sb.a = sb.b = b; if (salign == AlignX) { // extend along X sa.b.x = aabb.max.x; sb.a.x = aabb.min.x; } else { // extend along Y sa.b.y = aabb.max.y; sb.a.y = aabb.min.y; } break; } case AlignY: { Point a = mix(c0, c2, t); // along Y Point b = mix(c7, c5, u); // along Y sa.a = sa.b = a; sb.a = sb.b = b; if (salign == AlignX) { // extend along X sa.b.x = aabb.max.x; sb.a.x = aabb.min.x; } else { // extend along Z sa.b.z = aabb.max.z; sb.a.z = aabb.min.z; } break; } case AlignX: { Point a = mix(c0, c1, t); Point b = mix(c7, c6, u); sa.a = sa.b = a; sb.a = sb.b = b; if (salign == AlignY) { // extend along Y sa.b.y = aabb.max.y; sb.a.y = aabb.min.y; } else { // extend along Z sa.b.z = aabb.max.z; sb.a.z = aabb.min.z; } break; } } Segment saa, sbb; saa.a = sa.a; saa.b = sb.b; sbb.a = sa.b; sbb.b = sb.a; _usedPlanes.push_back(sa); _usedPlanes.push_back(saa); _usedPlanes.push_back(sb); _usedPlanes.push_back(sbb); vec4d plane = makePlane(sa.a, sa.b, sb.a); std::vector<Segment> segments; processMeshPlane(plane, &segments); // make the segments extra-long vec3d dsa = sa.b - sa.a; sa.a -= dsa; sa.b += dsa; vec3d dsb = sb.b - sb.a; sb.a -= dsb; sb.b += dsb; _dc2d->process(sa, sb, segments); unsigned tempMaximum = _dc2d->maximum(); if (tempMaximum >= _maximum) { if (tempMaximum > _maximum) { _maximumRays.clear(); _goodRays.resize(tempMaximum+1); _histogram.resize(tempMaximum+1); _intersectionPoints.clear(); // _intersectionSegments.clear(); } _maximum = tempMaximum; std::vector<Segment> tempRays = _dc2d->maximumRays(); // Testing rays and saving intersectin points. for (unsigned r=0; r<tempRays.size(); ++r) { for (unsigned s=0; s<segments.size(); ++s) { double t1, t2; if(segmentIntersection3D(tempRays[r], segments[s], &t1, &t2)) { _intersectionPoints.push_back(tempRays[r].a + t1*(tempRays[r].b-tempRays[r].a)); } } } // _intersectionSegments.insert(_intersectionSegments.end(), segments.begin(), segments.end()); // _intersectionPoints.insert(_intersectionPoints.end(), points.begin(), points.end()); _maximumRays.insert(_maximumRays.end(), tempRays.begin(), tempRays.end()); // Shouldn't the histogram be used without regard to the current tempMaximum? (changed it) } std::vector<unsigned long long> tempHist = _dc2d->histogram(); for(unsigned i=0; i< tempHist.size(); ++i) _histogram[i] += tempHist[i]; if(_computeGoodRays) { //std::cout << "size of goodRays: " << _goodRays.size() << " and _threshold = " << _threshold << std::endl; for(unsigned i = _threshold ; i <= tempMaximum ; ++i) { //std::cout << "i = " << i << " and size(i) = " << _dc2d->goodRays(i).size() << std::endl; std::vector<Segment> tempRays = _dc2d->goodRays(i); _goodRays[i].insert(_goodRays[i].begin(), tempRays.begin(), tempRays.end()); } } } } }
void ClientCourt::setup(void) { l = (courtType == FULL_COURT) ? FULL_COURT_LENGTH : PRACTICE_COURT_LENGTH; w = (courtType == FULL_COURT) ? FULL_COURT_WIDTH : PRACTICE_COURT_WIDTH; h = (courtType == FULL_COURT) ? FULL_COURT_HEIGHT : PRACTICE_COURT_HEIGHT; Ogre::SceneNode* wallNode = parentNode->createChildSceneNode(); if(courtNum == 0) { makePlane("cwallf", Ogre::Vector3::UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/KAMEN", l/2, w, h, wallNode); makePlane("cwallb", Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/KAMEN", l/2, w, h, wallNode); makePlane("cfloor", Ogre::Vector3::UNIT_Y, Ogre::Vector3::UNIT_Z, (courtType == FULL_COURT) ? "Examples/TennisCourt" : "Examples/GrassFloor", h/2, w, l, wallNode); makePlane("cceiling", Ogre::Vector3::NEGATIVE_UNIT_Y, Ogre::Vector3::UNIT_Z, "Examples/Flare", h/2, w, l, wallNode); makePlane("cwalll", Ogre::Vector3::UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/KAMEN", w/2, l, h, wallNode); makePlane("cwallr", Ogre::Vector3::NEGATIVE_UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/KAMEN", w/2, l, h, wallNode); } else if(courtNum == 1) { makePlane("cwallf", Ogre::Vector3::UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/Tron", l/2, w, h, wallNode); makePlane("cwallb", Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/Tron", l/2, w, h, wallNode); makePlane("cfloor", Ogre::Vector3::UNIT_Y, Ogre::Vector3::UNIT_Z, "Examples/Grid", h/2, w, l, wallNode); makePlane("cceiling", Ogre::Vector3::NEGATIVE_UNIT_Y, Ogre::Vector3::UNIT_Z, "Examples/Grid", h/2, w, l, wallNode); makePlane("cwalll", Ogre::Vector3::UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/Tron", w/2, l, h, wallNode); makePlane("cwallr", Ogre::Vector3::NEGATIVE_UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/Tron", w/2, l, h, wallNode); } else { makePlane("cwallf", Ogre::Vector3::UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/CloudySky", l/2, w, h, wallNode); makePlane("cwallb", Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/CloudySky", l/2, w, h, wallNode); makePlane("cfloor", Ogre::Vector3::UNIT_Y, Ogre::Vector3::UNIT_Z, "Examples/CloudySky", h/2, w, l, wallNode); makePlane("cceiling", Ogre::Vector3::NEGATIVE_UNIT_Y, Ogre::Vector3::UNIT_Z, "Examples/CloudySky", h/2, w, l, wallNode); makePlane("cwalll", Ogre::Vector3::UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/CloudySky", w/2, l, h, wallNode); makePlane("cwallr", Ogre::Vector3::NEGATIVE_UNIT_X, Ogre::Vector3::UNIT_Y, "Examples/CloudySky", w/2, l, h, wallNode); } if (courtType == FULL_COURT) { Ogre::SceneNode* net = parentNode->createChildSceneNode(Ogre::Vector3(0,-h/2+20, 0)); makePlane("cnet1", Ogre::Vector3::UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/TennisNet", 0, w, 40, net); makePlane("cnet2", -Ogre::Vector3::UNIT_Z, Ogre::Vector3::UNIT_Y, "Examples/TennisNet", 0, w, 40, net); } }
int main( int argc, char **argv ) { ChangeList::setReadWriteDefault(); osgInit(argc,argv); _main_thread = Thread::getCurrent(); _sync_barrier = Barrier::get("RenderBarrier"); QApplication::setColorSpec( QApplication::CustomColor ); QApplication a( argc, argv ); if(!QGLFormat::hasOpenGL()) { qWarning( "This system has no OpenGL support. Exiting." ); return -1; } _render_widget = new OpenSGWidget(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba | QGL::DirectRendering)); NodePtr scene = makePlane(1.0, 1.0, 50, 50); MaterialChunkPtr matc = MaterialChunk::create(); beginEditCP(matc); matc->setAmbient(Color4f(0.3, 0.3, 0.3, 1.0)); matc->setDiffuse(Color4f(0.2, 0.2, 0.8, 1.0)); matc->setSpecular(Color4f(0.6, 0.6, 0.6, 1.0)); matc->setShininess(100); endEditCP(matc); ChunkMaterialPtr cmat = ChunkMaterial::create(); beginEditCP(cmat); cmat->addChunk(matc); endEditCP(cmat); _geo = GeometryPtr::dcast(scene->getCore()); beginEditCP(_geo); _geo->setDlistCache(false); _geo->setMaterial(cmat); endEditCP(_geo); initWave(); resetWave(); _render_widget->getManager()->setRoot(scene); _render_widget->getManager()->showAll(); _render_widget->getManager()->getNavigator()->setFrom(Pnt3f(1.0f, -1.0f, 1.0f)); _render_widget->getManager()->getNavigator()->setUp(Vec3f(0.0f, 0.0f, 1.0f)); _render_widget->show(); while(!_render_widget->isInitialized()) qApp->processEvents(); // The gl widget is initialized in the main thread! // Without the doneCurrent() the next makeCurrent() call in the render thread // doesn't work because qt thinks that the context is already current but this // was in the main thread ... _render_widget->doneCurrent(); // start render thread _render_thread = dynamic_cast<Thread *>(ThreadManager::the()->getThread("RenderThread")); _render_thread->runFunction(renderThread, 1, NULL); // main loop while(!_quit) { mainThread(); // sync _sync_barrier->enter(2); if(_do_quit) _quit = true; _sync_barrier->enter(2); _main_thread->getChangeList()->clearAll(); qApp->processEvents(); } Thread::join(_render_thread); return 0; }
void Model::makePlane(const glm::vec3 &a, const glm::vec3 &b, const glm::vec3 &c) { makePlane(a, b, c, glm::vec4(0, 0, 1, 1)); }
int main(int argc, char **argv) { // OSG init osgInit(argc,argv); // Set up Window TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); TutorialWindow->setDisplayCallback(display); TutorialWindow->setReshapeCallback(reshape); TutorialKeyListener TheKeyListener; TutorialWindow->addKeyListener(&TheKeyListener); TutorialMouseListener TheTutorialMouseListener; TutorialMouseMotionListener TheTutorialMouseMotionListener; TutorialWindow->addMouseListener(&TheTutorialMouseListener); TutorialWindow->addMouseMotionListener(&TheTutorialMouseMotionListener); // Create the SimpleSceneManager helper mgr = new SimpleSceneManager; // Tell the Manager what to manage mgr->setWindow(TutorialWindow); //Particle System Material TextureObjChunkRefPtr QuadTextureObjChunk = TextureObjChunk::create(); ImageRefPtr LoadedImage = ImageFileHandler::the()->read("Data/Cloud.png"); QuadTextureObjChunk->setImage(LoadedImage); TextureEnvChunkRefPtr QuadTextureEnvChunk = TextureEnvChunk::create(); QuadTextureEnvChunk->setEnvMode(GL_MODULATE); BlendChunkRefPtr PSBlendChunk = BlendChunk::create(); PSBlendChunk->setSrcFactor(GL_SRC_ALPHA); PSBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA); MaterialChunkRefPtr PSMaterialChunk = MaterialChunk::create(); PSMaterialChunk->setAmbient(Color4f(0.3f,0.3f,0.3f,1.0f)); PSMaterialChunk->setDiffuse(Color4f(0.7f,0.7f,0.7f,1.0f)); PSMaterialChunk->setSpecular(Color4f(0.9f,0.9f,0.9f,1.0f)); PSMaterialChunk->setColorMaterial(GL_AMBIENT_AND_DIFFUSE); ChunkMaterialRefPtr PSMaterial = ChunkMaterial::create(); PSMaterial->addChunk(QuadTextureObjChunk); PSMaterial->addChunk(QuadTextureEnvChunk); PSMaterial->addChunk(PSMaterialChunk); PSMaterial->addChunk(PSBlendChunk); //Affector ExampleAgeSizeParticleAffector = OSG::AgeSizeParticleAffector::create(); //ages ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.0); ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.05); ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.2); ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.36); ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.7); ExampleAgeSizeParticleAffector->editMFAges()->push_back(0.8); ExampleAgeSizeParticleAffector->editMFAges()->push_back(1.0); //sizes ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(1.0,0.5,1.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(1.0,0.5,1.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(20.0,0.5,30.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(3.0,3.0,3.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(6.0,60.0,6.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(2.0,3.0,1.0)); ExampleAgeSizeParticleAffector->editMFSizes()->push_back(Vec3f(10.0,1.0,10.0)); //Particle System ExampleParticleSystem = OSG::ParticleSystem::create(); ExampleParticleSystem->attachUpdateListener(TutorialWindow); ExampleParticleSystem->pushToAffectors(ExampleAgeSizeParticleAffector); //Particle System Drawer ExampleParticleSystemDrawer = OSG::QuadParticleSystemDrawer::create(); ExampleBurstGenerator = OSG::BurstParticleGenerator::create(); //Attach the function objects to the Generator ExampleBurstGenerator->setPositionDistribution(createPositionDistribution()); ExampleBurstGenerator->setLifespanDistribution(createLifespanDistribution()); ExampleBurstGenerator->setBurstAmount(10.0); ExampleBurstGenerator->setVelocityDistribution(createVelocityDistribution()); //ExampleBurstGenerator->setAccelerationDistribution(createAccelerationDistribution()); ExampleBurstGenerator->setSizeDistribution(createSizeDistribution()); //Particle System Node ParticleSystemCoreRefPtr ParticleNodeCore = OSG::ParticleSystemCore::create(); ParticleNodeCore->setSystem(ExampleParticleSystem); ParticleNodeCore->setDrawer(ExampleParticleSystemDrawer); ParticleNodeCore->setMaterial(PSMaterial); NodeRefPtr ParticleNode = OSG::Node::create(); ParticleNode->setCore(ParticleNodeCore); //Ground Node NodeRefPtr GoundNode = makePlane(30.0,30.0,10,10); Matrix GroundTransformation; GroundTransformation.setRotate(Quaternion(Vec3f(1.0f,0.0,0.0), -3.14195f)); TransformRefPtr GroundTransformCore = Transform::create(); GroundTransformCore->setMatrix(GroundTransformation); NodeRefPtr GroundTransformNode = Node::create(); GroundTransformNode->setCore(GroundTransformCore); GroundTransformNode->addChild(GoundNode); // Make Main Scene Node and add the Torus NodeRefPtr scene = OSG::Node::create(); scene->setCore(OSG::Group::create()); scene->addChild(ParticleNode); scene->addChild(GroundTransformNode); mgr->setRoot(scene); // Show the whole Scene mgr->showAll(); //Open Window Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "07AgeSizeParticleAffector"); //Enter main Loop TutorialWindow->mainLoop(); osgExit(); return 0; }
void onMouseDrag( GLFWwindow* window, double fx, double fy) { int x=fx; int y=fy; #else void onMouseDrag( int x, int y) { #endif if (isDrag) { printf( "in drag mode %d\n", isDrag); if (isDrag==V_DRAG && cowFlag) { // vertical dragging // TODO: // create a dragging plane perpendicular to the ray direction, // and test intersection with the screen ray. // When dragged just change Y position Ray ray; screenCoordToRay(clickX, y, ray); PickInfo &pp=pickInfo; std::pair<bool, double> c=ray.intersects(dragPlane); vector3 currentPos=ray.getPoint(c.second); matrix4 T; T.setTranslation(currentPos - pp.cowPickPosition, false); cow2wld.mult(T, pp.cowPickConfiguration); } else { // horizontal dragging // Hint: read carefully the following block to implement vertical dragging. if(cursorOnCowBoundingBox) { Ray ray; screenCoordToRay(x, y, ray); PickInfo &pp=pickInfo; Plane p(vector3(0,1,0), pp.cowPickPosition); std::pair<bool, double> c=ray.intersects(p); vector3 currentPos=ray.getPoint(c.second); matrix4 T; T.setTranslation(currentPos-pp.cowPickPosition, false); cow2wld.mult(T, pp.cowPickConfiguration); } } } else { Ray ray; screenCoordToRay(x, y, ray); std::vector<Plane> planes; vector3 bbmin(cow->bbmin.x, cow->bbmin.y, cow->bbmin.z); vector3 bbmax(cow->bbmax.x, cow->bbmax.y, cow->bbmax.z); planes.push_back(makePlane(bbmin, bbmax, vector3(0,1,0))); planes.push_back(makePlane(bbmin, bbmax, vector3(0,-1,0))); planes.push_back(makePlane(bbmin, bbmax, vector3(1,0,0))); planes.push_back(makePlane(bbmin, bbmax, vector3(-1,0,0))); planes.push_back(makePlane(bbmin, bbmax, vector3(0,0,1))); planes.push_back(makePlane(bbmin, bbmax, vector3(0,0,-1))); std::pair<bool,double> o=ray.intersects(planes); cursorOnCowBoundingBox=o.first; PickInfo &pp=pickInfo; pp.cursorRayT=o.second; pp.cowPickPosition=ray.getPoint(pp.cursorRayT); pp.cowPickConfiguration=cow2wld; matrix4 invWorld; invWorld.inverse(cow2wld); // the local position (relative to the cow frame) of the pick position. pp.cowPickPositionLocal=invWorld*pp.cowPickPosition; } } /********************************************************************************* * Call this part whenever user types keyboard. **********************************************************************************/ #if GLFW_VERSION_MAJOR==3 void onKeyPress(GLFWwindow *__win, int key, int __scancode, int action, int __mods) #else void onKeyPress( int key, int action) #endif { if (action==GLFW_RELEASE) return ; // do nothing // If 'c' or space bar are pressed, alter the camera. // If a number is pressed, alter the camera corresponding the number. if ((key == ' ') || (key == 'c')) { printf( "Toggle camera %d\n", cameraIndex ); cameraIndex += 1; } else if ((key >= '0') && (key <= '9')) cameraIndex = key - '0'; if (cameraIndex >= (int)wld2cam.size() ) cameraIndex = 0; } void screenCoordToRay(int x, int y, Ray& ray) { int height , width; #if GLFW_VERSION_MAJOR==3 glfwGetWindowSize(g_window, &width, &height); #else glfwGetWindowSize(&width, &height); #endif matrix4 matProjection; matProjection.getCurrentOpenGLmatrix(GL_PROJECTION_MATRIX); matProjection*=wld2cam[cameraIndex]; matrix4 invMatProjection; invMatProjection.inverse(matProjection); vector3 vecAfterProjection, vecAfterProjection2; // -1<=v.x<1 when 0<=x<width // -1<=v.y<1 when 0<=y<height vecAfterProjection.x = ((double)(x - 0)/(double)width)*2.0-1.0; vecAfterProjection.y = ((double)(y - 0)/(double)height)*2.0-1.0; vecAfterProjection.y*=-1; vecAfterProjection.z = -10; //std::cout<<"cowPosition in clip coordinate (NDC)"<<matProjection*cow2wld.getTranslation()<<std::endl; vector3 vecBeforeProjection=invMatProjection*vecAfterProjection; // camera position ray.origin()=cam2wld[cameraIndex].getTranslation(); ray.direction()=vecBeforeProjection-ray.origin(); ray.direction().normalize(); //std::cout<<"dir" <<ray.direction()<<std::endl; }
// Setter void setPoints(Plane *plane, vector<Point> points) { *plane = makePlane(points); }
// calculate height at (x,y) with plane float Terrain::getHeight (float x_float, float z_float, Angle &a) { Vector v1, v2, v3, normal, ab, bc; Plane plane; bool up; int res_int = (int) res; int x = (int) (x_float); int z = (int) (z_float); int xs, zs; xs = (int) ((x+size/2) / res_int) * res_int - (size/2); zs = (int) ((z+size/2) / res_int) * res_int - (size/2); // FIND PLANE EQUATION (Ax + By + Cz + D) // find triangle - START // mi servono i vertici della diagonale -> 0 e 2 v1.x = xs; v1.z = zs; v1.y = getHeightFromMap (v1.x+size/2, v1.z+size/2); v3.x = xs + res; v3.z = zs + res; v3.y = getHeightFromMap (v3.x+size/2, v3.z+size/2); // calcolo la retta passante per i punti float rect_z = v3.z * ((xs-v1.x)/(v3.x-v1.x)); if (z_float > rect_z) up = true; else up = false; // find triangle - END if (up) { v2.x = xs; v2.z = zs + res; v2.y = getHeightFromMap (v2.x+size/2, v2.z+size/2); } else { v2.x = xs + res; v2.z = zs; v2.y = getHeightFromMap (v2.x+size/2, v2.z+size/2); } // calculate plane equation vectorSubtract (v1, v2, ab); vectorSubtract (v2, v3, bc); crossProduct (ab, bc, normal); makePlane (v1, normal, plane); // calculate height float height = (plane.d - (plane.a * x_float) - (plane.c * z_float)) / plane.b; // calculate angle x Vector temp_vec; temp_vec.x = 1.0; temp_vec.y = 0.0; temp_vec.z = 0.0; float temp_angle_x = vectorAngle (temp_vec, normal); a.x = 90.0 - temp_angle_x; // calculate angle z temp_vec.x = 0.0; temp_vec.y = 0.0; temp_vec.z = 1.0; float temp_angle_z = vectorAngle (temp_vec, normal); a.z = 90.0 - temp_angle_z; // return height return height; }