From 4b1e010ca08d456cfc597ffb1334e65fabc6c39f Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 14 Dec 2022 14:25:55 +0000 Subject: [PATCH] Use calculated projection matrix sorts out weird warping on the Pico. Allows for FOV to be overridden still for some effects (force speed) --- Projects/Android/jni/JKVR/JKVR_SurfaceView.cpp | 15 ++++++++++++++- Projects/Android/jni/JKVR/VrCommon.h | 3 +++ Projects/Android/jni/OpenJK/Android.mk | 2 +- .../Android/jni/OpenJK/code/cgame/cg_view.cpp | 1 + .../Android/jni/OpenJK/code/client/cl_cin.cpp | 2 ++ .../Android/jni/OpenJK/code/client/cl_main.cpp | 1 + .../Android/jni/OpenJK/code/rd-common/tr_public.h | 1 + .../Android/jni/OpenJK/code/rd-common/tr_types.h | 1 + .../jni/OpenJK/code/rd-gles/tr_backend.cpp | 8 +++----- .../Android/jni/OpenJK/code/rd-gles/tr_local.h | 2 ++ .../Android/jni/OpenJK/code/rd-gles/tr_main.cpp | 7 +++++++ .../Android/jni/OpenJK/code/rd-gles/tr_scene.cpp | 1 + Projects/Android/jni/OpenJK/code/ui/ui_shared.cpp | 1 + .../Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp | 1 + .../Android/jni/OpenJK/codeJK2/cgame/cg_view.cpp | 1 + 15 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Projects/Android/jni/JKVR/JKVR_SurfaceView.cpp b/Projects/Android/jni/JKVR/JKVR_SurfaceView.cpp index 8b0b617..b8bdda2 100644 --- a/Projects/Android/jni/JKVR/JKVR_SurfaceView.cpp +++ b/Projects/Android/jni/JKVR/JKVR_SurfaceView.cpp @@ -2130,6 +2130,20 @@ void JKVR_prepareEyeBuffer(int eye ) VR_ClearFrameBuffer(frameBuffer->ColorSwapChain.Width, frameBuffer->ColorSwapChain.Height); } +bool JKVR_GetVRProjection(int eye, float zNear, float zFar, float* projection) +{ + if (!vr.cgzoommode) + { + XrMatrix4x4f_CreateProjectionFov( + &(gAppState.ProjectionMatrices[eye]), GRAPHICS_OPENGL_ES, + gAppState.Projections[eye].fov, zNear, zFar); + memcpy(projection, gAppState.ProjectionMatrices[eye].m, 16 * sizeof(float)); + return true; + } + + return false; +} + void JKVR_finishEyeBuffer(int eye ) { ovrRenderer *renderer = &gAppState.Renderer; @@ -2366,7 +2380,6 @@ void JKVR_submitFrame() XrPosef xfHeadFromEye = gAppState.Projections[eye].pose; XrPosef xfStageFromEye = XrPosef_Multiply(gAppState.xfStageFromHead, xfHeadFromEye); viewTransform[eye] = XrPosef_Inverse(xfStageFromEye); - fov.angleLeft += gAppState.Projections[eye].fov.angleLeft / 2.0f; fov.angleRight += gAppState.Projections[eye].fov.angleRight / 2.0f; fov.angleUp += gAppState.Projections[eye].fov.angleUp / 2.0f; diff --git a/Projects/Android/jni/JKVR/VrCommon.h b/Projects/Android/jni/JKVR/VrCommon.h index b57bbeb..54e2af5 100644 --- a/Projects/Android/jni/JKVR/VrCommon.h +++ b/Projects/Android/jni/JKVR/VrCommon.h @@ -194,6 +194,8 @@ typedef struct GLboolean SessionActive; XrPosef xfStageFromHead; XrView* Projections; + XrMatrix4x4f ProjectionMatrices[2]; + float currentDisplayRefreshRate; float* SupportedDisplayRefreshRates; @@ -246,6 +248,7 @@ void VR_Recenter(); //Called from engine code bool JKVR_useScreenLayer(); +bool JKVR_GetVRProjection(int eye, float zNear, float zFar, float* projection); void JKVR_GetScreenRes(int *width, int *height); void JKVR_InitActions( void ); void JKVR_Vibrate(int duration, int channel, float intensity ); diff --git a/Projects/Android/jni/OpenJK/Android.mk b/Projects/Android/jni/OpenJK/Android.mk index a8ccc2a..4c1da15 100644 --- a/Projects/Android/jni/OpenJK/Android.mk +++ b/Projects/Android/jni/OpenJK/Android.mk @@ -5,7 +5,7 @@ LOCAL_PATH := $(call my-dir) #OPENXR_HMD = -DMETA_QUEST OPENXR_HMD = -DPICO_XR -JK3_BASE_CFLAGS = $(OPENXR_HMD) -DHAVE_GLES -DFINAL_BUILD -fexceptions -Wall -Wno-write-strings -Wno-comment -fno-caller-saves -fno-tree-vectorize -Wno-unused-but-set-variable -fvisibility=hidden +JK3_BASE_CFLAGS = $(OPENXR_HMD) -O1 -DHAVE_GLES -DFINAL_BUILD -fexceptions -Wall -Wno-write-strings -Wno-comment -fno-caller-saves -fno-tree-vectorize -Wno-unused-but-set-variable -fvisibility=hidden JK3_BASE_CPPFLAGS = -fvisibility-inlines-hidden -Wno-invalid-offsetof -fvisibility=hidden JK3_BASE_LDLIBS = diff --git a/Projects/Android/jni/OpenJK/code/cgame/cg_view.cpp b/Projects/Android/jni/OpenJK/code/cgame/cg_view.cpp index 100bf00..ce2953a 100644 --- a/Projects/Android/jni/OpenJK/code/cgame/cg_view.cpp +++ b/Projects/Android/jni/OpenJK/code/cgame/cg_view.cpp @@ -1317,6 +1317,7 @@ qboolean CG_CalcFOVFromX( float fov_x ) } // set it + cg.refdef.override_fov = true; cg.refdef.fov_x = fov_x; cg.refdef.fov_y = fov_y; diff --git a/Projects/Android/jni/OpenJK/code/client/cl_cin.cpp b/Projects/Android/jni/OpenJK/code/client/cl_cin.cpp index 89736c0..7d09295 100644 --- a/Projects/Android/jni/OpenJK/code/client/cl_cin.cpp +++ b/Projects/Android/jni/OpenJK/code/client/cl_cin.cpp @@ -1612,6 +1612,7 @@ static void CIN_AddTextCrawl() refdef.rdflags = RDF_NOWORLDMODEL; AxisClear( refdef.viewaxis ); + refdef.override_fov = true; refdef.fov_x = 130; refdef.fov_y = 130; @@ -2035,6 +2036,7 @@ void SCR_AddCreditTextCrawl() refdef.rdflags = RDF_NOWORLDMODEL; AxisClear( refdef.viewaxis ); + refdef.override_fov = true; refdef.fov_x = 150; refdef.fov_y = 150; diff --git a/Projects/Android/jni/OpenJK/code/client/cl_main.cpp b/Projects/Android/jni/OpenJK/code/client/cl_main.cpp index 9090f4b..ebaa527 100644 --- a/Projects/Android/jni/OpenJK/code/client/cl_main.cpp +++ b/Projects/Android/jni/OpenJK/code/client/cl_main.cpp @@ -1192,6 +1192,7 @@ void CL_InitRef( void ) { rit.saved_game = &ojk::SavedGame::get_instance(); rit.JKVR_useScreenLayer = JKVR_useScreenLayer; + rit.JKVR_GetVRProjection = JKVR_GetVRProjection; ret = GetRefAPI( REF_API_VERSION, &rit ); diff --git a/Projects/Android/jni/OpenJK/code/rd-common/tr_public.h b/Projects/Android/jni/OpenJK/code/rd-common/tr_public.h index 6943867..0378fee 100644 --- a/Projects/Android/jni/OpenJK/code/rd-common/tr_public.h +++ b/Projects/Android/jni/OpenJK/code/rd-common/tr_public.h @@ -129,6 +129,7 @@ typedef struct { //JKVR Functions bool (*JKVR_useScreenLayer) ( void ); + bool (*JKVR_GetVRProjection) (int eye, float zNear, float zFar, float* projection); } refimport_t; diff --git a/Projects/Android/jni/OpenJK/code/rd-common/tr_types.h b/Projects/Android/jni/OpenJK/code/rd-common/tr_types.h index ced4ea7..630b3a7 100644 --- a/Projects/Android/jni/OpenJK/code/rd-common/tr_types.h +++ b/Projects/Android/jni/OpenJK/code/rd-common/tr_types.h @@ -195,6 +195,7 @@ typedef enum { typedef struct { int x, y, width, height; + bool override_fov; float fov_x, fov_y; vec3_t vieworg; vec3_t viewaxis[3]; // transformation matrix diff --git a/Projects/Android/jni/OpenJK/code/rd-gles/tr_backend.cpp b/Projects/Android/jni/OpenJK/code/rd-gles/tr_backend.cpp index 0322308..6cda4cc 100644 --- a/Projects/Android/jni/OpenJK/code/rd-gles/tr_backend.cpp +++ b/Projects/Android/jni/OpenJK/code/rd-gles/tr_backend.cpp @@ -1430,11 +1430,9 @@ const void *RB_DrawBuffer( const void *data ) { const drawBufferCommand_t *cmd; cmd = (const drawBufferCommand_t *)data; - /* -#ifndef HAVE_GLES - qglDrawBuffer( cmd->buffer ); -#endif -*/ + + tr.stereoFrame = (stereoFrame_t )cmd->buffer; + // clear screen for debugging if ( r_clear->integer ) { qglClearColor( 0, 0, 0, 1 ); diff --git a/Projects/Android/jni/OpenJK/code/rd-gles/tr_local.h b/Projects/Android/jni/OpenJK/code/rd-gles/tr_local.h index 5ec2c97..179aa84 100644 --- a/Projects/Android/jni/OpenJK/code/rd-gles/tr_local.h +++ b/Projects/Android/jni/OpenJK/code/rd-gles/tr_local.h @@ -79,6 +79,7 @@ typedef struct { // as well as the locally generated scene information typedef struct { int x, y, width, height; + bool override_fov; float fov_x, fov_y; vec3_t vieworg; vec3_t viewaxis[3]; // transformation matrix @@ -1017,6 +1018,7 @@ typedef struct { model_t *currentModel; viewParms_t viewParms; + stereoFrame_t stereoFrame; float identityLight; // 1.0 / ( 1 << overbrightBits ) int identityLightByte; // identityLight * 255 diff --git a/Projects/Android/jni/OpenJK/code/rd-gles/tr_main.cpp b/Projects/Android/jni/OpenJK/code/rd-gles/tr_main.cpp index c73638d..ab5cb94 100644 --- a/Projects/Android/jni/OpenJK/code/rd-gles/tr_main.cpp +++ b/Projects/Android/jni/OpenJK/code/rd-gles/tr_main.cpp @@ -539,6 +539,13 @@ void R_SetupProjection( void ) { zNear = r_znear->value; zFar = tr.viewParms.zFar; + if (!tr.refdef.override_fov && + ri.JKVR_GetVRProjection((int)tr.stereoFrame, zNear, zFar, tr.viewParms.projectionMatrix)) + { + return; + } + + ymax = zNear * tan( tr.refdef.fov_y * M_PI / 360.0f ); ymin = -ymax; diff --git a/Projects/Android/jni/OpenJK/code/rd-gles/tr_scene.cpp b/Projects/Android/jni/OpenJK/code/rd-gles/tr_scene.cpp index 5e3ad36..0badabd 100644 --- a/Projects/Android/jni/OpenJK/code/rd-gles/tr_scene.cpp +++ b/Projects/Android/jni/OpenJK/code/rd-gles/tr_scene.cpp @@ -295,6 +295,7 @@ void RE_RenderScene( const refdef_t *fd ) { tr.refdef.y = fd->y; tr.refdef.width = fd->width; tr.refdef.height = fd->height; + tr.refdef.override_fov = fd->override_fov; tr.refdef.fov_x = fd->fov_x; tr.refdef.fov_y = fd->fov_y; diff --git a/Projects/Android/jni/OpenJK/code/ui/ui_shared.cpp b/Projects/Android/jni/OpenJK/code/ui/ui_shared.cpp index a6e3fd3..517f9a0 100644 --- a/Projects/Android/jni/OpenJK/code/ui/ui_shared.cpp +++ b/Projects/Android/jni/OpenJK/code/ui/ui_shared.cpp @@ -7164,6 +7164,7 @@ void Item_Model_Paint(itemDef_t *item) //refdef.fov_x = (modelPtr->fov_x) ? modelPtr->fov_x : w; //refdef.fov_y = (modelPtr->fov_y) ? modelPtr->fov_y : h; + refdef.override_fov = true; refdef.fov_x = 45; refdef.fov_y = 45; diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp index 5c56d6e..e0211e5 100644 --- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp +++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp @@ -85,6 +85,7 @@ static void CG_Draw3DModel( float x, float y, float w, float h, qhandle_t model, AxisClear( refdef.viewaxis ); + refdef.override_fov = true; refdef.fov_x = 30; refdef.fov_y = 30; diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_view.cpp b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_view.cpp index 54bb60c..d5d94e1 100644 --- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_view.cpp +++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_view.cpp @@ -1335,6 +1335,7 @@ float CG_ForceSpeedFOV( float infov ) fov = infov;//+amt; } + cg.refdef.override_fov = true; return fov; } /*