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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
|
/* Copyright (c) Mark J. Kilgard, 1996. */
/* This program is freely distributable without licensing fees and is
provided without guarantee or warrantee expressed or implied. This
program is -not- in the public domain. */
/* This demo shows off InfiniteReality's "dynamic video resize" (DVR)
capability. Dynamic video resizing let's you maintain a constant frame
rate even when the view becomes limited by your hardware's pixel fill
rate. The idea is simple: Draw the fill-limited frame into a smaller
area (ie, touch less pixels) and then have the video hardware "zoom" up
the image to fill the video screen. This trick does require special
hardware.
As written with no command line options, this demo works well on a 1RM
InfiniteReality. It keeps a scene containing colored large orbs that
rotate around the viewer at a constant 60 frames/sec, even though the
number of viewable orbs is changing (hence the required fullscreen pixel
fill rate is changing too). The demo adapts the rendered image size and
video resizing based on the last frame to keep a constant frame rate. */
/****************************************************************
Command line arguments:
-window ... run in a window instead of full screen and will not use
hardware resizing even if the hardware supports it. Good for
demos.
-novidresize ... start without using video resize hardware even though
the system might really support video resizing.
-debug ... output frame size to stdout.
-target # ... sets the target frame rate in increments of 60th of
a second; the default is 1.
-twosnaps ... sample the last frame rate as the average of the last
two frames; the default is to simply use the last frame.
-nice ... better tesselate the orbs; nice if you have transformation
rate to burn (ie, use an InfiniteReality).
-orbs # ... an initial number of orbs to populate the viewing space;
the default is 80.
Example command lines:
Because this program's behavior depends on adapting to the speed of the
system it is running at, a good demonstration of this program may depend
on the system performance. Adjust the command line options accordingly,
particularly -target and -orbs.
InfiniteReality (1RM) ... "videoresize"
(For the hardware listed below, you won't get actual video resizing
since the hardware lacks the support.)
Indigo^2 XL 200Mhz ... "videoresize -geometry 800x600 -target 5 -orbs 40 -window"
Key controls:
ESC or q ... exit the demo.
n ... disable dynamic video resizing.
r ... enable dynamic video resizing (the default if hardware video
resizing is detected; otherwise no video resizing is the
default).
NOTE: you can enable dynamic video resizing even without the
hardware and the demo will show you how the rendered area would
vary as if the feature were supported.
h ... toggle use of hardware video resizing (only if in fullscreen
mode and the hardware supports the feature).
m ... toggle display of the frame meter. Enabled by default.
c ... toggle display of the cursor. Sometimes it is nice to see the
cursor when video resizing so that you can see the effect of the
video resizing since the cursor resizes too, but in general a
resizing cursor is quite distracting. The cursor is enabled if
hardware video resizing is to be used.
Up arrow ... add 10 more orbs.
Down arrow ... subtract 10 less orbs.
Spacebar ... regenerate the set of viewing orbs.
Right arrow ... increase rightward rotation.
Left arrow ... increase rightward rotation.
Frame meter:
The frame meter shows how long each frame takes to render. It
operates differently depending on if video resizing is being
demonstrated or not.
The meter line is BLUE when video resizing is not being demonstrated.
When video resizing is being demonstrated, a GREEN meter line
indicates the frame is being shown at full (non-resized) resolution.
YELLOW indicates the frame is being video resized. RED indicates the
frame is being resized so much that dropping a frame is better than
the resulting poor resize quality (the demo won't zoom anymore than 8
to 1). A smaller MAGENTA line shows a calculation of the
"unadjusted" frame rate for the displayed scene. When the MAGENTA
line is longer than the base line, it is showing a relative measure
of how much resizing was needed to keep the frame at a sustained
frame rate.
Bugs:
This program assumes a frame rate of 60 Hz.
This program should use InfiniteReality's SGIX_instruments
extension for better fill rate measurement.
*****************************************************************/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#define OVERLOAD 0
#define ZOOMED 1
#define UNZOOMED 2
#define STATIC 3
GLfloat state_colors[4][3] =
{
{1.0, 0.0, 0.0},
{0.8, 0.8, 0.0},
{0.0, 0.7, 0.0},
{0.3, 0.3, 0.8},
};
int state;
extern void sphere(int level);
GLdouble angle, speed = 0.5;
GLfloat light0_ambient[] =
{0.1, 0.1, 0.1, 1.0};
GLfloat light0_diffuse[] =
{0.5, 0.5, 0.5, 1.0};
GLfloat light0_position[] =
{0.0, 0.0, 0.0, 1.0};
typedef struct {
GLfloat offset;
GLfloat distance;
GLfloat size;
GLfloat *color;
} Orb;
Orb *orbs = NULL;
int num_orbs = 80; /* Good default for 1 RM InfiniteReality. */
int fullscreen = 1;
int debug = 0;
int video_resizing = 1;
int hw_video_resizing = 1; /* Assume we have it until told otherwise. */
int hw_exists = 0;
int frame_time = 15;
int sphere_quality = 1;
int max_w, max_h;
GLfloat W, H;
int delta_w_resize = 1, delta_h_resize = 1;
GLfloat target_frame_time = 1000.0 / 60.0;
int show_cursor = 1, show_meter = 1;
#define NUM_SNAPS 2
int snap[NUM_SNAPS] =
{0};
int num_snaps = 1;
int frame = 0;
GLfloat color[][3] =
{
{0.0, 0.5, 0.0},
{0.5, 0.5, 0.0},
{1.0, 0.0, 0.5},
{0.0, 1.0, 0.5},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.0, 0.0, 0.8},
{0.0, 0.2, 0.5},
{1.0, 1.0, 0.0},
{1.0, 1.0, 1.0},
{0.3, 0.3, 0.3},
};
int num_colors = sizeof(color) / (sizeof(GLfloat) * 3);
void
calculate_frame_rate(int delta)
{
int sum, count, i;
snap[frame] = delta;
frame += 1;
frame %= num_snaps;
sum = 0;
count = 0;
for (i = 0; i < num_snaps; i++) {
if (snap[i] != 0) {
sum += snap[i];
count++;
}
}
frame_time = sum / count;
}
void
advancedAnimation(void)
{
angle = (GLfloat) fmod(angle + speed, 360.0);
glutPostRedisplay();
}
void
resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, (GLfloat) w / (GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void
reshape(int w, int h)
{
max_w = w;
max_h = h;
W = w;
H = h;
resize(w, h);
}
void
do_video_resize_logic(void)
{
GLfloat frame_ratio, adjusted_area, dimension_scale;
GLfloat area;
int Wint, Hint;
if (frame_time > (target_frame_time * 0.92)) {
/* Shrink case. */
frame_ratio = (target_frame_time * 0.92) / frame_time;
area = W * H;
adjusted_area = area * frame_ratio;
dimension_scale = sqrt(adjusted_area) / sqrt(area);
W = dimension_scale * W;
H = dimension_scale * H;
if (W < max_w / 8 || H < max_h / 8) {
W = max_w / 8;
H = max_h / 8;
state = OVERLOAD;
} else {
state = ZOOMED;
}
Wint = (((int) W) / delta_w_resize) * delta_w_resize;
Hint = (((int) H) / delta_h_resize) * delta_h_resize;
if (video_resizing) {
if (hw_video_resizing)
glutVideoResize(0, 0, -Wint, -Hint);
resize(Wint, Hint);
}
} else if (frame_time < (target_frame_time * 0.82)) {
/* Grow case. */
frame_ratio = (target_frame_time * 0.82) / frame_time;
area = W * H;
adjusted_area = area * frame_ratio;
dimension_scale = sqrt(adjusted_area) / sqrt(area);
/* Be a bit conservative about our growth. As Allan Greenspan would say,
"You don't want to overheat the economy; put on the brakes." */
dimension_scale = (dimension_scale - 1.0) * 0.75 + 1.0;
if (dimension_scale > 1.5) {
dimension_scale = 1.5;
}
W = dimension_scale * W;
H = dimension_scale * H;
if (W > max_w || H > max_h) {
W = max_w;
H = max_h;
state = UNZOOMED;
} else {
state = ZOOMED;
}
Wint = (((int) W) / delta_w_resize) * delta_w_resize;
Hint = (((int) H) / delta_h_resize) * delta_h_resize;
if (video_resizing) {
if (hw_video_resizing)
glutVideoResize(0, 0, -Wint, -Hint);
resize(Wint, Hint);
}
} else {
if (!hw_video_resizing) {
/* We keep the meter constant size, so we must keep restoring our
viewport. */
Wint = (((int) W) / delta_w_resize) * delta_w_resize;
Hint = (((int) H) / delta_h_resize) * delta_h_resize;
glViewport(0, 0, Wint, Hint);
}
}
if (debug)
printf("%gx%g\n\n", W, H);
}
void
display(void)
{
int i, start, stop;
if (video_resizing)
do_video_resize_logic();
start = glutGet(GLUT_ELAPSED_TIME);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 1.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -1.0);
for (i = 0; i < num_orbs; i++) {
glPushMatrix();
glRotatef(angle + orbs[i].offset, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, orbs[i].distance);
glScalef(orbs[i].size, orbs[i].size, orbs[i].size);
glColor3fv(orbs[i].color);
glCallList(1);
glPopMatrix();
}
if (show_meter) {
if (!hw_video_resizing) {
/* Make sure the meter stays constant size in window mode. */
glViewport(0, 0, max_w, max_h);
}
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 3000, 0, 3000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glColor3fv(state_colors[state]);
glRecti(100, 100, 100 + frame_time * 8, 200);
if (video_resizing) {
float render_area = W * H, full_area = max_w * max_h;
float advantage = sqrt(full_area / render_area);
glColor3f(1.0, 0.0, 1.0);
glRecti(100, 140, 100 + frame_time * advantage * 8, 160);
}
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
for (i = 0; i < 21; i++) {
glVertex2f(100 + i * 16.6 * 8, 80);
glVertex2f(100 + i * 16.6 * 8, 220);
}
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
glFinish();
stop = glutGet(GLUT_ELAPSED_TIME);
glutSwapBuffers();
calculate_frame_rate(stop - start);
}
void
visible(int vis)
{
if (vis == GLUT_VISIBLE)
glutIdleFunc(advancedAnimation);
else
glutIdleFunc(NULL);
}
void
set_cursor(int on)
{
if (on) {
glutSetCursor(GLUT_CURSOR_INHERIT);
} else {
glutSetCursor(GLUT_CURSOR_NONE);
}
}
void
generate_orbs(void)
{
int i, r, angle, chance, delta, d;
if (orbs)
free(orbs);
orbs = (Orb *) malloc(num_orbs * sizeof(Orb));
i = 0;
angle = 0;
while (i < num_orbs) {
angle += 37;
angle %= 360;
r = (int) lrand48();
chance = abs(r % 360 - 180);
delta = chance - abs(angle - 180);
if (delta > 0) {
orbs[i].color = color[lrand48() % num_colors];
orbs[i].offset = angle;
d = (r % 20) + (delta / 180.0) * 10;
orbs[i].distance = d + 3.0;
orbs[i].size = 1.4 * exp(d / 8.0) / exp(1.0);
i++;
}
}
}
/* ARGSUSED */
void
keyboard(unsigned char k, int x, int y)
{
switch (k) {
case 27:
case 'q':
case 'Q':
exit(0);
break;
case 'R':
case 'r':
video_resizing = 1;
break;
case 'H':
case 'h':
if (hw_exists) {
hw_video_resizing = 1 - hw_video_resizing;
if (!hw_video_resizing) {
glutVideoResize(0, 0, max_w, max_h);
resize(max_w, max_h);
}
}
break;
case 'N':
case 'n':
video_resizing = 0;
if (hw_video_resizing)
glutVideoResize(0, 0, max_w, max_h);
resize(max_w, max_h);
state = STATIC;
break;
case 'D':
case 'd':
debug = 1;
break;
case 'M':
case 'm':
show_meter = 1 - show_meter;
break;
case ' ':
generate_orbs();
break;
case 'C':
case 'c':
show_cursor = 1 - show_cursor;
set_cursor(show_cursor);
break;
}
}
/* ARGSUSED */
void
special(int k, int x, int y)
{
switch (k) {
case GLUT_KEY_RIGHT:
speed -= 0.25;
break;
case GLUT_KEY_LEFT:
speed += 0.25;
break;
case GLUT_KEY_UP:
num_orbs += 10;
generate_orbs();
break;
case GLUT_KEY_DOWN:
num_orbs -= 10;
generate_orbs();
break;
}
}
int
main(int argc, char **argv)
{
int i;
glutInit(&argc, argv);
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-orbs")) {
i++;
if (i >= argc) {
fprintf(stderr, "videoresize: need number of orbs\n");
exit(1);
}
num_orbs = atoi(argv[i]);
if (num_orbs < 1) {
fprintf(stderr, "videoresize: number of orbs must be 1 or more\n");
exit(1);
}
} else if (!strcmp(argv[i], "-target")) {
i++;
if (i >= argc) {
fprintf(stderr, "videoresize: need target number of frames\n");
exit(1);
}
target_frame_time = atoi(argv[i]) * 1000.0 / 60.0;
if (target_frame_time <= 0.0) {
fprintf(stderr, "videoresize: target frames must be 1 or more\n");
exit(1);
}
} else if (!strcmp(argv[i], "-novidresize")) {
hw_video_resizing = 0;
} else if (!strcmp(argv[i], "-debug")) {
debug = 1;
} else if (!strcmp(argv[i], "-window")) {
fullscreen = 0;
} else if (!strcmp(argv[i], "-twosnaps")) {
num_snaps = 2;
} else if (!strcmp(argv[i], "-nice")) {
sphere_quality = 2;
} else {
printf("usage: videoresize [-window] [-twosnaps] [-nice] [-target #] [-orbs #]\n");
printf(" -target # = target number of frame intervals to stay under (default=%g)\n", target_frame_time);
printf(" -orbs # = number of orbs to randomly position (default=%d)\n", num_orbs);
printf(" -window = do not go fullscreen\n");
printf(" -nice = use better tesselated spheres\n");
printf(" -novidresize = even if videoresizing is supported, don't use it\n");
printf(" -twosnaps = averages over last two frames instead of just estimating\n");
printf(" based on the last frame\n");
exit(1);
}
}
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("Dynamic video resizing for constant frame rates");
if (fullscreen) {
glutFullScreen();
if (glutVideoResizeGet(GLUT_VIDEO_RESIZE_POSSIBLE)) {
delta_w_resize = glutVideoResizeGet(GLUT_VIDEO_RESIZE_WIDTH_DELTA);
delta_h_resize = glutVideoResizeGet(GLUT_VIDEO_RESIZE_HEIGHT_DELTA);
show_cursor = 0;
glutSetupVideoResizing();
hw_exists = 1;
} else {
hw_video_resizing = 0;
}
} else {
hw_video_resizing = 0;
}
if (video_resizing) {
state = UNZOOMED;
} else {
state = STATIC;
}
glutReshapeFunc(reshape);
glDisable(GL_CULL_FACE); /* Makes us more fill limited. */
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glNewList(1, GL_COMPILE);
sphere(sphere_quality);
glEndList();
srand48(getpid() * 16 + glutGet(GLUT_ELAPSED_TIME));
generate_orbs();
glutDisplayFunc(display);
glutVisibilityFunc(visible);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
set_cursor(show_cursor);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|