fixed: compile issue
[opensg.git] / Tools / osgBench / TestWindow.cpp
blob957538203f4c0715212a17985379a1a179373521
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000,2001 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <OpenSG/OSGGLUT.h>
44 #include <OpenSG/OSGMatrixUtility.h>
45 #include <setjmp.h>
47 #include "TestWindow.h"
49 // This is ultra-ugly, but GLUT gives you no alternative
50 static jmp_buf jump;
53 TestWindow::TestWindow(void) :
54 _width(-1), _height(-1), _scene(OSG::NullFC), _window(OSG::NullFC), _ssm(NULL),
55 _open(false), _left(0), _right(1), _top(1), _bottom(0),
56 _near(0.1), _far(100), _fov(1), _beacon(OSG::NullFC),
57 _winid(-1)
61 TestWindow::~TestWindow()
63 if(_winid != -1)
64 glutDestroyWindow(_winid);
65 if(_window != OSG::NullFC)
66 OSG::subRef(_window);
67 if(_ssm != NULL)
68 delete _ssm;
71 void TestWindow::setFullscreen(void)
73 if(!isOpen())
75 _width = -1;
76 return;
79 _width = glutGet(GLUT_SCREEN_WIDTH);
80 _height = glutGet(GLUT_SCREEN_HEIGHT);
82 glutFullScreen();
84 if(!setjmp(jump))
85 glutMainLoop();
87 update();
90 void TestWindow::setSize(OSG::UInt16 width, OSG::UInt16 height)
92 _width = width;
93 _height = height;
95 if(isOpen())
97 glutReshapeWindow(width, height);
99 if(!setjmp(jump))
100 glutMainLoop();
103 update();
106 void TestWindow::setViewport(OSG::Real32 left, OSG::Real32 right,
107 OSG::Real32 bottom, OSG::Real32 top )
109 _left = left;
110 _right = right;
111 _bottom = bottom;
112 _top = top;
114 update();
117 void TestWindow::setCamera(OSG::Matrix mat)
119 // OSG::beginEditCP(_beacon);
120 _beacon->setMatrix(mat);
121 // OSG::endEditCP(_beacon);
124 void TestWindow::showAll(void)
126 update();
128 _ssm->showAll();
129 _ssm->redraw();
132 void TestWindow::setNearFar(OSG::Real32 n, OSG::Real32 f)
134 // OSG::beginEditCP(getCamera());
135 getCamera()->setNear(n);
136 getCamera()->setFar(f);
137 // OSG::endEditCP(getCamera());
140 void TestWindow::setFov(OSG::Real32 fov)
142 // OSG::beginEditCP(getCamera());
143 getCamera()->setFov(fov);
144 // OSG::endEditCP(getCamera());
147 void TestWindow::redraw(void)
149 // _ssm->redraw();
151 if(!_ssm->getUseTraversalAction())
152 _window->render(dynamic_cast<OSG::RenderAction*>(_ssm->getAction()));
153 #ifdef OSG_CLEANED_RENDERACTION
154 else
155 _window->render(_ssm->getRenderTraversalAction());
156 #endif
159 OSG::ImagePtr TestWindow::snapshot(void)
161 // _ssm->redraw();
163 OSG::ViewportPtr port = _ssm->getWindow()->getPort(0);
165 // OSG::beginEditCP(port);
166 port->addForeground(_grabber);
167 // OSG::endEditCP(port);
169 OSG::ImagePtr img = OSG::Image::create();
170 // OSG::beginEditCP(img);
171 img->setPixelFormat(OSG::Image::OSG_RGB_PF);
172 // OSG::endEditCP(img);
174 // OSG::beginEditCP(_grabber);
175 _grabber->setImage(img);
176 // OSG::endEditCP(_grabber);
178 redraw();
180 // OSG::beginEditCP(port);
181 port->removeFromForegrounds(port->getForegrounds().size()-1);
182 // OSG::endEditCP(port);
184 return img;
187 void TestWindow::finish(void)
189 glFinish();
191 OSG::UInt32 dummy;
192 glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &dummy);
195 void TestWindow::setCamera(OSG::Real32 fromx, OSG::Real32 fromy, OSG::Real32 fromz,
196 OSG::Real32 atx, OSG::Real32 aty, OSG::Real32 atz,
197 OSG::Real32 upx, OSG::Real32 upy, OSG::Real32 upz)
199 OSG::Matrix m;
201 OSG::MatrixLookAt(m, fromx, fromy, fromz,
202 atx, aty, atz,
203 upx, upy, upz);
204 setCamera(m);
207 static void display(void)
209 // Make sure the window is really open...
210 glClear(GL_COLOR_BUFFER_BIT);
212 glFinish();
214 OSG::UInt32 dummy;
215 glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &dummy);
218 static void idle(void)
220 longjmp(jump, 0);
223 void TestWindow::open(void)
225 static bool inited = false;
227 if(isOpen())
228 return;
230 if(!inited)
232 int argc = 2;
233 char *argv[2]={"OpenSG", "Benchmarks"};
235 glutInit(&argc, argv);
236 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
237 inited = true;
240 glutInitWindowPosition(0,0);
241 if(_width > 0)
242 glutInitWindowSize(_width, _height);
244 _winid = glutCreateWindow("OpenSG Benchmark");
246 if(_width < 0)
248 glutFullScreen();
249 _width = glutGet(GLUT_SCREEN_WIDTH);
250 _height = glutGet(GLUT_SCREEN_HEIGHT);
253 glutDisplayFunc(display);
254 glutIdleFunc(idle);
256 update();
258 _window->setId(_winid);
259 _window->init();
261 if(!setjmp(jump))
262 glutMainLoop();
264 _open = true;
267 void TestWindow::close(void)
269 glutDestroyWindow(_window->getId());
270 _open = false;
273 void TestWindow::update(void)
275 if(_window == OSG::NullFC)
276 _window = OSG::GLUTWindow::create();
278 if(_grabber == OSG::NullFC)
280 _grabber = OSG::GrabForeground::create();
281 // OSG::beginEditCP(_grabber);
282 _grabber->setAutoResize(true);
283 _grabber->setActive(true);
284 // OSG::endEditCP(_grabber);
287 if(_ssm == NULL)
288 _ssm = new OSG::SimpleSceneManager;
290 _ssm->setWindow(_window);
292 _ssm->setRoot(_scene);
294 _beacon = OSG::cast_dynamic<OSG::TransformPtr>(
295 getCamera()->getBeacon()->getCore());
297 _ssm->resize(_width, _height);
299 for(int i = 0; i < _ssm->getWindow()->getPort().size(); ++i)
301 OSG::ViewportPtr port = _ssm->getWindow()->getPort(i);
303 // OSG::beginEditCP(port);
304 port->setLeft(_left);
305 port->setRight(_right);
306 port->setBottom(_bottom);
307 port->setTop(_top);
308 // OSG::endEditCP(port);