source: trunk/src/testing/app/gl/gl.cpp @ 4

Revision 4, 10.0 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * SAGE - Scalable Adaptive Graphics Environment
3 *
4 * Copyright (C) 2004 Electronic Visualization Laboratory,
5 * University of Illinois at Chicago
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following disclaimer
16 *    in the documentation and/or other materials provided with the distribution.
17 *  * Neither the name of the University of Illinois at Chicago nor
18 *    the names of its contributors may be used to endorse or promote
19 *    products derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
34 *
35 *****************************************************************************/
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <dlfcn.h>
40#include <X11/Xlib.h>
41#include <X11/Xutil.h>
42#include <X11/keysym.h>
43#include <X11/Xatom.h>
44
45
46#if defined(__APPLE__)
47//#include <GLUT/glut.h>
48#include <OpenGL/glu.h>
49#else
50//#include <GL/glut.h>
51#include <GL/glu.h>
52#endif
53#include <GL/glx.h>
54
55
56#define SAGE2 1
57
58
59// headers for SAGE
60#include "sail.h"
61#include "misc.h"
62int winWidth, winHeight;
63int maxwinWidth, maxwinHeight;
64GLubyte *rgbBuffer  = NULL;
65GLubyte *rgbBuffer2 = NULL;
66sail sageInf; // sail object
67
68typedef void (*glXSwapBuffersFunc_t) (Display*, GLXDrawable);
69typedef void (*glViewportFunc_t) (GLint, GLint, GLsizei, GLsizei);
70typedef void (*glFlushFunc_t) (void);
71typedef void (*glFinishFunc_t) (void);
72
73static glXSwapBuffersFunc_t funcswap = NULL;
74static glViewportFunc_t     funcviewport = NULL;
75static glFlushFunc_t        funcflush = NULL;
76static glFinishFunc_t       funcfinish = NULL;
77
78static int count = 0;
79
80void InitTrap()
81{
82    void *result;
83    void* handle;
84
85#if defined(SAGE2)
86        // SAGE Configuration
87    sageRect renderImageMap;
88    sailConfig scfg;
89
90    scfg.init("gl.conf");
91    scfg.setAppName("gl");
92    scfg.rank = 0;
93
94        scfg.resX = maxwinWidth;
95        scfg.resY = maxwinHeight;
96
97        // set the window size to double the movie size
98    scfg.winWidth = scfg.resX*2;
99    scfg.winHeight = scfg.resY*2;
100
101    renderImageMap.left = 0.0;
102    renderImageMap.right = 1.0;
103    renderImageMap.bottom = 0.0;
104    renderImageMap.top = 1.0;
105
106        scfg.imageMap = renderImageMap;
107        scfg.pixFmt = PIXFMT_8888;
108        scfg.rowOrd = BOTTOM_TO_TOP;
109
110        sageInf.init(scfg);
111#else
112
113
114        // Init SAGE
115    sageRect glImageMap;
116    glImageMap.left = 0.0;
117    glImageMap.right = 1.0;
118    glImageMap.bottom = 0.0;
119    glImageMap.top = 1.0;
120
121    sailConfig scfg;
122    scfg.cfgFile = strdup("sage.conf");
123    scfg.appName = strdup("gl");
124    scfg.rank = 0;
125    scfg.execConfig = NULL;
126    scfg.ip = NULL;
127
128    scfg.resX = maxwinWidth;
129    scfg.resY = maxwinHeight;
130    scfg.imageMap = glImageMap;
131    scfg.colorDepth = 32;
132    scfg.pixFmt = TVPIXFMT_8888;
133    scfg.rowOrd = BOTTOM_TO_TOP;
134
135    sageInf.init(scfg);
136#endif
137
138    fprintf(stderr, "GL> sail initialized\n");
139
140        // Get function symbols
141#if defined(__APPLE__)
142    if ( ! (handle =  dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",RTLD_NOW)) )
143#else
144            //if ( ! (handle =  dlopen("/usr/lib/tls/libGL.so.1",RTLD_NOW)) )
145            //if ( ! (handle =  dlopen("/usr/lib64/libGL.so.1",RTLD_NOW)) )
146        char *gllib = getenv("SAGE_GL");
147    if (gllib == NULL)
148        gllib = strdup("/usr/lib64/libGL.so.1");
149
150    fprintf(stderr, "GL> Using [%s] OpenGL library\n", gllib);
151
152    if ( ! (handle =  dlopen(gllib,RTLD_NOW)) )
153    {
154        fprintf(stderr, "GL> Error opening libGL\n");
155        exit(0);
156    }
157#endif
158
159    if ( ! (result = dlsym(handle,"glXSwapBuffers")) )
160    {
161        fprintf(stderr, "GL> Error getting symbol glXSwapBuffers\n");
162        exit(0);
163    }
164    else
165        funcswap     = (glXSwapBuffersFunc_t)result;
166
167    if ( ! (result = dlsym(handle,"glFlush")) )
168    {
169        fprintf(stderr, "GL> Error getting symbol glFlush\n");
170        exit(0);
171    }
172    else
173        funcflush     = (glFlushFunc_t)result;
174
175    if ( ! (result = dlsym(handle,"glFinish")) )
176    {
177        fprintf(stderr, "GL> Error getting symbol glFinish\n");
178        exit(0);
179    }
180    else
181        funcfinish     = (glFinishFunc_t)result;
182
183
184    if ( ! (result = dlsym(handle,"glViewport")) )
185    {
186        fprintf(stderr, "GL> Error getting symbol glViewport\n");
187        exit(0);
188    }
189    else
190        funcviewport = (glViewportFunc_t)result;
191
192        //dlclose(handle);
193
194}
195
196void glFlush(void)
197{
198        // Real call to glFlush
199    if (funcflush) (*funcflush)();
200
201    if (rgbBuffer)
202    {
203        //fprintf(stderr, "GL> GLFlush %d\n", count++);
204
205        glReadBuffer(GL_FRONT);
206
207        if ( (maxwinWidth == winWidth) & (maxwinHeight == winHeight) )
208        {
209#if defined(SAGE2)
210          rgbBuffer = (unsigned char*)sageInf.getBuffer();
211          glReadPixels(0, 0, maxwinWidth, maxwinHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer);
212          sageInf.swapBuffer();
213#else
214          glReadPixels(0, 0, maxwinWidth, maxwinHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer);
215          sageInf.swapBuffer((unsigned char *)rgbBuffer);
216#endif
217        }
218        else
219        {
220#if defined(SAGE2)
221        glReadPixels(0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer2);
222        rgbBuffer = (unsigned char*)sageInf.getBuffer();
223        memset(rgbBuffer, 0, maxwinWidth*maxwinHeight*4);
224        for (int k=0;k<winHeight;k++)
225            {
226            memcpy(rgbBuffer+(k*maxwinWidth*4), rgbBuffer2+(k*winWidth*4), winWidth*4);
227            }
228        sageInf.swapBuffer();
229#else
230            glReadPixels(0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer2);
231            memset(rgbBuffer, 0, maxwinWidth*maxwinHeight*4);
232            for (int k=0;k<winHeight;k++)
233            {
234                memcpy(rgbBuffer+(k*maxwinWidth*4), rgbBuffer2+(k*winWidth*4), winWidth*4);
235            }
236            sageInf.swapBuffer((unsigned char *)rgbBuffer);
237#endif
238
239        }
240
241    }
242}
243
244void glFinish(void)
245{
246        // Real call to glFinish
247    if (funcfinish) (*funcfinish)();
248}
249
250void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
251{
252        //fprintf(stderr, "glXSwapBuffers -- rgbBuffer %p\n", rgbBuffer);
253
254#if defined(SAGE2)
255    if (!rgbBuffer2)
256    {
257        maxwinWidth  = DisplayWidth(dpy, DefaultScreen(dpy));
258        maxwinHeight = DisplayHeight(dpy, DefaultScreen(dpy));
259
260        fprintf(stderr, "GL> creating %dx%d buffer\n", maxwinWidth, maxwinHeight);
261
262        rgbBuffer2 = (GLubyte*)malloc(maxwinWidth*maxwinHeight*4);
263        memset(rgbBuffer2, 0, maxwinWidth*maxwinHeight*4);
264
265        InitTrap();
266
267        rgbBuffer = (unsigned char*)sageInf.getBuffer();
268    }
269#else
270    if (!rgbBuffer)
271    {
272        maxwinWidth  = DisplayWidth(dpy, DefaultScreen(dpy));
273        maxwinHeight = DisplayHeight(dpy, DefaultScreen(dpy));
274
275        fprintf(stderr, "GL> creating %dx%d buffer\n", maxwinWidth, maxwinHeight);
276
277        if (rgbBuffer) free(rgbBuffer);
278        rgbBuffer = (GLubyte*)malloc(maxwinWidth*maxwinHeight*4);
279        memset(rgbBuffer, 0, maxwinWidth*maxwinHeight*4);
280        rgbBuffer2 = (GLubyte*)malloc(maxwinWidth*maxwinHeight*4);
281        memset(rgbBuffer2, 0, maxwinWidth*maxwinHeight*4);
282
283        InitTrap();
284    }
285#endif
286
287#if !defined(SAGE2)
288    if (rgbBuffer)
289#endif
290    {
291            //fprintf(stderr, "GL> Reading pixels %dx%d buffer\n", winWidth, winHeight);
292
293        glReadBuffer(GL_BACK);
294
295        if ( (maxwinWidth == winWidth) & (maxwinHeight == winHeight) )
296        {
297#if defined(SAGE2)
298          rgbBuffer = (unsigned char*)sageInf.getBuffer();
299          glReadPixels(0, 0, maxwinWidth, maxwinHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer);
300          sageInf.swapBuffer();
301#else
302          glReadPixels(0, 0, maxwinWidth, maxwinHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer);
303          sageInf.swapBuffer((unsigned char *)rgbBuffer);
304#endif
305        }
306        else
307        {
308#if defined(SAGE2)
309          rgbBuffer = (unsigned char*)sageInf.getBuffer();
310
311          glReadPixels(0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer2);
312          memset(rgbBuffer, 0, maxwinWidth*maxwinHeight*4);
313          for (int k=0;k<winHeight;k++)
314            {
315              memcpy(rgbBuffer+(k*maxwinWidth*4), rgbBuffer2+(k*winWidth*4), winWidth*4);
316            }
317
318          sageInf.swapBuffer();
319#else
320          glReadPixels(0, 0, winWidth, winHeight, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuffer2);
321          memset(rgbBuffer, 0, maxwinWidth*maxwinHeight*4);
322          for (int k=0;k<winHeight;k++)
323            {
324              memcpy(rgbBuffer+(k*maxwinWidth*4), rgbBuffer2+(k*winWidth*4), winWidth*4);
325            }
326          sageInf.swapBuffer((unsigned char *)rgbBuffer);
327#endif
328        }
329
330        count = 0;
331
332        //fprintf(stderr, "GL> Sending pixels\n");
333
334        sageMessage msg;
335         if (sageInf.checkMsg(msg, false) > 0) {
336                 switch (msg.getCode()) {
337                         case APP_QUIT : {
338                                exit(1);
339                                break;
340                         }
341                 }
342         }
343
344    }
345
346        // Real call to glXSwapBuffers
347    if (funcswap) (*funcswap)(dpy,drawable);
348
349}
350
351
352
353void glViewport (GLint x, GLint y, GLsizei width, GLsizei height)
354{
355    fprintf(stderr, "GL> glViewport %d %d %d %d\n", x,y,width,height);
356
357    winWidth = width;
358    winHeight = height;
359
360        // Real call to glViewport
361    if (funcviewport) (*funcviewport)(x,y,width,height);
362}
363
Note: See TracBrowser for help on using the repository browser.