-
Notifications
You must be signed in to change notification settings - Fork 1
/
wglspheres.c
491 lines (422 loc) · 13.3 KB
/
wglspheres.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Copyright (C)2008 Sun Microsystems, Inc.
* Copyright (C)2013 D. R. Commander
*
* This library is free software and may be redistributed and/or modified under
* the terms of the wxWindows Library License, Version 3 or (at your option)
* any later version. The full license is in the LICENSE.txt file included
* with this distribution.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* wxWindows Library License for more details.
*/
/* This program's goal is to reproduce, as closely as possible, the image
output of the nVidia SphereMark demo by R. Stephen Glanville using the
simplest available rendering method. GLXSpheres is meant primarily to
serve as an image pipeline benchmark for VirtualGL. */
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "rrtimer.h"
#define snprintf(str, n, format, ...) \
_snprintf_s(str, n, _TRUNCATE, format, __VA_ARGS__)
static char __lasterror[1024]="No error";
#define _throw(m) {fprintf(stderr, "ERROR in line %d:\n%s\n", __LINE__, m); \
goto bailout;}
#define _throww32(m) { \
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), \
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), __lasterror, 1024, NULL)) \
fprintf(stderr, "WIN32 ERROR in line %d:\n%s\n%s", __LINE__, m, __lasterror); \
goto bailout; \
}
#define np2(i) ((i)>0? (1<<(int)(log((double)(i))/log(2.))) : 0)
#define _catch(f) {if((f)==-1) goto bailout;}
#define spherered(f) (GLfloat)fabs(MAXI*(2.*f-1.))
#define spheregreen(f) (GLfloat)fabs(MAXI*(2.*fmod(f+2./3., 1.)-1.))
#define sphereblue(f) (GLfloat)fabs(MAXI*(2.*fmod(f+1./3., 1.)-1.))
#define DEF_WIDTH 1240
#define DEF_HEIGHT 900
#define DEF_SLICES 32
#define DEF_STACKS 32
#define NSPHERES 20
#define _2PI 6.283185307180
#define MAXI (220./255.)
#define DEFBENCHTIME 2.0
HWND win=0; HDC hdc=0;
int usestereo=0, useimm=0, interactive=0, locolor=0, maxframes=0,
totalframes=0;
double benchtime=DEFBENCHTIME;
HGLRC ctx=0;
int spherelist=0, fontlistbase=0;
GLUquadricObj *spherequad=NULL;
int slices=DEF_SLICES, stacks=DEF_STACKS;
float x=0., y=0., z=-3.;
float outer_angle=0., middle_angle=0., inner_angle=0.;
float lonesphere_color=0.;
unsigned int transpixel=0;
int dodisplay=0, advance=0;
int width=DEF_WIDTH, height=DEF_HEIGHT;
void reshape(int w, int h)
{
if(w<=0) w=1; if(h<=0) h=1;
width=w; height=h;
}
void setspherecolor(double c)
{
GLfloat mat[4]={spherered(c), spheregreen(c), sphereblue(c), 0.25};
glColor3d(spherered(c), spheregreen(c), sphereblue(c));
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
}
void renderspheres(int buf)
{
int i;
GLfloat xaspect, yaspect;
GLfloat stereocameraoffset=0.;
GLfloat neardist=1.5, fardist=40., zeroparallaxdist=17.;
glDrawBuffer(buf);
xaspect=(GLfloat)width/(GLfloat)(min(width, height));
yaspect=(GLfloat)height/(GLfloat)(min(width, height));
if(buf==GL_BACK_LEFT)
stereocameraoffset=-xaspect*zeroparallaxdist/neardist*0.035f;
else if(buf==GL_BACK_RIGHT)
stereocameraoffset=xaspect*zeroparallaxdist/neardist*0.035f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-xaspect, xaspect, -yaspect, yaspect, neardist, fardist);
glTranslatef(-stereocameraoffset, 0., 0.);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, width, height);
// Begin rendering
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(20.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Lone sphere
glPushMatrix();
glTranslatef(0., 0., z);
setspherecolor(lonesphere_color);
if(useimm) gluSphere(spherequad, 1.3, slices, stacks);
else glCallList(spherelist);
glPopMatrix();
// Outer ring
glPushMatrix();
glRotatef(outer_angle, 0., 0., 1.);
for(i=0; i<NSPHERES; i++)
{
double f=(double)i/(double)NSPHERES;
glPushMatrix();
glTranslatef((GLfloat)sin(_2PI*f)*5.0f, (GLfloat)cos(_2PI*f)*5.0f, -10.0f);
setspherecolor(f);
if(useimm) gluSphere(spherequad, 1.3, slices, stacks);
else glCallList(spherelist);
glPopMatrix();
}
glPopMatrix();
// Middle ring
glPushMatrix();
glRotatef(middle_angle, 0., 0., 1.);
for(i=0; i<NSPHERES; i++)
{
double f=(double)i/(double)NSPHERES;
glPushMatrix();
glTranslatef((GLfloat)sin(_2PI*f)*5.0f, (GLfloat)cos(_2PI*f)*5.0f, -17.0f);
setspherecolor(f);
if(useimm) gluSphere(spherequad, 1.3, slices, stacks);
else glCallList(spherelist);
glPopMatrix();
}
glPopMatrix();
// Inner ring
glPushMatrix();
glRotatef(inner_angle, 0., 0., 1.);
for(i=0; i<NSPHERES; i++)
{
double f=(double)i/(double)NSPHERES;
glPushMatrix();
glTranslatef((GLfloat)sin(_2PI*f)*5.0f, (GLfloat)cos(_2PI*f)*5.0f, -29.0f);
setspherecolor(f);
if(useimm) gluSphere(spherequad, 1.3, slices, stacks);
else glCallList(spherelist);
glPopMatrix();
}
glPopMatrix();
// Move the eye back to the middle
glMatrixMode(GL_PROJECTION);
glTranslatef(stereocameraoffset, 0., 0.);
}
int display(int advance)
{
static int first=1;
static double start=0., elapsed=0., mpixels=0.;
static unsigned long frames=0;
static char temps[256];
GLfloat xaspect, yaspect;
if(first)
{
GLfloat id4[]={1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light0_amb[]={0.3f, 0.3f, 0.3f, 1.0f};
GLfloat light0_dif[]={0.8f, 0.8f, 0.8f, 1.0f};
GLfloat light0_pos[]={1.0f, 1.0f, 1.0f, 0.0f};
spherelist=glGenLists(1);
if(!(spherequad=gluNewQuadric()))
_throw("Could not allocate GLU quadric object");
glNewList(spherelist, GL_COMPILE);
gluSphere(spherequad, 1.3, slices, stacks);
glEndList();
if(!locolor)
{
glMaterialfv(GL_FRONT, GL_AMBIENT, id4);
glMaterialfv(GL_FRONT, GL_DIFFUSE, id4);
glMaterialfv(GL_FRONT, GL_SPECULAR, id4);
glMaterialf(GL_FRONT, GL_SHININESS, 50.);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_dif);
glLightfv(GL_LIGHT0, GL_SPECULAR, id4);
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
if(locolor) glShadeModel(GL_FLAT);
else glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
fontlistbase=glGenLists(256+1);
if(!wglUseFontBitmaps(hdc, 0, 256, fontlistbase))
{
DWORD err=GetLastError();
fprintf(stderr, "WARNING: Cannot create font display lists\n");
if(err!=ERROR_SUCCESS && FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), __lasterror, 1024,
NULL))
fprintf(stderr, " %s", __lasterror);
}
snprintf(temps, 255, "Measuring performance ...");
first=0;
}
if(advance)
{
z-=0.5;
if(z<-29.) z=-3.5;
outer_angle+=0.1f; if(outer_angle>360.) outer_angle-=360.;
middle_angle-=0.37f; if(middle_angle<-360.) middle_angle+=360.;
inner_angle+=0.63f; if(inner_angle>360.) inner_angle-=360.;
lonesphere_color+=0.005f;
if(lonesphere_color>1.) lonesphere_color-=1.;
}
if(usestereo)
{
renderspheres(GL_BACK_LEFT); renderspheres(GL_BACK_RIGHT);
}
else renderspheres(GL_BACK);
glDrawBuffer(GL_BACK);
glPushAttrib(GL_CURRENT_BIT);
glPushAttrib(GL_LIST_BIT);
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
glColor3f(1., 1., 1.);
xaspect=(GLfloat)width/(GLfloat)(min(width, height));
yaspect=(GLfloat)height/(GLfloat)(min(width, height));
glRasterPos3f(-0.95f*xaspect, -0.95f*yaspect, -1.5f);
glListBase(fontlistbase);
glCallLists((int)strlen(temps), GL_UNSIGNED_BYTE, temps);
glPopAttrib();
glPopAttrib();
glPopAttrib();
SwapBuffers(hdc);
if(start>0.)
{
elapsed+=rrtime()-start; frames++; totalframes++;
mpixels+=(double)width*(double)height/1000000.;
if(elapsed>benchtime || (maxframes && totalframes>maxframes))
{
snprintf(temps, 255, "%f frames/sec - %f Mpixels/sec",
(double)frames/elapsed, mpixels/elapsed);
printf("%s\n", temps);
elapsed=mpixels=0.; frames=0;
}
}
if(maxframes && totalframes>maxframes) goto bailout;
start=rrtime();
return 0;
bailout:
if(spherequad) {gluDeleteQuadric(spherequad); spherequad=NULL;}
return -1;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch(iMsg)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_PAINT:
if(interactive) dodisplay=1;
return 0;
case WM_SIZE:
reshape(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_CHAR:
if(wParam==27 || wParam=='q' || wParam=='Q')
{
PostQuitMessage(0);
return 0;
}
break;
case WM_MOUSEMOVE:
if(wParam & MK_LBUTTON)
{
dodisplay=advance=1;
return 0;
}
break;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
void usage(char **argv)
{
printf("\nUSAGE: %s [options]\n\n", argv[0]);
printf("Options:\n");
printf("-fs = Full-screen mode\n");
printf("-i = Interactive mode. Frames advance in response to mouse movement\n");
printf("-l = Use fewer than 24 colors (to force non-JPEG encoding in TurboVNC)\n");
printf("-m = Use immediate mode rendering (default is display list)\n");
printf("-p <p> = Use (approximately) <p> polygons to render scene\n");
printf("-s = Use stereographic rendering initially\n");
printf(" (this can be switched on and off in the application)\n");
printf("-f <n> = max frames to render\n");
printf("-bt <t> = print benchmark results every <t> seconds (default=%.1f)\n",
DEFBENCHTIME);
printf("-w <wxh> = specify window width and height\n");
printf("-ic = Use indirect rendering context\n");
printf("\n");
exit(0);
}
int main(int argc, char **argv)
{
int i;
WNDCLASSEX wndclass; MSG msg;
int bw=GetSystemMetrics(SM_CXFRAME)*2;
int bh=GetSystemMetrics(SM_CYFRAME)*2+GetSystemMetrics(SM_CYCAPTION);
DWORD winstyle=WS_OVERLAPPEDWINDOW | WS_VISIBLE;
int pixelformat=0;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0,
PFD_MAIN_PLANE, 0, 0, 0, 0
};
int fullscreen=0, winwidth=width+bw, winheight=height+bh;
RECT rect;
for(i=0; i<argc; i++)
{
if(!strnicmp(argv[i], "-h", 2)) usage(argv);
if(!strnicmp(argv[i], "-?", 2)) usage(argv);
else if(!strnicmp(argv[i], "-i", 2)) interactive=1;
if(!strnicmp(argv[i], "-l", 2)) locolor=1;
if(!strnicmp(argv[i], "-m", 2)) useimm=1;
if(!strnicmp(argv[i], "-w", 2) && i<argc-1)
{
int w=0, h=0;
if(sscanf(argv[++i], "%dx%d", &w, &h)==2 && w>0 && h>0)
{
winwidth=w; winheight=h;
printf("Window dimensions: %d x %d\n", width, height);
}
}
if(!strnicmp(argv[i], "-p", 2) && i<argc-1)
{
int npolys=atoi(argv[++i]);
if(npolys>0)
{
slices=stacks=(int)(sqrt((double)npolys/((double)(3*NSPHERES+1))));
if(slices<1) slices=stacks=1;
}
}
if(!stricmp(argv[i], "-fs"))
{
winwidth=GetSystemMetrics(SM_CXSCREEN);
winheight=GetSystemMetrics(SM_CYSCREEN);
winstyle=WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE;
fullscreen=1;
}
else if(!strnicmp(argv[i], "-f", 2) && i<argc-1)
{
int mf=atoi(argv[++i]);
if(mf>0)
{
maxframes=mf;
printf("Number of frames to render: %d\n", maxframes);
}
}
if(!strnicmp(argv[i], "-bt", 3) && i<argc-1)
{
double temp=atof(argv[++i]);
if(temp>0.0) benchtime=temp;
}
if(!strnicmp(argv[i], "-s", 2))
{
pfd.dwFlags|=PFD_STEREO;
usestereo=1;
}
}
fprintf(stderr, "Polygons in scene: %d\n", (NSPHERES*3+1)*slices*stacks);
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = GetModuleHandle(NULL);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "WGLspheres";
wndclass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if(!RegisterClassEx(&wndclass)) _throww32("Cannot create window class");
if(!(win=CreateWindowEx(0, "WGLspheres", "WGLspheres", winstyle, 0, 0,
winwidth, winheight, NULL, NULL, GetModuleHandle(NULL), NULL)))
_throww32("Cannot create window");
GetClientRect(win, &rect);
fprintf(stderr, "Client area of window: %d x %d pixels\n",
rect.right-rect.left, rect.bottom-rect.top);
if(!(hdc=GetDC(win))) _throww32("Cannot create device context");
if(!(pixelformat=ChoosePixelFormat(hdc, &pfd)))
_throww32("Cannot create pixel format");
fprintf(stderr, "Pixel Format of window: %d\n", pixelformat);
if(!SetPixelFormat(hdc, pixelformat, &pfd))
_throww32("Cannot set pixel format");
if(!(ctx=wglCreateContext(hdc))) _throww32("Cannot create OpenGL context");
if(!wglMakeCurrent(hdc, ctx))
_throww32("Cannot make OpenGL context current");
fprintf(stderr, "OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
while(1)
{
BOOL ret;
advance=0; dodisplay=0;
if((ret=GetMessage(&msg, NULL, 0, 0))==-1)
{_throww32("GetMessage() failed");}
else if(ret==0) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
if(!interactive) {_catch(display(1));}
else {if(dodisplay) {_catch(display(advance));}}
}
if(ctx) {wglDeleteContext(ctx); ctx=0;}
if(hdc) {ReleaseDC(win, hdc); hdc=0;}
if(win) {DestroyWindow(win); win=0;}
return msg.wParam;
bailout:
if(ctx) {wglDeleteContext(ctx); ctx=0;}
if(hdc) {ReleaseDC(win, hdc); hdc=0;}
if(win) {DestroyWindow(win); win=0;}
return -1;
}