root/branches/Carsten_PtrWork2/Tools/osgBench/TestWindow.cpp

Revision 397, 8.7 kB (checked in by dshipton, 2 years ago)

Merge Dirks RenderTraversal? branch back into trunk.
This add's Occlusion Culling and a new node type called ScreenLOD
which is an automatic LOD node.

Line 
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 \*---------------------------------------------------------------------------*/
38
39 //---------------------------------------------------------------------------
40 //  Includes
41 //---------------------------------------------------------------------------
42
43 #include <OpenSG/OSGGLUT.h>
44 #include <OpenSG/OSGMatrixUtility.h>
45 #include <setjmp.h>
46
47 #include "TestWindow.h"
48
49 // This is ultra-ugly, but GLUT gives you no alternative
50 static jmp_buf jump;
51
52
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)
58 {
59 }
60
61 TestWindow::~TestWindow()
62 {
63     if(_winid != -1)
64         glutDestroyWindow(_winid);
65     if(_window != OSG::NullFC)
66         OSG::subRef(_window);
67     if(_ssm != NULL)
68         delete _ssm;
69 }
70
71 void TestWindow::setFullscreen(void)
72 {
73     if(!isOpen())
74     {
75         _width = -1;
76         return;
77     }
78        
79     _width = glutGet(GLUT_SCREEN_WIDTH);
80     _height = glutGet(GLUT_SCREEN_HEIGHT);
81    
82     glutFullScreen();
83
84     if(!setjmp(jump))
85         glutMainLoop();
86    
87     update();
88 }
89
90 void TestWindow::setSize(OSG::UInt16 width, OSG::UInt16 height)
91 {
92     _width = width;
93     _height = height;
94    
95     if(isOpen())
96     {
97         glutReshapeWindow(width, height);
98    
99         if(!setjmp(jump))
100             glutMainLoop();
101     }
102    
103     update();
104 }
105
106 void TestWindow::setViewport(OSG::Real32 left,   OSG::Real32 right,
107                              OSG::Real32 bottom, OSG::Real32 top   )
108 {
109     _left   = left;
110     _right  = right;
111     _bottom = bottom;
112     _top    = top;
113
114     update();
115 }
116
117 void TestWindow::setCamera(OSG::Matrix mat)
118 {
119     // OSG::beginEditCP(_beacon);
120     _beacon->setMatrix(mat);
121     // OSG::endEditCP(_beacon);
122 }
123
124 void TestWindow::showAll(void)
125 {
126     update();
127    
128     _ssm->showAll();
129     _ssm->redraw();
130 }
131
132 void TestWindow::setNearFar(OSG::Real32 n, OSG::Real32 f)
133 {
134     // OSG::beginEditCP(getCamera());
135     getCamera()->setNear(n);
136     getCamera()->setFar(f);
137     // OSG::endEditCP(getCamera());
138 }
139
140 void TestWindow::setFov(OSG::Real32 fov)
141 {
142     // OSG::beginEditCP(getCamera());
143     getCamera()->setFov(fov);
144     // OSG::endEditCP(getCamera()); 
145 }
146
147 void TestWindow::redraw(void)
148 {
149 //    _ssm->redraw();
150
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       
157 }
158
159 OSG::ImagePtr TestWindow::snapshot(void)
160 {
161 //    _ssm->redraw();
162
163     OSG::ViewportPtr port = _ssm->getWindow()->getPort(0);
164
165     // OSG::beginEditCP(port);
166     port->addForeground(_grabber);
167     // OSG::endEditCP(port);
168     
169     OSG::ImagePtr img = OSG::Image::create();
170     // OSG::beginEditCP(img);
171     img->setPixelFormat(OSG::Image::OSG_RGB_PF);
172     // OSG::endEditCP(img);
173     
174     // OSG::beginEditCP(_grabber);
175     _grabber->setImage(img);
176     // OSG::endEditCP(_grabber);
177     
178     redraw();
179    
180     // OSG::beginEditCP(port);
181     port->removeFromForegrounds(port->getForegrounds().size()-1);
182     // OSG::endEditCP(port);
183     
184     return img;
185 }
186
187 void TestWindow::finish(void)
188 {   
189     glFinish();
190    
191     OSG::UInt32 dummy;
192     glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &dummy);
193 }
194
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)
198 {
199     OSG::Matrix m;
200    
201     OSG::MatrixLookAt(m, fromx, fromy, fromz,
202                            atx,   aty,   atz,
203                            upx,   upy,   upz);
204     setCamera(m);
205 }
206
207 static void display(void)
208 {
209     // Make sure the window is really open...
210     glClear(GL_COLOR_BUFFER_BIT);
211
212     glFinish();
213    
214     OSG::UInt32 dummy;
215     glReadPixels(0, 0, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &dummy);
216 }
217
218 static void idle(void)
219 {
220     longjmp(jump, 0);
221 }
222
223 void TestWindow::open(void)
224 {
225     static bool inited = false;
226    
227     if(isOpen())
228         return;
229        
230     if(!inited)
231     {
232         int argc = 2;
233         char *argv[2]={"OpenSG", "Benchmarks"};
234        
235         glutInit(&argc, argv);
236         glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
237         inited = true;       
238     }
239
240     glutInitWindowPosition(0,0);
241     if(_width > 0)
242         glutInitWindowSize(_width, _height);
243    
244     _winid = glutCreateWindow("OpenSG Benchmark");
245
246     if(_width < 0)
247     {
248         glutFullScreen();
249         _width = glutGet(GLUT_SCREEN_WIDTH);
250         _height = glutGet(GLUT_SCREEN_HEIGHT);       
251     }
252    
253     glutDisplayFunc(display);
254     glutIdleFunc(idle);
255    
256     update();
257    
258     _window->setId(_winid);
259     _window->init();
260    
261     if(!setjmp(jump))
262         glutMainLoop();
263    
264     _open = true;
265 }
266
267 void TestWindow::close(void)
268 {
269     glutDestroyWindow(_window->getId());
270     _open = false;
271
272
273 void TestWindow::update(void)
274
275     if(_window == OSG::NullFC)
276         _window = OSG::GLUTWindow::create();
277    
278     if(_grabber == OSG::NullFC)
279     {
280         _grabber = OSG::GrabForeground::create();
281         // OSG::beginEditCP(_grabber);
282         _grabber->setAutoResize(true);
283         _grabber->setActive(true);       
284         // OSG::endEditCP(_grabber);
285     }
286    
287     if(_ssm == NULL)
288         _ssm = new OSG::SimpleSceneManager;
289    
290     _ssm->setWindow(_window);
291  
292     _ssm->setRoot(_scene);
293
294     _beacon = OSG::cast_dynamic<OSG::TransformPtr>(
295                         getCamera()->getBeacon()->getCore());
296    
297     _ssm->resize(_width, _height);
298    
299     for(int i = 0; i < _ssm->getWindow()->getPort().size(); ++i)
300     {
301         OSG::ViewportPtr port = _ssm->getWindow()->getPort(i);
302        
303         // OSG::beginEditCP(port);
304         port->setLeft(_left);
305         port->setRight(_right);
306         port->setBottom(_bottom);
307         port->setTop(_top);
308         // OSG::endEditCP(port);
309     }
310
311 }
Note: See TracBrowser for help on using the browser.