#include "inc.h" #include #include #include #include #include #include #include #include #include #include "camera.h" //#define STRIP_TEST //#define DUMP_TXD #define CDROM uint frame; void draw(void); void dumpDma(uint *packet, int data); rw::EngineOpenParams engineOpenParams; SceneStruct Scene; CCamera Cam; char* GetModulePath(char *dst, char *module) { #ifdef CDROM sprintf(dst, "cdrom0:\\modules\\%s;1", module); for(char *p = dst+7; *p; p++) if(islower(*p)) *p = toupper(*p); #else sprintf(dst, "host0:/usr/local/sce/iop/modules/%s", module); #endif return dst; } char* GetFilePath(char *dst, char *file) { #ifdef CDROM sprintf(dst, "cdrom0:\\%s;1", file); for(char *p = dst+7; *p; p++) { if(islower(*p)) *p = toupper(*p); if(*p == '/') *p = '\\'; } #else sprintf(dst, "host0:%s", file); #endif return dst; } rw::Camera* CameraCreate(rw::int32 width, rw::int32 height, rw::bool32 z) { rw::Camera *cam; cam = rw::Camera::create(); cam->setFrame(rw::Frame::create()); cam->frameBuffer = rw::Raster::create(width, height, 0, rw::Raster::CAMERA); cam->zBuffer = rw::Raster::create(width, height, 0, rw::Raster::ZBUFFER); return cam; } void toStrip(rw::Clump *clump) { FORLIST(lnk, clump->atomics){ rw::Atomic *atm = rw::Atomic::fromClump(lnk); atm->geometry->flags |= rw::Geometry::TRISTRIP; atm->geometry->buildMeshes(); } } int Init3D(void) { char buf[128]; uint32 len; uint8 *data; // rw::Image::setSearchPath("host0:files/"); // rw::Image::setSearchPath("host0:spyro/"); rw::StreamMemory in; #if 1 #if 1 data = rw::getFileContents(GetFilePath(buf, "files/textures.txd"), &len); // data = rw::getFileContents(GetFilePath(buf, "files/od_hotels1.txd", &len); // data = rw::getFileContents(GetFilePath(buf, "files/admiral.txd", &len); if(data == nil) return 1; in.open(data, len); rw::findChunk(&in, rw::ID_TEXDICTIONARY, NULL, NULL); rw::TexDictionary *txd = rw::TexDictionary::streamRead(&in); assert(txd); rw::TexDictionary::setCurrent(txd); in.close(); rwFree(data); #endif data = rw::getFileContents(GetFilePath(buf, "files/world.dff"), &len); // data = rw::getFileContents(GetFilePath(buf, "files/admiral.dff", &len); // data = rw::getFileContents(GetFilePath(buf, "files/od_newscafe_dy.dff", &len); // data = rw::getFileContents(GetFilePath(buf, "files/teapot.dff", &len); // data = rw::getFileContents(GetFilePath(buf, "files/sphere.dff", &len); if(data == nil) return 1; in.open(data, len); rw::findChunk(&in, rw::ID_CLUMP, NULL, NULL); Scene.clump = rw::Clump::streamRead(&in); assert(Scene.clump); FORLIST(lnk, Scene.clump->atomics){ rw::Atomic *a = rw::Atomic::fromClump(lnk); char *nodename = rw::gta::getNodeName(a->getFrame()); if(strstr(nodename, "_vlo") || strstr(nodename, "_dam") || strstr(nodename, "_lo_")) a->object.object.flags &= ~rw::Atomic::RENDER; } in.close(); rwFree(data); data = rw::getFileContents(GetFilePath(buf, "files/sky.dff"), &len); if(data == nil) return 1; in.open(data, len); rw::findChunk(&in, rw::ID_CLUMP, NULL, NULL); Scene.backgroundClump = rw::Clump::streamRead(&in); assert(Scene.backgroundClump); in.close(); rwFree(data); rw::ps2::doModulate2 = 1; // spyro #else static #include "teapot.inc" in.open(files_teapot_dff, files_teapot_dff_len); rw::findChunk(&in, rw::ID_CLUMP, NULL, NULL); Scene.clump = rw::Clump::streamRead(&in); in.close(); #endif if(Scene.clump == nil) return 1; #ifdef STRIP_TEST toStrip(Scene.clump); #endif #ifdef DUMP_TXD rw::StreamFile out; if(out.open("host0:files/textures.txd", "wb")) { rw::TexDictionary::getCurrent()->streamWrite(&out); out.close(); } #endif // Scene.tex = rw::Texture::read("texture", nil); // Scene.tex = rw::Texture::read("texture32", nil); // Scene.tex = rw::Texture::read("texture32", "mask"); Scene.world = rw::World::create(); Scene.ambient = rw::Light::create(rw::Light::AMBIENT); Scene.ambient->setColor(0.3f, 0.3f, 0.3f); Scene.world->addLight(Scene.ambient); rw::V3d xaxis = { 1.0f, 0.0f, 0.0f }; Scene.direct = rw::Light::create(rw::Light::DIRECTIONAL); Scene.direct->setColor(0.8f, 0.8f, 0.8f); Scene.direct->setFrame(rw::Frame::create()); Scene.direct->getFrame()->rotate(&xaxis, -120.0f, rw::COMBINEREPLACE); Scene.world->addLight(Scene.direct); Cam.m_rwcam = CameraCreate(SCREEN_WIDTH, SCREEN_HEIGHT, 1); // Cam.m_rwcam->setNearPlane(1.5f); Cam.m_rwcam->setNearPlane(0.2f); Cam.m_rwcam->setFarPlane(450.0f); // Cam.m_position.set(3.0f, 3.0f, 1.0f); // Cam.m_position.set(0.0f, -7.0f, 0.0f); // Cam.m_position.set(30.0f, 30.0f, 1.0f); // Cam.m_target.set(0.0f, 0.0f, 0.0f); // spyro world Cam.m_target.set(0.0f, -10.0f, 10.0f); Cam.m_position.set(-10.0f, 5.0f, 13.0f); Scene.camera = Cam.m_rwcam; Cam.m_aspectRatio = 4.0f/3.0f; Scene.world->addCamera(Scene.camera); } void pluginattach(void) { rw::ps2::registerPDSPlugin(40); rw::ps2::registerPluginPDSPipes(); rw::registerMeshPlugin(); rw::registerNativeDataPlugin(); rw::registerAtomicRightsPlugin(); rw::registerMaterialRightsPlugin(); rw::xbox::registerVertexFormatPlugin(); rw::registerSkinPlugin(); rw::registerHAnimPlugin(); rw::registerMatFXPlugin(); rw::registerUVAnimPlugin(); rw::ps2::registerADCPlugin(); rw::gta::registerNodeNamePlugin(); } int InitRW(void) { rw::ps2::adcHack = true; rw::ps2::renderdevice.clearCamera = rw::ps2::clearCamera; rw::ps2::renderdevice.beginUpdate = rw::ps2::beginUpdate; rw::ps2::renderdevice.endUpdate = rw::ps2::endUpdate; rw::ps2::renderdevice.setRenderState = rw::ps2::setRenderState; rw::ps2::renderdevice.getRenderState = rw::ps2::getRenderState; rw::ps2::renderdevice.im2DRenderPrimitive = rw::ps2::renderPrim_VU; rw::ps2::renderdevice.im2DRenderIndexedPrimitive = rw::ps2::renderIndexedPrim_VU; rw::ps2::renderdevice.system = rw::ps2::deviceSystem; #if 1 rw::ps2::renderdevice.im3DTransform = rw::ps2::vuIm3DTransform; rw::ps2::renderdevice.im3DRenderIndexedPrimitive = rw::ps2::vuIm3DRenderIndexed; rw::ps2::renderdevice.im3DEnd = rw::ps2::vuIm3DEnd; #else rw::ps2::renderdevice.im3DTransform = rw::genIm3DTransform; rw::ps2::renderdevice.im3DRenderIndexedPrimitive = rw::genIm3DRenderIndexed; rw::ps2::renderdevice.im3DEnd = rw::genIm3DEnd; #endif if(!rw::Engine::init(nil)) return 1; pluginattach(); if(!rw::Engine::open(&engineOpenParams)) return 1; rw::engine->filefuncs.rwfopen = ps2fopen; rw::engine->filefuncs.rwfclose = ps2fclose; rw::engine->filefuncs.rwfseek = ps2fseek; rw::engine->filefuncs.rwftell = ps2ftell; rw::engine->filefuncs.rwfread = ps2fread; rw::engine->filefuncs.rwfwrite = ps2fwrite; rw::engine->filefuncs.rwfeof = ps2feof; if(!rw::Engine::start()) return 1; rw::ps2::defaultObjPipe->impl.render = rw::ps2::defaultAtomicRender; rw::matFXGlobals.pipelines[rw::PLATFORM_PS2] = rw::ps2::defaultObjPipe; return 0; } int main() { uint128 tmp; char buf[128]; path2Textures = 0; synchTextures = 0; gateTextures = 1; sceSifInitRpc(0); #ifdef CDROM while(!sceSifRebootIop("cdrom0:\\MODULES\\"IOP_IMAGE_FILE";1")); while(!sceSifSyncIop()); sceSifInitRpc(0); sceSifLoadFileReset(); sceCdInit(SCECdINIT); sceCdMmode(SCECdCD); sceFsReset(); sceSifInitIopHeap(); #endif while(sceSifLoadModule(GetModulePath(buf, "sio2man.irx"), 0, NULL) < 0) printf("can't load module sio2man\n"); while(sceSifLoadModule(GetModulePath(buf, "padman.irx"), 0, NULL) < 0) printf("can't load module padman\n"); gotPadirx = true; InitPad(); if(InitRW()) return 1; Init3D(); rw::SetRenderState(rw::FOGCOLOR, 0xFFFFFFFF); // rw::SetRenderState(rw::FOGCOLOR, 0xFF0000FF); frame = 0; // rw::RGBA clearcol = { 64, 64, 64, 255 }; rw::RGBA clearcol = { 82, 186, 68, 255 }; // spyro green float cpuTime, drawTime, synchTime, finishTime; for(;;){ finishTime = finishCycle/294912.0f; printf("%.3f %.3f %.3f %.3f\n", cpuTime, drawTime, finishTime, synchTime); StartTime(); rw::ps2::beginFrame(frame); UpdatePad(&pad); Cam.process(&pad); Cam.update(); Scene.camera->clear(&clearcol, rw::Camera::CLEARIMAGE|rw::Camera::CLEARZ); Scene.camera->beginUpdate(); draw(); Scene.camera->endUpdate(); MAKEQ(tmp, VIFdirect + 2, VIFnop, 0, DMAcnt + 2); vifPacket[vifPacksz++].q_u128 = tmp; MAKE128(tmp, 0xe, SCE_GIF_SET_TAG(1, 1, 0,0, SCE_GIF_PACKED, 1)); vifPacket[vifPacksz++].q_u128 = tmp; MAKE128(tmp, SCE_GS_FINISH, 0); vifPacket[vifPacksz++].q_u128 = tmp; cpuTime = GetTimeF(); rw::ps2::endFrame(&drawTime, &synchTime); if(pad.now.start) while(!pad.rising.cross) UpdatePad(&pad); frame++; } return 0; }