/*----------------------------------------------------------------------------- Display controller model -----------------------------------------------------------------------------*/ void ModelController(Dynamic* dynamic_ptr,ModelMtx *model_mtx) { Mtx rotate_x; Mtx rotate_y; Mtx rotate_z; /* calculate model coordinates */ guTranslate(&(model_mtx->trans), model_mtx->pos_x, model_mtx->pos_y, model_mtx->pos_z); guScale(&(model_mtx->scale), model_mtx->size, model_mtx->size, model_mtx->size); guRotate(&rotate_x, model_mtx->rot_x, 1.0, 0.0, 0.0); guRotate(&rotate_y, model_mtx->rot_y, 0.0, 1.0, 0.0); guRotate(&rotate_z, model_mtx->rot_z, 0.0, 0.0, 1.0); guMtxCatL(&rotate_x,&rotate_y, &model_mtx->rotate); guMtxCatL(&rotate_z,&model_mtx->rotate,&model_mtx->rotate); /* set model matrix */ gSPMatrix(glist_ptr++, &(model_mtx->trans), G_MTX_MODELVIEW | G_MTX_LOAD | G_MTX_NOPUSH); gSPMatrix(glist_ptr++, &(model_mtx->rotate), G_MTX_MODELVIEW | G_MTX_MUL | G_MTX_NOPUSH); gSPMatrix(glist_ptr++, &(model_mtx->scale), G_MTX_MODELVIEW | G_MTX_MUL | G_MTX_NOPUSH); gSPSegment(glist_ptr++, STATIC_SEGMENT, osVirtualToPhysical(gfx_dlist_buf[0])); /* model display list */ gSPDisplayList(glist_ptr++, cont_finish_dl); }
/*---------------------------------------------------------------------------- gfxRCPIinit Initialize RSP RDP ----------------------------------------------------------------------------*/ void gfxRCPInit(void) { static int rdpinit_flag = 1; /* RDP initialization flag */ /* RSP segment register settings */ gSPSegment(glist_ptr++, 0, 0x0); /* for CPU virtual address use */ /* RSP settings */ gSPDisplayList(glist_ptr++, OS_K0_TO_PHYSICAL(setup_rspstate)); /* initialize RDP (only once) */ if(rdpinit_flag){ gSPDisplayList(glist_ptr++, OS_K0_TO_PHYSICAL(rdpstateinit_dl)); rdpinit_flag = 0; } /* RDP settings */ gSPDisplayList(glist_ptr++, OS_K0_TO_PHYSICAL(setup_rdpstate)); }
void F3D_DList( u32 w0, u32 w1 ) { switch (_SHIFTR( w0, 16, 8 )) { case G_DL_PUSH: gSPDisplayList( w1 ); break; case G_DL_NOPUSH: gSPBranchList( w1 ); break; } }
void gfx_flush(void) { flush_chars(); gSPEndDisplayList(gfx_disp_p++); gSPDisplayList(z64_ctxt.gfx->overlay.p++, gfx_disp); Gfx *disp_w = gfx_disp_w; gfx_disp_w = gfx_disp; gfx_disp = disp_w; gfx_disp_p = gfx_disp; gfx_disp_d = gfx_disp + (GFX_DISP_SIZE + sizeof(*gfx_disp) - 1) / sizeof(*gfx_disp); gfx_synced = 0; }
void F3D_DList( u32 w0, u32 w1 ) { switch (_SHIFTR( w0, 16, 8 )) { case G_DL_PUSH: gSPDisplayList( w1 ); break; case G_DL_NOPUSH: gSPBranchList( w1 ); break; } #ifdef __TRIBUFFER_OPT //since PCi can be changed in gSPDisplayList gSPFlushTriangles(); #endif }
static void drawframe(dynamic_stuff *generate, Gfx **glistpParam, float t) { Gfx *glistp; Vtx *valist[2]; /* Array of vertex lists */ float weights[2]; /* Array of weights */ u16 perspnorm; int i; unsigned char a2; glistp = *glistpParam; /* Copy to local var */ /* * Set up matricies */ /* Z-distance to force for faces */ #define ZDIST (700*4) guPerspective(&(generate->projection), &perspnorm, 60.0, 320.0/240.0, ZDIST-500.0, ZDIST+500.0, 1.0); gSPPerspNormalize(glistp++, perspnorm); /* guRotate(&(generate->modeling_rotate1), 40.0, 1.0, 1.0, 1.0); */ guTranslate(&(generate->modeling_translate), 0.0, 0.0, 0.0); /* -... */ gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->projection)), G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH); gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->modeling_translate)), G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH); /* gSPMatrix(glistp++, OS_K0_TO_PHYSICAL(&(generate->modeling_rotate1)), G_MTX_MODELVIEW|G_MTX_MUL|G_MTX_NOPUSH); */ /* * Morph */ valist[0] = vface1; weights[0] = t; valist[1] = vface2; weights[1] = 1.0 - t; /* Two sets of key verticies, tricnt*3 verticies per set, alpha = 255 */ /* Texture coords come from vface1 */ morph(valist, weights, 2, &(generate->tvface1[0]), tricnt*3, 255, -ZDIST); /* * 2nd set of verticies, same except for different alpha and s,t */ a2 = 255*(1.0-t); /* alpha value */ for (i=0; i<tricnt*3; i++) { generate->tvface2[i] = generate->tvface1[i]; generate->tvface2[i].v.tc[0] = vface2[i].v.tc[0] << 6; generate->tvface2[i].v.tc[1] = vface2[i].v.tc[1] << 6; generate->tvface2[i].v.cn[3] = a2; } /* for i */ /* Pass 1 -- opaque */ gSPSetGeometryMode(glistp++, G_SHADE); gDPPipeSync(glistp++); gDPSetRenderMode(glistp++, G_RM_AA_ZB_OPA_SURF, G_RM_AA_ZB_OPA_SURF2); gSPDisplayList(glistp++, OS_K0_TO_PHYSICAL(dlist1)); /* Pass 2 -- transparent */ gSPSetGeometryMode(glistp++, G_SHADE); gDPPipeSync(glistp++); gDPSetRenderMode(glistp++, G_RM_AA_XLU_SURF, G_RM_AA_XLU_SURF2); gSPDisplayList(glistp++, OS_K0_TO_PHYSICAL(dlist2)); *glistpParam = glistp; /* Copy back from local var */ } /* drawframe */
/* * gameloop -- main loop of game * Called by gameproc; runs in gameThread. * Parameter: rsp_static_addr, the R4300 address where the * rsp_static segment was loaded */ static void gameloop(char *rsp_static_addr) { int oddframe = 0; /* Odd or even rendered frame? Start with even. */ int firstframe = 1; /* First frame? */ dynamic_stuff *generate; /* Dynamic info we're generating now */ Gfx *glistp0; /* Start of this frame's dynamic display list */ Gfx *glistp; /* Current position in dynamic display list */ OSTask *gentask; /* Task we're generating */ float t; /* Weight of 1st set of morph verticies */ int dir; /* Direction in which we're morphing */ int pausecnt; /* Countdown for "stickiness" at morph extremes */ t = 0.0; dir = 1; pausecnt = 0; while (1) { /* Start task on RSP, built in previous iteration of loop. */ if (!firstframe) osSpTaskStart(gentask); /* * Set up pointers to dynamic stuff. We're always generating one set * of dynamic data and drawing another. */ generate = &(dynamic[(oddframe ? 0 : 1)]); /* glist portion of generated dynamic data */ glistp0 = &(generate->glist[0]); glistp = glistp0; /* This one will be incremented as list is built */ /* * Tell RSP where each segment is */ gSPSegment(glistp++, 0, 0x0); /* Physical address segment */ /* Static segment (mapping never changes) */ gSPSegment(glistp++, STATIC_SEG, OS_K0_TO_PHYSICAL(rsp_static_addr)); /* Dynamic segment (mapping changes every frame) */ gSPSegment(glistp++, DYNAMIC_SEG, OS_K0_TO_PHYSICAL(generate)); /* RSP and RDP setup, and screen clear */ gSPDisplayList(glistp++, rspinit_dl); gSPDisplayList(glistp++, rdpinit_dl); #ifdef FB32BIT gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_32b, SCREEN_WD, OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B)); #else gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD, OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B)); #endif gSPDisplayList(glistp++, scrnclr_dl); /* Must set color image again after zbuffer clear */ #ifdef FB32BIT gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_32b, SCREEN_WD, OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B)); #else gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD, OS_K0_TO_PHYSICAL(oddframe ? cfb_A : cfb_B)); #endif /* Set Z-buffer */ gDPSetDepthImage(glistp++, OS_K0_TO_PHYSICAL(zbuffer)); /* * Matrix setup, and call display list that does real work */ drawframe(generate, &glistp, t); /* * Put an END on the top-level display list, and check that we * haven't overflowed the buffer. */ gDPFullSync(glistp++); /* Only need this if you want 'accurate' SP done? */ gSPEndDisplayList(glistp++); #ifdef DEBUG if ((int)(glistp - glistp0) > GLIST_SIZE) { /* does this check work?? */ osSyncPrintf("*** GLIST OVERFLOW ***\n"); /* Could quit here or something */ } #endif /* Update morph parameter t */ if (pausecnt == 0) { t += dir*(1.0/60.0); /* A full 60 frames to do the morph */ if (t < 0.0) {t = 0.0; dir = 1; pausecnt = 60;} if (t > 1.0) {t = 1.0; dir = -1; pausecnt = 60;} } else { pausecnt--; } /* * Build graphics task * Note that all addresses are KSEG0, even if used by the RSP. * Conversion is done by the task routines. */ gentask = &(task[oddframe ? 0 : 1]); gentask->t.type = M_GFXTASK; gentask->t.flags = 0x0; gentask->t.ucode_boot = (u64*) rspbootTextStart; gentask->t.ucode_boot_size = ((int)rspbootTextEnd - (int)rspbootTextStart); gentask->t.ucode = (u64*) gspF3DEX2_xbusTextStart; /* use XBUS */ gentask->t.ucode_data = (u64*) gspF3DEX2_xbusDataStart; gentask->t.ucode_size = 4096; gentask->t.ucode_data_size = 2048; gentask->t.dram_stack = (u64*) dram_stack; gentask->t.dram_stack_size = SP_DRAM_STACK_SIZE64; gentask->t.output_buff = (u64*) NULL; gentask->t.output_buff_size= (u64*) NULL; gentask->t.yield_data_ptr = (u64*) NULL; /* Graphics only - no yielding */ gentask->t.yield_data_size = 0x0; gentask->t.data_ptr = (u64*) glistp0; gentask->t.data_size = ((int) glistp - (int) glistp0); /* * Flush the whole cache. Should just do parts of it. */ osWritebackDCacheAll(); if (!firstframe) { /* Wait for task completion (message from RDP) */ osRecvMesg(&RDPDoneMessageQ, NULL, OS_MESG_BLOCK); /* * Specify frame buffer to be displayed starting at next retrace. * We display the one that we've just finished rendering. */ osViSwapBuffer(oddframe ? cfb_B : cfb_A); } /* * Wait for the retrace before rendering next frame * (otherwise we might overwrite the frame which is being displayed). * But first, make sure that the retrace queue is empty in case we * took too long to render the last frame. */ if (MQ_IS_FULL(&RetraceMessageQ)) osRecvMesg(&RetraceMessageQ, NULL, OS_MESG_NOBLOCK); osRecvMesg(&RetraceMessageQ, NULL, OS_MESG_BLOCK); /* Switch to our other set of variables */ oddframe ^= 1; /* No longer the first frame */ firstframe = 0; } /* while(1) */ } /* gameloop */
/*---------------------------------------------------------------------------* * M A I N *---------------------------------------------------------------------------*/ void Main(void *arg) { u8 draw_frame = 0; u32 objRM; Gfx *gp, *gtop; OSTime rspstart; u32 rsptime, rdptime; bg16seg = (u32)_codeSegmentEnd + (u32)_staticSegmentEnd - (u32)_staticSegmentStart; bg8seg = bg16seg + + (u32)_bg_rgbaSegmentRomEnd - (u32)_bg_rgbaSegmentRomStart; loadSegment(bg16seg, bg8seg); menuInit(); actionInit(); rsptime = rdptime = 0; while(1){ /*------ Start to read the controller. ------*/ osContStartReadData(&siMessageQ); /*------ Wait for the retrace. ------*/ osRecvMesg(&retraceMessageQ, NULL, OS_MESG_BLOCK); /*------ Setting the Bg structure. ------*/ setBg(); /*------ Setting the Object structure. ------*/ setObject(); /*------ Create the Gfx list. ------*/ gtop = gp = glist[draw_frame]; /*------ RSP initialization setting. ------*/ gSPSegment(gp ++, 0, 0x0); gSPSegment(gp ++, STATIC_SEGMENT, _codeSegmentEnd); if (aMenu[MENU_BG_TX_FORMAT]){ gSPSegment(gp ++, BG_SEGMENT, bg8seg); } gDPSetColorImage(gp ++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD, system_cfb[draw_frame]); gSPDisplayList(gp ++, rdpInit_dl); gSPDisplayList(gp ++, clearCfb_dl); gSPDisplayList(gp ++, spriteInit_dl); /*------ Bg output. ------*/ if (aMenu[MENU_BG_TX_FORMAT]){ gDPSetTextureLUT(gp ++, G_TT_RGBA16); gSPObjLoadTxtr(gp ++, &objBgTLUT); } if (aMenu[MENU_BG_SCALABLE] == 0){ /* Unscalable BG plane */ gDPSetRenderMode(gp ++, G_RM_NOOP, G_RM_NOOP2); gDPSetCycleType(gp ++, G_CYC_COPY); gSPBgRectCopy(gp ++, &objBg); } else { /* Scalable BG plane */ gDPSetRenderMode(gp ++, G_RM_SPRITE, G_RM_SPRITE2); gDPSetCycleType(gp ++, G_CYC_1CYCLE); gDPSetTextureFilter(gp ++, RMmodeTable[aMenu[MENU_RENDERMODE]].txtrFilter); if (aMenu[MENU_BG_SCALABLE] == 1){ /* Emulated by CPU */ guS2DEmuSetScissor(0, 0, SCREEN_WD, SCREEN_HT, (RMmodeTable[aMenu[MENU_RENDERMODE]].txtrFilter == G_TF_BILERP)); guS2DEmuBgRect1Cyc(&gp, &objBg); } else { /* GBI command */ gSPObjRenderMode(gp ++, RMmodeTable[aMenu[MENU_RENDERMODE]].objRMode); gSPBgRect1Cyc(gp ++, &objBg); } } gDPPipeSync(gp ++); /*------ Setting the Render Mode. ------*/ objRM = RMmodeTable[aMenu[MENU_RENDERMODE]].objRMode; if (RMmodeTable[aMenu[MENU_RENDERMODE]].cycleType != G_CYC_COPY){ if (!aMenu[MENU_RENDERMODE_2]){ /* Opaque */ if (RMmodeTable[aMenu[MENU_RENDERMODE]].antiAlias){ gDPSetRenderMode(gp ++, G_RM_AA_SPRITE, G_RM_AA_SPRITE2); } else { gDPSetRenderMode(gp ++, G_RM_SPRITE, G_RM_SPRITE2); } } else { /* Translucent */ if (RMmodeTable[aMenu[MENU_RENDERMODE]].antiAlias){ gDPSetRenderMode(gp ++, G_RM_AA_XLU_SPRITE, G_RM_AA_XLU_SPRITE2); } else { gDPSetRenderMode(gp ++, G_RM_XLU_SPRITE, G_RM_XLU_SPRITE2); } gDPSetCombineMode(gp ++, G_CC_MODULATEIA_PRIM, G_CC_MODULATEIA_PRIM); gDPSetPrimColor(gp ++, 0, 0, 255, 255, 255, 128); } } /*------ Setting the Texture Filter and CycleType. ------*/ gDPSetTextureFilter(gp ++, RMmodeTable[aMenu[MENU_RENDERMODE]].txtrFilter); gDPSetCycleType (gp ++, RMmodeTable[aMenu[MENU_RENDERMODE]].cycleType ); /*------ Setting the Texture Window. ------*/ if (aMenu[MENU_OBJ_TX_WINDOW]) objRM |= G_OBJRM_NOTXCLAMP; /*------ Setting the Shrink. ------*/ objRM |= ShrinkTable[aMenu[MENU_OBJ_SHRINK]]; /*------ Setting the Object Render Mode. ------*/ gSPObjRenderMode(gp ++, objRM); /*------ Load setting of the Texture. -----*/ if (!aMenu[MENU_OBJ_TX_TYPE]){ gDPSetTextureLUT(gp ++, G_TT_NONE); gSPObjLoadTxtr(gp ++, (aMenu[MENU_OBJ_TX_LOAD_BY] ? &objTxtrTile_RGBA16 : &objTxtrBlock_RGBA16)); } else { gDPSetTextureLUT(gp ++, G_TT_RGBA16); gSPObjLoadTxtr(gp ++, (aMenu[MENU_OBJ_TX_LOAD_BY] ? &objTxtrTile_CI4 : &objTxtrBlock_CI4)); gSPObjLoadTxtr(gp ++, &objTLUT_CI4); } /*------ Output of Object:Rectangle1. ------*/ gSPObjRectangle(gp ++, &(objRect[0])); if (RMmodeTable[aMenu[MENU_RENDERMODE]].cycleType != G_CYC_COPY){ /*------ Output of Object:Rectangle2. ------*/ gSPObjMatrix(gp ++, &(objMtx[0])); gSPObjSprite(gp ++, &(objRect[1])); /*------ Output of Object:Ball. ------*/ /* Ball is displayed by combining two sprite pieces. Because of this, you need to change the processing method by setting the Texture Filter. If the Texture Filter is PointSample, you don't have to specify SHRINKSIZE; but, If it is Bilerp, it must be SHRINKSIZE_1. When you specify SHRINKSIZE_1, the circumference of Sprite for 0.5 texel is clamped. The area excepted by this clamp becomes a part that the Bilerp process gives no effect. Because you need to load the part of adjoining of Sprite twice in Bilerp, Ball draws only for 64x63 texels. It is important to understand the differences between objBall[1] and objBall[2] well. */ if (!aMenu[MENU_RENDERMODE_2]){ /* Draw one size larger to hide the joining part. (Only opaque.) It became unnecessary after S2DEX 1.05. */ /* objRM |= G_OBJRM_WIDEN; */ } gDPPipeSync(gp ++); gDPSetTextureLUT(gp ++, G_TT_RGBA16); gSPObjLoadTxtr(gp ++, &objBallTLUT); if (RMmodeTable[aMenu[MENU_RENDERMODE]].txtrFilter == G_TF_POINT){ gSPObjRenderMode(gp ++, objRM); gSPObjMatrix(gp ++, &(objMtx[2])); gSPObjLoadTxRectR(gp ++, &(objBall[0])); gSPObjLoadTxRectR(gp ++, &(objBall[1])); gSPObjMatrix(gp ++, &(objMtx[1])); gSPObjLoadTxSprite(gp ++, &(objBall[0])); gSPObjLoadTxSprite(gp ++, &(objBall[1])); } else { gSPObjRenderMode(gp ++, objRM|G_OBJRM_SHRINKSIZE_1); gSPObjMatrix(gp ++, &(objMtx[2])); gSPObjLoadTxRectR(gp ++, &(objBall[0])); gSPObjLoadTxRectR(gp ++, &(objBall[2])); gSPObjMatrix(gp ++, &(objMtx[1])); gSPObjLoadTxSprite(gp ++, &(objBall[0])); gSPObjLoadTxSprite(gp ++, &(objBall[2])); } } /*------ Output the processing meter. ------*/ if (rsptime){ gDPPipeSync(gp ++); gDPSetCycleType(gp ++, G_CYC_FILL); gDPSetRenderMode(gp ++, G_RM_OPA_SURF, G_RM_OPA_SURF2); gDPSetFillColor(gp ++, (GPACK_RGBA5551(128,128,255,1) << 16 | GPACK_RGBA5551(128,128,255,1))); gDPFillRectangle(gp ++, 30, 201, 30+rsptime/100, 202); gDPFillRectangle(gp ++, 30, 205, 30+rdptime/100, 206); gDPSetFillColor(gp ++, (GPACK_RGBA5551(255,255,255,1) << 16 | GPACK_RGBA5551(255,255,255,1))); gDPFillRectangle(gp ++, 30, 200, 30, 207); gDPFillRectangle(gp ++, 30+166, 200, 30+166, 207); } gDPFullSync(gp ++); gSPEndDisplayList(gp ++); /*------ Execute the Gfx task. ------*/ tlist.t.data_ptr = (u64 *)gtop; osWritebackDCache(gtop, ((u32)gp)-((u32)gtop)); rspstart = osGetTime(); osSpTaskStart(&tlist); /*------ Wait for the end. ------*/ osRecvMesg(&rspMessageQ, NULL, OS_MESG_BLOCK); rsptime = OS_CYCLES_TO_NSEC(osGetTime() - rspstart) / 1000; #ifdef RSP_DEBUG /*------ The ASSERT process of the micro-code. ------*/ if (ucCheckAssert()){ ucDebugGfxLogPrint(&tlist); /* Output the process log of RSP, Gfx and DL. */ // ucDebugRdpFifoPrint(&tlist); /* Output the RDP and fifo buffers. */ // ucDebugIMEMPrint(); /* Output IMEM. */ // ucDebugDMEMPrint(); /* Output DMEM. */ ucDebugAssertPrint(); /* Output Assert stop location, etc. */ ucDebugInfoPrint(); /* Output the work area for Debugging. */ while(1); } #endif #if 0 /*------ Output the DEBUG information. ------*/ if (Ac.pad[0].push & Z_TRIG){ ucDebugRdpFifoPrint(&tlist); /* Output the RDP and fifo buffers. */ ucDebugInfoPrint(); /* Output the work area for Debugging. */ } #endif osRecvMesg(&rdpMessageQ, NULL, OS_MESG_BLOCK); rdptime = OS_CYCLES_TO_NSEC(osGetTime() - rspstart) / 1000; /* Switching the Frame. */ osViSwapBuffer(system_cfb[draw_frame]); draw_frame ^= 1; /* Accept the controller data. */ osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK); osContGetReadData(contPad); /* The input process. */ actionUpdate(); } }
static int draw_proc(struct menu_item *item, struct menu_draw_params *draw_params) { static Gfx null_dl = gsSPEndDisplayList(); struct item_data *data = item->data; /* handle input */ if (!input_bind_held(COMMAND_PREVROOM) && !input_bind_held(COMMAND_NEXTROOM)) { uint16_t pad = input_pad(); if (pad & BUTTON_Z) { if (pad & BUTTON_D_UP) data->y += 50.f; if (pad & BUTTON_D_DOWN) data->y += -50.f; if (pad & BUTTON_D_LEFT) { data->x -= cos(data->yaw) * 50.f; data->z += sin(data->yaw) * 50.f; } if (pad & BUTTON_D_RIGHT) { data->x -= cos(data->yaw) * -50.f; data->z += sin(data->yaw) * -50.f; } } else { if (pad & BUTTON_D_UP) { data->x -= sin(data->yaw) * 50.f; data->z -= cos(data->yaw) * 50.f; } if (pad & BUTTON_D_DOWN) { data->x -= sin(data->yaw) * -50.f; data->z -= cos(data->yaw) * -50.f; } if (pad & BUTTON_D_LEFT) data->yaw += .2f; if (pad & BUTTON_D_RIGHT) data->yaw -= .2f; } } /* load resources */ if (data->state == STATE_LOAD) { /* initialize segment table */ z64_stab_t stab = z64_stab; /* load scene */ if (data->scene_index != data->scene_next || !data->scene_file) { if (data->scene_file) free(data->scene_file); data->scene_index = data->scene_next; data->room_index = -1; z64_scene_table_t *scene_entry = &z64_scene_table[data->scene_index]; uint32_t scene_vrom_start = scene_entry->scene_vrom_start; uint32_t scene_vrom_end = scene_entry->scene_vrom_end; uint32_t scene_vrom_size = scene_vrom_end - scene_vrom_start; data->scene_file = malloc(scene_vrom_size); zu_getfile(scene_vrom_start, data->scene_file, scene_vrom_size); } stab.seg[Z64_SEG_SCENE] = MIPS_KSEG0_TO_PHYS(data->scene_file); /* populate room list */ struct zu_file room_files[0x100]; zu_scene_rooms(zu_sr_header(data->scene_file, z64_file.scene_setup_index, &stab), room_files, 0x100, &data->n_rooms, &stab); /* load room */ if (data->room_index != data->room_next || !data->room_file) { if (data->room_file) { free(data->room_file); zu_mesh_destroy(&data->room_mesh); } data->room_index = data->room_next; uint32_t room_vrom_start = room_files[data->room_index].vrom_start; uint32_t room_vrom_end = room_files[data->room_index].vrom_end; uint32_t room_vrom_size = room_vrom_end - room_vrom_start; data->room_file = malloc(room_vrom_size); zu_getfile(room_vrom_start, data->room_file, room_vrom_size); stab.seg[Z64_SEG_ROOM] = MIPS_KSEG0_TO_PHYS(data->room_file); /* populate mesh */ zu_room_mesh(zu_sr_header(data->room_file, z64_file.scene_setup_index, &stab), &data->room_mesh, &stab); /* populate vertex list */ struct zu_vlist vlist; zu_vlist_init(&vlist); stab.seg[0x08] = MIPS_KSEG0_TO_PHYS(&null_dl); stab.seg[0x09] = MIPS_KSEG0_TO_PHYS(&null_dl); stab.seg[0x0A] = MIPS_KSEG0_TO_PHYS(&null_dl); stab.seg[0x0B] = MIPS_KSEG0_TO_PHYS(&null_dl); stab.seg[0x0C] = MIPS_KSEG0_TO_PHYS(&null_dl); stab.seg[0x0D] = MIPS_KSEG0_TO_PHYS(&null_dl); for (int i = 0; i < ZU_MESH_TYPES; ++i) for (int j = 0; j < data->room_mesh.all[i].size; ++j) zu_vlist_add_dl(&vlist, &stab, zu_seg_locate(&stab, data->room_mesh.all[i].dlists[j])); /* compute bounding box */ struct zu_bbox bbox; zu_vlist_bbox(&vlist, &bbox); /* set orientation */ { data->x = (bbox.x1 + bbox.x2) / 2.f; data->y = (bbox.y1 + bbox.y2) / 2.f; data->z = (bbox.z1 + bbox.z2) / 2.f; data->yaw = 0.f; } zu_vlist_destroy(&vlist); } /* proceed to rendering */ data->state = STATE_RENDER; } /* render room */ if (data->state == STATE_RENDER && data->room_file) { /* initialize rcp for rendering rooms */ static void *zbuf = NULL; if (!zbuf) zbuf = memalign(64, 2 * Z64_SCREEN_WIDTH * Z64_SCREEN_HEIGHT); gDisplayListAppend(&data->gfx.poly_opa.p, /* clear z buffer */ gsDPPipeSync(), gsDPSetCycleType(G_CYC_FILL), gsDPSetRenderMode(G_RM_NOOP, G_RM_NOOP2), gsDPSetColorImage(G_IM_FMT_RGBA, G_IM_SIZ_16b, Z64_SCREEN_WIDTH, zbuf), gsDPSetFillColor((GPACK_ZDZ(G_MAXFBZ, 0) << 16) | GPACK_ZDZ(G_MAXFBZ, 0)), gsDPFillRectangle(0, 0, Z64_SCREEN_WIDTH - 1, Z64_SCREEN_HEIGHT - 1), gsDPPipeSync(), gsDPSetColorImage(G_IM_FMT_RGBA, G_IM_SIZ_16b, Z64_SCREEN_WIDTH, ZU_MAKE_SEG(Z64_SEG_CIMG, 0)), gsDPSetDepthImage(zbuf), /* rsp settings */ gsSPSegment(Z64_SEG_SCENE, data->scene_file), gsSPSegment(Z64_SEG_ROOM, data->room_file), gsSPSegment(0x08, &null_dl), gsSPSegment(0x09, &null_dl), gsSPSegment(0x0A, &null_dl), gsSPSegment(0x0B, &null_dl), gsSPSegment(0x0C, &null_dl), gsSPSegment(0x0D, &null_dl), gsSPLoadGeometryMode(G_ZBUFFER | G_SHADE | G_CULL_BACK | G_LIGHTING | G_SHADING_SMOOTH), /* rdp settings */ gsDPSetAlphaCompare(G_AC_NONE), gsDPSetDepthSource(G_ZS_PIXEL), gsDPSetAlphaDither(G_AD_DISABLE), gsDPSetColorDither(G_CD_DISABLE), gsDPSetCombineKey(G_OFF), gsDPSetTextureConvert(G_TC_FILT), gsDPSetTextureFilter(G_TF_BILERP), gsDPSetTextureLOD(G_TL_TILE), gsDPSetTexturePersp(G_TP_PERSP), gsDPSetCycleType(G_CYC_2CYCLE), gsDPPipelineMode(G_PM_NPRIMITIVE), gsDPSetEnvColor(0xFF, 0xFF, 0xFF, 0xFF), gsDPSetFogColor(0x00, 0x00, 0x00, 0x00), gsDPSetScissor(G_SC_NON_INTERLACE, 32, 32, Z64_SCREEN_WIDTH - 32, Z64_SCREEN_HEIGHT - 32), ); /* create projection matrix */ { Mtx m; MtxF mf; MtxF mt; guPerspectiveF(&mf, NULL, atanf(2.f), (float)Z64_SCREEN_WIDTH / (float)Z64_SCREEN_HEIGHT, 50.f, 5000.f, 1.f); { guScaleF(&mt, data->scale, data->scale, data->scale); guMtxCatF(&mt, &mf, &mf); } { guRotateF(&mt, M_PI / 6.f, 1.f, 0.f, 0.f); guMtxCatF(&mt, &mf, &mf); } { guTranslateF(&mt, 0.f, -100.f, -200.f); guMtxCatF(&mt, &mf, &mf); } { guRotateF(&mt, -data->yaw, 0.f, 1.f, 0.f); guMtxCatF(&mt, &mf, &mf); } { guTranslateF(&mt, -data->x, -data->y, -data->z); guMtxCatF(&mt, &mf, &mf); } guMtxF2L(&mf, &m); gSPMatrix(data->gfx.poly_opa.p++, gDisplayListData(&data->gfx.poly_opa.d, m), G_MTX_PROJECTION | G_MTX_LOAD); gSPMatrix(data->gfx.poly_xlu.p++, gDisplayListData(&data->gfx.poly_xlu.d, m), G_MTX_PROJECTION | G_MTX_LOAD); } /* create modelview matrix */ { Mtx m; guMtxIdent(&m); gSPMatrix(data->gfx.poly_opa.p++, gDisplayListData(&data->gfx.poly_opa.d, m), G_MTX_MODELVIEW | G_MTX_LOAD); gSPMatrix(data->gfx.poly_xlu.p++, gDisplayListData(&data->gfx.poly_xlu.d, m), G_MTX_MODELVIEW | G_MTX_LOAD); } /* configure lights */ zu_gfx_inject(&data->gfx); set_lighting(); /* execute scene config */ z64_scene_config_table[z64_scene_table[ z64_game.scene_index].scene_config](&z64_game); zu_gfx_restore(&data->gfx); /* draw scene */ for (int i = 0; i < ZU_MESH_TYPES; ++i) for (int j = 0; j < data->room_mesh.all[i].size; ++j) { if (i == ZU_MESH_OPA || i == ZU_MESH_NEAR) { gSPDisplayList(data->gfx.poly_opa.p++, data->room_mesh.all[i].dlists[j]); } else if (i == ZU_MESH_XLU || i == ZU_MESH_FAR) { gSPDisplayList(data->gfx.poly_xlu.p++, data->room_mesh.all[i].dlists[j]); } } /* draw actors */ if (z64_game.pause_state == 0) { zu_gfx_inject(&data->gfx); z64_DrawActors(&z64_game, &z64_game.actor_ctxt); zu_gfx_restore(&data->gfx); } /* draw additional stuff */ draw_crosshair(item); /* flush */ gfx_disp(gsSPDisplayList(zu_gfx_flush(&data->gfx))); /* restore rcp modes */ gfx_mode_init(); /* draw info */ gfx_mode_set(GFX_MODE_COLOR, GPACK_RGBA8888(0xC0, 0xC0, 0xC0, draw_params->alpha)); gfx_printf(draw_params->font, 36, 44, "scene %i", data->scene_index); gfx_printf(draw_params->font, 36, 44 + menu_get_cell_height(item->owner, 1), "room %i", data->room_index); } /* wait for rendering to finish before unloading */ if (data->state == STATE_UNLOAD) data->state = STATE_LOAD; return 1; }