// DemoSceneGraph.cpp // NKU CSC 480/580 - Kirby // -------------------------------------------- // A demo of our first attempt at a scene graph. // -------------------------------------------- #include "SceneGraph.h" #include // The root of the scene graph. Node* G_scene= NULL ; void viewProjection() // Set up a viewpoint (external to scene graph, in this version). { glMatrixMode( GL_PROJECTION ) ; glOrtho( -7, 7, -7, 7, -7, 7 ) ; glMatrixMode( GL_MODELVIEW ) ; glLoadIdentity() ; } void display() // Display the scene graph. { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ; G_scene->render() ; glutSwapBuffers() ; } Node* buildScene() // Build the scene graph. { // One primitive artifact: a sphere. Node* sphere= new GlutSphere() ; // Place two solid red spheres on the scene. TransformationNode* west= new TransformationNode() ; west->set( Vector3(1,0,0), 0, 1, Vector3( -2,0,0 ) ) ; west->addChild( sphere ) ; TransformationNode* east= new TransformationNode() ; east->set( Vector3(1,0,0), 0, 1, Vector3( 2,0,0 ) ) ; east->addChild( sphere ) ; MaterialNode* redMaterial= new MaterialNode() ; redMaterial->setColor( 1,0,0 ) ; redMaterial->addChild( west ) ; redMaterial->addChild( east ) ; // Place a solid blue sphere on the scene. TransformationNode* above= new TransformationNode() ; above->set( Vector3(1,0,0), 0, 2, Vector3( 0,4,0 ) ) ; above->addChild( sphere ) ; MaterialNode* blueMaterial= new MaterialNode() ; blueMaterial->setColor( 0,0,1 ) ; blueMaterial->addChild( above ) ; // Light these red and blue spheres with a white light at (1,1,1). LightNode* light= new LightNode() ; light->setPosition( Point3( 1,1,1 ) ) ; light->setDiffuseColor( 1,1,1 ) ; light->lightsOn( true ) ; light->addChild( blueMaterial ) ; light->addChild( redMaterial ) ; // Put a green sphere in the center and use a different light to light it. TransformationNode* center= new TransformationNode() ; center->set( Vector3(1,0,0), 0, 0.5, Vector3(0,0,0) ) ; center->addChild( sphere ) ; MaterialNode* greenMaterial= new MaterialNode() ; greenMaterial->setColor( 0,1,0 ) ; greenMaterial->addChild( center ) ; LightNode* light2= new LightNode() ; light2->setPosition( Point3( -1,0,1 ) ) ; light2->setDiffuseColor( 1,1,1 ) ; light2->lightsOn( true ) ; light2->addChild( greenMaterial ) ; // The scene graph consists of both lit sub-scenes. Node* scene= new Node() ; scene->addChild( light ) ; scene->addChild( light2 ) ; return scene ; } int main( int argc, char** argv ) { // Initialize OpenGL. glutInit( &argc, argv ) ; glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH ) ; glutInitWindowSize( 600, 600 ) ; glutInitWindowPosition( 100, 100 ) ; glutCreateWindow( "Scene Graph - Demo" ) ; glutDisplayFunc( display ) ; // Initialize lighting mechanisms. glEnable( GL_DEPTH_TEST ) ; glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ) ; glEnable( GL_NORMALIZE ) ; glEnable( GL_LIGHTING ) ; // Build scene and viewer. G_scene= buildScene() ; viewProjection() ; // Go. glutMainLoop() ; return 0 ; }