int Game_Init(void *parms) { // this function is where you do all the initialization // for your game int index; // looping var // start up DirectDraw (replace the parms as you desire) DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP); // initialize directinput DInput_Init(); // acquire the keyboard DInput_Init_Keyboard(); // add calls to acquire other directinput devices here... // initialize directsound and directmusic DSound_Init(); DMusic_Init(); // hide the mouse if (!WINDOWED_APP) ShowCursor(FALSE); // seed random number generator srand(Start_Clock()); Open_Error_File("ERROR.TXT"); // initialize math engine Build_Sin_Cos_Tables(); // initialize the camera with 90 FOV, normalized coordinates Init_CAM4DV1(&cam, // the camera object CAM_MODEL_EULER, // the euler model &cam_pos, // initial camera position &cam_dir, // initial camera angles &cam_target, // no target 200.0, // near and far clipping planes 12000.0, 120.0, // field of view in degrees WINDOW_WIDTH, // size of final screen viewport WINDOW_HEIGHT); // load the master tank object VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75); Load_OBJECT4DV1_PLG(&obj_tank, "tank3_8b.plg",&vscale, &vpos, &vrot); // load player object for 3rd person view VECTOR4D_INITXYZ(&vscale,0.75,0.75,0.75); Load_OBJECT4DV1_PLG(&obj_player, "tank2_8b.plg",&vscale, &vpos, &vrot); // load the master tower object VECTOR4D_INITXYZ(&vscale,1.0, 2.0, 1.0); Load_OBJECT4DV1_PLG(&obj_tower, "tower1_8b.plg",&vscale, &vpos, &vrot); // load the master ground marker VECTOR4D_INITXYZ(&vscale,3.0,3.0,3.0); Load_OBJECT4DV1_PLG(&obj_marker, "marker1_8b.plg",&vscale, &vpos, &vrot); // position the tanks for (index = 0; index < NUM_TANKS; index++) { // randomly position the tanks tanks[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); tanks[index].y = 0; // obj_tank.max_radius; tanks[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); tanks[index].w = RAND_RANGE(0,360); } // end for // position the towers for (index = 0; index < NUM_TOWERS; index++) { // randomly position the tower towers[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); towers[index].y = 0; // obj_tower.max_radius; towers[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); } // end for // set up lights // return success return(1); } // end Game_Init
int Game_Init(void *parms) { // this function is where you do all the initialization // for your game int index; // looping var // start up DirectDraw (replace the parms as you desire) DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP); // initialize directinput DInput_Init(); // acquire the keyboard DInput_Init_Keyboard(); // add calls to acquire other directinput devices here... // initialize directsound and directmusic DSound_Init(); DMusic_Init(); // hide the mouse if (!WINDOWED_APP) ShowCursor(FALSE); // seed random number generator srand(Start_Clock()); Open_Error_File("ERROR.TXT"); // initialize math engine Build_Sin_Cos_Tables(); // initialize the camera with 90 FOV, normalized coordinates Init_CAM4DV1(&cam, // the camera object CAM_MODEL_EULER, // the euler model &cam_pos, // initial camera position &cam_dir, // initial camera angles &cam_target, // no target 200.0, // near and far clipping planes 12000.0, 120.0, // field of view in degrees WINDOW_WIDTH, // size of final screen viewport WINDOW_HEIGHT); // load constant shaded water VECTOR4D_INITXYZ(&vscale,10.00,10.00,10.00); Load_OBJECT4DV2_COB(&obj_constant_water,"water_constant_01.cob", &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD ); // load flat shaded water VECTOR4D_INITXYZ(&vscale,10.00,10.00,10.00); Load_OBJECT4DV2_COB(&obj_flat_water,"water_flat_01.cob", &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD ); // load gouraud shaded water VECTOR4D_INITXYZ(&vscale,10.00,10.00,10.00); Load_OBJECT4DV2_COB(&obj_gouraud_water,"water_gouraud_01.cob", &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD ); // set up lights Reset_Lights_LIGHTV1(); // create some working colors white.rgba = _RGBA32BIT(255,255,255,0); gray.rgba = _RGBA32BIT(100,100,100,0); black.rgba = _RGBA32BIT(0,0,0,0); red.rgba = _RGBA32BIT(255,0,0,0); green.rgba = _RGBA32BIT(0,255,0,0); blue.rgba = _RGBA32BIT(0,0,255,0); // ambient light Init_Light_LIGHTV1(AMBIENT_LIGHT_INDEX, LIGHTV1_STATE_ON, // turn the light on LIGHTV1_ATTR_AMBIENT, // ambient light type gray, black, black, // color for ambient term only NULL, NULL, // no need for pos or dir 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D dlight_dir = {-1,0,-1,0}; // directional light Init_Light_LIGHTV1(INFINITE_LIGHT_INDEX, LIGHTV1_STATE_ON, // turn the light on LIGHTV1_ATTR_INFINITE, // infinite light type black, gray, black, // color for diffuse term only NULL, &dlight_dir, // need direction only 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D plight_pos = {0,200,0,0}; // point light Init_Light_LIGHTV1(POINT_LIGHT_INDEX, LIGHTV1_STATE_ON, // turn the light on LIGHTV1_ATTR_POINT, // pointlight type black, green, black, // color for diffuse term only &plight_pos, NULL, // need pos only 0,.001,0, // linear attenuation only 0,0,1); // spotlight info NA VECTOR4D slight2_pos = {0,200,0,0}; VECTOR4D slight2_dir = {-1,0,-1,0}; // spot light2 Init_Light_LIGHTV1(SPOT_LIGHT2_INDEX, LIGHTV1_STATE_ON, // turn the light on LIGHTV1_ATTR_SPOTLIGHT2, // spot light type 2 black, red, black, // color for diffuse term only &slight2_pos, &slight2_dir, // need pos only 0,.001,0, // linear attenuation only 0,0,1); // create lookup for lighting engine RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565, // format we want to build table for palette, // source palette rgblookup); // lookup table // return success return(1); } // end Game_Init
int Game_Init(void *parms) { // this function is where you do all the initialization // for your game int index; // looping var // start up DirectDraw (replace the parms as you desire) DDraw_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,0); // initialize directinput DInput_Init(); // acquire the keyboard DInput_Init_Keyboard(); // add calls to acquire other directinput devices here... // initialize directsound and directmusic DSound_Init(); DMusic_Init(); // hide the mouse if (!WINDOWED_APP) ShowCursor(FALSE); // seed random number generator srand(Start_Clock()); Open_Error_File("MD2ERROR.TXT"); // initialize math engine Build_Sin_Cos_Tables(); // initialize the camera with 90 FOV, normalized coordinates Init_CAM4DV1(&cam, // the camera object CAM_MODEL_EULER, // the euler model &cam_pos, // initial camera position &cam_dir, // initial camera angles &cam_target, // no target 10.0, // near and far clipping planes 12000.0, 90.0, // field of view in degrees WINDOW_WIDTH, // size of final screen viewport WINDOW_HEIGHT); VECTOR4D terrain_pos = {0,0,0,0}; Generate_Terrain_OBJECT4DV2(&obj_terrain, // pointer to object TERRAIN_WIDTH, // width in world coords on x-axis TERRAIN_HEIGHT, // height (length) in world coords on z-axis TERRAIN_SCALE, // vertical scale of terrain "height_grass_40_40_01.bmp", // filename of height bitmap encoded in 256 colors "stone256_256_01.bmp", // "grass256_256_01.bmp", //"checker2562562.bmp", // filename of texture map RGB16Bit(255,255,255), // color of terrain if no texture &terrain_pos, // initial position NULL, // initial rotations POLY4DV2_ATTR_RGB16 //| POLY4DV2_ATTR_SHADE_MODE_FLAT | POLY4DV2_ATTR_SHADE_MODE_GOURAUD | POLY4DV2_ATTR_SHADE_MODE_TEXTURE); // set a scaling vector VECTOR4D_INITXYZ(&vscale, 20, 20, 20); // load all the light objects in for (int index_obj=0; index_obj < NUM_LIGHT_OBJECTS; index_obj++) { Load_OBJECT4DV2_COB2(&obj_light_array[index_obj], object_light_filenames[index_obj], &vscale, &vpos, &vrot, VERTEX_FLAGS_INVERT_WINDING_ORDER | VERTEX_FLAGS_TRANSFORM_LOCAL | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD ,0 ); } // end for index // set current object curr_light_object = 0; obj_light = &obj_light_array[curr_light_object]; // set up lights Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS); // create some working colors white.rgba = _RGBA32BIT(255,255,255,0); gray.rgba = _RGBA32BIT(100,100,100,0); black.rgba = _RGBA32BIT(0,0,0,0); red.rgba = _RGBA32BIT(255,0,0,0); green.rgba = _RGBA32BIT(0,255,0,0); blue.rgba = _RGBA32BIT(0,0,255,0); orange.rgba = _RGBA32BIT(255,128,0,0); yellow.rgba = _RGBA32BIT(255,255,0,0); // ambient light Init_Light_LIGHTV2(lights2, AMBIENT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_AMBIENT, // ambient light type gray, black, black, // color for ambient term only NULL, NULL, // no need for pos or dir 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D dlight_dir = {-1,1,-1,1}; // directional light Init_Light_LIGHTV2(lights2, INFINITE_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_INFINITE, // infinite light type black, gray, black, // color for diffuse term only NULL, &dlight_dir, // need direction only 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D plight_pos = {0,500,0,1}; // point light Init_Light_LIGHTV2(lights2, POINT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_POINT, // pointlight type black, orange, black, // color for diffuse term only &plight_pos, NULL, // need pos only 0,.001,0, // linear attenuation only 0,0,1); // spotlight info NA // point light Init_Light_LIGHTV2(lights2, POINT_LIGHT2_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_POINT, // pointlight type black, yellow, black, // color for diffuse term only &plight_pos, NULL, // need pos only 0,.002,0, // linear attenuation only 0,0,1); // spotlight info NA VECTOR4D slight2_pos = {0,200,0,1}; VECTOR4D slight2_dir = {-1,1,-1,1}; // create lookup for lighting engine RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565, // format we want to build table for palette, // source palette rgblookup); // lookup table // create the z buffer Create_Zbuffer(&zbuffer, WINDOW_WIDTH, WINDOW_HEIGHT, ZBUFFER_ATTR_32BIT); // build alpha lookup table RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table); // load background sounds wind_sound_id = DSound_Load_WAV("STATIONTHROB.WAV"); // start the sounds DSound_Play(wind_sound_id, DSBPLAY_LOOPING); DSound_Set_Volume(wind_sound_id, 100); #if 0 // load in the cockpit image Create_BOB(&cockpit, 0,0,800,600,2, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); Load_Bitmap_File(&bitmap16bit, "lego02.BMP"); Load_Frame_BOB16(&cockpit, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS); Unload_Bitmap_File(&bitmap16bit); Load_Bitmap_File(&bitmap16bit, "lego02b.BMP"); Load_Frame_BOB16(&cockpit, &bitmap16bit,1,0,0,BITMAP_EXTRACT_MODE_ABS); Unload_Bitmap_File(&bitmap16bit); #endif // load background image that scrolls Load_Bitmap_File(&bitmap16bit, "sunset800_600_03.bmp"); Create_Bitmap(&background_bmp,0,0,800,600,16); Load_Image_Bitmap16(&background_bmp, &bitmap16bit,0,0,BITMAP_EXTRACT_MODE_ABS); Unload_Bitmap_File(&bitmap16bit); static VECTOR4D vs = {4,4,4,1}; static VECTOR4D vp = {0,0,0,1}; // load the md2 object Load_Object_MD2(&obj_md2, // the loaded md2 file placed in container "./md2/q2mdl-tekkblade/tris.md2", // "D:/Games/quakeII/baseq2/players/male/tris.md2", // the filename of the .MD2 model &vs, &vp, NULL, "./md2/q2mdl-tekkblade/blade_black.bmp", //"D:/Games/quakeII/baseq2/players/male/claymore.bmp", // the texture filename for the model POLY4DV2_ATTR_RGB16 | POLY4DV2_ATTR_SHADE_MODE_FLAT | POLY4DV2_ATTR_SHADE_MODE_TEXTURE, RGB16Bit(255,255,255), VERTEX_FLAGS_SWAP_YZ); // control ordering etc. // prepare OBJECT4DV2 for md2 Prepare_OBJECT4DV2_For_MD2(&obj_model, // pointer to destination object &obj_md2); // md2 object to extract frame from // set the animation Set_Animation_MD2(&obj_md2,MD2_ANIM_STATE_STANDING_IDLE, MD2_ANIM_LOOP); #if 0 // play with these for more speed :) // set single precission _control87( _PC_24, _MCW_PC ); // set to flush mode _control87( _DN_FLUSH, _MCW_DN ); // set rounding mode _control87( _RC_NEAR, _MCW_RC ); #endif // return success return(1); } // end Game_Init
int Game_Init(void *parms) { // this function is where you do all the initialization // for your game int index; // looping var // start up DirectDraw (replace the parms as you desire) DDraw_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,1); // initialize directinput DInput_Init(); // acquire the keyboard DInput_Init_Keyboard(); // add calls to acquire other directinput devices here... // initialize directsound and directmusic DSound_Init(); DMusic_Init(); // hide the mouse if (!WINDOWED_APP) ShowCursor(FALSE); // seed random number generator srand(Start_Clock()); Open_Error_File("ERROR.TXT"); // initialize math engine Build_Sin_Cos_Tables(); // initialize the camera with 90 FOV, normalized coordinates Init_CAM4DV1(&cam, // the camera object CAM_MODEL_EULER, // the euler model &cam_pos, // initial camera position &cam_dir, // initial camera angles &cam_target, // no target 10.0, // near and far clipping planes 12000.0, 120.0, // field of view in degrees WINDOW_WIDTH, // size of final screen viewport WINDOW_HEIGHT); // set a scaling vector VECTOR4D_INITXYZ(&vscale,20.00,20.00,20.00); // load all the objects in for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++) { Load_OBJECT4DV2_COB2(&obj_array[index_obj], object_filenames[index_obj], &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL // | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD | VERTEX_FLAGS_INVERT_TEXTURE_V ,0 ); } // end for index_obj // set current object curr_object = 0; obj_work = &obj_array[curr_object]; // position the scenery objects randomly for (index = 0; index < NUM_SCENE_OBJECTS; index++) { // set master object link scene_objects[index].obj = obj_work; // randomly position object scene_objects[index].pos.x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); scene_objects[index].pos.y = RAND_RANGE(-200, 200); scene_objects[index].pos.z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); scene_objects[index].rot.y = RAND_RANGE(0,360); scene_objects[index].auxi[0] = RAND_RANGE(1,5); // set state of object container scene_objects[index].state = ((OBJECT4DV2_PTR)scene_objects[index].obj)->state; // set attributes scene_objects[index].attr = ((OBJECT4DV2_PTR)scene_objects[index].obj)->attr; } // end for // set up lights Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS); // create some working colors white.rgba = _RGBA32BIT(255,255,255,0); gray.rgba = _RGBA32BIT(150,150,150,0); black.rgba = _RGBA32BIT(0,0,0,0); red.rgba = _RGBA32BIT(255,0,0,0); green.rgba = _RGBA32BIT(0,255,0,0); blue.rgba = _RGBA32BIT(0,0,255,0); // ambient light Init_Light_LIGHTV2(lights2, // array of lights to work with AMBIENT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_AMBIENT, // ambient light type gray, black, black, // color for ambient term only NULL, NULL, // no need for pos or dir 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D dlight_dir = {-1,0,-1,1}; // directional light Init_Light_LIGHTV2(lights2, // array of lights to work with INFINITE_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_INFINITE, // infinite light type black, gray, black, // color for diffuse term only NULL, &dlight_dir, // need direction only 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D plight_pos = {0,200,0,1}; // point light Init_Light_LIGHTV2(lights2, // array of lights to work with POINT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_POINT, // pointlight type black, green, black, // color for diffuse term only &plight_pos, NULL, // need pos only 0,.002,0, // linear attenuation only 0,0,1); // spotlight info NA VECTOR4D slight2_pos = {0,1000,0,1}; VECTOR4D slight2_dir = {-1,0,-1,1}; // spot light2 Init_Light_LIGHTV2(lights2, // array of lights to work with SPOT_LIGHT2_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_SPOTLIGHT2, // spot light type 2 black, red, black, // color for diffuse term only &slight2_pos, &slight2_dir, // need pos only 0,.001,0, // linear attenuation only 0,0,1); // create lookup for lighting engine RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565, // format we want to build table for palette, // source palette rgblookup); // lookup table // create the z buffer Create_Zbuffer(&zbuffer, WINDOW_WIDTH, WINDOW_HEIGHT, ZBUFFER_ATTR_32BIT); // create the alpha lookup table RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table); // load in the background Create_BOB(&background, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); Load_Bitmap_File(&bitmap16bit, "nebblue01.bmp"); Load_Frame_BOB16(&background, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS); Unload_Bitmap_File(&bitmap16bit); // build the bounding hierarchical volume tree with 3 divisions per level BHV_Build_Tree(&bhv_tree, scene_objects, NUM_SCENE_OBJECTS, 0, 3, UNIVERSE_RADIUS); // return success return(1); } // end Game_Init
void Init_CAM4D(LPCAM4D cam, int attr, LPPOINT4D postion, LPVECTOR4D direction, LPPOINT4D target, float nearClipZ, float farClipZ, float fov, float viewPortWidth, float viewPortHeight) { cam->attr = attr; VECTOR4D_COPY(&cam->pos, postion); VECTOR4D_COPY(&cam->dir, direction); VECTOR4D_INITXYZ(&cam->u, 1, 0, 0); VECTOR4D_INITXYZ(&cam->v, 0, 1, 0); VECTOR4D_INITXYZ(&cam->n, 0, 0, 1); if (target != NULL) VECTOR4D_COPY(&cam->target, target); else VECTOR4D_ZERO(&cam->target); cam->nearClipZ = nearClipZ; cam->farClipZ = farClipZ; cam->viewPortWidth = viewPortWidth; cam->viewPortHeight = viewPortHeight; cam->viewPortCenterX = (viewPortWidth - 1) / 2; cam->viewPortCenterY = (viewPortHeight - 1) / 2; cam->aspectRatio = (float)viewPortWidth / (float)viewPortHeight; MATRIX_IDENTITY_4X4(&cam->mWordToCam); MATRIX_IDENTITY_4X4(&cam->mCamToPer); MATRIX_IDENTITY_4X4(&cam->mPerToScreen); cam->fov = fov; cam->viewPlaneWidth = 2.0; cam->viewPlaneHeight = 2.0 / cam->aspectRatio; cam->viewDistance = 0.5 * (cam->viewPlaneWidth) / tan(DEG_TO_RAD(fov / 2)); if (fov == 90) { VECTOR3D vn; POINT3D ptOrigin; VECTOR3D_INITXYZ(&ptOrigin, 0, 0, 0); VECTOR3D_INITXYZ(&vn, 1, 0, -1); Plane3D_Init(&cam->rightClipPlane, &ptOrigin, &vn, 1); //x=z plane VECTOR3D_INITXYZ(&vn, -1, 0, -1); Plane3D_Init(&cam->leftClipPlane, &ptOrigin, &vn ,1); //-x=z plane VECTOR3D_INITXYZ(&vn, 0, 1, -1); Plane3D_Init(&cam->topClipPlane, &ptOrigin, &vn ,1); //y=z plane VECTOR3D_INITXYZ(&vn, 0, -1, -1); Plane3D_Init(&cam->bottomClipPlane, &ptOrigin, &vn ,1); //-y=z plane } else { VECTOR3D vn; POINT3D ptOrigin; VECTOR3D_INITXYZ(&ptOrigin, 0, 0, 0); VECTOR3D_INITXYZ(&vn, cam->viewDistance, 0, -cam->viewPlaneWidth / 2); Plane3D_Init(&cam->rightClipPlane, &ptOrigin, &vn, 1); VECTOR3D_INITXYZ(&vn, -cam->viewDistance, 0, -cam->viewPlaneWidth / 2); Plane3D_Init(&cam->leftClipPlane, &ptOrigin, &vn, 1); VECTOR3D_INITXYZ(&vn, 0, cam->viewDistance, -cam->viewPlaneWidth / 2); Plane3D_Init(&cam->topClipPlane, &ptOrigin, &vn, 1); VECTOR3D_INITXYZ(&vn, 0, -cam->viewDistance, -cam->viewPlaneWidth / 2); Plane3D_Init(&cam->bottomClipPlane, &ptOrigin, &vn, 1); } }
int Game_Init(void *parms) { // this function is where you do all the initialization // for your game int index; // looping var // start up DirectDraw (replace the parms as you desire) DDraw_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,0); // initialize directinput DInput_Init(); // acquire the keyboard DInput_Init_Keyboard(); // add calls to acquire other directinput devices here... // initialize directsound and directmusic DSound_Init(); DMusic_Init(); // hide the mouse if (!WINDOWED_APP) ShowCursor(FALSE); // seed random number generator srand(Start_Clock()); Open_Error_File("ERROR.TXT"); // initialize math engine Build_Sin_Cos_Tables(); // initialize the camera with 90 FOV, normalized coordinates Init_CAM4DV1(&cam, // the camera object CAM_MODEL_EULER, // the euler model &cam_pos, // initial camera position &cam_dir, // initial camera angles &cam_target, // no target 10.0, // near and far clipping planes 12000.0, 120.0, // field of view in degrees WINDOW_WIDTH, // size of final screen viewport WINDOW_HEIGHT); #if 0 VECTOR4D terrain_pos = {0,0,0,0}; Generate_Terrain_OBJECT4DV2(&obj_terrain, // pointer to object TERRAIN_WIDTH, // width in world coords on x-axis TERRAIN_HEIGHT, // height (length) in world coords on z-axis TERRAIN_SCALE, // vertical scale of terrain "checkerheight05.bmp", // filename of height bitmap encoded in 256 colors "checker256256.bmp", // filename of texture map RGB16Bit(255,255,255), // color of terrain if no texture &terrain_pos, // initial position NULL, // initial rotations POLY4DV2_ATTR_RGB16 | POLY4DV2_ATTR_SHADE_MODE_CONSTANT // | POLY4DV2_ATTR_SHADE_MODE_FLAT // | POLY4DV2_ATTR_SHADE_MODE_GOURAUD | POLY4DV2_ATTR_SHADE_MODE_TEXTURE); #endif // set a scaling vector VECTOR4D_INITXYZ(&vscale,TERRAIN_WIDTH,1.00,TERRAIN_HEIGHT); // set position VECTOR4D_INITXYZ(&vpos, 0, 0, 0); Load_OBJECT4DV2_COB2(&obj_terrain, "plane01.cob", &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL /* VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD*/,0 ); // set a scaling vector VECTOR4D_INITXYZ(&vscale,60.00,60.00,60.00); // load all the objects in for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++) { Load_OBJECT4DV2_COB2(&obj_array[index_obj], object_filenames[index_obj], &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ | VERTEX_FLAGS_TRANSFORM_LOCAL /* VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD*/,0 ); } // end for index_obj // position the scenery objects randomly for (index = 0; index < NUM_SCENE_OBJECTS; index++) { // randomly position object scene_objects[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); scene_objects[index].y = 75; // RAND_RANGE(-(UNIVERSE_RADIUS/2), (UNIVERSE_RADIUS/2)); scene_objects[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); // select random object, use w to store value scene_objects[index].w = RAND_RANGE(0,NUM_OBJECTS-1); } // end for // select random velocities for (index = 0; index < NUM_SCENE_OBJECTS; index++) { // randomly position object scene_objects_vel[index].x = RAND_RANGE(-MAX_VEL, MAX_VEL); scene_objects_vel[index].y = 0; // RAND_RANGE(-MAX_VEL, MAX_VEL); scene_objects_vel[index].z = RAND_RANGE(-MAX_VEL, MAX_VEL); } // end for // set up lights Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS); // create some working colors white.rgba = _RGBA32BIT(255,255,255,0); gray.rgba = _RGBA32BIT(150,150,150,0); black.rgba = _RGBA32BIT(0,0,0,0); red.rgba = _RGBA32BIT(255,0,0,0); green.rgba = _RGBA32BIT(0,255,0,0); blue.rgba = _RGBA32BIT(0,0,255,0); // ambient light Init_Light_LIGHTV2(lights2, // array of lights to work with AMBIENT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_AMBIENT, // ambient light type gray, black, black, // color for ambient term only NULL, NULL, // no need for pos or dir 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D dlight_dir = {-1,0,-1,1}; // directional light Init_Light_LIGHTV2(lights2, // array of lights to work with INFINITE_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_INFINITE, // infinite light type black, gray, black, // color for diffuse term only NULL, &dlight_dir, // need direction only 0,0,0, // no need for attenuation 0,0,0); // spotlight info NA VECTOR4D plight_pos = {0,200,0,1}; // point light Init_Light_LIGHTV2(lights2, // array of lights to work with POINT_LIGHT_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_POINT, // pointlight type black, green, black, // color for diffuse term only &plight_pos, NULL, // need pos only 0,.002,0, // linear attenuation only 0,0,1); // spotlight info NA VECTOR4D slight2_pos = {0,1000,0,1}; VECTOR4D slight2_dir = {-1,0,-1,1}; // spot light2 Init_Light_LIGHTV2(lights2, // array of lights to work with SPOT_LIGHT2_INDEX, LIGHTV2_STATE_ON, // turn the light on LIGHTV2_ATTR_SPOTLIGHT2, // spot light type 2 black, red, black, // color for diffuse term only &slight2_pos, &slight2_dir, // need pos only 0,.001,0, // linear attenuation only 0,0,1); // create lookup for lighting engine RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565, // format we want to build table for palette, // source palette rgblookup); // lookup table // create the z buffer Create_Zbuffer(&zbuffer, WINDOW_WIDTH, WINDOW_HEIGHT, ZBUFFER_ATTR_32BIT); // build alpha lookup table RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table); // load in the background Create_BOB(&background, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); Load_Bitmap_File(&bitmap16bit, "cloud03.bmp"); Load_Frame_BOB16(&background, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS); Unload_Bitmap_File(&bitmap16bit); // return success return(1); } // end Game_Init
void uav3D_init(void) { uint8_t i = 0; cam3D_init(); uav3D.state = 3; uav3D.attr = 0; VECTOR4D_INITXYZ(&(uav3D.world_pos), 0.0, 0.0, 100.0); VECTOR4D_ZERO(&(uav3D.dir)); ////////////////////////////////////////////////////////// uav3D.num_vertices = 8; VECTOR4D_INITXYZ(&(uav3D.vlist_local[0]), 0.0, 12.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[1]), 5.0, -12.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[2]), 0.0, -5.0, -5.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[3]), -5.0, -12.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[4]), 10.0, -12.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[5]), -10.0, -12.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[6]), 2.0, 0.0, 0.0); VECTOR4D_INITXYZ(&(uav3D.vlist_local[7]), -2.0, 0.0, 0.0); for (i = 0; i < uav3D.num_vertices; i++) { VECTOR4D_ZERO(&(uav3D.vlist_trans[i])); } uav3D.num_polys = 5; for (i = 0; i < uav3D.num_polys; i++) { uav3D.plist[i].state = 1; uav3D.plist[i].attr = 137; uav3D.plist[i].color = 1920; VECTOR4D_COPY(uav3D.plist[i].vlist, uav3D.vlist_local); } uav3D.plist[0].vert[0] = 0; uav3D.plist[0].vert[1] = 1; uav3D.plist[0].vert[2] = 2; uav3D.plist[1].vert[0] = 6; uav3D.plist[1].vert[1] = 4; uav3D.plist[1].vert[2] = 1; uav3D.plist[2].vert[0] = 0; uav3D.plist[2].vert[1] = 2; uav3D.plist[2].vert[2] = 3; uav3D.plist[3].vert[0] = 7; uav3D.plist[3].vert[1] = 3; uav3D.plist[3].vert[2] = 5; uav3D.plist[4].vert[0] = 2; uav3D.plist[4].vert[1] = 1; uav3D.plist[4].vert[2] = 3; VECTOR4D v; //translate the obj VECTOR4D_INITXYZ(&v, eeprom_buffer.params.Atti_3D_posX - GRAPHICS_X_MIDDLE, -eeprom_buffer.params.Atti_3D_posY + GRAPHICS_Y_MIDDLE, 0); Translate_OBJECT4DV1(&uav3D, &v); //scale the mode VECTOR4D_INITXYZ(&v, atti_3d_scale, atti_3d_scale, 0); Scale_OBJECT4DV1(&uav3D, &v); }
// 使用3D图形算法来获取图片旋转后的四个顶点位置 void C3DTransform::GetQuadByAnimateValue(int nDegreeX, int nDegreeY, int nDegreeZ, int nZOffset, Quad* pOutQuad) { // .局部坐标(以矩形的中心作为局部坐标的原点) float x = (float)m_nSrcWndWidth/2; float y = (float)m_nSrcWndHeight/2; POINT3D pt3dModel[4] = { {-x, y, 0}, {x, y, 0}, {x, -y, 0}, {-x, -y, 0} }; POINT3D pt3DWorld[4] = {0}; POINT3D pt3DCamera[4] = {0}; #pragma region // .局部坐标->世界坐标 { // .以当前值作为角度进行旋转 float fDegreeX = (float)nDegreeX; float fDegreeY = (float)nDegreeY; float fDegreeZ = (float)nDegreeZ; MATRIX_4_4 matTemp1, matTemp2; MATRIX_4_4 matRotateY, matRotateX, matRotateZ; MATRIX_4_4_PTR pLeftArg = NULL; if (0 != fDegreeY) { MAT_IDENTITY_4_4(&matRotateY); matRotateY.M00 = Fast_Cos(fDegreeY); matRotateY.M02 = -Fast_Sin(fDegreeY); matRotateY.M20 = Fast_Sin(fDegreeY); matRotateY.M22 = Fast_Cos(fDegreeY); pLeftArg = &matRotateY; } if (0 != fDegreeX) { MAT_IDENTITY_4_4(&matRotateX); matRotateX.M11 = Fast_Cos(fDegreeX); matRotateX.M12 = Fast_Sin(fDegreeX); matRotateX.M21 = -Fast_Sin(fDegreeX); matRotateX.M22 = Fast_Cos(fDegreeX); if (NULL == pLeftArg) { pLeftArg = &matRotateX; } else { Mat_Mul_4X4(pLeftArg, &matRotateX, &matTemp1); pLeftArg = &matTemp1; } } if (0 != fDegreeZ) { MAT_IDENTITY_4_4(&matRotateZ); matRotateZ.M00 = Fast_Cos(fDegreeZ); matRotateZ.M01 = Fast_Sin(fDegreeZ); matRotateZ.M10 = -Fast_Sin(fDegreeZ); matRotateZ.M11 = Fast_Cos(fDegreeZ); if (NULL == pLeftArg) { pLeftArg = &matRotateZ; } else { Mat_Mul_4X4(pLeftArg, &matRotateZ, &matTemp2); pLeftArg = &matTemp2; } } if (pLeftArg) { for (int i = 0; i < 4; i++) Mat_Mul_VECTOR3D_4X4(&pt3dModel[i], pLeftArg, &pt3DWorld[i]); } else { for (int i = 0; i < 4; i++) { pt3DWorld[i].x = pt3dModel[i].x; pt3DWorld[i].y = pt3dModel[i].y; } } // .由于仍然是平移到世界坐标的 (0,0,0)位置,因此不用计算平移 for (int i = 0; i < 4; i++) { pt3DWorld[i].z += nZOffset; } } #pragma endregion // 相机位置(这里默认将视平面放在与矩形所在面的同一位置,这样透视出来的坐标直接就可以 // 当成屏幕坐标来用了,省了一步操作。 // 但是需要注意的是,如果nCameraPos过小的话看到的图像就会缩小, CAMERA camerpos = {0}; float fCameraPos = 2000.0f; // 相机位置 float d = fCameraPos; // 相机与视平面的距离。将两值设成一样,避免了一次到屏幕坐标的转换 VECTOR4D_INITXYZ(&camerpos.WorldPos, 0,0, -fCameraPos); #pragma region // 世界坐标转换为相机坐标 { // 平移矩阵 MATRIX_4_4 matCameraTrans = {0}; MAT_IDENTITY_4_4(&matCameraTrans); matCameraTrans.M30 = -camerpos.WorldPos.x; matCameraTrans.M31 = -camerpos.WorldPos.y; matCameraTrans.M32 = -camerpos.WorldPos.z; // 相机角度为0,不旋转 for (int i = 0; i < 4; i++) { Mat_Mul_VECTOR3D_4X4(&pt3DWorld[i], &matCameraTrans, &pt3DCamera[i]); } } #pragma endregion #pragma region // 相机坐标转换为透视坐标 POINT3D pt3DPerspectivePos[4]; for (int i = 0; i < 4; i++) { float z = pt3DCamera[i].z; // 这里的z是用于和d相比的距离,不是坐标. 当d值取的比较小时,会导致z为负 float i_z = 1/z; if (pt3DCamera[i].z != 0) { pt3DPerspectivePos[i].x = d * pt3DCamera[i].x * i_z; // nCameraPos相当于d pt3DPerspectivePos[i].y = d * pt3DCamera[i].y * i_z; // } } #pragma endregion // 转换到屏幕坐标上 for (int i = 0; i < 4; ++i) { pt3DPerspectivePos[i].x += (m_nSrcWndWidth>>1); pt3DPerspectivePos[i].y = -pt3DPerspectivePos[i].y; pt3DPerspectivePos[i].y += (m_nSrcWndHeight>>1); } // 赋值给返回值 for (int i = 0; i < 4; i++) { pOutQuad->pos[2*i] = (int)pt3DPerspectivePos[i].x; pOutQuad->pos[2*i+1] = (int)pt3DPerspectivePos[i].y; } }
static void init() { Build_Sin_Cos_Tables(); RGB16Bit = RGB16Bit565; //glClearColor(0, 0, 0, 1); //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); //glEnable(GL_DEPTH_TEST); //glDepthFunc(GL_LESS); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); VECTOR4D cam_pos = {0, 40, 40, 1}; VECTOR4D cam_dir = {0, 0, 0, 1}; VECTOR4D cam_tar = { 0, 0, 0, 1}; Init_CAM4DV1(&cam, CAM_MODEL_EULER, &cam_pos, &cam_dir, &cam_tar, 1.0f, 12000.0f, 90, WINDOW_WIDTH, WINDOW_HEIGHT); VECTOR4D vscale = { 1.0f, 1.0f, 1.0f, 1.0f }; VECTOR4D vpos = { 0.0f, 0, 0, 1.0f }; VECTOR4D vrot = { 0.0f, 180.0f, 0.0f, 1.0f }; //Load_OBJECT4DV1_PLG(&obj, "model/towerg1.plg", &vScale, &vPos, &vRot); // load the master tank object VECTOR4D_INITXYZ(&vscale, 0.75, 0.75, 0.75); Load_OBJECT4DV1_PLG(&obj_tank, "model/tank2.plg", &vscale, &vpos, &vrot); // load player object for 3rd person view VECTOR4D_INITXYZ(&vscale, 0.75, 0.75, 0.75); Load_OBJECT4DV1_PLG(&obj_player, "model/tank3.plg", &vscale, &vpos, &vrot); // load the master tower object VECTOR4D_INITXYZ(&vscale, 1.0, 2.0, 1.0); Load_OBJECT4DV1_PLG(&obj_tower, "model/tower1.plg", &vscale, &vpos, &vrot); // load the master ground marker VECTOR4D_INITXYZ(&vscale, 3.0, 3.0, 3.0); Load_OBJECT4DV1_PLG(&obj_marker, "model/marker1.plg", &vscale, &vpos, &vrot); // position the tanks for (int index = 0; index < NUM_TANKS; index++) { // randomly position the tanks tanks[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); tanks[index].y = 0; // obj_tank.max_radius; tanks[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); tanks[index].w = RAND_RANGE(0, 360); } // end for // position the towers for (int index = 0; index < NUM_TOWERS; index++) { // randomly position the tower towers[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); towers[index].y = 0; // obj_tower.max_radius; towers[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS); } // end for RGBAV1 ambient; ambient.a = 255; ambient.r = 128; ambient.g = 128; ambient.b = 128; RGBAV1 diffuse; diffuse.a = 255; diffuse.r = 255; diffuse.g = 255; diffuse.b = 255; RGBAV1 specular; specular.a = 255; specular.r = 255; specular.g = 255; specular.b = 255; VECTOR4D lightPos = {200, 200, 200, 1}; VECTOR4D lightDir = { 1, 1, 1, 1 }; //Init_Light_LIGHTV1(0, LIGHTV1_STATE_ON, LIGHTV1_ATTR_AMBIENT, ambient, diffuse, specular, &lightPos, &lightDir, 0, 0, 0, 0, 0, 0); Init_Light_LIGHTV1(0, LIGHTV1_STATE_ON, LIGHTV1_ATTR_INFINITE, ambient, diffuse, specular, &lightPos, &lightDir, 0, 0, 0, 0, 0, 0); //Init_Light_LIGHTV1(0, LIGHTV1_STATE_ON, LIGHTV1_ATTR_POINT, ambient, diffuse, specular, &lightPos, &lightDir, 1, 0, 0, 0, 0, 0); //Init_Light_LIGHTV1(0, LIGHTV1_STATE_ON, LIGHTV1_ATTR_SPOTLIGHT1, ambient, diffuse, specular, &lightPos, &lightDir, 1, 0, 0, 5, 20, 10); //Init_Light_LIGHTV1(0, LIGHTV1_STATE_ON, LIGHTV1_ATTR_SPOTLIGHT2, ambient, diffuse, specular, &lightPos, &lightDir, 1, 0, 0, 30, 60, 10); //LoadBmp(); //indexBuffer = new GLuint[obj.num_polys * 3]; //glGenVertexArrays(NumVAOs, VAOs); //glBindVertexArray(VAOs[VAO_1]); //glGenBuffers(NumBuffers, Buffers); //glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); //glBufferData(GL_ARRAY_BUFFER, sizeof(POINT4D)* obj.num_vertices, NULL, GL_DYNAMIC_DRAW); // //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[ElementBuffer]); //glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); //glEnableVertexAttribArray(vPosition); //ShaderInfo shaders[] = { // { GL_VERTEX_SHADER, "shader/common.vert" }, // { GL_FRAGMENT_SHADER, "shader/common.frag" }, // { GL_NONE, NULL } //}; //GLuint program = LoadShaders(shaders); //glUseProgram(program); }
void Init_CAM4DV1(CAM4DV1_PTR cam, int cam_attr, POINT4D_PTR cam_pos, VECTOR4D_PTR cam_dir , POINT4D_PTR cam_target, float near_clip_z, float far_clip_z, float fov, float viewport_width, float viewport_height) { cam->attr = cam_attr; VECTOR4D_COPY(&cam->pos, cam_pos); VECTOR4D_COPY(&cam->dir, cam_dir); VECTOR4D_INITXYZ(&cam->u, 1.f, .0f, .0f); VECTOR4D_INITXYZ(&cam->v, 0.f, 1.f, .0f); VECTOR4D_INITXYZ(&cam->n, .0f, .0f, 1.f); if (cam_pos != NULL) VECTOR4D_COPY(&cam->target, cam_target); else VECTOR4D_ZERO(&cam->target); cam->near_clip_z = near_clip_z; cam->far_clip_z = far_clip_z; cam->viewport_width = viewport_width; cam->viewport_height = viewport_height; cam->viewport_center_x = (viewport_width - 1) / 2; cam->viewport_center_y = (viewport_height - 1) / 2; cam->aspect_ratio = (float)viewport_width / (float)viewport_height; MAT_IDENTITY_4X4(&cam->mcam); MAT_IDENTITY_4X4(&cam->mper); MAT_IDENTITY_4X4(&cam->mscr); cam->fov = fov; cam->viewplane_width = 2; cam->viewplane_height = 2 / cam->aspect_ratio; float tan_fov_div2 = tan(DEG_TO_RAD(fov / 2)); //cam->view_dist = 0.5f * cam->viewplane_width * tan_fov_div2; cam->view_dist = 0.5f * cam->viewplane_width / tan_fov_div2; cam->viewport_dist = 0.5f * cam->viewport_width / tan_fov_div2; if (fov == 90.0) { POINT3D pt_origin; VECTOR3D_INITXYZ(&pt_origin, 0, 0, 0); VECTOR3D vn; //ÓҲüôÃæ VECTOR3D_INITXYZ(&vn, 1, 0, 11); PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1); //×ó²Ã¼ôÃæ VECTOR3D_INITXYZ(&vn, -1, 0, 1); PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1); //ÉϲüôÃæ VECTOR3D_INITXYZ(&vn, 0, 1, 1); PLANE3D_Init(&cam->tp_clip_plane, &pt_origin, &vn, 1); //ϲüôÃæ VECTOR3D_INITXYZ(&vn, 0, -1, 1); PLANE3D_Init(&cam->bt_clip_plane, &pt_origin, &vn, 1); } else { POINT3D pt_origin; VECTOR3D_INITXYZ(&pt_origin, 0, 0, 0); VECTOR3D vn; VECTOR3D_INITXYZ(&vn, cam->view_dist, 0, cam->viewplane_width / 2.0f); PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1); VECTOR3D_INITXYZ(&vn, -cam->view_dist, 0, cam->viewplane_width / 2.0f); PLANE3D_Init(&cam->lt_clip_plane, &pt_origin, &vn, 1); VECTOR3D_INITXYZ(&vn, 0, cam->view_dist, cam->viewplane_width / 2.0f); PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1); VECTOR3D_INITXYZ(&vn, 0, -cam->view_dist, cam->viewplane_width / 2.0f); PLANE3D_Init(&cam->rt_clip_plane, &pt_origin, &vn, 1); } }