Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #99 from m4gr3d/add_fov_utilities_methods
Browse files Browse the repository at this point in the history
Add utilities methods to access the fov and viewport bounds for each eye
  • Loading branch information
Fredia Huya-Kouadio authored Apr 11, 2020
2 parents 8ddbac7 + 94c5e32 commit 1394d3d
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,46 @@ func _initialize_ovr_mobile_arvr_interface():
if (ovr_utilities): ovr_utilities = ovr_utilities.new()
if (ovr_hand_tracking): ovr_hand_tracking = ovr_hand_tracking.new()
if (ovr_vr_api_proxy): ovr_vr_api_proxy = ovr_vr_api_proxy.new()

# Connect to the plugin signals
_connect_to_signals()

print("Loaded OVRMobile")
return true
else:
print("Failed to enable OVRMobile")
return false

func _connect_to_signals():
if Engine.has_singleton("OVRMobile"):
var singleton = Engine.get_singleton("OVRMobile")
print("Connecting to OVRMobile signals")
singleton.connect("HeadsetMounted", self, "_on_headset_mounted")
singleton.connect("HeadsetUnmounted", self, "_on_headset_unmounted")
singleton.connect("InputFocusGained", self, "_on_input_focus_gained")
singleton.connect("InputFocusLost", self, "_on_input_focus_lost")
singleton.connect("EnterVrMode", self, "_on_enter_vr_mode")
singleton.connect("LeaveVrMode", self, "_on_leave_vr_mode")
else:
print("Unable to load OVRMobile singleton...")

func _on_headset_mounted():
print("VR headset mounted")

func _on_headset_unmounted():
print("VR headset unmounted")

func _on_input_focus_gained():
print("Input focus gained")

func _on_input_focus_lost():
print("Input focus lost")

func _on_enter_vr_mode():
print("Entered Oculus VR mode")

func _on_leave_vr_mode():
print("Left Oculus VR mode")

# many settings should only be applied once when running; this variable
# gets reset on application start or when it wakes up from sleep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,46 @@ func _initialize_ovr_mobile_arvr_interface():
if (ovr_tracking_transform): ovr_tracking_transform = ovr_tracking_transform.new()
if (ovr_utilities): ovr_utilities = ovr_utilities.new()
if (ovr_vr_api_proxy): ovr_vr_api_proxy = ovr_vr_api_proxy.new()

# Connect to the plugin signals
_connect_to_signals()

print("Loaded OVRMobile")
return true
else:
print("Failed to enable OVRMobile")
return false

func _connect_to_signals():
if Engine.has_singleton("OVRMobile"):
var singleton = Engine.get_singleton("OVRMobile")
print("Connecting to OVRMobile signals")
singleton.connect("HeadsetMounted", self, "_on_headset_mounted")
singleton.connect("HeadsetUnmounted", self, "_on_headset_unmounted")
singleton.connect("InputFocusGained", self, "_on_input_focus_gained")
singleton.connect("InputFocusLost", self, "_on_input_focus_lost")
singleton.connect("EnterVrMode", self, "_on_enter_vr_mode")
singleton.connect("LeaveVrMode", self, "_on_leave_vr_mode")
else:
print("Unable to load OVRMobile singleton...")

func _on_headset_mounted():
print("VR headset mounted")

func _on_headset_unmounted():
print("VR headset unmounted")

func _on_input_focus_gained():
print("Input focus gained")

func _on_input_focus_lost():
print("Input focus lost")

func _on_enter_vr_mode():
print("Entered Oculus VR mode")

func _on_leave_vr_mode():
print("Left Oculus VR mode")

# many settings should only be applied once when running; this variable
# gets reset on application start or when it wakes up from sleep
Expand Down
Binary file modified plugin/libs/godot-lib.3.2.2.alpha.release.aar
Binary file not shown.
43 changes: 42 additions & 1 deletion plugin/src/main/cpp/jni/jni_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@

ovrmobile::OvrMobileSession* get_session() {
return ovrmobile::OvrMobileSession::get_singleton_instance();
}
}

jfloatArray from_ovrVector2f(JNIEnv *env, ovrVector2f vector) {
jfloatArray result = env->NewFloatArray(2);
if (result) {
float vector_data[2];
vector_data[0] = vector.x;
vector_data[1] = vector.y;

env->SetFloatArrayRegion(result, 0, 2, vector_data);
}

return result;
}

jfloatArray from_ovrVector3f(JNIEnv *env, ovrVector3f vector) {
jfloatArray result = env->NewFloatArray(3);
if (result) {
float vector_data[3];
vector_data[0] = vector.x;
vector_data[1] = vector.y;
vector_data[2] = vector.z;

env->SetFloatArrayRegion(result, 0, 3, vector_data);
}

return result;
}

jfloatArray from_ovrVector4f(JNIEnv *env, ovrVector4f vector) {
jfloatArray result = env->NewFloatArray(4);
if (result) {
float vector_data[4];
vector_data[0] = vector.x;
vector_data[1] = vector.y;
vector_data[2] = vector.z;
vector_data[3] = vector.w;

env->SetFloatArrayRegion(result, 0, 4, vector_data);
}
return result;
}
6 changes: 6 additions & 0 deletions plugin/src/main/cpp/jni/jni_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@

ovrmobile::OvrMobileSession* get_session();

jfloatArray from_ovrVector2f(JNIEnv *env, ovrVector2f vector);

jfloatArray from_ovrVector3f(JNIEnv *env, ovrVector3f vector);

jfloatArray from_ovrVector4f(JNIEnv *env, ovrVector4f vector);

#endif // JNI_COMMON_H
24 changes: 24 additions & 0 deletions plugin/src/main/cpp/jni/ovr_mobile_plugin_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jmethodID OvrMobilePluginWrapper::on_headset_mounted_id = nullptr;
jmethodID OvrMobilePluginWrapper::on_headset_unmounted_id = nullptr;
jmethodID OvrMobilePluginWrapper::on_input_focus_gained_id = nullptr;
jmethodID OvrMobilePluginWrapper::on_input_focus_lost_id = nullptr;
jmethodID OvrMobilePluginWrapper::on_enter_vr_mode_id = nullptr;
jmethodID OvrMobilePluginWrapper::on_leave_vr_mode_id = nullptr;

OvrMobilePluginWrapper::OvrMobilePluginWrapper() {}

Expand Down Expand Up @@ -41,6 +43,12 @@ void OvrMobilePluginWrapper::initializeWrapper(JNIEnv* env,

on_input_focus_lost_id = env->GetMethodID(ovr_mobile_plugin_class, "onInputFocusLost", "()V");
ALOG_ASSERT(on_input_focus_lost_id != nullptr, "Unable to find onInputFocusLost");

on_enter_vr_mode_id = env->GetMethodID(ovr_mobile_plugin_class, "onEnterVrMode", "()V");
ALOG_ASSERT(on_enter_vr_mode_id != nullptr, "Unable to find onEnterVrMode");

on_leave_vr_mode_id = env->GetMethodID(ovr_mobile_plugin_class, "onLeaveVrMode", "()V");
ALOG_ASSERT(on_leave_vr_mode_id != nullptr, "Unable to find onLeaveVrMode");
}

void OvrMobilePluginWrapper::uninitializeWrapper(JNIEnv* env) {
Expand All @@ -51,6 +59,8 @@ void OvrMobilePluginWrapper::uninitializeWrapper(JNIEnv* env) {
on_headset_unmounted_id = nullptr;
on_input_focus_gained_id = nullptr;
on_input_focus_lost_id = nullptr;
on_enter_vr_mode_id = nullptr;
on_leave_vr_mode_id = nullptr;
}
}

Expand Down Expand Up @@ -81,4 +91,18 @@ void OvrMobilePluginWrapper::on_input_focus_lost() {
env->CallVoidMethod(ovr_mobile_plugin_instance, on_input_focus_lost_id);
}
}

void OvrMobilePluginWrapper::on_enter_vr_mode() {
if (ovr_mobile_plugin_instance && on_enter_vr_mode_id) {
JNIEnv *env = android_api->godot_android_get_env();
env->CallVoidMethod(ovr_mobile_plugin_instance, on_enter_vr_mode_id);
}
}

void OvrMobilePluginWrapper::on_leave_vr_mode() {
if (ovr_mobile_plugin_instance && on_leave_vr_mode_id) {
JNIEnv *env = android_api->godot_android_get_env();
env->CallVoidMethod(ovr_mobile_plugin_instance, on_leave_vr_mode_id);
}
}
} // namespace ovrmobile
6 changes: 6 additions & 0 deletions plugin/src/main/cpp/jni/ovr_mobile_plugin_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class OvrMobilePluginWrapper {

static void on_input_focus_lost();

static void on_enter_vr_mode();

static void on_leave_vr_mode();

private:
OvrMobilePluginWrapper();
~OvrMobilePluginWrapper();
Expand All @@ -33,6 +37,8 @@ class OvrMobilePluginWrapper {
static jmethodID on_headset_unmounted_id;
static jmethodID on_input_focus_gained_id;
static jmethodID on_input_focus_lost_id;
static jmethodID on_enter_vr_mode_id;
static jmethodID on_leave_vr_mode_id;

};

Expand Down
35 changes: 19 additions & 16 deletions plugin/src/main/cpp/jni/ovr_utilities_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,31 @@
#undef JNI_CLASS_NAME
#define JNI_CLASS_NAME OvrUtilities

namespace {
inline jfloatArray from_ovrVector3f(JNIEnv *env, ovrVector3f vector) {
jfloatArray result = env->NewFloatArray(3);
if (result) {
float vector_data[3];
vector_data[0] = vector.x;
vector_data[1] = vector.y;
vector_data[2] = vector.z;

env->SetFloatArrayRegion(result, 0, 3, vector_data);
}

return result;
}
} // namespace

extern "C" {

JNIEXPORT jfloat JNICALL JNI_METHOD(nativeGetIpd)(JNIEnv *env, jclass clazz) {
return ovrmobile::get_ipd(get_session());
}

JNIEXPORT jint JNICALL JNI_METHOD(nativeGetRenderTargetWidth)(JNIEnv *env, jclass ) {
return get_session()->get_render_target_width();
}

JNIEXPORT jint JNICALL JNI_METHOD(nativeGetRenderTargetHeight)(JNIEnv *env, jclass ) {
return get_session()->get_render_target_height();
}

JNIEXPORT jfloatArray JNICALL JNI_METHOD(nativeGetEyeFov)(JNIEnv *env, jclass, jint eye) {
ovrVector4f eye_fov = get_session()->get_eye_fov(static_cast<ovrEye>(eye));
return from_ovrVector4f(env, eye_fov);
}

JNIEXPORT jfloatArray JNICALL JNI_METHOD(nativeGetEyeViewportBounds)(JNIEnv *env, jclass, jint eye) {
ovrVector4f eye_viewport_bounds = get_session()->get_eye_viewport_bound(
static_cast<ovrEye>(eye));
return from_ovrVector4f(env, eye_viewport_bounds);
}

JNIEXPORT jboolean JNICALL
JNI_METHOD(nativeSetDefaultLayerColorScale)(JNIEnv *env, jclass clazz, jfloat red, jfloat green,
jfloat blue, jfloat alpha) {
Expand Down
26 changes: 26 additions & 0 deletions plugin/src/main/cpp/ovr_mobile_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,30 @@ bool OvrMobileSession::enter_vr_mode() {

vrapi_SetPropertyInt(&java, VRAPI_REORIENT_HMD_ON_CONTROLLER_RECENTER,
VRAPI_TRUE);

// Update the eye fovs.
const ovrTracking2 current_tracking = vrapi_GetPredictedTracking2(ovr, 0.0);
for (int i = 0; i < VRAPI_EYE_COUNT; i++) {
float left_degrees, right_degrees, up_degrees, down_degrees;
ovrMatrix4f_ExtractFov(&current_tracking.Eye[i].ProjectionMatrix,
&left_degrees, &right_degrees, &up_degrees,
&down_degrees);

eye_fovs[i] = {left_degrees, right_degrees, down_degrees, up_degrees};
}

// Setup the eye viewport bounds
eye_viewport_bounds[VRAPI_EYE_LEFT].x = 0.0f;
eye_viewport_bounds[VRAPI_EYE_LEFT].y = 0.0f;
eye_viewport_bounds[VRAPI_EYE_LEFT].z = width / 2.0f;
eye_viewport_bounds[VRAPI_EYE_LEFT].w = height;

eye_viewport_bounds[VRAPI_EYE_RIGHT].x = width / 2.0f;
eye_viewport_bounds[VRAPI_EYE_RIGHT].y = 0.0f;
eye_viewport_bounds[VRAPI_EYE_RIGHT].z = width;
eye_viewport_bounds[VRAPI_EYE_RIGHT].w = height;

OvrMobilePluginWrapper::on_enter_vr_mode();
}

return true;
Expand All @@ -347,6 +371,8 @@ void OvrMobileSession::exit_vr_mode() {
vrapi_LeaveVrMode(ovr);
ovr = nullptr;
ALOGV("Left Oculus Mobile VR mode.");

OvrMobilePluginWrapper::on_leave_vr_mode();
}

if (native_window != nullptr) {
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/main/cpp/ovr_mobile_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ class OvrMobileSession {
return width;
}

/// Get the fov for the given eye.
/// The returned vector is of the form {left_fov_in_degrees, right_fov_in_degrees, down_fov_in_degrees, up_fov_in_degrees}.
ovrVector4f get_eye_fov(ovrEye eye) {
return eye_fovs[eye];
}

/// Get the viewport bound for the given eye.
/// The returned vector is of the form {start_x, start_y, end_x, end_y}.
ovrVector4f get_eye_viewport_bound(ovrEye eye) {
return eye_viewport_bounds[eye];
}

void set_render_target_size_multiplier(double multiplier) {
this->render_target_size_multiplier = multiplier;
}
Expand Down Expand Up @@ -108,6 +120,8 @@ class OvrMobileSession {
uint64_t frame_index = 1;
double predicted_display_time = 0;
bool headset_mounted = true;
ovrVector4f eye_fovs[VRAPI_EYE_COUNT];
ovrVector4f eye_viewport_bounds[VRAPI_EYE_COUNT];

ovrVector4f default_layer_color_scale;

Expand Down
Loading

0 comments on commit 1394d3d

Please sign in to comment.