/* Copyright (C) 2001 Sony Computer Entertainment Inc. This file is subject to the terms and conditions of the GNU Library General Public License Version 2. See the file "COPYING.LIB" in the main directory of this archive for more details. */ #include #include #include #include #include "ps2base.h" #include "ps2vpu.h" #include "ps2dma.h" #include "ps2vpufile.h" #include "mathfunc.h" #include "sjoy.h" const float pi = (float)M_PI; const float pi2 = pi * 2; const int IMAGE_SIZE = 256; int obj_switch = 0; IMPORT_VPU_SYMBOL(float *, My_matrix, 0); IMPORT_VPU_SYMBOL(float *, My_rot_trans_matrix, 0); IMPORT_VPU_SYMBOL(float *, My_light_matrix, 0); IMPORT_VPU_SYMBOL(float *, My_lcolor_matrix, 0); IMPORT_VPU_SYMBOL(__u128 *, My_texture1, 0); IMPORT_VPU_SYMBOL(__u128 *, My_cube_start, 0); IMPORT_VPU_SYMBOL(__u128 *, My_torus_start, 0); IMPORT_VPU_SYMBOL(__u128 *, My_dma_start, 0); IMPORT_VPU_SYMBOL(__u128 *, My_dma_next, 0); ps2Samp0FVECTOR camera_p = { 0, 0, -25, 0 }; ps2Samp0FVECTOR camera_zd = { 0, 0, 1, 1 }; ps2Samp0FVECTOR camera_yd = { 0, 1, 0, 1 }; ps2Samp0FVECTOR camera_rot = { 0, 0, 0, 0 }; ps2Samp0FVECTOR light0 = { 0.0, 1.5, 0.5, 0 }; ps2Samp0FVECTOR light1 = { 1.5, -0.5, 0.5, 0 }; ps2Samp0FVECTOR light2 = { -1.5, -0.5, 0.5, 0 }; ps2Samp0FVECTOR color0 = { 0.3, 0.3, 0.8, 0 }; ps2Samp0FVECTOR color1 = { 0.8, 0.3, 0.3, 0 }; ps2Samp0FVECTOR color2 = { 0.3, 0.8, 0.3, 0 }; ps2Samp0FVECTOR ambient = { 0.2, 0.2, 0.2, 0 }; ps2Samp0FVECTOR obj_trans = { 0, 0, 0, 0 }; ps2Samp0FVECTOR obj_rot = { 0, 0, 0, 0 }; ps2Samp0FMATRIX local_world; ps2Samp0FMATRIX world_view; ps2Samp0FMATRIX view_screen; ps2Samp0FMATRIX local_screen; ps2Samp0FMATRIX normal_light; ps2Samp0FMATRIX light_color; ps2Samp0FMATRIX local_light; ps2Samp0FMATRIX local_color; ps2Samp0FMATRIX work; struct ps2_image g_img; TexEnv texenv; void load_teximages( int texad64 ) { ps2_gs_set_image(&g_img, texad64 , IMAGE_SIZE / 64, PS2_GS_PSMCT32, 0, 0, IMAGE_SIZE, IMAGE_SIZE, My_texture1); ps2_gs_load_image(&g_img); } class DemoPs2 : public Ps2Simple { public: virtual void doExtraAcquire() { load_teximages( getNext2k() * 2048 / 64 ); sjoy_open(); } virtual void doExtraRelease() { sjoy_close(); } }; //---------------------------------------------------------------------- void CopySamp0Matrix(float *dst, ps2Samp0FMATRIX *src) { dst[ 0] = (*src)[0][0]; dst[ 1] = (*src)[1][0]; dst[ 2] = (*src)[2][0]; dst[ 3] = (*src)[3][0]; dst[ 4] = (*src)[0][1]; dst[ 5] = (*src)[1][1]; dst[ 6] = (*src)[2][1]; dst[ 7] = (*src)[3][1]; dst[ 8] = (*src)[0][2]; dst[ 9] = (*src)[1][2]; dst[10] = (*src)[2][2]; dst[11] = (*src)[3][2]; dst[12] = (*src)[0][3]; dst[13] = (*src)[1][3]; dst[14] = (*src)[2][3]; dst[15] = (*src)[3][3]; } //---------------------------------------------------------------------- void SetVu1PacketMatrix(void) { CopySamp0Matrix(My_light_matrix, &local_light); CopySamp0Matrix(My_lcolor_matrix, &light_color); CopySamp0Matrix(My_rot_trans_matrix, &local_world); CopySamp0Matrix(My_matrix, &local_screen); } void ChangeElement( int paddata, int mask1, int mask2, float &out, float delta, float min=0, float max=0 ) { if (paddata & mask1 ) out += delta; if (paddata & mask2 ) out -= delta; if ( max || min ) { if ( out < min ) out = min; if ( out > max ) out = max; } else { if ( out < -pi ) out += pi2; if ( out > pi ) out -= pi2; } } void DoInput() { const float delta = 1.0f * (float)M_PI / 180.0f; static int toggle = 0; sjoy_poll(); int paddata = sjoy_get_ps2_button(0); // --- object rotate & change view point --- ChangeElement( paddata, SJOY_PS2_L_DOWN, SJOY_PS2_L_UP, obj_rot[0], delta ); ChangeElement( paddata, SJOY_PS2_L_RIGHT, SJOY_PS2_L_LEFT, obj_rot[1], delta ); ChangeElement( paddata, SJOY_PS2_L1, SJOY_PS2_L2, obj_rot[2], delta ); ChangeElement( paddata, SJOY_PS2_R_DOWN, SJOY_PS2_R_UP, camera_rot[0], delta, -.4*pi, .4*pi ); ChangeElement( paddata, SJOY_PS2_R_RIGHT, SJOY_PS2_R_LEFT, camera_rot[1], delta ); ChangeElement( paddata, SJOY_PS2_R1, SJOY_PS2_R2, camera_p[2], delta*5, -100, -10 ); if (!toggle && (paddata & SJOY_PS2_SELECT)) { obj_switch ^= 1; toggle = 1; } else if (!(paddata & SJOY_PS2_SELECT)) { toggle = 0; } } int main(int argc, char *argv[]) { VPUFILE *vfd; vfd = vpuobj_open("flower.elf", O_DATA_PS2MEM); if (vfd == NULL) { perror("vpuobj_open"); exit(1); } DemoPs2 Ps2; Ps2.init( argc, argv ); ps2_gs_gparam *gp = ps2_gs_get_gparam(); Ps2.setRgbAq( 0x10, 0x10, 0x20 ); int frame = 0; Ps2.start(); for (;;) { DoInput(); // set texture env texenv.draw2( &g_img, 8, 8, PS2_GS_SETREG_ALPHA(0, 1, 0, 1, 0), PS2_GS_SETREG_TEST(0, 0, 0, 0, 0, 0, 1, !Ps2.hasZBuffer() ? PS2_GS_ZALWAYS : PS2_GS_ZGREATER)); // local -> world (rotate)matrix ps2Samp0UnitMatrix(work); ps2Samp0RotMatrix(local_world, work, obj_rot); // color&normal matrix setting ps2Samp0NormalLightMatrix(normal_light, light0, light1, light2); ps2Samp0LightColorMatrix(light_color, color0, color1, color2, ambient); // light(normal) -> local_light matrix ps2Samp0MulMatrix(local_light, normal_light, local_world); // local -> world (rotate&translate)matrix ps2Samp0TransMatrix(local_world, local_world, obj_trans); // world -> view matrix ps2Samp0RotCameraMatrix(world_view, camera_p, camera_zd, camera_yd, camera_rot); // view -> screen matrix ps2Samp0ViewScreenMatrix(view_screen, 512.0f, 1.0f, gp->pixel_ratio, gp->center_x, gp->center_y, 1.0f, !Ps2.hasZBuffer() ? 2 : Ps2.zBufferEnd(), 1.0f, 65536.0f); // local -> screen matrix ps2Samp0MulMatrix(local_screen, view_screen, world_view); SetVu1PacketMatrix(); // use ioctl ((ps2_dmatag *)My_dma_next)->ADDR = obj_switch ? (__u32)My_torus_start : (__u32)My_cube_start; Ps2.dmaStartForVpu1( vfd, (ps2_dmatag *)My_dma_start); Ps2.flip(); } return 0; }