void display(void) { Real32 time = glutGet(GLUT_ELAPSED_TIME); updateMesh(time); // we extract the core out of the root node // as we now this is a geometry node GeometryPtr geo = GeometryPtr::dcast(scene->getCore()); //now modify it's content // first we need a pointer to the position data field GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions()); //this loop is similar to when we generted the data during createScenegraph() beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); // here they all come for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) pos->setValue(Pnt3f(x, wMesh[x][z], z), N*x+z); endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); mgr->redraw(); }
void updateRayGeo(void) { Line &ray = testRays[uiCurrentRay]; IntersectResult &res = resultsP[uiCurrentRay]; Pnt3f startPnt = ray.getPosition(); Pnt3f endPnt = startPnt + (rayLength * ray.getDirection()); beginEditCP(pPoints); pPoints->setValue(startPnt, 0); pPoints->setValue(endPnt, 1); if(res._hit == true) { TriangleIterator triIt(res._pObj); Matrix matrix; Pnt3f point; triIt.seek(res._tri); res._pObj->getToWorld(matrix); point = triIt.getPosition(0); matrix.mult(point, point); pPoints->setValue(point, 2); point = triIt.getPosition(1); matrix.mult(point, point); pPoints->setValue(point, 3); point = triIt.getPosition(2); matrix.mult(point, point); pPoints->setValue(point, 4); } else { pPoints->setValue(Pnt3f(0.0, 0.0, 0.0), 2); pPoints->setValue(Pnt3f(0.0, 0.0, 0.0), 3); pPoints->setValue(Pnt3f(0.0, 0.0, 0.0), 4); } endEditCP (pPoints); }
void key( unsigned char key, int , int ) { switch ( key ) { case 27: exit(0); } // Intersect IntersectAction * act = IntersectAction::create(); static Pnt3f pnts[] = { Pnt3f( 0,0,1 ), Pnt3f( 1,0,1), Pnt3f( 2,0,1), Pnt3f( 3,0,1), Pnt3f( 0,0,1 ), Pnt3f( 0,0,1 ), Pnt3f( 0,0,1 ),Pnt3f( 0,0,1 ), Pnt3f( 0.9,0.9,1 ), Pnt3f(-Inf,-Inf,-Inf) }; static Vec3f dirs[] = { Vec3f( 0,0,-1), Vec3f( 0,0,-1), Vec3f( 0,0,-1), Vec3f( 0,0,-1), Vec3f( 0,-.2,-1), Vec3f( 0,.2,-1), Vec3f( -.2,-.2,-1), Vec3f( .2,.2,-1), Vec3f( 0,0,-1 ), Vec3f(-Inf,-Inf,-Inf) }; static int i = 0; act->setLine( Line( pnts[i], dirs[i]) ); act->apply( iroot ); std::cerr << "Line " << act->getLine().getPosition() << " dir " << act->getLine().getDirection() << " hit: " << act->didHit() << " "; beginEditCP(points); points->setValue( pnts[i], 0 ); points->setValue( pnts[i] + (dirs[i] * Real32(3.0)), 1 ); if ( act->didHit() ) { std::cerr << " object " << act->getHitObject() << " tri " << act->getHitTriangle() << " at " << act->getHitPoint(); TriangleIterator it( act->getHitObject() ); it.seek( act->getHitTriangle() ); Matrix m; act->getHitObject()->getToWorld(m); Pnt3f p = it.getPosition(0); m.mult(p, p); points->setValue( p, 2 ); p = it.getPosition(1); m.mult(p, p); points->setValue( p, 3 ); p = it.getPosition(2); m.mult(p, p); points->setValue( p, 4 ); } else { points->setValue( Pnt3f(0,0,0), 2 ); points->setValue( Pnt3f(0,0,0), 3 ); points->setValue( Pnt3f(0,0,0), 4 ); } endEditCP(points); std::cerr << std::endl; glutPostRedisplay(); if ( pnts[++i][0] == -Inf ) i = 0; }
// react to keys void keyboard(unsigned char k, int x, int y) { switch(k) { case 27: { OSG::osgExit(); exit(0); } break; case ' ': // send a ray through the clicked pixel /* Intersection testing for rays is done using an IntersectAction. The ray itself is calculated by the SimpleSceneManager, given the clicked pixel. It needs to be set up with the line that is to be intersected. A line is a semi-infinite ray which has a starting point and a direction, and extends in the direction to infinity. To do the actual test the Action's apply() method is used. The results can be received from the Action. The main difference is if something was hit or not, which is returned in didHit(). If an intersection did occur, the other data elements are valid, otherwise they are undefined. The information that is stored in the action is the object which was hit, the triangle of the object that was hit (in the form of its index) and the actual hit position. */ { Line l; l = mgr->calcViewRay(x, y); std::cerr << "From " << l.getPosition () << ", dir " << l.getDirection() << std::endl; IntersectAction *act = IntersectAction::create(); act->setLine(l); act->apply(fileroot); beginEditCP(isectPoints); isectPoints->setValue(l.getPosition(), 0); isectPoints->setValue(l.getPosition() + l.getDirection(), 1); // did we hit something? if (act->didHit()) { // yes!! print and highlight it std::cerr << " object " << act->getHitObject () << " tri " << act->getHitTriangle() << " at " << act->getHitPoint (); mgr->setHighlight(act->getHitObject()); // stop the ray on the hit surface Pnt3f is = l.getPosition() + l.getDirection() * act->getHitT(); isectPoints->setValue(is, 1); // find the triangle that was hit TriangleIterator it(act->getHitObject()); it.seek(act->getHitTriangle()); // Draw its normal at the intersection point isectPoints->setValue(is, 2); isectPoints->setValue(is + act->getHitNormal() * 5, 3); // calculate its vertex positions in world space Matrix m; act->getHitObject()->getToWorld(m); // and turn them into a triangle Pnt3f p = it.getPosition(0); m.mult(p, p); isectPoints->setValue(p, 4); p = it.getPosition(1); m.mult(p, p); isectPoints->setValue(p, 5); p = it.getPosition(2); m.mult(p, p); isectPoints->setValue(p, 6); } else { // no, get rid of the triangle and highlight. isectPoints->setValue(Pnt3f(0,0,0), 2); isectPoints->setValue(Pnt3f(0,0,0), 3); isectPoints->setValue(Pnt3f(0,0,0), 4); mgr->setHighlight(NullFC); } endEditCP(isectPoints); // free the action delete act; // the geometry won't notice automatically, tell it that the // points changed beginEditCP(testgeocore, Geometry::PositionsFieldMask); endEditCP (testgeocore, Geometry::PositionsFieldMask); std::cerr << std::endl; glutPostRedisplay(); } break; } }