/* Copyright (C) 1997, 1998, 1999 by Alex Pfaffe (Digital Dawn Graphics Inc) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* This sample application will create a scene with a grid and a rotating box which is lit by a light. As the box rotates, the lighting will affect the color of the box. Last update Jul 12, 2000 */ // --------------------------------------------------------------------- // // DDG definitions. // #include "util/ddgclock.h" #include "render/ddgviewport.h" #include "render/ddgobjects.h" #include "render/ddgmaterial.h" #include "render/ddgtransform.h" #include "render/ddglight.h" #include "render/ddgbox.h" #include "render/ddgtext.h" #include "render/ddgtexture.h" #include "render/ddgkcntl.h" // --------------------------------------------------------------------- // // Global Objects used in this app. // ddgViewportSet *vps = 0; ddgViewport *rootvp = 0; ddgCamera *camera = 0; ddgClock *clock = 0; ddgScene *scene = 0; ddgGrid *grid = 0; ddgTransform *boxTrans = 0; ddgText *text = 0; ddgLightModel *lightmodel = 0; ddgLight *light = 0; ddgMaterial *material = 0; ddgKeyControl *control = 0; // --------------------------------------------------------------------- // // Clean up Global Objects used in this app. // bool shutdown (void) { delete scene; delete camera; delete clock; delete lightmodel; delete text; delete material; return ddgSuccess; } // --------------------------------------------------------------------- // // Methods called each frame by each camera to update state. // void update(void) { // Get the simulation time in hours from the clock. // Default 1sec of realtime = 10mins of simulation time. float h; clock->getSimTime(&h); // Rotate the box a bit ( a full turn in 1 hour). boxTrans->rotate(new ddgVector3(0,h * 180,h * 360)); // Display some statistics. float fps = clock->getFrameRate(); ddgVector3 *pos = control->position(); ddgVector3 *rot = control->orientation(); text->printf("%5.2fFPS Pos: %3.0f %3.0f %3.0f Rot: %3.0f %3.0f %3.0f", fps, pos->v[0],pos->v[1],pos->v[2],rot->v[0],rot->v[1],rot->v[2],fps); } // --------------------------------------------------------------------- // // Create and Initialize Global Objects and enter the event loop. // int main(int argc, char **argv) { // Set up the rendering viewports. vps = new ddgViewportSet("Digital Dawn Graphics Toolkit Tutorial"); // Create a camera // associated with a context that draws everything in medium quality. // at location 50,10,50 looking in direction back to the origin. control = new ddgKeyControl(new ddgVector3(50,10,50),new ddgVector3(0,77,0)); clock = new ddgClock(); camera = new ddgCamera( new ddgContext(ddgContext::ALL, ddgContext::MEDIUM, control, clock ) ); // Create a scene that the camera can watch. camera->scene(scene = new ddgScene()); // Add a lightmodel to the scene. scene->add(lightmodel = new ddgLightModel()); // Add lights. lightmodel->add(new ddgTranslate(new ddgVector3(100,0,100),light = new ddgLight())); // Position the light. light->pos(27,15,58); // Create a representation for the light so we can see where it is in the scene. scene->add(new ddgLightObj(light)); // Add a 100x100 grid to the scene. scene->add(new ddgGrid()); // Add a reddish material as the 1st child of the lightmodel, all subsequent // children will use this material until another material is added. lightmodel->add(material = new ddgMaterial(200,100,100)); // Set the ambient color of the light. material->ambient(50,50,50); // We want the box to be affected by the light and material so add it as // a child of the lightmodel. lightmodel->add(boxTrans = new ddgTranslate(new ddgVector3(25,3,26),new ddgBox())); // Make the default box a bit bigger and non cubic. boxTrans->scale(new ddgVector3(3,3,2)); // Create a viewport of size 640x480 at position 10,10 on the desktop. rootvp = new ddgViewport(camera,10,10,640,480); // Define the update function which is called each frame. rootvp->update(update); // Each viewport has a uigroup to which you can add objects which are // flat rendered ontop of the scene we will add a text object. rootvp->uigroup()->add(text = new ddgText(1,92)); // Add the viewport to the viewport set. vps->add(rootvp); // Initialize the viewport and its camera and its scene etc. vps->init( argc, argv ); // Define the shutdown function. vps->shutdown(shutdown); // Enter the event loop. vps->enable(); // Exit. return 0; }