diff --git a/src/display/camera/camera.c b/src/display/camera/camera.c index 087f169..4a1f6af 100644 --- a/src/display/camera/camera.c +++ b/src/display/camera/camera.c @@ -48,6 +48,91 @@ void cameraInitOrthographic(camera_t *camera) { void cameraPushMatrix(camera_t *camera) { assertNotNull(camera, "Not a camera component"); + #if DOLPHIN + Mtx44 guProjection; + Mtx guView; + Mtx modelView; + + switch(camera->projType) { + case CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC: + guOrtho( + guProjection, + camera->orthographic.left, + camera->orthographic.right, + camera->orthographic.bottom, + camera->orthographic.top, + camera->nearClip, + camera->farClip + ); + break; + + case CAMERA_PROJECTION_TYPE_PERSPECTIVE: + case CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED: + guPerspective( + guProjection, + // FOV is in degrees. + camera->perspective.fov * (180.0f / GLM_PIf), + (float_t)frameBufferGetWidth(FRAMEBUFFER_BOUND) / + (float_t)frameBufferGetHeight(FRAMEBUFFER_BOUND), + camera->nearClip, + camera->farClip + ); + break; + + default: + assertUnreachable("Invalid camera projection type"); + } + + switch(camera->viewType) { + case CAMERA_VIEW_TYPE_LOOKAT: + guVector eye = { + camera->lookat.position[0], + camera->lookat.position[1], + camera->lookat.position[2] + }; + guVector up = { + camera->lookat.up[0], + camera->lookat.up[1], + camera->lookat.up[2] + }; + guVector look = { + camera->lookat.target[0], + camera->lookat.target[1], + camera->lookat.target[2] + }; + guLookAt(guView, &eye, &up, &look); + break; + + case CAMERA_VIEW_TYPE_MATRIX: + break; + + case CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT: + break; + + case CAMERA_VIEW_TYPE_2D: + break; + + default: + assertUnreachable("Invalid camera view type"); + } + + // Set Projection Matrix + GX_LoadProjectionMtx( + guProjection, + camera->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC ? + GX_ORTHOGRAPHIC : + GX_PERSPECTIVE + ); + + // Set view and model matrix. Dunno how I'll handle models but whatever. + guMtxIdentity(modelView); + guMtxTransApply(modelView, modelView, 0.0F, 0.0F, 0.0F); + guMtxConcat(guView,modelView,modelView); + GX_LoadPosMtxImm(modelView, GX_PNMTX0); + + return; + #endif + mat4 projection; mat4 view; diff --git a/src/display/display.c b/src/display/display.c index 2c8373b..c155bc5 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -16,9 +16,45 @@ #include "debug/debug.h" #include "display/text.h" #include "assert/assert.h" +#include "util/memory.h" + +#include +#include +#include +#include +#include display_t DISPLAY = { 0 }; +static vu8 readyForCopy; +PADStatus pads[4]; +Mtx view; +Mtx44 projection; +#define FIFO_SIZE (256*1024) + +float_t vertices[] ATTRIBUTE_ALIGN(32) = { + 0, 15, 0, + -15, -15, 0, + 15, -15, 0}; + + +u8 colors[] ATTRIBUTE_ALIGN(32) = { + 255, 0, 0, 255, + 0, 255, 0, 255, + 0, 0, 255, 255 +}; + +static void copy_buffers(u32 count __attribute__ ((unused))) +{ + if (readyForCopy==GX_TRUE) { + GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE); + GX_SetColorUpdate(GX_TRUE); + GX_CopyDisp(DISPLAY.frameBuffer,GX_TRUE); + GX_Flush(); + readyForCopy = GX_FALSE; + } +} + errorret_t displayInit(void) { #if DISPLAY_SDL2 uint32_t flags = SDL_INIT_VIDEO; @@ -81,11 +117,55 @@ errorret_t displayInit(void) { VIDEO_Configure(DISPLAY.screenMode); VIDEO_SetNextFramebuffer(DISPLAY.frameBuffer); + VIDEO_SetPostRetraceCallback(copy_buffers); VIDEO_SetBlack(FALSE); VIDEO_Flush(); - VIDEO_WaitVSync(); - if(DISPLAY.screenMode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync(); + // VIDEO_WaitVSync(); + // if(DISPLAY.screenMode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync(); + DISPLAY.fifoBuffer = MEM_K0_TO_K1(memalign(32, FIFO_SIZE)); + memoryZero(DISPLAY.fifoBuffer, FIFO_SIZE); + + GX_Init(DISPLAY.fifoBuffer, FIFO_SIZE); + + // This seems to be mostly related to interlacing vs progressive + GX_SetDispCopyYScale( + (f32)DISPLAY.screenMode->xfbHeight / + (f32)DISPLAY.screenMode->efbHeight + ); + GX_SetScissor(0,0,DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->efbHeight); + GX_SetDispCopySrc(0,0,DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->efbHeight); + GX_SetDispCopyDst(DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->xfbHeight); + GX_SetCopyFilter(DISPLAY.screenMode->aa,DISPLAY.screenMode->sample_pattern, + GX_TRUE,DISPLAY.screenMode->vfilter); + GX_SetFieldMode(DISPLAY.screenMode->field_rendering, ( + (DISPLAY.screenMode->viHeight == 2 * DISPLAY.screenMode->xfbHeight) ? + GX_ENABLE : GX_DISABLE + )); + + // Setup cull modes + GX_SetCullMode(GX_CULL_NONE); + GX_SetDispCopyGamma(GX_GM_1_0); + + //? + GX_CopyDisp(DISPLAY.frameBuffer, GX_TRUE); + + // Set up view and projection matrices + guPerspective(projection, 60, 1.33F, 10.0F, 300.0F); + GX_LoadProjectionMtx(projection, GX_PERSPECTIVE); + + // Prepare Vertex descriptor + GX_ClearVtxDesc(); + GX_SetVtxDesc(GX_VA_POS, GX_INDEX8); + GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8); + GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0); + GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0); + GX_SetArray(GX_VA_POS, vertices, 3*sizeof(float_t)); + GX_SetArray(GX_VA_CLR0, colors, 4*sizeof(u8)); + GX_SetNumChans(1); + GX_SetNumTexGens(0); + GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0); + GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR); #endif quadInit(); @@ -145,6 +225,37 @@ errorret_t displayUpdate(void) { ); errorChain(sceneRender()); + GX_InvVtxCache(); + GX_InvalidateTexAll(); + + // This seems to setup camera + // guVector camera = {0.0F, 0.0F, 0.0F}; + // guVector up = {0.0F, 1.0F, 0.0F}; + // guVector look = {0.0F, 0.0F, -1.0F}; + // guLookAt(view, &camera, &up, &look); + // Mtx modelView; + // guMtxIdentity(modelView); + // guMtxTransApply(modelView, modelView, 0.0F, 0.0F, -50.0F); + // guMtxConcat(view,modelView,modelView); + // // This seems to load the matrix into the GX pipeline? + // GX_LoadPosMtxImm(modelView, GX_PNMTX0); + + camera_t camera; + cameraInitPerspective(&camera); + camera.lookat.position[0] = 32; + camera.lookat.position[1] = 32; + camera.lookat.position[2] = 32; + cameraPushMatrix(&camera); + GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3); + GX_Position1x8(0); + GX_Color1x8(0); + GX_Position1x8(1); + GX_Color1x8(1); + GX_Position1x8(2); + GX_Color1x8(2); + GX_End(); + cameraPopMatrix(); + // Render UI uiRender(); @@ -160,8 +271,11 @@ errorret_t displayUpdate(void) { debugPrint("GL Error: %d\n", err); } #elif DOLPHIN + GX_DrawDone(); + readyForCopy = GX_TRUE; + VIDEO_WaitVSync(); - if(DISPLAY.screenMode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync(); + // if(DISPLAY.screenMode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync(); #endif diff --git a/src/display/displaydefs.h b/src/display/displaydefs.h index 4b28e41..2e60b77 100644 --- a/src/display/displaydefs.h +++ b/src/display/displaydefs.h @@ -6,6 +6,7 @@ */ #pragma once +#include "dusk.h" #if DISPLAY_SDL2 #include diff --git a/src/display/framebuffer.c b/src/display/framebuffer.c index 26339e2..1dbc476 100644 --- a/src/display/framebuffer.c +++ b/src/display/framebuffer.c @@ -103,10 +103,7 @@ int32_t frameBufferGetHeight(const framebuffer_t *framebuffer) { void frameBufferBind(const framebuffer_t *framebuffer) { if(framebuffer == NULL) { - #if DISPLAY_SDL2 - frameBufferBind(&FRAMEBUFFER_BACKBUFFER); - #endif - + frameBufferBind(&FRAMEBUFFER_BACKBUFFER); FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER; return; } @@ -131,6 +128,15 @@ void frameBufferBind(const framebuffer_t *framebuffer) { 0, 0, frameBufferGetWidth(framebuffer), frameBufferGetHeight(framebuffer) ); + + #elif DOLPHIN + GX_SetViewport( + 0, 0, + frameBufferGetWidth(framebuffer), + frameBufferGetHeight(framebuffer), + 0, 1 + ); + #endif FRAMEBUFFER_BOUND = framebuffer; @@ -155,6 +161,12 @@ void frameBufferClear(uint8_t flags, color_t color) { } glClear(glFlags); + #elif DOLPHIN + GX_SetCopyClear( + (GXColor){ color.r, color.g, color.b, color.a }, + GX_MAX_Z24 + ); + #endif } diff --git a/src/engine/engine.c b/src/engine/engine.c index 281d0da..0c73218 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -31,7 +31,7 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) { // Init systems. Order is important. timeInit(); inputInit(); - errorChain(assetInit()); + // errorChain(assetInit()); errorChain(localeManagerInit()); errorChain(scriptManagerInit()); errorChain(displayInit()); @@ -41,10 +41,10 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) { backpackInit(); // Run the initial script. - scriptcontext_t ctx; - errorChain(scriptContextInit(&ctx)); - errorChain(scriptContextExecFile(&ctx, "init.dsf")); - scriptContextDispose(&ctx); + // scriptcontext_t ctx; + // errorChain(scriptContextInit(&ctx)); + // errorChain(scriptContextExecFile(&ctx, "init.dsf")); + // scriptContextDispose(&ctx); errorOk(); }