1 | /****************************************************************************** |
---|
2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
3 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
4 | * University of Illinois at Chicago |
---|
5 | * |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions are met: |
---|
10 | * |
---|
11 | * * Redistributions of source code must retain the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer. |
---|
13 | * * Redistributions in binary form must reproduce the above |
---|
14 | * copyright notice, this list of conditions and the following disclaimer |
---|
15 | * in the documentation and/or other materials provided with the distribution. |
---|
16 | * * Neither the name of the University of Illinois at Chicago nor |
---|
17 | * the names of its contributors may be used to endorse or promote |
---|
18 | * products derived from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 | * |
---|
32 | * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu |
---|
33 | *****************************************************************************/ |
---|
34 | |
---|
35 | //Shalini Venkataraman |
---|
36 | //A simple glut program that draws spheres and cones, reads the frame buffer |
---|
37 | //and sends it across to a display client |
---|
38 | //Feb 2004 |
---|
39 | // |
---|
40 | //modified by Byungil Jeong |
---|
41 | //July 2004 |
---|
42 | |
---|
43 | #include <stdio.h> |
---|
44 | #include <math.h> |
---|
45 | #include <stdlib.h> |
---|
46 | |
---|
47 | #if defined(__APPLE__) |
---|
48 | #include <GLUT/glut.h> |
---|
49 | #else |
---|
50 | #include <GL/glut.h> |
---|
51 | #endif |
---|
52 | |
---|
53 | #include <time.h> |
---|
54 | #ifdef WIN32 |
---|
55 | #include "gettimeofday.hxx" |
---|
56 | #else |
---|
57 | #include <sys/timeb.h> |
---|
58 | #include <sys/time.h> |
---|
59 | #endif |
---|
60 | |
---|
61 | // headers for SAGE |
---|
62 | #include "sail.h" |
---|
63 | #include "misc.h" |
---|
64 | |
---|
65 | #define PI 3.1415926535898f |
---|
66 | #define DEG_TO_RAD PI/180.0f |
---|
67 | #define TRANSLATION_RATE 0.2f |
---|
68 | #define ROTATION_RATE 0.01f |
---|
69 | |
---|
70 | enum {SPHERE = 1, CONE}; |
---|
71 | enum {X, Y, Z}; |
---|
72 | |
---|
73 | int winWidth = 400, winHeight = 400; |
---|
74 | bool refresh = false; |
---|
75 | int cnt, target; |
---|
76 | float rgbReadTime; |
---|
77 | GLubyte *rgbBuffer = 0; |
---|
78 | GLfloat transformMatrix[] = {1.0, 0.0, 0.0, 0.0, |
---|
79 | 0.0, 1.0, 0.0, 0.0, |
---|
80 | 0.0, 0.0, 1.0, 0.0, |
---|
81 | 0.0, 0.0, 0.0, 1.0}; |
---|
82 | |
---|
83 | float eyeX, eyeY, eyeZ; //for the camera position for glulookat |
---|
84 | float centreX, centreY, centreZ; //the point the camera looks at in glulookat |
---|
85 | float rot = 0.0f; |
---|
86 | sail sageInf; // sail object |
---|
87 | |
---|
88 | |
---|
89 | double getTimeInSecs() { |
---|
90 | const double oneMicroSec = 0.000001; |
---|
91 | struct timeval timeSec; |
---|
92 | gettimeofday(&timeSec, NULL); |
---|
93 | return (((double) timeSec.tv_sec) + |
---|
94 | (((double) timeSec.tv_usec) * oneMicroSec)); |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | void rotateCameraY(float rot_inc) { |
---|
99 | rot += rot_inc; |
---|
100 | centreX = eyeX - sin(rot); |
---|
101 | centreZ = eyeZ - cos(rot); |
---|
102 | } |
---|
103 | |
---|
104 | void moveCameraZ(float trans_inc) { |
---|
105 | float inc_x = -trans_inc*sin(rot); //increment in camera X for the given translation |
---|
106 | float inc_z = -trans_inc*cos(rot); //increment in camera Y for the given translation |
---|
107 | eyeX += inc_x; |
---|
108 | eyeZ += inc_z; |
---|
109 | centreX += inc_x; |
---|
110 | centreZ += inc_z; |
---|
111 | } |
---|
112 | |
---|
113 | void moveCameraY(float trans_inc) { |
---|
114 | eyeY += trans_inc; |
---|
115 | centreY += trans_inc; |
---|
116 | } |
---|
117 | |
---|
118 | GLfloat *make_texture(int maxs, int maxt) |
---|
119 | { |
---|
120 | int s, t; |
---|
121 | static GLfloat *texture; |
---|
122 | |
---|
123 | texture = (GLfloat *)malloc(3*maxs * maxt * sizeof(GLfloat)); |
---|
124 | for(t = 0; t < maxt; t++) { |
---|
125 | for(s = 0; s < maxs; s++) { |
---|
126 | texture[s*3 + 3*maxs * t] = 0.0f; |
---|
127 | texture[s*3 + 3*maxs * t+1] = 0.4f*(((s >> 4) & 0x1) ^ ((t >> 4) & 0x1)); |
---|
128 | texture[s*3 + 3*maxs * t+2] = 0.0f*(((s >> 4) & 0x1) ^ ((t >> 4) & 0x1)); |
---|
129 | } |
---|
130 | } |
---|
131 | return texture; |
---|
132 | } |
---|
133 | |
---|
134 | //writes the grabbed framebuffer to a ppm file |
---|
135 | void writePPM(const char* filename) { |
---|
136 | if (!rgbBuffer) |
---|
137 | return; |
---|
138 | FILE* fp = fopen(filename, "wb"); |
---|
139 | if (fp==NULL) { printf("PPM ERROR (WritePPM) : unable to open %s!\n",filename); return; } |
---|
140 | fprintf(fp, "P6\n%d %d\n255\n", winWidth, winHeight); |
---|
141 | fwrite(rgbBuffer,sizeof(GLubyte),winWidth*winHeight*3,fp); |
---|
142 | fclose(fp); |
---|
143 | printf("RGB data written to file %s\n",filename); |
---|
144 | } |
---|
145 | |
---|
146 | //key handler |
---|
147 | //'s' - will save the grabbed image to a ppm file |
---|
148 | void key(unsigned char key, int x, int y) |
---|
149 | { |
---|
150 | switch(key) { |
---|
151 | case 's': //save the read pixels to out.ppm |
---|
152 | writePPM("out.ppm"); |
---|
153 | break; |
---|
154 | case 't': //display time taken for readpixels |
---|
155 | printf("Frame Buffer Readback Time: %dx%d %f \n",winWidth, winHeight, rgbReadTime); |
---|
156 | break; |
---|
157 | case '\033': |
---|
158 | sageInf.shutdown(); |
---|
159 | exit(0); |
---|
160 | break; |
---|
161 | } |
---|
162 | glutPostRedisplay(); |
---|
163 | } |
---|
164 | |
---|
165 | // The function called whenever a "special" key is pressed. |
---|
166 | void special(int key, int x, int y) |
---|
167 | { |
---|
168 | switch (key) { |
---|
169 | case GLUT_KEY_UP: |
---|
170 | moveCameraZ(TRANSLATION_RATE); |
---|
171 | break; |
---|
172 | case GLUT_KEY_DOWN: |
---|
173 | moveCameraZ(-TRANSLATION_RATE); |
---|
174 | break; |
---|
175 | case GLUT_KEY_LEFT: |
---|
176 | rotateCameraY(ROTATION_RATE); |
---|
177 | break; |
---|
178 | case GLUT_KEY_RIGHT: |
---|
179 | rotateCameraY(-ROTATION_RATE); |
---|
180 | break; |
---|
181 | case GLUT_KEY_PAGE_UP: |
---|
182 | moveCameraY(TRANSLATION_RATE); |
---|
183 | break; |
---|
184 | case GLUT_KEY_PAGE_DOWN: |
---|
185 | moveCameraY(-TRANSLATION_RATE); |
---|
186 | break; |
---|
187 | } |
---|
188 | glutPostRedisplay(); |
---|
189 | } |
---|
190 | |
---|
191 | void |
---|
192 | motion(int x, int y) |
---|
193 | { |
---|
194 | y = winHeight - y; |
---|
195 | glutPostRedisplay(); |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | /* |
---|
200 | //reallocate the rgbBuffer depending on the new window dimesions |
---|
201 | void |
---|
202 | reshape(int width, int height) |
---|
203 | { |
---|
204 | glViewport(0, 0, width, height); |
---|
205 | winWidth = width; |
---|
206 | winHeight = height; |
---|
207 | if (rgbBuffer) |
---|
208 | delete [] rgbBuffer; |
---|
209 | rgbBuffer = new GLubyte[width*height*3]; |
---|
210 | glMatrixMode(GL_PROJECTION); |
---|
211 | glLoadIdentity(); |
---|
212 | gluPerspective(40.0,(GLfloat)width/ (GLfloat)height, 0.5, 100.0); |
---|
213 | glMatrixMode(GL_MODELVIEW); |
---|
214 | } |
---|
215 | |
---|
216 | */ |
---|
217 | |
---|
218 | void |
---|
219 | mouse(int button, int state, int x, int y) |
---|
220 | { |
---|
221 | y = winHeight - y; |
---|
222 | if(state == GLUT_DOWN) { |
---|
223 | switch(button) { |
---|
224 | case GLUT_LEFT_BUTTON: //rotate |
---|
225 | break; |
---|
226 | case GLUT_MIDDLE_BUTTON: //zoom |
---|
227 | break; |
---|
228 | case GLUT_RIGHT_BUTTON: |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | else {//GLUT_UP |
---|
233 | switch(button) { |
---|
234 | case GLUT_LEFT_BUTTON: |
---|
235 | break; |
---|
236 | case GLUT_MIDDLE_BUTTON: |
---|
237 | break; |
---|
238 | case GLUT_RIGHT_BUTTON: |
---|
239 | break; |
---|
240 | } |
---|
241 | } |
---|
242 | glutPostRedisplay(); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | //display function |
---|
247 | void redraw(void) |
---|
248 | { |
---|
249 | /* material properties for objects in scene */ |
---|
250 | static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f}; |
---|
251 | static GLfloat sphere_mat[] = {0.5f, 0.0f, 1.f, 1.f}; |
---|
252 | static GLfloat cone_mat[] = {1.0f, 0.5f, 0.0f, 1.f}; |
---|
253 | static GLfloat sphere_mat2[] = {1.0f, 0.0f, 0.5f, 1.f}; |
---|
254 | static GLfloat cone_mat2[] = {0.0f, 0.5f, 1.0f, 1.f}; |
---|
255 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
256 | glClearColor(0.0, 0.0, 0.2, 0.0); |
---|
257 | glMatrixMode(GL_MODELVIEW); |
---|
258 | glLoadIdentity(); |
---|
259 | gluLookAt(eyeX, eyeY, eyeZ, |
---|
260 | centreX, centreY, centreZ, |
---|
261 | 0.0,1.0,0.0); |
---|
262 | /* |
---|
263 | ** Note: wall verticies are ordered so they are all front facing |
---|
264 | ** this lets me do back face culling to speed things up. |
---|
265 | */ |
---|
266 | |
---|
267 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat); |
---|
268 | |
---|
269 | glEnable(GL_TEXTURE_2D); |
---|
270 | glBegin(GL_QUADS); |
---|
271 | glNormal3f(0.f, 1.f, 0.f); |
---|
272 | glTexCoord2i(0, 0); |
---|
273 | glVertex3f(-6.f, -4.f, -2.f); |
---|
274 | glTexCoord2i(1, 0); |
---|
275 | glVertex3f( 6.f, -4.f, -2.f); |
---|
276 | glTexCoord2i(1, 1); |
---|
277 | glVertex3f( 6.f, -4.f, -15.f); |
---|
278 | glTexCoord2i(0, 1); |
---|
279 | glVertex3f(-6.f, -4.f, -15.f); |
---|
280 | glEnd(); |
---|
281 | glDisable(GL_TEXTURE_2D); |
---|
282 | |
---|
283 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat); |
---|
284 | glPushMatrix(); |
---|
285 | glTranslatef(-3.f, 0.5f, -10.f); |
---|
286 | glScalef(1, 1, 1); |
---|
287 | glCallList(SPHERE); |
---|
288 | glPopMatrix(); |
---|
289 | |
---|
290 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat); |
---|
291 | glPushMatrix(); |
---|
292 | glTranslatef(0.f, 0.3f, -3.f); |
---|
293 | glRotatef(30.f, 2.f, 0.0, -1.0f); |
---|
294 | glScalef(1.0, 1.0, 1.0); |
---|
295 | glCallList(CONE); |
---|
296 | glPopMatrix(); |
---|
297 | |
---|
298 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat2); |
---|
299 | glPushMatrix(); |
---|
300 | glTranslatef(-2.f, -2.f, -7.f); |
---|
301 | glScalef(1, 1, 1); |
---|
302 | glCallList(SPHERE); |
---|
303 | glPopMatrix(); |
---|
304 | |
---|
305 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat2); |
---|
306 | glPushMatrix(); |
---|
307 | glTranslatef(4.f, -2.f, -13.f); |
---|
308 | glRotatef(-45.f, 1.f, 0.0f, 0.0f); |
---|
309 | glScalef(0.8, 0.8, 0.8); |
---|
310 | glCallList(CONE); |
---|
311 | glPopMatrix(); |
---|
312 | |
---|
313 | //finished drawing. At this point capture the frame buffer |
---|
314 | //expensive new!! TODO: put this in reshape |
---|
315 | |
---|
316 | double prevtime = getTimeInSecs(); |
---|
317 | glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, rgbBuffer); |
---|
318 | rgbReadTime = getTimeInSecs()-prevtime; |
---|
319 | //TODO-send it to QUANTA |
---|
320 | |
---|
321 | sageInf.swapBuffer(); |
---|
322 | rgbBuffer = (GLubyte *)sageInf.getBuffer(); |
---|
323 | |
---|
324 | // if (refresh) { |
---|
325 | // cnt++; |
---|
326 | // if (cnt >= target) |
---|
327 | // refresh = false; |
---|
328 | // } |
---|
329 | |
---|
330 | glutSwapBuffers(); |
---|
331 | } |
---|
332 | |
---|
333 | void update(int value) { |
---|
334 | //std::cout << "swap buffer " << winWidth << "," << winHeight << std::endl; |
---|
335 | // sageInf.swapBuffer((void *)rgbBuffer); |
---|
336 | // sageInf.mainLoop(); |
---|
337 | |
---|
338 | //glutPostRedisplay(); |
---|
339 | sageMessage msg; |
---|
340 | |
---|
341 | if (sageInf.checkMsg(msg, false) > 0) { |
---|
342 | switch (msg.getCode()) { |
---|
343 | case APP_QUIT : { |
---|
344 | exit(0); |
---|
345 | break; |
---|
346 | } |
---|
347 | |
---|
348 | //case APP_REFRESH_FRAME : { |
---|
349 | //glutPostRedisplay(); |
---|
350 | //refresh = true; |
---|
351 | //target = atoi((char *)msg.getData()); |
---|
352 | //cnt = 0; |
---|
353 | //break; |
---|
354 | // } |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | // if (refresh) { |
---|
359 | // glutPostRedisplay(); |
---|
360 | // } |
---|
361 | |
---|
362 | glutTimerFunc(50, update, 1); |
---|
363 | } |
---|
364 | |
---|
365 | |
---|
366 | main(int argc, char *argv[]) |
---|
367 | { |
---|
368 | GLfloat *tex; |
---|
369 | static GLfloat lightpos[] = {10.f, 10.f, -20.0f, 1.f}; |
---|
370 | GLUquadricObj *sphere, *cone, *base; |
---|
371 | |
---|
372 | glutInit(&argc, argv); |
---|
373 | |
---|
374 | glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); |
---|
375 | glutInitWindowSize(winWidth,winHeight); |
---|
376 | glutCreateWindow("sage render"); |
---|
377 | glutDisplayFunc(redraw); |
---|
378 | //glutIdleFunc(redraw); |
---|
379 | glutKeyboardFunc(key); |
---|
380 | glutMouseFunc(mouse); |
---|
381 | glutMotionFunc(motion); |
---|
382 | glutSpecialFunc(special); |
---|
383 | // glutReshapeFunc(reshape); |
---|
384 | |
---|
385 | glViewport(0, 0, winWidth, winHeight); |
---|
386 | glMatrixMode(GL_PROJECTION); |
---|
387 | glLoadIdentity(); |
---|
388 | gluPerspective(40.0,(GLfloat)winWidth/ (GLfloat)winHeight, 0.5, 100.0); |
---|
389 | glMatrixMode(GL_MODELVIEW); |
---|
390 | //glutFullScreen(); |
---|
391 | |
---|
392 | glMatrixMode(GL_MODELVIEW); |
---|
393 | |
---|
394 | glEnable(GL_DEPTH_TEST); |
---|
395 | glEnable(GL_LIGHTING); |
---|
396 | glEnable(GL_LIGHT0); |
---|
397 | glLightfv(GL_LIGHT0, GL_POSITION, lightpos); |
---|
398 | |
---|
399 | glCullFace(GL_BACK); |
---|
400 | |
---|
401 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
402 | |
---|
403 | //create the display list to draw a sphere |
---|
404 | glNewList(SPHERE, GL_COMPILE); |
---|
405 | sphere = gluNewQuadric(); |
---|
406 | gluSphere(sphere, 1.f, 20, 20); |
---|
407 | gluDeleteQuadric(sphere); |
---|
408 | glEndList(); |
---|
409 | |
---|
410 | //create the display list to draw a cone |
---|
411 | glNewList(CONE, GL_COMPILE); |
---|
412 | cone = gluNewQuadric(); |
---|
413 | base = gluNewQuadric(); |
---|
414 | glRotatef(-90.f, 1.f, 0.f, 0.f); |
---|
415 | gluQuadricOrientation(base, GLU_INSIDE); |
---|
416 | gluDisk(base, 0., 1., 20, 1); |
---|
417 | gluCylinder(cone, 1., 0., 2., 20, 20); |
---|
418 | gluDeleteQuadric(cone); |
---|
419 | gluDeleteQuadric(base); |
---|
420 | glEndList(); |
---|
421 | |
---|
422 | //create a 2D texture for our floor |
---|
423 | tex = make_texture(256, 256); |
---|
424 | glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_FLOAT, tex); |
---|
425 | free(tex); |
---|
426 | |
---|
427 | eyeX = eyeY = centreX = centreY = 0.0f; |
---|
428 | eyeZ = 10.0f; centreZ = 9.0f; |
---|
429 | |
---|
430 | glutTimerFunc(100, update, 1); |
---|
431 | |
---|
432 | int appID; |
---|
433 | if (argc < 2) |
---|
434 | appID = 0; |
---|
435 | else |
---|
436 | appID = atoi(argv[1]); |
---|
437 | |
---|
438 | int nodeID; |
---|
439 | if (argc < 3) |
---|
440 | nodeID = 0; |
---|
441 | else |
---|
442 | nodeID = atoi(argv[2]); |
---|
443 | |
---|
444 | sailConfig cfg; |
---|
445 | cfg.init("render.conf"); // every app has a config file named "appName.conf" |
---|
446 | std::cout << "SAIL configuration was initialized by render.conf" << std::endl; |
---|
447 | |
---|
448 | cfg.setAppName("render"); |
---|
449 | cfg.rank = nodeID; |
---|
450 | //cfg.appID = appID; //appLauncher will do this |
---|
451 | cfg.resX = winWidth; |
---|
452 | cfg.resY = winHeight; |
---|
453 | |
---|
454 | sageRect renderImageMap; |
---|
455 | renderImageMap.left = 0.0; |
---|
456 | renderImageMap.right = 1.0; |
---|
457 | renderImageMap.bottom = 0.0; |
---|
458 | renderImageMap.top = 1.0; |
---|
459 | |
---|
460 | cfg.imageMap = renderImageMap; |
---|
461 | cfg.pixFmt = PIXFMT_888; |
---|
462 | cfg.rowOrd = BOTTOM_TO_TOP; |
---|
463 | cfg.master = true; |
---|
464 | |
---|
465 | sageInf.init(cfg); |
---|
466 | std::cout << "sail initialized " << std::endl; |
---|
467 | |
---|
468 | if (rgbBuffer) |
---|
469 | delete [] rgbBuffer; |
---|
470 | |
---|
471 | rgbBuffer = (GLubyte *)sageInf.getBuffer(); |
---|
472 | |
---|
473 | glutMainLoop(); |
---|
474 | |
---|
475 | return 0; |
---|
476 | } |
---|