⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfx_init.c

📁 利用VC++和OpenGVS结合编的一个关于火车运行的三维模拟程序。是OpenGVS三维初学者的应该参考的好范例。
💻 C
字号:
/************************************************************************** **  gfx_init.c -- **	Graphics initialization for the light reil (train) demo****************************************************************************/#include <g_sys.h>#include <stdlib.h>#include <string.h>#include <g_consts.h>#include <gv_cmd.h>#include <gv_env.h>#include <gv_cam.h>#include <gv_chn.h>#include <gv_fog.h>#include <gv_lsr.h>#include <gv_scn.h>#include <gv_sys.h>#include <gv_geo.h>#include <gv_txr.h>#include <gvu_sge.h>#include <tgifts.h>#include "train.h"#include "hostdata.h"/************************************  Globals************************************/GV_Fbf fbf[TMAX_CHANNELS];GV_Channel tchannel[TMAX_CHANNELS];/************************************  Global through this module only************************************/static int nunits = 1;static GV_Rgba erase_color= {0.63, 0.63, 1.00, 1.0};static float usf  	  = 1.0f ;	/* Used for units setup */static float visibility   = 100000.0f;  /* Feet initially but may be altered */static float clip_far     = 100000.0f;  /* Feet initially but may be altered */static float clip_near	  = 3.0f;       /* Feet initially but may be altered */static GV_Obi owntrain     = NULL;static GV_Fog haze   	  = NULL;static GV_Obi sky	  = NULL;static GV_Scene scene     = NULL;static GV_Camera cam_engineer = NULL;static GV_Light sunlight  = NULL;static TParse_data *tdata = NULL;static GV_Obi train_town  = NULL;static GV_Obi skydome     = NULL;/****************************************************************************  *  nogfx_setup --**       Setup when running without 3D graphics (e.g., network master)*****************************************************************************/static void nogfx_setup( GV_Channel chnhdl ){    static GV_Rgba black = {0.0, 0.0, 0.0, 1.0};    static GV_Viewport smallvp = {-0.35, 0.35, -0.35, 0.35 };    G_Name newname;    GV_Scene scnhdl;    GV_Fog foghdl;    /* Set erase colors to BLACK to make it obvious this is a mouse         arena window; always show border on as well in this mode */    GV_chn_set_viewport( chnhdl, &smallvp );    GV_chn_set_erase_color( chnhdl, &black );    GV_chn_set_erase_mode( chnhdl, GV_CHN_ERASE_MODE_OFF );    GV_chn_set_border( chnhdl, G_ON );    GV_chn_inq_scene( chnhdl, &scnhdl );    if( scnhdl )    {        GV_scn_set_state( scnhdl, G_OFF );        GV_scn_inq_fog( scnhdl, &foghdl );        if( foghdl )            GV_fog_set_color( foghdl, &black );    }    GV_chn_inq_fog( chnhdl, &foghdl );    if( foghdl )        GV_fog_set_color( foghdl, &black );    strcpy( newname, "Train Mouse Arena" );    if( tdata && tdata->network_duties == NETWORK_DUTIES_MASTER )        strcat( newname, ", NetMaster" );    else if( tdata && tdata->network_duties == NETWORK_DUTIES_SLAVE )        strcat( newname, ", NetSlave" );    GV_chn_set_name( chnhdl, newname );}/****************************************************************************  gfx_init_cameras -- **	Create required software cameras for the train demo****************************************************************************/static void gfx_init_cameras( void ){    static G_Position design_eye_offset = { 0.0, 0.0, 0.0 };    /* Setup main hierarchical camera with:       three platform camera mounted to object for flying around        platform 0 -> base platform; often mounted to a moving object        platform 1 -> design eye relative to base platform        platform 2 -> helmet mounted display platform relative     	              to design eye platform */    GV_cam_create( &cam_engineer);    GV_cam_set_name( cam_engineer, "ENGINEER" );    GV_cam_add_platforms( cam_engineer, 2 );    GV_cam_set_position( cam_engineer, DESIGN_EYE, &design_eye_offset );}/***************************************************************************  gfx_init_fog_models --**	Create fog models for the train demo***************************************************************************/static void gfx_init_fog_models( void ){    GV_fog_create( &haze);    GV_fog_set_type( haze, GV_FOG_TYPE_EXP2 );    GV_fog_set_name( haze, "HAZE" );    GV_fog_set_color( haze, &erase_color );    visibility *= usf;    GV_fog_set_opaque_distance( haze, visibility );}/***************************************************************************  gfx_init_light_sources --**	Create light sources for the train demo**************************************************************************/static void gfx_init_light_sources( void ){    static G_Vector3 direction = {90.*G_DEG_TO_RAD, 0.6, 0.0};	/* East */    static GV_Rgba ambient  = { 0.60, 0.60, 0.60, 1.0 };    static GV_Rgba diffuse  = { 0.40, 0.40, 0.60, 1.0 };    static GV_Rgba specular = { 0.80, 0.80, 0.80, 1.0 };    GV_lsr_create( &sunlight);    GV_lsr_set_name( sunlight, "SUN" );    GV_lsr_set_direction( sunlight, &direction );    GV_lsr_set_ambient( sunlight, &ambient );    GV_lsr_set_diffuse( sunlight, &diffuse );    GV_lsr_set_specular( sunlight, &specular );}/***************************************************************************  gfx_init_scenes --**	Create all scenes that will be required for the train demo***************************************************************************/static void gfx_init_scenes( void ){    GV_scn_create( &scene);    GV_scn_set_name( scene, "OTW" );}/****************************************************************************  gfx_init_objects --**	Create initial object instances for the train demo****************************************************************************/static void gfx_init_objects( void ){    int nchrs;    int inx;    nchrs = strlen( pcf_train_file );    if( nchrs > 0 )    {	/* User specified a train database file on command line ... use it */	char istring[1024];	GV_Obd def;	/* If a TRAIN_TOWN object definition already exists, kill it and	     use the one specified by the user on the command line */	GV_obd_inq_by_name( "TRAIN_TOWN", &def );	if( def )	    GV_obd_free( def );        sprintf( istring, 	    "import file=%s structure=on geometry=on static=on "	    "name=TRAIN_TOWN", pcf_train_file);	/* Import it now */        GV_cmd_service( istring );    }    /* Create train town instance */    GV_obi_instance_by_name( "TRAIN_TOWN", &train_town );    GV_obi_instance_by_name( "TRAIN", &vobi[0] );    GV_obi_instance_by_name( "TRAIN", &vobi[1] );    GV_obi_instance_by_name( "TRAIN", &vobi[2] );    for( inx=0; inx<NUM_VEHICLES; inx++)    {        if( vobi[inx] )	{	    G_Name name;	    sprintf( name, "TRAIN%d", inx );            GV_obi_set_name( vobi[inx], name );        }    }    /* Create owntrain object instance and turn the graphics       for it off by default... */    GV_obi_instance_by_name( "OWNTRAIN", &owntrain );    if( !owntrain )    {	/* Object definition for owntrain does not exist; create a NULL one 	    since the demo requires one to function... */	GV_Obd odef;	GV_Obd part;	static float angle=-5.0*G_DEG_TO_RAD;	GV_obd_open_by_name( "OWNTRAIN", &odef );	{	    /* For spotlight articulation */	    GV_obd_open_by_name( "spotlight", &part );	    GV_obd_close( part );	}	GV_obd_close( odef );	GV_obd_set_rotation_x( part, angle );	GV_obi_instance( odef, &owntrain );    }    GV_obi_set_gfx_state( owntrain, G_OFF );    GV_obi_instance_by_name( "SKYDOME", &skydome );    GV_obi_set_state( skydome, G_OFF );}/****************************************************************************  gfx_setup_scenes --**	Setup project scene(s) with required objects;* 	add objects, lights, and/or fog models to scene as req'd****************************************************************************/static void gfx_setup_scenes( void ){    GVU_Plsr plsr;    GV_Obi summer_time, autumn_time,  winter_time;    GVU_Sge sgehdl;    int inx;    if( sky )        GV_scn_add_object( scene, sky );	    GV_scn_add_object( scene, train_town );	    GV_scn_add_object( scene, skydome );    GV_scn_add_object( scene, owntrain );    for( inx=0; inx<NUM_VEHICLES; inx++)    {	if( vobi[inx] )            GV_scn_add_object( scene, vobi[inx] );	    }    GV_scn_add_light( scene, sunlight );    GV_scn_set_fog( scene, haze );    GV_obi_inq_by_name_relative( train_town, ".../summer", &summer_time );    GV_obi_set_state( summer_time, G_ON );    GV_obi_inq_by_name_relative( train_town, ".../autumn", &autumn_time );    GV_obi_set_state( autumn_time, G_OFF );    GV_obi_inq_by_name_relative( train_town, ".../winter", &winter_time );    GV_obi_set_state( winter_time, G_OFF );    GVU_sge_inq_by_name( TGIFTS_SGE, &sgehdl );    if( !sgehdl )    {	GVU_sge_create( &sgehdl );	GVU_sge_set_name( sgehdl, TGIFTS_SGE );	GVU_sge_set_state( sgehdl, G_ON );	GVU_sge_set_light( sgehdl, sunlight );	GVU_sge_add_to_scene( sgehdl, scene );    }    /* Defaults for this project */    GVU_sge_set_land_state( sgehdl, G_OFF );    GVU_sge_set_sky_state( sgehdl, G_OFF );    GVU_sge_set_lens_flare_state( sgehdl, G_OFF );    if( pcf_cursor && tdata && tdata->network_duties != NETWORK_DUTIES_SLAVE )	tcursor_add( scene );    /* Create a spotlight, add it to the scene, and attach to our train        with the effect initially turned off */    plsr = tspotlight_init( owntrain, scene, G_OFF );    if( !plsr )    {        if( tdata && !tdata->quiet )            tgprintf( stderr, "Note that this platform does not have "                    "appropriate Pixel Shader Hardware.  Thus, spotlight and "                    "landing light effects will not be functional here.\n" );    }}/*****************************************************************************  gfx_setup_cameras -- **	Make resource connections for cameras as required.  Resource setup*	functions are done after all "creation" activities so that other *	related graphics resources associated with our cameras *	(e.g., objects, channels) will already have been defined.*****************************************************************************/static void gfx_setup_cameras( void ){    /* Mount the owntrain object to the camera named "ENGINEER" */    GV_cam_add_mountable_obi( cam_engineer, owntrain );    GV_cam_set_mount_obi( cam_engineer, owntrain );    if( !cam_engineer )	fprintf( stderr, "gfx_setup_cameras: missing cameras!\n" );}/*****************************************************************************  gfx_init_channel -- **	Create required channel(s) for the train demo****************************************************************************/static GV_Channel gfx_init_channel( void ){    static char channel_name[80] = "OpenGVS Train";    const char *gl_vendor;    const char *gl_renderer;    GV_Channel newchannel;    gl_vendor = (const char *)glGetString( GL_VENDOR );    gl_renderer = (const char *)glGetString( GL_RENDERER );    sprintf(channel_name, "%s: %s,%s", channel_name, gl_vendor, gl_renderer);    GV_chn_create( &newchannel);    GV_chn_set_name( newchannel, channel_name );    GV_chn_set_erase_color( newchannel, &erase_color );    /* Only set clipping if not already done by command line -clip=near,far */    if( tdata && !tdata->process_clip )    {        clip_far *= usf;        clip_near *= usf;        GV_chn_set_clip_near( newchannel, clip_near );        GV_chn_set_clip_far( newchannel, clip_far );    }    /* Force this channel to always have input focus even if the mouse	is not pointing at it */    GV_chn_set_input_focus( newchannel );    return newchannel;}/******************************************************************************  gfx_connect_resources -- **	Make OpenGVS resource connections as required.  Strictly by demo*	convention here (not an OpenGVS restriction), resource setup*	functions are purposely done after all "creation" activities *	have been done to avoid resource creation ordering dependencies.  **	For example, graphics resources which rely on other resources being*	present (e.g., an object instance has to be defined before it can *	be mounted to a camera), so all instance and creation functions are*	done in gfx_create_resources, and all resource connections (e.g.,*	GV_chn_set_camera, GV_fbf_add_channel, GV_cam_set_mount_obi) are*	done in this section.******************************************************************************/void gfx_connect_resources( void ){    int unit;    gfx_setup_scenes();		/* Add objects to project scene     */    gfx_setup_cameras();	/* Setup object/camera relations    */    for( unit=0; unit<nunits; unit++)    {        GV_chn_set_scene( tchannel[unit], scene );        GV_chn_set_camera( tchannel[unit], cam_engineer);	tmultifbf_setup_chn_geometry( tchannel[unit], unit );	/* Modify system setup based upon command line arguments (tparse_cmd);	    e.g., altering clip planes, turning window border off, etc. */	tparse_init( fbf[unit], tchannel[unit], sunlight );        if( tdata && tdata->nogfx )            nogfx_setup( tchannel[unit] );    }}/*************************************************************************  setup_unit_scale --**	Create a scale factor.  This can be applied to any distance unit*	of measure to scale to the proper internal units.*************************************************************************/static void setup_unit_scale( float *unit_scale ){    float scale_factor_out ;    G_Sys_units units ;    G_sys_inq_units( &scale_factor_out , &units ) ;    if ( units == G_SYS_UNITS_FEET )	*unit_scale = 1.0f / scale_factor_out ;    else if ( units == G_SYS_UNITS_METERS )	*unit_scale = G_FEET_TO_METERS / scale_factor_out ;}/*************************************************************************  gfx_create_resoures --**	Create all project graphics resources, including but*	not limited to cameras, channels, scenes, light sources,*	object instances, fog models, and frame buffers.*************************************************************************/void gfx_create_resources( void ){    int unit;    GV_Obi inst;    /* This will get the scale factor if the unit are other than feet. */    setup_unit_scale( &usf ) ;    /* Get pointer to parse data */    tparse_inq_data( &tdata );    /* See how many graphics frame buffers have been requested &     	are available to us */    nunits = tmultifbf_inq_nunits();    for( unit=0; unit<nunits; unit++)    {        GV_fbf_create( &fbf[unit] );        tchannel[unit] = gfx_init_channel();    }    gfx_init_scenes();		/* Create project scenes             */    gfx_init_objects();		/* Create initial object instances   */    gfx_init_cameras();		/* Create software cameras           */    gfx_init_light_sources();	/* Create light sources              */    gfx_init_fog_models();	/* Create fog models                 */    /* Setup function callbacks for keyboard */    inst = tkui_init( scene );	/* Keyboard user interface and help  */    (void) inst;		/* Not used after set */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -