home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
3D Madness! Companion CD
/
3DMADNESS.iso
/
dosshow
/
zero
/
view3d
/
view3d.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-04
|
18KB
|
561 lines
/*-------------------------------------------------------------------------*
* VIEW3D.C : general viewer for zero objects
*
* (c) 1993 ZeRO Computing, all rights reserved
*
*-------------------------------------------------------------------------*/
#define RHO_MIN_LIM 1
#define RHO_MAX_LIM 179
/*--- Include files ---*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <conio.h>
/*--- ZeRO 3DLIB include file ---*/
#include "zbnt.h"
#include "ms_key.h"
extern int trg2scheda;
/*-------------------------------------------------------------------------*
* main : view code
*-------------------------------------------------------------------------*/
void main( int argc, char *argv[] )
{
MODEL3D_LIST *list;
H_VPORT vp3d, vp2d1, vp2d2;
H_OBJ3D *hObj3D;
H_OBJ2D hHelp;
H_MDL2D hModelFrame,hMHelp;
OBJ_INFO obj;
H_FONT hFont;
GRAPHADAPTER graph;
KBDCODE keyboard;
float px, py, pz, sq;
float theta, rho, radius, min_radius, step;
float max_x, min_x, max_y, min_y, max_z, min_z, centre_x, centre_y, centre_z;
float par_red = 0.5, par_green = 0.5, par_blue=0.5, ambient = 0.2;
double seconds, tri_per_second, frames_per_second, drawn_per_second;
char light, rotatex, rotatey, rotatez, first, info;
char str[80], end;
short dx, dy, x, y, s1, mouse;
int models, j, num_triangles = 0, frames = 0, tot_drawn = 0, tot_tri = 0, off = 5;
int bit = 4;
int help , help_x = 0, help_y = 0;
clock_t start, elapsed_time;
mouse = InitMouse();
DisableMouseCursor();
DefineMouseSensitivity( 8, 16 );
if ( argc == 1 )
{
printf("usage: view3D object_filename [ palette_filename ] \n");
exit(-1);
}
/**********************************************/
/* Load 3D & 2D WORLD FILES and PALETTE FILES */
/**********************************************/
printf("Loading Palette...\n");
if ( argc == 3 )
LoadPalette( argv[ 2 ] );
else
LoadPalette( "vga.pal" );
printf("Loading Objects...\n");
if ( ! ( LoadWorld( argv[ 1 ] ) ) )
exit ( -1 );
if ( ! ( LoadWorld( "data2d.wrd") ) )
exit ( -1 );
printf("Loading Font...\n");
if ( ( hFont = LoadFont( "font.fnt") ) == NULL )
exit( -1 );
/***************************************************/
/* Get List of 3D Models Loaded */
/* Creates array of Handle for Initialized Objects.*/
/* Get Handle for 2D Objects */
/***************************************************/
models = Get3DModelList( &list );
if ( ( hObj3D = ( H_OBJ3D *) malloc( models * sizeof( H_OBJ3D ) ) ) == NULL )
{
printf("Not enough memory for Model List.\n");
exit(-1);
}
if ( ( hModelFrame = GetModel2DHandle("frame") ) == NULL )
{
printf("Error: 2D Model not present\n");
exit( -1 );
}
if ( ( hMHelp = GetModel2DHandle("help") ) == NULL )
{
printf("Error: 2D Model not present\n");
exit( -1 );
}
/*****************************************/
/* Init Graphics Adapter and Set Palette */
/*****************************************/
InitGraphics( &graph );
SetPalette();
/******************************/
/* Sets Frame Rate to Maximum */
/******************************/
SetMaxFrameRate( 0 );
/****************************************/
/* Creates Viewport: */
/* DIRECT_2D : Title Bar */
/* MAIN_3D : Main3D Viewport */
/* OVERLAY_3D : Overlay for Help Screen */
/****************************************/
vp2d1 = CreateViewport( DIRECT_2D, 0 , 0, 320, 200, 0 );
vp3d = CreateViewport( MAIN_3D , 0, 30, 320, 170, 0 );
vp2d2 = CreateViewport( OVERLAY_3D , vp3d );
/******************************************************/
/* Draws Frame box in DIRECT_2D Viewport */
/******************************************************/
DrawObj2D( vp2d1 , hModelFrame, 0.0 , 0.0 );
/***************************************************************************/
/* Initializes Help Object in OVERLAY_3D Viewport and hides it immediately */
/***************************************************************************/
if ( ( hHelp = InitObj2D( vp2d2 , hMHelp , help_x , help_y ) ) == NULL )
{
printf("Error InitObj2D\n");
exit( -1 );
}
HideObj2D( hHelp );
/********************************************************/
/* Sets Ambient and Parallel Light for MAIN_3D Viewport */
/********************************************************/
SetAmbientLight( vp3d, ambient, ambient, ambient );
SetParallelSource( vp3d, 0.5, 0.5, 0.5, -1.0, -2.0, -3.0 );
min_x = min_y = min_z = 1e06;
max_x = max_y = max_z = -1e06;
/************************************************************/
/* Initializes all models into MAIN_3D Viewport. */
/* Sets Object Shading type to FLAT */
/* Calcs max dimension and centers the object on the screen */
/************************************************************/
for ( j = 0 ; j < models ; j++ )
{
if ( ( hObj3D[j] = InitObj3D( vp3d, list[j].h_mdl3d, 0.0, 0.0, 0.0 ) ) == NULL )
{
printf("Mode name not correct.");
exit(-1);
}
SetObj3DShadingType( hObj3D[j], FLAT );
GetObj3DInfo( hObj3D[j] , &obj );
num_triangles += obj.n_triangles;
if ( ( obj.alfa_x + obj.centre_x ) > max_x )
max_x = ( obj.alfa_x + obj.centre_x );
if ( ( obj.beta_y + obj.centre_y ) > max_y )
max_y = ( obj.beta_y + obj.centre_y );
if ( ( obj.gamma_z + obj.centre_z ) > max_z )
max_z = ( obj.gamma_z + obj.centre_z );
if ( ( -obj.alfa_x + obj.centre_x ) < min_x )
min_x = ( -obj.alfa_x + obj.centre_x );
if ( ( -obj.beta_y + obj.centre_y ) < min_y )
min_y = ( -obj.beta_y + obj.centre_y );
if ( ( -obj.gamma_z + obj.centre_z ) < min_z )
min_z = ( -obj.gamma_z + obj.centre_z );
}
centre_x = ( max_x + min_x ) / 2;
centre_y = ( max_y + min_y ) / 2;
centre_z = ( max_z + min_z ) / 2;
for ( j=0 ; j<models; j++)
TranslateObj3D( hObj3D[j] , -centre_x , -centre_y, -centre_z );
min_radius = 3 * max3( ( max_x - min_x ) / 2, ( max_y - min_y ) / 2, ( max_z - min_z ) / 2 );
radius = 4 * min_radius;
/************************/
/* Sets Viewing Optics */
/************************/
SetOptics( vp3d , 1.0, 3.0, radius * 300 );
/******************/
/* Init Variables */
/******************/
step = radius / 100;
theta = rho = 0.0;
light = first = TRUE;
info = rotatex = rotatey = rotatez = FALSE;
start = clock();
end = FALSE;
help = FALSE;
while ( ! end )
{
/***********************************/
/* Checks MOUSE and KEYBOARD Input */
/***********************************/
if ( mouse )
{
ReadMouseMotionNumber( &dx, &dy );
ReadMouseCursorPos( &x, &y, &s1 );
if ( s1 == 0 )
{
theta += (float) dx * 0.2;
rho += (float) dy * 0.2;
}
else
if ( s1 == 2 )
{
theta += (float) dx * 0.04;
rho += (float) dy * 0.04;
}
else
if ( s1 == 1 )
{
radius += (float) ( dy * ( step / 2.0 ) );
if ( radius < min_radius )
radius = min_radius;
}
}
while ( KeyBoardReady() )
{
ReadKeyBoard( &keyboard );
switch ( keyboard.ascii )
{
case 0:
switch ( keyboard.scan )
{
case 45:
end = TRUE;
break;
case 72: /* up arrow */
rho -= 0.5;
break;
case 80: /* down arrow */
rho += 0.5;
break;
case 75: /* left arrow */
theta -= 0.5;
break;
case 77: /* right arrow */
theta += 0.5;
break;
case 141:
rho -= 3.0;
break;
case 145:
rho += 3.0;
break;
case 116:
theta += 3.0;
break;
case 115:
theta -= 3.0;
break;
case 144:
radius -= ( 10 * step);
if ( radius < min_radius )
radius = min_radius;
break;
case 142:
radius += ( 10 * step );
break;
case 59:
par_green -= 0.05;
if ( par_green < 0.0 )
par_green = 0.0;
break;
case 60:
par_green += 0.05;
if ( par_green > 1.0 )
par_green = 1.0;
break;
case 61:
ambient -=0.05;
if ( ambient< 0.0 )
ambient = 0.0;
SetAmbientLight( vp3d, ambient, ambient, ambient );
break;
case 62:
ambient +=0.05;
if ( ambient > 1.0 )
ambient = 1.0;
SetAmbientLight( vp3d, ambient, ambient, ambient );
break;
case 152:
if ( help )
{
help_y -= 5;
if ( help_y < 0 )
help_y = 0;
PositionObj2D( hHelp, help_x , help_y );
}
break;
case 160:
if ( help )
{
help_y += 5;
if ( help_y > 320 )
help_y = 320;
PositionObj2D( hHelp, help_x , help_y );
}
break;
case 155:
if ( help )
{
help_x -= 5;
if ( help_x < 0 )
help_x = 0;
PositionObj2D( hHelp, help_x , help_y );
}
break;
case 157:
if ( help )
{
help_x += 5;
if ( help_x > 320 )
help_x = 320;
PositionObj2D( hHelp, help_x , help_y );
}
break;
}
break;
case '+':
radius -= step;
if ( radius < min_radius )
radius = min_radius;
break;
case '-':
radius += step;
break;
case '8':
rho -= 0.5;
break;
case '2':
rho += 0.5;
break;
case '4':
theta -= 0.5;
break;
case '6':
theta += 0.5;
break;
case 'l':
case 'L':
light = !light;
break;
case 'h':
case 'H':
help = !help;
if ( help )
UnHideObj2D( hHelp );
else
HideObj2D( hHelp );
break;
/**************************************************************/
/* The red & blue light components work only with 24bit color */
/**************************************************************/
case 'q':
case 'Q':
par_red -= 0.05;
if ( par_red < 0.0 )
par_red = 0.0;
break;
case 'w':
case 'W':
par_red += 0.05;
if ( par_red > 1.0 )
par_red = 1.0;
break;
case 'z':
case 'Z':
par_blue -= 0.05;
if ( par_blue < 0.0 )
par_blue = 0.0;
break;
case 'x':
case 'X':
par_blue += 0.05;
if ( par_blue > 1.0 )
par_blue = 1.0;
break;
}
}
/****************************/
/* avoids flipping over top */
/****************************/
if ( rho < RHO_MIN_LIM )
rho = RHO_MIN_LIM;
else
if ( rho > RHO_MAX_LIM )
rho = RHO_MAX_LIM;
/*****************************************/
/* Calculates new Point of View Position */
/*****************************************/
pz = radius * cos ( DEGtoRAD(rho) );
py = radius * sin ( DEGtoRAD(rho) ) * cos( DEGtoRAD(theta) );
px = radius * sin ( DEGtoRAD(rho) ) * sin( DEGtoRAD(theta) );
sq = sqrt( px * px + py * py + pz * pz );
MovePw( vp3d , px, py, pz, - px / sq , -py / sq , - pz / sq, 0.0 );
/*******************************************/
/* If Parallel Light is enabled, update it */
/*******************************************/
if ( light )
SetParallelSource( vp3d , par_red,par_green,par_blue, -px, -py, -pz );
/*******************/
/* Draws New Frame */
/*******************/
DrawFrame( TRUE );
frames++;
tot_tri += num_triangles;
tot_drawn += trg2scheda;
elapsed_time = clock();
/********************************************/
/* Draw Information onto DIRECT_2D Viewport */
/********************************************/
if ( ( ! ( frames % bit ) ) || ( first ) )
{
seconds = ( ( double ) ( elapsed_time - start ) / ( double ) CLK_TCK );
tri_per_second = tot_tri / seconds;
frames_per_second = frames / seconds;
drawn_per_second = tot_drawn / seconds;
DrawObj2D( vp2d1 , hModelFrame, 0.0 , 0.0 );
sprintf(str , " *** ZeRO 3D VIEWER v1.2 ***" );
DrawText( vp2d1 , hFont , str , 10 + off , 2 , 255 , 255, 255 );
sprintf(str , "(c) 1993 ZeRO Computing S.a.s.");
DrawText( vp2d1 , hFont , str , 150 + off, 2 , 255 , 255 , 255 );
sprintf(str , " Trg/sec: %6d", (int) tri_per_second );
DrawText( vp2d1 , hFont , str , 0 + off, 14 , 255 , 255 , 255 );
sprintf(str , " Frames/sec: %5.2f", frames_per_second );
DrawText( vp2d1 , hFont , str , 100 + off, 14 , 255 , 255 , 255 );
sprintf(str , " Drawn/sec: %5d", (int) drawn_per_second );
DrawText( vp2d1 , hFont , str , 200 + off, 14 , 255 , 255 , 255 );
if ( light )
DrawText( vp2d1, hFont , "*", 303 + off, 2 , 255 , 255 , 255 );
bit = (int) frames_per_second;
if ( bit == 0 )
bit = 1;
tot_tri = 0;
tot_drawn = 0;
frames = 0;
start = clock();
first = FALSE;
}
}
MyExit(1);
puts("╔═════════════════════════════════════════════════════════════════════════════╗");
puts("║ For more information about the ZeRO Computing 3D real-time libraries ║");
puts("║ please contact : ║");
puts("║ ZeRO Computing S.a.s. ║");
puts("║ via T. Cremona, 29 ║");
puts("║ 20145 Milano - ITALY ║");
puts("║ Phone: +39.2.48017946 ║");
puts("║ FAX: +39.2.58114647 ║");
puts("║ Internet : zero@bench.sublink.ORG ║");
puts("║ BIX : ppennisi ║");
puts("║ CIS : 100022,411 ║");
puts("║ DELPHI : MSANTOLI ║");
puts("╚═════════════════════════════════════════════════════════════════════════════╝");
printf("\n\n");
} /*--- end of main ---*/