distance *= cos ( phi - angle ); // adjustment for fish-eye drawSpan ( distance, WALL_COLOR, col);
}
}
void setVideoMode (int mode ) {
asm {
mov ax, mode int 10h
}
}
void initTables () {
for (int i = 0; i < SCREEN_WIDTH; i++ )
rayAngle [i] = atan (-swing + 2 * i * swing / ( SCREEN_WIDTH -1 ));
}
main () {
int done = 0;
angle = 0; locX =1.5; locY = 1.5;
swing = tan ( VIEW_WIDTH / 2 );
initTables (); setVideoMode (0x13 );
int start = clock ();
while (Idone ) {
drawView ();
if ( bioskey ( 1 )) {
float vx = cos ( angle ); float vy = .sin ( angle ); float x, y;
13. Элементы виртуальной реально
switch ( bioskey ( 0 )) {
case LEFT:
angle -= 5.0*M_PI/180.0; break;
case RIGHT:
angle +=5.0*M_PI/180.0; break;
case UP:
x = locX + 0.3 * vx; у = locY + 0.3 * vy;
if ( worldMap [(int) y][(int) x] == " ) {
locX = x; locY = y;
}
break;
case DOWN:
x = locX-0.3 *'vx; у = locY - 0.3 * vy;
if ( worldMap [(int) y][(int) x ] == " ) {
locX = x; locY = y;
}
break;
case ESC: done = 1;
}
if ( angle < 0 )
angle += 2 * M_PI;
else
if ( angle >= 2 * M_PI) angle-= 2 * M_PI;
}
}
float totalTime = ( clock () - start) / CLK_TCK; setVideoMode ( 0x03 );
printf ("\nFrames rendered : %ld", totalFrames );
printf ("\nTotal time ( sec ) : %7.2f\ totalTime );
printf ("\nFPS : %7.2f\ totalFrames / totalTime );
}
Для получения каркасного изображения приведенную программу несложно Дифицировать.
El // File wolf2.cpp
Компьютерная графика. Полигональные модели
#include <bios.h> #inclucfe <dos.h> #include <math.h> #include <stdio.h> #include <time.h>
#define VIEW_WIDTH ( M_PI / 3 ) // viewing angle ( 60 degrees )
#define SCREEN_WIDTH 320 // size of rendering window
#define SCREENJHEIGHT 200 // basic colors
#define FLOOR_COLOR 0 #define CEILING_COLOR 0 #define WALL_COLOR 1
// angles
#define ANGLE_90 M_PI_2 //90 degrees
#define ANGLE J 80 M_PI //180 degrees
#define ANGLE_270 (M_PI*1.5) // 270 degrees
// bios key defintions
#define ESC 0x011b