Am Freitag, 19. Mai 2006 08:53 schrieb Adrian Egli: > 2006/5/19, Rüdiger Hacke <[EMAIL PROTECTED]>: > > Hi List! > > > > I'm new to osg and currently trying to learn it. I'm using glut and have > > set > > up a simple sceneView containing two objects (a box and a loaded model) > > and a > > hud to display some text. The hud is set up like in the osghud example. > > ok > > > Everything displays fine until i turn the camara far enough to any side, so > > > that the two objects wander off screen (i.e. 90deg to the right). When I > > turn > > the camara back, the objects won't be displayed anymore. When I tuck the > > Window border to issue a resize event, they reapear. The hud is visible > > all > > the time. > > what are you doing in the resize methode? are you resetting the > viewmatrix? No, I set the ProjectionMatrix.
> Upon further inspection, I discovered that as soon as the objects leave the > > > fov, the projection matrix gets screwed up. > > sceneView->getProjectionMatrixAsPerspective(fov, ratio, near, far); > > returns 0.0, 0.0, 0.0, 1.0 > > what are the inputs? and how do you compute fov, ration, .... Those are references which get filled in by the method. I just copied the line to state what the numbers ment and how I got them. > Now if I disable the hud by not adding it to the root node, the above won't > > > happen. > > ok, where do you add the HUD? exactly like the osgHUD example ? Yes, right beneath the root node. You might want to take a look at the attached source. [snip] > (by the way: why you use GLUT and Sceneview? i guess it would be eassier to > start with producer) I want to take one step at a time by means of abstraction levels and features. Besides, I plan on using QT later on for the windowing... Attached is the simplyfied Version of my Sourcecode which illustrates the problem. It has to be linked against libosg, libosgUtil, libosgDB, libosgText & libglut if anyone would care to try it. Thanks, __ Rud
#include <osgUtil/SceneView>
#include <osgDB/ReadFile>
#include <osgText/Text>
#include <osg/Timer>
#include <iostream>
#include <GL/glut.h>
osg::ref_ptr<osgUtil::SceneView> sceneView;
osg::ref_ptr<osg::CameraNode> hud;
osg::ref_ptr<osgText::Text> text;
osg::Timer_t start_tick;
unsigned int frameNum;
int lastx, lasty;
osg::Vec3 viewPos;
float viewRot, viewElev, viewElevStart=osg::PI/2.0;
void display(void)
{
char s[256];
double fov, ratio, near, far;
sceneView->getProjectionMatrixAsPerspective(fov, ratio, near, far);
sprintf(s,"viewRot: %f\nviewElev: %f\nviewPos: %f %f %f\n", viewRot, viewElev, viewPos.x(), viewPos.y(), viewPos.z());
sprintf(s+strlen(s),"fov: %f ratio: %f zNear: %f zFar: %f", fov, ratio, near, far);
text->setText( s );
// set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
frameStamp->setFrameNumber( frameNum++ );
// pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
sceneView->setFrameStamp( frameStamp.get() );
// set the view
osg::Matrix matview;
osg::Matrix mattrans = osg::Matrix::translate( viewPos );
osg::Matrix matrot = osg::Matrix::rotate(
0.0, osg::Vec3(0,1,0), // roll
viewElev, osg::Vec3(1,0,0) , // pitch
viewRot, osg::Vec3(0,0,1) ); // heading
matview = matrot * mattrans;
sceneView->setViewMatrix( osg::Matrix::inverse( matview ) );
sceneView->update();
sceneView->cull();
sceneView->draw();
glutSwapBuffers();
}
void reshape( int w, int h )
{
// update the viewport dimensions, in case the window has been resized.
sceneView->setViewport( 0, 0, w, h );
sceneView->setProjectionMatrixAsPerspective( 60.0, sceneView->getViewport()->aspectRatio(), 1.0, 1000.0);
hud->setProjectionMatrixAsOrtho2D(0,w,0,h);
text->setPosition(osg::Vec3(10, h-10, 0));
}
void mousedown( int /*button*/, int /*state*/, int x, int y )
{
lastx = x;
lasty = y;
}
void mousemove( int x, int y )
{
viewRot -= (x - lastx) / 100.0f;
viewElev -= (y - lasty) / 100.0f;
if( viewElev < viewElevStart-1.5f ) viewElev = viewElevStart-1.5f;
if( viewElev > viewElevStart+1.5f ) viewElev = viewElevStart+1.5f;
lastx = x;
lasty = y;
glutPostRedisplay();
}
void keyboard( unsigned char key, int /*x*/, int /*y*/ )
{
osg::Matrix heading;
heading.makeRotate(viewRot, osg::Vec3(0.0,0.0,1.0));
heading = osg::Matrix::inverse( heading );
switch( key )
{
case 27:
exit(0);
break;
case ' ':
viewRot = 0.0f;
viewElev = viewElevStart;
break;
case 'w': //forward
viewPos += heading*osg::Vec3( 0.0, 1.0, 0.0);
break;
case 's': //backward
viewPos += heading*osg::Vec3( 0.0,-1.0, 0.0);
break;
case 'a': //left
viewPos += heading*osg::Vec3(-1.0, 0.0, 0.0);
break;
case 'd': //right
viewPos += heading*osg::Vec3( 1.0, 0.0, 0.0);
break;
default:
break;
}
glutPostRedisplay();
}
osg::CameraNode* makehud()
{
osg::Geode* geode = new osg::Geode();
// turn lighting off for the text and disable depth test to ensure its always ontop.
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
osgText::Font* font = osgText::readFontFile("fonts/Arial.ttf");
text = new osgText::Text;
text->setFont(font);
text->setColor(osg::Vec4(1.0,1.0,1.0,1.0));
text->setCharacterSize(12.0);
text->setAlignment(osgText::Text::LEFT_TOP);
geode->addDrawable( text.get() );
osg::CameraNode* camera = new osg::CameraNode;
// projection matrix will be set by reshape()
// set the view matrix
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
// only clear the depth buffer
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
// draw subgraph after main camera view.
camera->setRenderOrder(osg::CameraNode::POST_RENDER);
camera->addChild(geode);
return camera;
}
int main( int argc, char **argv )
{
glutInit(&argc, argv);
if (argc<2)
{
std::cout << argv[0] <<": requires filename argument." << std::endl;
return 1;
}
// load the scene.
osg::ref_ptr<osg::Node> loadedModel;
loadedModel = osgDB::readNodeFile(argv[1]);
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild( loadedModel.get() );
// add Hud
hud = makehud();
root->addChild( hud.get() );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( argv[0] );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mousedown );
glutMotionFunc( mousemove );
glutKeyboardFunc( keyboard );
// create the view of the scene.
sceneView = new osgUtil::SceneView;
sceneView->setDefaults();
sceneView->setClearColor( osg::Vec4(0.0,0.0,0.0,1.0) );
sceneView->setSceneData(root.get());
// initialize the view to look at the center of the scene graph
viewRot = 0.0f;
viewElev = viewElevStart;
viewPos = osg::Vec3(0.0, -7.0,2.0);
// record the timer tick at the start of rendering.
start_tick = osg::Timer::instance()->tick();
frameNum = 0;
glutMainLoop();
return 0;
}
pgpJG4oWCchsl.pgp
Description: PGP signature
_______________________________________________ osg-users mailing list [email protected] http://openscenegraph.net/mailman/listinfo/osg-users http://www.openscenegraph.org/
