Debian Codegraph @

apps/examples/Mentor/CXX/04.1.Cameras.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*--------------------------------------------------------------
38 * This is an example from the Inventor Mentor,
39 * chapter 4, example 1.
40 *
41 * Camera example.
42 * A blinker node is used to switch between three
43 * different views of the same scene. The cameras are
44 * switched once per second.
45 *------------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <Inventor/SbLinear.h>
49
50 #include <Inventor/SoDB.h>
51 #include <Inventor/SoInput.h>
52 #include <Inventor/Xt/SoXt.h>
53 #include <Inventor/Xt/SoXtRenderArea.h>
54 #include <Inventor/nodes/SoBlinker.h>
55 #include <Inventor/nodes/SoDirectionalLight.h>
56 #include <Inventor/nodes/SoMaterial.h>
57 #include <Inventor/nodes/SoOrthographicCamera.h>
58 #include <Inventor/nodes/SoPerspectiveCamera.h>
59 #include <Inventor/nodes/SoSeparator.h>
60 #include <Inventor/nodes/SoTransform.h>
61
62 int
63 main(int, char **argv)
64 {
65 // Initialize Inventor and Xt
66 Widget myWindow = SoXt::init(argv[0]);
67 if (myWindow == NULL) exit(1);
68
69 SoSeparator *root = new SoSeparator;
70 root->ref();
71
72 // Create a blinker node and put it in the scene. A blinker
73 // switches between its children at timed intervals.
74 SoBlinker *myBlinker = new SoBlinker;
75 root->addChild(myBlinker);
76
77 // Create three cameras. Their positions will be set later.
78 // This is because the viewAll method depends on the size
79 // of the render area, which has not been created yet.
80 SoOrthographicCamera *orthoViewAll =new SoOrthographicCamera;
81 SoPerspectiveCamera *perspViewAll = new SoPerspectiveCamera;
82 SoPerspectiveCamera *perspOffCenter =new SoPerspectiveCamera;
83 myBlinker->addChild(orthoViewAll);
84 myBlinker->addChild(perspViewAll);
85 myBlinker->addChild(perspOffCenter);
86
87 // Create a light
88 root->addChild(new SoDirectionalLight);
89
90 // Read the object from a file and add to the scene
91 SoInput myInput;
92 if (!myInput.openFile("/usr/share/src/Inventor/examples/data/parkbench.iv"))
93 exit (1);
94 SoSeparator *fileContents = SoDB::readAll(&myInput);
95 if (fileContents == NULL)
96 exit (1);
97
98 SoMaterial *myMaterial = new SoMaterial;
99 myMaterial->diffuseColor.setValue(0.8, 0.23, 0.03);
100 root->addChild(myMaterial);
101 root->addChild(fileContents);
102
103 SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow);
104
105 // Establish camera positions.
106 // First do a viewAll on all three cameras.
107 // Then modify the position of the off-center camera.
108 SbViewportRegion myRegion(myRenderArea->getSize());
109 orthoViewAll->viewAll(root, myRegion);
110 perspViewAll->viewAll(root, myRegion);
111 perspOffCenter->viewAll(root, myRegion);
112 SbVec3f initialPos;
113 initialPos = perspOffCenter->position.getValue();
114 float x, y, z;
115 initialPos.getValue(x,y,z);
116 perspOffCenter->position.setValue(x+x/2., y+y/2., z+z/4.);
117
118 myRenderArea->setSceneGraph(root);
119 myRenderArea->setTitle("Cameras");
120 myRenderArea->show();
121
122 SoXt::show(myWindow);
123 SoXt::mainLoop();
124 }