// *********************************************** // GRAPHICS.C - 2d and 3d graphics operations // *********************************************** // **** includes #include "graphics.h" // **** This function transfers a .TIM form main memory to video memory void LoadTimData(u_long tMemAddress) { RECT tRect; GsIMAGE tTim; tMemAddress += 4; GsGetTimInfo((u_long *) tMemAddress, &tTim); tRect.x = tTim.px; tRect.y = tTim.py; tRect.w = tTim.pw; tRect.h = tTim.ph; LoadImage(&tRect, tTim.pixel); if (tTim.pmode>3) { tRect.x = tTim.cx; tRect.y = tTim.cy; tRect.w = tTim.cw; tRect.h = tTim.ch; LoadImage(&tRect, tTim.clut); } DrawSync(0); } // **** This sets up sprite information, such as x,y, what .TIM data to use etc. etc. void SetSpriteInfo(GsSPRITE *tSprite, u_long tMemAddress, long tX, long tY) { GsIMAGE tTim; u_long tMultiplier; GsGetTimInfo((u_long *) (tMemAddress+4), &tTim); tMultiplier = 1<<(2-(tTim.pmode&0x03)); tSprite->x = tX; tSprite->y = tY; tSprite->attribute = 0; BitSet(&tSprite->attribute, (tTim.pmode&0x03)<<24); tSprite->w = tTim.pw * tMultiplier; tSprite->h = tTim.ph; tSprite->tpage = GetTPage((tTim.pmode&0x03), 0, tTim.px, tTim.py); tSprite->u = (tTim.px%64) * tMultiplier; tSprite->v = tTim.py%256; if ((tTim.pmode&0x03)<2) { tSprite->cx = tTim.cx; tSprite->cy = tTim.cy; } tSprite->r = tSprite->g = tSprite->b = 128; tSprite->mx = tSprite->w>>1; tSprite->my = tSprite->h; tSprite->scalex = tSprite->scaley = ONE; tSprite->rotate = 0; } // **** Set up a model void SetModelInfo(Model *tModel, long tX, long tY, long tZ, u_long tMemAddress) { u_long * tMemAddress2 = (u_long *) (tMemAddress); GsMapModelingData(++tMemAddress2); GsInitCoordinate2(WORLD, &tModel->Object_Coord); tMemAddress2 += 2; GsLinkObject4((u_long) tMemAddress2, &tModel->Object_Handler, 0); tModel->Object_Handler.coord2 = &tModel->Object_Coord; tModel->Object_Coord.coord.t[0] = tX; tModel->Object_Coord.coord.t[1] = tY; tModel->Object_Coord.coord.t[2] = tZ; tModel->Object_Coord.flg = 0; } // **** Set viewpoint for scene void SetViewPoint(GsRVIEW2 *tView, long tProjDist, long tRz, long tFromX, long tFromY, long tFromZ, long tToX, long tToY, long tToZ, GsCOORDINATE2 *tRelative) { GsSetProjection(tProjDist); tView->vpx = tFromX; tView->vpy = tFromY; tView->vpz = tFromZ; tView->vrx = tToX; tView->vry = tToY; tView->vrz = tToZ; tView->rz = -tRz; if (tRelative == WORLD) tView->super = WORLD; else tView->super = tRelative; GsSetRefView2(tView); } // **** Draw the model void DrawModel(Model *tModel, GsOT *OTable, u_long tPriority) { MATRIX tLocalWorld, tLocalScreen; GsGetLws(tModel->Object_Handler.coord2, &tLocalWorld, &tLocalScreen); GsSetLsMatrix(&tLocalScreen); GsSortObject4(&tModel->Object_Handler, OTable, tPriority, (u_long*)getScratchAddr(0)); } // **** Draw the model void DrawModel2(Model *tModel, GsOT *OTable, u_long tPriority) { MATRIX tLocalWorld, tLocalScreen; GsGetLws(tModel->Object_Handler.coord2, &tLocalWorld, &tLocalScreen); GsSetLightMatrix(&tLocalWorld); GsSetLsMatrix(&tLocalScreen); GsSortObject4(&tModel->Object_Handler, OTable, tPriority, (u_long*)getScratchAddr(0)); } // **** Rotate Model void RotateModel(Model *tModel, long tXr, long tYr, long tZr) { SVECTOR tOrientation; MATRIX tMatrix; tModel->Object_Coord.coord.m[0][0] = tModel->Object_Coord.coord.m[1][1] = tModel->Object_Coord.coord.m[2][2] = ONE; tModel->Object_Coord.coord.m[0][1] = tModel->Object_Coord.coord.m[0][2] = tModel->Object_Coord.coord.m[1][0] = tModel->Object_Coord.coord.m[1][2] = tModel->Object_Coord.coord.m[2][0] = tModel->Object_Coord.coord.m[2][1] = 0; tModel->Orientation.vx = (tModel->Orientation.vx + tXr+ONE) % ONE; tModel->Orientation.vy = (tModel->Orientation.vy + tYr+ONE) % ONE; tModel->Orientation.vz = (tModel->Orientation.vz + tZr+ONE) % ONE; RotMatrix(&tModel->Orientation, &tMatrix); MulMatrix0(&tModel->Object_Coord.coord, &tMatrix, &tModel->Object_Coord.coord); tModel->Object_Coord.flg = 0; } // **** Translate an object according to the direction it is facing void ReturnMoveForward(Model *tModel, long tX, long tY, long tZ, long *tattX, long *tattZ) { MATRIX tMatrix; // temporary rotation matrix SVECTOR tOneUnitForward, tDirectionResult; // two vectors for rotation tOneUnitForward.vx = tX; // to move forward = no x translation tOneUnitForward.vy = tY; // to move forward = no y translation tOneUnitForward.vz = tZ; // to move forward = 4096 z translation RotMatrix(&tModel->Orientation, &tMatrix); // turn vector into rotation matrix ApplyMatrixSV(&tMatrix, &tOneUnitForward, &tDirectionResult); // multiply our direction vector with // the rotation matrix and give the // result as another direction vector *tattX = tDirectionResult.vx; *tattZ = tDirectionResult.vz; } // **** Translate an object according to the direction it is facing void MoveModelForward(Model *tModel, long tX, long tY, long tZ) { MATRIX tMatrix; // temporary rotation matrix SVECTOR tOneUnitForward, tDirectionResult; // two vectors for rotation tOneUnitForward.vx = tX; // to move forward = no x translation tOneUnitForward.vy = tY; // to move forward = no y translation tOneUnitForward.vz = tZ; // to move forward = 4096 z translation RotMatrix(&tModel->Orientation, &tMatrix); // turn vector into rotation matrix ApplyMatrixSV(&tMatrix, &tOneUnitForward, &tDirectionResult); // multiply our direction vector with // the rotation matrix and give the // result as another direction vector tModel->Object_Coord.coord.t[0] += tDirectionResult.vx; tModel->Object_Coord.coord.t[1] += tDirectionResult.vy; tModel->Object_Coord.coord.t[2] += tDirectionResult.vz; tModel->Object_Coord.flg = 0; }