home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
3D Madness! Companion CD
/
3DMADNESS.iso
/
dosshow
/
zero
/
wave3d
/
wave3d.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-05
|
22KB
|
664 lines
/*-------------------------------------------------------------------------*
* WAVE3D.C : sample animation demo
*
* (c) 1993 ZeRO Computing S.a.s, all rights reserved
*
*-------------------------------------------------------------------------*/
#define RHO_MIN_LIM 1
#define RHO_MAX_LIM 88
/*--- 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"
float zvalues[2600];
/*-------------------------------------------------------------------------*
* main
*-------------------------------------------------------------------------*/
void main( void )
{
H_OBJ3D hObj3D, hObjLight;
H_MDL3D hModel3D,hModelLight;
H_OBJ2D hHelp;
H_MDL2D hBitmap, hMHelp, hTextOver;
OBJ_INFO obj;
H_VPORT vp3d,vp2d1,vp2d2;
SPOT_LIGHT spot;
H_SPOTL hSpot;
H_FONT hFont;
GRAPHADAPTER hardware_info;
KBDCODE keyboard;
float px, py, pz, sq, i;
float tt;
int play, kk;
float theta, rho, radius, min_radius, step, parallel, ambient = 0.2;
float lx , ly , lz;
short dx, dy, x, y, s1, mouse;
float max_x, min_x, max_y, min_y, max_z, min_z, centre_x, centre_y, centre_z;
float light_radius, angle, animation_step, seconds, frames_per_second;
float spot_int;
char end,light = TRUE, spot_on = TRUE, str[80], animate, over;
clock_t start, elapsed_time;
int bit=4, first, frames = 0, off = 5;
int help , help_x = 0, help_y = 0;
mouse = InitMouse();
DisableMouseCursor();
DefineMouseSensitivity( 8,16 );
/**********************************************/
/* Load 3D & 2D WORLD FILES and PALETTE FILES */
/**********************************************/
printf("Loading World Data...\n");
LoadWorld( "wave.wrd" );
LoadPalette( "wave.pal" );
if ( ! ( LoadWorld( "wave2d.wrd") ) )
exit ( -1 );
printf("Loading Font...\n");
if ( ( hFont = LoadFont( "font.fnt") ) == NULL )
exit( -1 );
/************************************************************************/
/* Get Model Handles. */
/* All Object are initialized into the world trough their MODEL HANDLES */
/************************************************************************/
if ( ( hModel3D = GetModel3DHandle("flatzoll") ) == NULL )
{
printf("Error: 3D Model not present\n");
exit( -1 );
}
if ( ( hModelLight = GetModel3DHandle("sph10") ) == NULL )
{
printf("Error: 3D Model not present\n");
exit( -1 );
}
if ( ( hBitmap = 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 );
}
if ( ( hTextOver = GetModel2DHandle("text_over") ) == NULL )
{
printf("Error: 2D Model not present\n");
exit( -1 );
}
/*****************************************/
/* Init Graphics Adapter and Set Palette */
/*****************************************/
InitGraphics( &hardware_info );
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 );
/******************************************************/
/* Disable eventual Object Merging in Main3D Viewport */
/******************************************************/
SetMerging( vp3d , FALSE );
/******************************************************/
/* Draws Frame box in DIRECT_2D Viewport */
/******************************************************/
DrawObj2D( vp2d1 , hBitmap , 0.0 , 0.0 );
/***************************************************************************/
/* Initializes Help Object in OVERLAY_3D Viewport and hides it immediately */
/***************************************************************************/
if ( ( hHelp = InitObj2D( vp2d2 , hMHelp , help_x , help_y , FALSE ) ) == NULL )
{
printf("Error InitObj2D\n");
exit( -1 );
}
/*******************************************/
/* Sets Ambient Light for MAIN_3D Viewport */
/*******************************************/
SetAmbientLight( vp3d, ambient , ambient , ambient );
/**********************************************************/
/* Initializes CARTESIAN GRID Object in MAIN_3D viewport */
/* Sets Object Shading type to FLAT */
/* Calculates Dimension and centers it onto screen */
/**********************************************************/
hObj3D = InitObj3D( vp3d, hModel3D , 0.0, 0.0, 0.0 , TRUE );
SetObj3DShadingType( hObj3D, FLAT );
GetObj3DInfo( hObj3D , &obj );
max_x = ( obj.alfa_x + obj.centre_x );
max_y = ( obj.beta_y + obj.centre_y );
max_z = ( obj.gamma_z + obj.centre_z );
min_x = ( -obj.alfa_x + obj.centre_x );
min_y = ( -obj.beta_y + obj.centre_y );
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;
TranslateObj3D( hObj3D , -centre_x , -centre_y, -centre_z );
min_radius = 2 * max3( ( max_x - min_x ) / 2, ( max_y - min_y ) / 2, ( max_z - min_z ) / 2 );
/************************************************************/
/* Initializes Spot-Light Object and assigns it its Overlay */
/************************************************************/
hObjLight = InitObj3D( vp3d, hModelLight , lx = ( 2 * centre_x) , ly = -centre_y, lz = 10.0 , TRUE );
ScaleObj3D( hObjLight, 0.1 );
SetObj3DOverlay( hObjLight , hTextOver , FALSE );
light_radius = sqrt ( ( centre_x * centre_x ) + ( centre_y * centre_y ) );
light_radius = centre_x;
radius = 5 * min_radius;
/************************/
/* Sets Viewing Optics */
/************************/
SetOptics( vp3d, 1.0, 3.0, radius * 30.0 );
i = 0;
theta = rho = 0.0;
end = FALSE;
step = radius / 100;
angle = 0.0;
/*********************/
/* Creates SpotLight */
/*********************/
spot.x = 0.0;
spot.y = 0.0;
spot.z = 0.0;
spot.r = 0.4;
spot_int = spot.g = 0.4;
spot.b = 0.4;
spot.warn_light = FALSE;
hSpot = CreateSpotLight( vp3d , &spot );
parallel = 0.5;
animation_step = 0.1;
animate = TRUE;
first = TRUE;
help = FALSE;
over = FALSE;
start = clock();
tt = 0.0;
while ( ! end )
{
/************************************************************************/
/* Applies Sin() Function to Cartesian Grid Object to achieve animation */
/************************************************************************/
if ( animate )
{
if ( play == 1 )
{
tt += 0.1;
if( tt > 2.5 ) play = -1;
}
else
{
tt -= 0.1;
if( tt < 0.1 ) play = +1;
}
for ( kk = 0; kk < 51*51 ; kk++ )
{
float xx, yy, rr;
xx = ( kk % 51 ) ;
yy = ( ( (float) kk) / 51.0 ) ;
rr = ( xx - 25.0 )*( xx - 25.0 )+( yy - 25.0 )*( yy - 25.0 ) ;
if( fabs(rr) != 0.0 )
zvalues[kk] = 100.0 * sin( tt * rr / 30.0 ) / rr ;
else
zvalues[kk] = 1.0;
}
LiftCartesian( hObj3D , zvalues );
}
/***********************************/
/* 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:
if ( light )
{
parallel -= 0.05;
if ( parallel < 0.0 )
parallel = 0.0;
}
break;
case 60:
if ( light )
{
parallel += 0.05;
if ( parallel > 1.0 )
parallel = 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 63:
if ( spot_on )
{
spot_int -=0.05;
if ( spot_int < 0.0 )
spot_int = 0.0;
spot.g = spot_int;
ChangeSpotLightIntensity( hSpot, spot_int, spot_int, spot_int );
}
break;
case 64:
if ( spot_on )
{
spot_int +=0.05;
if ( spot_int > 1.0 )
spot_int = 1.0;
spot.g = spot_int;
ChangeSpotLightIntensity( hSpot, spot_int, spot_int, spot_int );
}
break;
case 65:
animation_step -= 0.05;
if ( animation_step < 0 )
animation_step = 0;
break;
case 66:
animation_step += 0.05;
if ( animation_step > 1.0 )
animation_step = 1.0;
break;
case 67:
lz -= 1;
if ( lz < 4 )
lz = 4;
break;
case 68:
lz += 1;
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 's':
case 'S':
HideObj3D( hObjLight , spot_on );
spot_on = !spot_on;
if ( spot_on )
hSpot = CreateSpotLight( vp3d , &spot );
else
DeleteSpotLight( hSpot );
break;
case 'a':
case 'A':
animate = !animate;
break;
case 'h':
case 'H':
HideObj2D( hHelp, help );
help = !help;
break;
case 'o':
case 'O':
over = !over;
EnableObj3DOverlay( hObjLight, over );
break;
}
}
/* avoids flipping over the 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 SpotLight is enabled. */
/* Calculates new position and moves it */
/****************************************/
if ( spot_on )
{
lx = light_radius * cos ( angle );
ly = light_radius * sin ( angle );
PositionObj3D ( hObjLight , lx , ly , lz );
MoveSpotLight( hSpot , lx , ly , lz );
}
/*******************************************/
/* If Parallel Light is enabled, update it */
/*******************************************/
if ( light )
SetParallelSource( vp3d , parallel, parallel, parallel, -px, -py, -pz );
/*******************/
/* Draws New Frame */
/*******************/
DrawFrame();
angle += animation_step;
elapsed_time = clock();
frames++;
/********************************************/
/* Draw Information onto DIRECT_2D Viewport */
/********************************************/
if ( ( ! ( frames % bit ) ) || ( first ) )
{
seconds = ( ( double ) ( elapsed_time - start ) / ( double ) CLK_TCK );
DrawObj2D( vp2d1 , hBitmap , 0.0 , 0.0 );
frames_per_second = frames / seconds;
sprintf(str , " ZeRO 3D Animation Demo" );
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 , " Frames/sec: %5.2f", frames_per_second );
DrawText( vp2d1 , hFont , str , 180 + off, 14 , 255 , 255 , 255 );
sprintf(str , "PAR: %3.2f", parallel );
DrawText( vp2d1 , hFont , str , 10 + off, 14 , 255 , 255 , 255 );
sprintf(str , "AMB: %3.2f", ambient );
DrawText( vp2d1 , hFont , str , 55 + off, 14 , 255 , 255 , 255 );
if ( spot_on )
sprintf(str , "SPT: %3.2f", spot_int );
else
sprintf(str , "SPT: *** ", spot_int );
DrawText( vp2d1 , hFont , str , 105 + off, 14 , 255 , 255 , 255 );
if ( light )
DrawText( vp2d1, hFont , "*", 303 + off, 14 , 255 , 255 , 255 );
if ( animate )
DrawText( vp2d1, hFont , "A", 297 + off, 14 , 255 , 255 , 255 );
bit = (int) frames_per_second;
if ( bit == 0 )
bit = 1;
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 ---*/