fixed: gcc8 compile issues
[opensg.git] / Examples / Tutorial / 15stereo_client.cpp
blob3f0a952232cf38437c4f2a07ecd4a62f100bdb7f
1 // all needed include files
2 #ifdef OSG_BUILD_ACTIVE
3 #include <OSGGLUT.h>
4 #include <OSGConfig.h>
5 #include <OSGSimpleGeometry.h>
6 #include <OSGGLUTWindow.h>
7 #include <OSGSolidBackground.h>
8 #include <OSGDirectionalLight.h>
9 #include <OSGPerspectiveCamera.h>
10 #include <OSGTransform.h>
11 #include <OSGRenderAction.h>
12 #include <OSGViewport.h>
13 #include <OSGGradientBackground.h>
14 #include <OSGImage.h>
15 #include <OSGImageForeground.h>
16 #include <OSGFileGrabForeground.h>
17 #include <OSGShearedStereoCameraDecorator.h>
18 #include <OSGMultiDisplayWindow.h>
19 #else
20 #include <OpenSG/OSGGLUT.h>
21 #include <OpenSG/OSGConfig.h>
22 #include <OpenSG/OSGSimpleGeometry.h>
23 #include <OpenSG/OSGGLUTWindow.h>
24 #include <OpenSG/OSGSolidBackground.h>
25 #include <OpenSG/OSGDirectionalLight.h>
26 #include <OpenSG/OSGPerspectiveCamera.h>
27 #include <OpenSG/OSGTransform.h>
28 #include <OpenSG/OSGRenderAction.h>
29 #include <OpenSG/OSGViewport.h>
30 #include <OpenSG/OSGGradientBackground.h>
31 #include <OpenSG/OSGImage.h>
32 #include <OpenSG/OSGImageForeground.h>
33 #include <OpenSG/OSGFileGrabForeground.h>
34 #include <OpenSG/OSGShearedStereoCameraDecorator.h>
35 #include <OpenSG/OSGMultiDisplayWindow.h>
36 #endif
38 OSG::NodeRecPtr scene;
40 // one camera will be used only - the
41 // decorators will distinguish of left and right
42 // eye position
43 OSG::PerspectiveCameraRecPtr camera;
45 // we need two viewports - one for the left
46 // and one for the right eye
47 OSG::ViewportRecPtr leftViewport;
48 OSG::ViewportRecPtr rightViewport;
50 OSG::MultiDisplayWindowRecPtr multiWindow;
52 OSG::NodeRecPtr camBeacon, lightBeacon, lightNode;
54 OSG::RenderActionRefPtr renderAction;
56 int setupGLUT( int *argc, char *argv[] );
58 OSG::NodeTransitPtr createScenegraph(void)
60 //create geometry - just a simple torus
61 OSG::NodeRecPtr torus = OSG::makeTorus(1,5,8,16);
63 //create transformations & beacons for cameras & light
64 camBeacon = OSG::Node::create();
65 lightBeacon = OSG::Node::create();
67 // the following style is a bit different than from before
68 // this is only to remind you that beginEditCP()'s can also
69 // be interleaved
71 //create Transformations
72 OSG::TransformRecPtr camTrans, lightTrans;
74 camTrans = OSG::Transform::create();
75 lightTrans = OSG::Transform::create();
77 OSG::Matrix camM, lightM;
78 camM .setTransform(OSG::Vec3f(0, 1, 10));
79 lightM.setTransform(OSG::Vec3f(1, 10, 2));
81 camTrans ->setMatrix(camM );
82 lightTrans->setMatrix(lightM);
84 camBeacon ->setCore(camTrans );
85 lightBeacon->setCore(lightTrans);
86 // -- end of camera beacon creation
88 //create the light source
89 OSG::DirectionalLightRecPtr dLight = OSG::DirectionalLight::create();
91 dLight->setDirection(OSG::Vec3f(0,1,2));
93 //color information
94 dLight->setDiffuse (OSG::Color4f(1, 1, 1, 1));
95 dLight->setAmbient (OSG::Color4f(0.2, 0.2, 0.2, 1));
96 dLight->setSpecular(OSG::Color4f(1, 1, 1, 1));
98 //set the beacon
99 dLight->setBeacon(lightBeacon);
101 // create the node that will contain the light source
103 lightNode = OSG::Node::create();
104 lightNode->setCore(dLight);
105 lightNode->addChild(torus);
107 // now create the root and add all children
109 OSG::NodeRecPtr root = OSG::Node::create();
110 root->setCore (OSG::GroupRecPtr(OSG::Group::create()));
111 root->addChild(lightNode);
112 root->addChild(camBeacon);
113 root->addChild(lightBeacon);
115 return OSG::NodeTransitPtr(root);
118 int main(int argc, char **argv)
120 OSG::osgInit(argc,argv);
122 int winid = setupGLUT(&argc, argv);
125 //create the main window for navigation
126 OSG::GLUTWindowRecPtr navWindow = OSG::GLUTWindow::create();
127 navWindow->setGlutId(winid);
128 navWindow->setSize(300,300);
129 navWindow->init();
131 scene = createScenegraph();
133 //we beginn with creating our cameras
134 camera = OSG::PerspectiveCamera::create();
136 camera->setBeacon(camBeacon);
137 camera->setFov(OSG::osgDegree2Rad(90));
138 camera->setNear(0.1);
139 camera->setFar(100);
141 //next we create the backgrounds
143 OSG::SolidBackgroundRecPtr bkg = OSG::SolidBackground::create();
144 bkg->setColor(OSG::Color3f(0,0,0));
146 leftViewport = OSG::Viewport::create();
147 rightViewport = OSG::Viewport::create();
149 //the decorator decarates the camera and will create the left eye
150 OSG::ShearedStereoCameraDecoratorRecPtr cameraDecorator =
151 OSG::ShearedStereoCameraDecorator::create();
152 cameraDecorator->setLeftEye(true);
153 //unit length assume that one unit equals one meter
154 cameraDecorator->setEyeSeparation(0.06);
155 cameraDecorator->setDecoratee(camera);
156 cameraDecorator->setZeroParallaxDistance(2);
158 leftViewport->setCamera (cameraDecorator);
159 leftViewport->setBackground(bkg);
160 leftViewport->setRoot (scene);
161 leftViewport->setSize (0,0,.5,1);
163 //thr right decorator for the right eye
164 cameraDecorator = OSG::ShearedStereoCameraDecorator::create();
166 cameraDecorator->setLeftEye(false);
167 cameraDecorator->setEyeSeparation(0.06);
168 cameraDecorator->setDecoratee(camera);
169 cameraDecorator->setZeroParallaxDistance(2);
171 rightViewport->setCamera (cameraDecorator);
172 rightViewport->setBackground(bkg);
173 rightViewport->setRoot (scene);
174 rightViewport->setSize (.5,0,1,1);
176 multiWindow = OSG::MultiDisplayWindow::create();
178 multiWindow->setClientWindow(navWindow);
179 multiWindow->setConnectionType("Multicast");
180 multiWindow->editMFServers()->push_back("Server1");
181 multiWindow->editMFServers()->push_back("Server2");
182 multiWindow->setHServers(2);
183 multiWindow->setVServers(1);
184 multiWindow->addPort(leftViewport);
185 multiWindow->addPort(rightViewport);
186 multiWindow->init();
188 // add an logo foreground to the right viwport
190 //and the render action - more on that later
191 renderAction = OSG::RenderAction::create();
193 std::cout << "create scenegraph..." << std::endl;
196 glutMainLoop();
198 return 0;
201 void reshape(int w, int h)
203 multiWindow->resize(w, h);
204 glutPostRedisplay();
207 void display(void)
209 multiWindow->render(renderAction);
211 //the changelist should be cleared - else things
212 //could be copied multiple times
213 OSG::Thread::getCurrentChangeList()->clear();
215 // to ensure a black navigation window
216 glClear(GL_COLOR_BUFFER_BIT);
217 glutSwapBuffers();
220 void mouse(int button, int state, int x, int y)
222 glutPostRedisplay();
225 void motion(int x, int y)
227 glutPostRedisplay();
230 void keyboard(unsigned char k, int x, int y)
232 switch(k)
234 case 27:
236 // clean up global variables
237 scene = NULL;
238 camera = NULL;
239 rightViewport = NULL;
240 leftViewport = NULL;
241 multiWindow = NULL;
242 camBeacon = NULL;
243 lightBeacon = NULL;
244 lightNode = NULL;
245 renderAction = NULL;
247 OSG::osgExit();
248 exit(1);
250 break;
254 int setupGLUT(int *argc, char *argv[])
256 glutInit(argc, argv);
257 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
259 int winid = glutCreateWindow("OpenSG Navigation Window");
261 glutDisplayFunc(display);
262 glutMouseFunc(mouse);
263 glutMotionFunc(motion);
264 glutReshapeFunc(reshape);
265 glutKeyboardFunc(keyboard);
266 glutIdleFunc(display);
268 return winid;