fixed: gcc8 compile issues
[opensg.git] / Examples / Tutorial / 11withoutSSM_complete.cpp
blob5aefe7ace6c27eb75283eca3f4c179cf6466a15d
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 <OSGTextureBackground.h>
15 #include <OSGTextureObjChunk.h>
16 #include <OSGImage.h>
17 #include <OSGImageForeground.h>
18 #include <OSGFileGrabForeground.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/OSGTextureBackground.h>
32 #include <OpenSG/OSGTextureObjChunk.h>
33 #include <OpenSG/OSGImage.h>
34 #include <OpenSG/OSGImageForeground.h>
35 #include <OpenSG/OSGFileGrabForeground.h>
36 #endif
38 OSG::NodeRecPtr scene;
40 OSG::PerspectiveCameraRecPtr leftCamera;
41 OSG::PerspectiveCameraRecPtr rightCamera;
43 OSG::ViewportRecPtr leftViewport;
44 OSG::ViewportRecPtr rightViewport;
46 OSG::WindowRecPtr window;
48 OSG::NodeRecPtr leftCamBeacon, rightCamBeacon, lightBeacon, lightNode;
50 OSG::RenderActionRefPtr renderAction;
52 int setupGLUT(int *argc, char *argv[]);
54 OSG::NodeTransitPtr createScenegraph(void)
56 //create geometry - just a simple torus
57 OSG::NodeRecPtr torus = OSG::makeTorus(1,5,8,16);
59 //create transformations & beacons for cameras & light
60 leftCamBeacon = OSG::Node::create();
61 rightCamBeacon = OSG::Node::create();
62 lightBeacon = OSG::Node::create();
64 // the following style is a bit different than from before
65 // this is only to remind you that beginEditCP()'s can also
66 // be interleaved
69 //create Transformations
70 OSG::TransformRecPtr leftCamTrans, rightCamTrans, lightTrans;
72 leftCamTrans = OSG::Transform::create();
73 rightCamTrans = OSG::Transform::create();
74 lightTrans = OSG::Transform::create();
76 OSG::Matrix leftM, rightM, lightM;
77 leftM .setTransform(OSG::Vec3f(-5, 6, 10));
78 rightM.setTransform(OSG::Vec3f( 5, -6, 10));
79 lightM.setTransform(OSG::Vec3f( 1, 10, 2));
81 leftCamTrans ->setMatrix(leftM );
82 rightCamTrans->setMatrix(rightM);
83 lightTrans ->setMatrix(lightM);
85 leftCamBeacon ->setCore(leftCamTrans );
86 rightCamBeacon->setCore(rightCamTrans);
87 lightBeacon ->setCore(lightTrans );
88 // -- end of camera beacon creation
90 //create the light source
91 OSG::DirectionalLightRecPtr dLight = OSG::DirectionalLight::create();
93 dLight->setDirection(OSG::Vec3f(0,1,2));
95 //color information
96 dLight->setDiffuse(OSG::Color4f(1,1,1,1));
97 dLight->setAmbient(OSG::Color4f(0.2,0.2,0.2,1));
98 dLight->setSpecular(OSG::Color4f(1,1,1,1));
100 //set the beacon
101 dLight->setBeacon(lightBeacon);
103 // create the node that will contain the light source
105 lightNode = OSG::Node::create();
106 lightNode->setCore(dLight);
107 lightNode->addChild(torus);
109 // now create the root and add all children
111 OSG::NodeRecPtr root = OSG::Node::create();
112 root->setCore(OSG::Group::create());
113 root->addChild(lightNode);
114 root->addChild(leftCamBeacon);
115 root->addChild(rightCamBeacon);
116 root->addChild(lightBeacon);
118 return OSG::NodeTransitPtr(root);
121 int main(int argc, char **argv)
123 OSG::osgInit(argc,argv);
126 int winid = setupGLUT(&argc, argv);
128 scene = createScenegraph();
130 //we beginn with creating our cameras
132 leftCamera = OSG::PerspectiveCamera::create();
133 rightCamera = OSG::PerspectiveCamera::create();
135 leftCamera->setBeacon(leftCamBeacon);
136 leftCamera->setFov(OSG::osgDegree2Rad(90));
137 leftCamera->setNear(0.1);
138 leftCamera->setFar(100);
140 rightCamera->setBeacon(rightCamBeacon);
141 rightCamera->setFov(OSG::osgDegree2Rad(90));
142 rightCamera->setNear(0.1);
143 rightCamera->setFar(100);
145 //next we create the backgrounds
147 OSG::GradientBackgroundRecPtr leftBkg =
148 OSG::GradientBackground::create();
150 leftBkg->addLine(OSG::Color3f(0,0,0),0);
151 leftBkg->addLine(OSG::Color3f(1,1,1),1);
153 // load the image file
154 OSG::ImageRecPtr bkgImage = OSG::Image::create();
155 bkgImage->read("Data/front.jpg");
157 // make a texture from the image
158 OSG::TextureObjChunkRecPtr bkgTex = OSG::TextureObjChunk::create();
159 bkgTex->setImage(bkgImage);
161 OSG::TextureBackgroundRecPtr rightBkg =
162 OSG::TextureBackground::create();
164 rightBkg->setTexture(bkgTex);
165 rightBkg->setColor(OSG::Color4f(0.8, 0.8, 0.8, 1.0));
167 //now the viewports
169 leftViewport = OSG::Viewport::create();
170 rightViewport = OSG::Viewport::create();
172 leftViewport->setCamera(leftCamera);
173 leftViewport->setBackground(leftBkg);
174 leftViewport->setRoot(scene);
175 leftViewport->setSize(0,0,0.5,1);
177 rightViewport->setCamera(rightCamera);
178 rightViewport->setBackground(rightBkg);
179 rightViewport->setRoot(scene);
180 rightViewport->setSize(0.5,0,1,1);
182 // -- end of viewport creation
184 // add an logo foreground to the right viwport
186 //load the logo image file
187 OSG::ImageRecPtr frgImage = OSG::Image::create();
188 frgImage->read("Data/logo.png");
190 OSG::ImageForegroundRecPtr imgFrg = OSG::ImageForeground::create();
191 //NOTE: the position values are between 0 and 1
192 //and are relative to the viewport!
193 imgFrg->addImage(frgImage, OSG::Pnt2f(0.1,0));
195 //add the created foreground by appending it to
196 //the vieports foreground multifield
197 rightViewport->editMFForegrounds()->push_back(imgFrg);
199 //create the foreground for screenshot functionality
200 OSG::FileGrabForegroundRecPtr fileGrab =
201 OSG::FileGrabForeground::create();
203 fileGrab->setActive(false);
204 fileGrab->setName("Data/screenshot%04d.jpg");
206 //add this one to the right viewport, too
207 rightViewport->editMFForegrounds()->push_back(fileGrab);
209 //to make a screenshot the foreground has to be set to active
210 //were doing that if the user pressen the 's' key
211 //have a look at the keyboard callback function
213 //and the render action - more on that later
214 renderAction = OSG::RenderAction::create();
216 //create the window now
218 OSG::GLUTWindowRecPtr gwin = OSG::GLUTWindow::create();
219 gwin->setGlutId(winid);
220 gwin->setSize(300,300);
221 window = gwin;
223 window->addPort(leftViewport );
224 window->addPort(rightViewport);
226 window->init();
228 OSG::commitChanges();
231 glutMainLoop();
233 return 0;
236 void reshape(int w, int h)
238 window->resize(w, h);
239 glutPostRedisplay();
242 void display(void)
244 OSG::commitChanges();
246 window->render(renderAction);
249 void mouse(int button, int state, int x, int y)
251 glutPostRedisplay();
254 void motion(int x, int y)
256 //mgr->mouseMove(x, y);
257 glutPostRedisplay();
260 void keyboard(unsigned char k, int x, int y)
262 switch(k)
264 case 27:
266 // clean up global variables
267 scene = NULL;
268 window = NULL;
269 leftCamera = NULL;
270 rightCamera = NULL;
271 leftViewport = NULL;
272 rightViewport = NULL;
273 leftCamBeacon = NULL;
274 rightCamBeacon = NULL;
275 lightBeacon = NULL;
276 lightNode = NULL;
277 renderAction = NULL;
279 OSG::osgExit();
280 exit(1);
282 break;
284 case 's':
286 // The return value is actually a pointer to on osgPtr class
287 // I don't know if that makes much sense at all...
288 const OSG::Viewport::MFForegroundsType *fgField =
289 window->getPort(1)->getMFForegrounds();
291 OSG::FileGrabForegroundRecPtr fg =
292 dynamic_cast<OSG::FileGrabForeground *>((*fgField)[1]);
294 if (!fg->getActive())
296 std::cout << "start recording..." << std::endl;
297 fg->setActive(true);
299 else
301 std::cout << "stopped" << std::endl;
302 fg->setActive(false);
305 break;
309 int setupGLUT(int *argc, char *argv[])
311 glutInit(argc, argv);
312 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
314 int winid = glutCreateWindow("OpenSG First Application");
316 glutDisplayFunc(display);
317 glutMouseFunc(mouse);
318 glutMotionFunc(motion);
319 glutReshapeFunc(reshape);
320 glutKeyboardFunc(keyboard);
321 glutIdleFunc(display);
323 return winid;