oculus quest running and android compiling and running

This commit is contained in:
2019-05-30 22:46:26 +02:00
parent d750f80482
commit 9b0f2fe4ac
13 changed files with 284 additions and 196 deletions

View File

@@ -0,0 +1,126 @@
#include "pch.h"
#include "oculus_vr.h"
#include "log.h"
#include "rtt.h"
#include "app.h"
#include <unistd.h>
ovrJava java;
RTT ovr_eyes[3][2];
ovrTextureSwapChain* swap_chain[2];
int swap_chain_count = 0;
int swap_chain_index = 0;
ovrMobile* ovr_context = nullptr;
uint64_t ovr_frame = 0;
void oculus_init(JavaVM* vm, JNIEnv* jni, jobject activity_class)
{
java.Vm = vm;
java.Env = jni;
java.ActivityObject = activity_class;
LOG("init OVR");
const ovrInitParms initParms = vrapi_DefaultInitParms( &java );
int32_t initResult = vrapi_Initialize( &initParms );
if ( initResult != VRAPI_INITIALIZE_SUCCESS )
{
// If intialization failed, vrapi_* function calls will not be available.
exit( 0 );
}
}
void oculus_init_vr(EGLDisplay display, EGLContext context, ANativeWindow* surface)
{
LOG("init swapchain");
int rtt_w = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_WIDTH);
int rtt_h = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_HEIGHT);
LOG("ovr suggested texture size %d %d", rtt_w, rtt_h);
for (int eye = 0; eye < 2; eye++)
{
swap_chain[eye] = vrapi_CreateTextureSwapChain3(VRAPI_TEXTURE_TYPE_2D, GL_RGBA8, rtt_w, rtt_h, 1, 3);
swap_chain_count = vrapi_GetTextureSwapChainLength(swap_chain[eye]);
for (int i = 0; i < swap_chain_count; i++)
{
auto texid = vrapi_GetTextureSwapChainHandle(swap_chain[eye], i);
if (!ovr_eyes[i][eye].create(rtt_w, rtt_h, texid, GL_RGBA8, true))
{
ovr_eyes[i][eye].bindFramebuffer();
ovr_eyes[i][eye].clear({1, 0, 1, 1});
ovr_eyes[i][eye].unbindFramebuffer();
LOG("FAILED create fb for eye %d", eye);
}
else
{
LOG("create eye %d", eye);
}
}
}
LOG("vrapi_DefaultModeParms");
ovrModeParms parms = vrapi_DefaultModeParms( &java );
// No need to reset the FLAG_FULLSCREEN window flag when using a View
//parms.Flags &= ~VRAPI_MODE_FLAG_RESET_WINDOW_FULLSCREEN;
parms.Flags |= VRAPI_MODE_FLAG_NATIVE_WINDOW;
parms.Display = (size_t)display;
parms.WindowSurface = (size_t)surface;
parms.ShareContext = (size_t)context;
LOG("enter vr mode");
ovr_context = vrapi_EnterVrMode(&parms);
if (!ovr_context)
{
LOG("EnterVRMode FAILED");
}
LOG("vr mode entered");
vrapi_SetClockLevels(ovr_context, 2, 2);
vrapi_SetPerfThread(ovr_context, VRAPI_PERF_THREAD_TYPE_MAIN, gettid());
vrapi_SetPerfThread(ovr_context, VRAPI_PERF_THREAD_TYPE_RENDERER, 0);
}
void oculus_draw()
{
// BEGIN OVR
const double predictedDisplayTime = vrapi_GetPredictedDisplayTime(ovr_context, ovr_frame);
const ovrTracking2 tracking = vrapi_GetPredictedTracking2(ovr_context, predictedDisplayTime);
auto layer = vrapi_DefaultLayerProjection2();
//ovrVector4f red = {1, 1, 0, 1};
//auto layer = vrapi_DefaultLayerSolidColorProjection2(&red);
layer.HeadPose = tracking.HeadPose;
for (int eye = 0; eye < 2; eye++)
{
auto& rtt = ovr_eyes[swap_chain_index][eye];
rtt.bindFramebuffer();
rtt.clear({1, 0, 1, 1});
glViewport(0, 0, rtt.getWidth(), rtt.getHeight());
auto proj_ovr = ovrMatrix4f_Transpose(&tracking.Eye[eye].ProjectionMatrix);
glm::mat4 proj = glm::make_mat4(reinterpret_cast<const float*>(&proj_ovr));
auto view_ovr = ovrMatrix4f_Transpose(&tracking.Eye[eye].ViewMatrix);
glm::mat4 view = glm::make_mat4(reinterpret_cast<const float*>(&view_ovr));
auto pose_ovr = ovrMatrix4f_CreateFromQuaternion(&tracking.HeadPose.Pose.Orientation);
auto pose_ovr_tp = ovrMatrix4f_Transpose(&pose_ovr);
glm::mat4 pose = glm::make_mat4(reinterpret_cast<float*>(&pose_ovr_tp));
App::I.vr_draw(proj, view, pose);
rtt.unbindFramebuffer();
layer.Textures[eye].ColorSwapChain = swap_chain[eye];
layer.Textures[eye].SwapChainIndex = swap_chain_index;
layer.Textures[eye].TexCoordsFromTanAngles =
ovrMatrix4f_TanAngleMatrixFromProjection(&tracking.Eye[eye].ProjectionMatrix);
}
layer.Header.Flags |= VRAPI_FRAME_LAYER_FLAG_CHROMATIC_ABERRATION_CORRECTION;
const ovrLayerHeader2 * layers[] = { &layer.Header };
ovrSubmitFrameDescription2 frameDesc = { 0 };
frameDesc.Flags = 0;
frameDesc.SwapInterval = 1;
frameDesc.FrameIndex = ovr_frame;
frameDesc.DisplayTime = predictedDisplayTime;
frameDesc.LayerCount = 1;
frameDesc.Layers = layers;
vrapi_SubmitFrame2(ovr_context, &frameDesc);
ovr_frame++;
swap_chain_index = (swap_chain_index + 1) % swap_chain_count;
}