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 | #include <mpi.h> |
---|
66 | |
---|
67 | #define PI 3.1415926535898f |
---|
68 | #define DEG_TO_RAD PI/180.0f |
---|
69 | #define TRANSLATION_RATE 0.2f |
---|
70 | #define ROTATION_RATE 0.01f |
---|
71 | |
---|
72 | int rank = 0; // node rank |
---|
73 | int size = 0; // number of nodes |
---|
74 | |
---|
75 | enum {SPHERE = 1, CONE}; |
---|
76 | enum {X, Y, Z}; |
---|
77 | |
---|
78 | int winWidth, winHeight; |
---|
79 | float rgbReadTime; |
---|
80 | GLubyte *rgbBuffer = 0; |
---|
81 | GLfloat transformMatrix[] = {1.0, 0.0, 0.0, 0.0, |
---|
82 | 0.0, 1.0, 0.0, 0.0, |
---|
83 | 0.0, 0.0, 1.0, 0.0, |
---|
84 | 0.0, 0.0, 0.0, 1.0}; |
---|
85 | |
---|
86 | float eyeX, eyeY, eyeZ; //for the camera position for glulookat |
---|
87 | float centreX, centreY, centreZ; //the point the camera looks at in glulookat |
---|
88 | float rot = 0.0f; |
---|
89 | sail sageInf; // sail object |
---|
90 | |
---|
91 | |
---|
92 | double getTimeInSecs() { |
---|
93 | const double oneMicroSec = 0.000001; |
---|
94 | struct timeval timeSec; |
---|
95 | gettimeofday(&timeSec, NULL); |
---|
96 | return (((double) timeSec.tv_sec) + |
---|
97 | (((double) timeSec.tv_usec) * oneMicroSec)); |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | void rotateCameraY(float rot_inc) { |
---|
102 | rot += rot_inc; |
---|
103 | centreX = eyeX - sin(rot); |
---|
104 | centreZ = eyeZ - cos(rot); |
---|
105 | } |
---|
106 | |
---|
107 | void moveCameraZ(float trans_inc) { |
---|
108 | float inc_x = -trans_inc*sin(rot); //increment in camera X for the given translation |
---|
109 | float inc_z = -trans_inc*cos(rot); //increment in camera Y for the given translation |
---|
110 | eyeX += inc_x; |
---|
111 | eyeZ += inc_z; |
---|
112 | centreX += inc_x; |
---|
113 | centreZ += inc_z; |
---|
114 | } |
---|
115 | |
---|
116 | void moveCameraY(float trans_inc) { |
---|
117 | eyeY += trans_inc; |
---|
118 | centreY += trans_inc; |
---|
119 | } |
---|
120 | |
---|
121 | GLfloat *make_texture(int maxs, int maxt) |
---|
122 | { |
---|
123 | int s, t; |
---|
124 | static GLfloat *texture; |
---|
125 | |
---|
126 | texture = (GLfloat *)malloc(3*maxs * maxt * sizeof(GLfloat)); |
---|
127 | for(t = 0; t < maxt; t++) { |
---|
128 | for(s = 0; s < maxs; s++) { |
---|
129 | texture[s*3 + 3*maxs * t] = 0.0f; |
---|
130 | texture[s*3 + 3*maxs * t+1] = 0.4f*(((s >> 4) & 0x1) ^ ((t >> 4) & 0x1)); |
---|
131 | texture[s*3 + 3*maxs * t+2] = 0.0f*(((s >> 4) & 0x1) ^ ((t >> 4) & 0x1)); |
---|
132 | } |
---|
133 | } |
---|
134 | return texture; |
---|
135 | } |
---|
136 | |
---|
137 | //writes the grabbed framebuffer to a ppm file |
---|
138 | void writePPM(const char* filename) { |
---|
139 | if (!rgbBuffer) |
---|
140 | return; |
---|
141 | FILE* fp = fopen(filename, "wb"); |
---|
142 | if (fp==NULL) { printf("PPM ERROR (WritePPM) : unable to open %s!\n",filename); return; } |
---|
143 | fprintf(fp, "P6\n%d %d\n255\n", winWidth, winHeight); |
---|
144 | fwrite(rgbBuffer,sizeof(GLubyte),winWidth*winHeight*3,fp); |
---|
145 | fclose(fp); |
---|
146 | printf("RGB data written to file %s\n",filename); |
---|
147 | } |
---|
148 | |
---|
149 | //key handler |
---|
150 | //'s' - will save the grabbed image to a ppm file |
---|
151 | void key(unsigned char key, int x, int y) |
---|
152 | { |
---|
153 | switch(key) { |
---|
154 | case 's': //save the read pixels to out.ppm |
---|
155 | writePPM("out.ppm"); |
---|
156 | break; |
---|
157 | case 't': //display time taken for readpixels |
---|
158 | printf("Frame Buffer Readback Time: %dx%d %f \n",winWidth, winHeight, rgbReadTime); |
---|
159 | break; |
---|
160 | case '\033': |
---|
161 | // finalize |
---|
162 | MPI_Finalize(); |
---|
163 | |
---|
164 | exit(0); |
---|
165 | break; |
---|
166 | } |
---|
167 | glutPostRedisplay(); |
---|
168 | } |
---|
169 | |
---|
170 | // The function called whenever a "special" key is pressed. |
---|
171 | void special(int key, int x, int y) |
---|
172 | { |
---|
173 | switch (key) { |
---|
174 | case GLUT_KEY_UP: |
---|
175 | moveCameraZ(TRANSLATION_RATE); |
---|
176 | break; |
---|
177 | case GLUT_KEY_DOWN: |
---|
178 | moveCameraZ(-TRANSLATION_RATE); |
---|
179 | break; |
---|
180 | case GLUT_KEY_LEFT: |
---|
181 | rotateCameraY(ROTATION_RATE); |
---|
182 | break; |
---|
183 | case GLUT_KEY_RIGHT: |
---|
184 | rotateCameraY(-ROTATION_RATE); |
---|
185 | break; |
---|
186 | case GLUT_KEY_PAGE_UP: |
---|
187 | moveCameraY(TRANSLATION_RATE); |
---|
188 | break; |
---|
189 | case GLUT_KEY_PAGE_DOWN: |
---|
190 | moveCameraY(-TRANSLATION_RATE); |
---|
191 | break; |
---|
192 | } |
---|
193 | glutPostRedisplay(); |
---|
194 | } |
---|
195 | |
---|
196 | void idle() |
---|
197 | { |
---|
198 | glutPostRedisplay(); |
---|
199 | } |
---|
200 | |
---|
201 | void |
---|
202 | motion(int x, int y) |
---|
203 | { |
---|
204 | y = winHeight - y; |
---|
205 | glutPostRedisplay(); |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | //reallocate the rgbBuffer depending on the new window dimesions |
---|
210 | void |
---|
211 | reshape(int width, int height) |
---|
212 | { |
---|
213 | glViewport(0, 0, width, height); |
---|
214 | winWidth = width; |
---|
215 | winHeight = height; |
---|
216 | if (rgbBuffer) |
---|
217 | delete [] rgbBuffer; |
---|
218 | rgbBuffer = new GLubyte[width*height*3]; |
---|
219 | glMatrixMode(GL_PROJECTION); |
---|
220 | glLoadIdentity(); |
---|
221 | gluPerspective(40.0,(GLfloat)width/ (GLfloat)height, 0.5, 100.0); |
---|
222 | glMatrixMode(GL_MODELVIEW); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void |
---|
227 | mouse(int button, int state, int x, int y) |
---|
228 | { |
---|
229 | y = winHeight - y; |
---|
230 | if(state == GLUT_DOWN) { |
---|
231 | switch(button) { |
---|
232 | case GLUT_LEFT_BUTTON: //rotate |
---|
233 | break; |
---|
234 | case GLUT_MIDDLE_BUTTON: //zoom |
---|
235 | break; |
---|
236 | case GLUT_RIGHT_BUTTON: |
---|
237 | break; |
---|
238 | } |
---|
239 | } |
---|
240 | else {//GLUT_UP |
---|
241 | switch(button) { |
---|
242 | case GLUT_LEFT_BUTTON: |
---|
243 | break; |
---|
244 | case GLUT_MIDDLE_BUTTON: |
---|
245 | break; |
---|
246 | case GLUT_RIGHT_BUTTON: |
---|
247 | break; |
---|
248 | } |
---|
249 | } |
---|
250 | glutPostRedisplay(); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | //display function |
---|
255 | void redraw(void) |
---|
256 | { |
---|
257 | /* material properties for objects in scene */ |
---|
258 | static GLfloat wall_mat[] = {1.f, 1.f, 1.f, 1.f}; |
---|
259 | static GLfloat sphere_mat[] = {0.5f, 0.0f, 1.f, 1.f}; |
---|
260 | static GLfloat cone_mat[] = {1.0f, 0.5f, 0.0f, 1.f}; |
---|
261 | static GLfloat sphere_mat2[] = {1.0f, 0.0f, 0.5f, 1.f}; |
---|
262 | static GLfloat cone_mat2[] = {0.0f, 0.5f, 1.0f, 1.f}; |
---|
263 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
---|
264 | glClearColor(0.0, 0.0, 0.2, 0.0); |
---|
265 | glMatrixMode(GL_MODELVIEW); |
---|
266 | glLoadIdentity(); |
---|
267 | gluLookAt(eyeX, eyeY, eyeZ, |
---|
268 | centreX, centreY, centreZ, |
---|
269 | 0.0,1.0,0.0); |
---|
270 | /* |
---|
271 | ** Note: wall verticies are ordered so they are all front facing |
---|
272 | ** this lets me do back face culling to speed things up. |
---|
273 | */ |
---|
274 | |
---|
275 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat); |
---|
276 | |
---|
277 | glEnable(GL_TEXTURE_2D); |
---|
278 | glBegin(GL_QUADS); |
---|
279 | glNormal3f(0.f, 1.f, 0.f); |
---|
280 | glTexCoord2i(0, 0); |
---|
281 | glVertex3f(-6.f, -4.f, -2.f); |
---|
282 | glTexCoord2i(1, 0); |
---|
283 | glVertex3f( 6.f, -4.f, -2.f); |
---|
284 | glTexCoord2i(1, 1); |
---|
285 | glVertex3f( 6.f, -4.f, -15.f); |
---|
286 | glTexCoord2i(0, 1); |
---|
287 | glVertex3f(-6.f, -4.f, -15.f); |
---|
288 | glEnd(); |
---|
289 | glDisable(GL_TEXTURE_2D); |
---|
290 | |
---|
291 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat); |
---|
292 | glPushMatrix(); |
---|
293 | glTranslatef(-3.f, 0.5f, -10.f); |
---|
294 | glScalef(1, 1, 1); |
---|
295 | glCallList(SPHERE); |
---|
296 | glPopMatrix(); |
---|
297 | |
---|
298 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat); |
---|
299 | glPushMatrix(); |
---|
300 | glTranslatef(0.f, 0.3f, -3.f); |
---|
301 | glRotatef(30.f, 2.f, 0.0, -1.0f); |
---|
302 | glScalef(1.0, 1.0, 1.0); |
---|
303 | glCallList(CONE); |
---|
304 | glPopMatrix(); |
---|
305 | |
---|
306 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat2); |
---|
307 | glPushMatrix(); |
---|
308 | glTranslatef(-2.f, -2.f, -7.f); |
---|
309 | glScalef(1, 1, 1); |
---|
310 | glCallList(SPHERE); |
---|
311 | glPopMatrix(); |
---|
312 | |
---|
313 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat2); |
---|
314 | glPushMatrix(); |
---|
315 | glTranslatef(4.f, -2.f, -13.f); |
---|
316 | glRotatef(-45.f, 1.f, 0.0f, 0.0f); |
---|
317 | glScalef(0.8, 0.8, 0.8); |
---|
318 | glCallList(CONE); |
---|
319 | glPopMatrix(); |
---|
320 | |
---|
321 | //finished drawing. At this point capture the frame buffer |
---|
322 | //expensive new!! TODO: put this in reshape |
---|
323 | |
---|
324 | if (winWidth > 0) { |
---|
325 | if (sageInf.getBufSize() == 0) |
---|
326 | sageInf.resizeBuffer(winWidth, winHeight, 24); |
---|
327 | |
---|
328 | //cout << "read pixels" << endl; |
---|
329 | double prevtime = getTimeInSecs(); |
---|
330 | glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, rgbBuffer); |
---|
331 | rgbReadTime = getTimeInSecs()-prevtime; |
---|
332 | //TODO-send it to QUANTA |
---|
333 | |
---|
334 | cout << rank << " swap buffer " << winWidth << "," << winHeight << endl; |
---|
335 | |
---|
336 | MPI_Barrier(MPI_COMM_WORLD); |
---|
337 | |
---|
338 | sageInf.swapBuffer(rgbBuffer); |
---|
339 | sageInf.mainLoop(); |
---|
340 | |
---|
341 | } |
---|
342 | |
---|
343 | glutSwapBuffers(); |
---|
344 | } |
---|
345 | |
---|
346 | void update(int value) { |
---|
347 | |
---|
348 | //sageInf.swapBuffer((unsigned char *)rgbBuffer); |
---|
349 | sageInf.mainLoop(); |
---|
350 | |
---|
351 | //glutTimerFunc(1000, update, 1); |
---|
352 | } |
---|
353 | |
---|
354 | main(int argc, char *argv[]) |
---|
355 | { |
---|
356 | |
---|
357 | // MPI Initialization |
---|
358 | MPI_Init(&argc, &argv); |
---|
359 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
---|
360 | MPI_Comm_size(MPI_COMM_WORLD, &size); |
---|
361 | MPI_Barrier(MPI_COMM_WORLD); |
---|
362 | |
---|
363 | GLfloat *tex; |
---|
364 | static GLfloat lightpos[] = {10.f, 10.f, -20.0f, 1.f}; |
---|
365 | GLUquadricObj *sphere, *cone, *base; |
---|
366 | |
---|
367 | glutInit(&argc, argv); |
---|
368 | |
---|
369 | glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); |
---|
370 | glutInitWindowSize(1024,1024); |
---|
371 | glutCreateWindow("sage render"); |
---|
372 | glutDisplayFunc(redraw); |
---|
373 | glutIdleFunc(idle); |
---|
374 | glutKeyboardFunc(key); |
---|
375 | glutMouseFunc(mouse); |
---|
376 | //glutMotionFunc(motion); |
---|
377 | glutSpecialFunc(special); |
---|
378 | glutReshapeFunc(reshape); |
---|
379 | //glutFullScreen(); |
---|
380 | |
---|
381 | glMatrixMode(GL_MODELVIEW); |
---|
382 | |
---|
383 | glEnable(GL_DEPTH_TEST); |
---|
384 | glEnable(GL_LIGHTING); |
---|
385 | glEnable(GL_LIGHT0); |
---|
386 | glLightfv(GL_LIGHT0, GL_POSITION, lightpos); |
---|
387 | |
---|
388 | glCullFace(GL_BACK); |
---|
389 | |
---|
390 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
391 | |
---|
392 | //create the display list to draw a sphere |
---|
393 | glNewList(SPHERE, GL_COMPILE); |
---|
394 | sphere = gluNewQuadric(); |
---|
395 | gluSphere(sphere, 1.f, 20, 20); |
---|
396 | gluDeleteQuadric(sphere); |
---|
397 | glEndList(); |
---|
398 | |
---|
399 | //create the display list to draw a cone |
---|
400 | glNewList(CONE, GL_COMPILE); |
---|
401 | cone = gluNewQuadric(); |
---|
402 | base = gluNewQuadric(); |
---|
403 | glRotatef(-90.f, 1.f, 0.f, 0.f); |
---|
404 | gluQuadricOrientation(base, GLU_INSIDE); |
---|
405 | gluDisk(base, 0., 1., 20, 1); |
---|
406 | gluCylinder(cone, 1., 0., 2., 20, 20); |
---|
407 | gluDeleteQuadric(cone); |
---|
408 | gluDeleteQuadric(base); |
---|
409 | glEndList(); |
---|
410 | |
---|
411 | //create a 2D texture for our floor |
---|
412 | tex = make_texture(256, 256); |
---|
413 | glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_FLOAT, tex); |
---|
414 | free(tex); |
---|
415 | |
---|
416 | eyeX = eyeY = centreX = centreY = 0.0f; |
---|
417 | eyeZ = 10.0f; centreZ = 9.0f; |
---|
418 | |
---|
419 | //glutTimerFunc(100, update, 1); |
---|
420 | |
---|
421 | MPI_Barrier(MPI_COMM_WORLD); |
---|
422 | |
---|
423 | char procname[MPI_MAX_PROCESSOR_NAME]; |
---|
424 | int lenproc; |
---|
425 | MPI_Get_processor_name(procname, &lenproc); |
---|
426 | fprintf(stderr, "Processor %2d is machine [%s]\n", rank, procname); |
---|
427 | |
---|
428 | sageInf.init("sail.conf", "render", rank, 0, 0, 24, TVPIXFMT_888, BOTTOM_TO_TOP); |
---|
429 | |
---|
430 | MPI_Barrier(MPI_COMM_WORLD); |
---|
431 | |
---|
432 | glutMainLoop(); |
---|
433 | |
---|
434 | // finalize |
---|
435 | MPI_Finalize(); |
---|
436 | |
---|
437 | // exit |
---|
438 | return EXIT_SUCCESS; |
---|
439 | } |
---|