Skip to content

Commit

Permalink
fix 360 degree wrong orientation (or provide workaround with gvr layout)
Browse files Browse the repository at this point in the history
  • Loading branch information
di57mec committed Sep 17, 2019
1 parent 353be80 commit b3d2a56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/src/main/cpp/GLRenderer/GLRMono360.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void GLRMono360::onSurfaceChanged(int width, int height) {
void GLRMono360::onDrawFrame() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
cpuFrameTime.start();
mMatricesM.calculateNewHeadPoseIfNeeded(gvr_api_.get(),0);
mMatricesM.calculateNewHeadPose360(gvr_api_.get(),0);
Matrices& worldMatrices=mMatricesM.getWorldMatrices();
mVideoRenderer->drawVideoCanvas360(worldMatrices.monoViewTracked,worldMatrices.projection360);
Expand Down
42 changes: 33 additions & 9 deletions app/src/main/cpp/Other/MatricesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//#define TAG "HeadTrackerExtended"
//#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)

constexpr auto NANO_TO_MS=1000*1000;

static glm::mat4 toGLM(const gvr::Mat4f &matrix) {
glm::mat4 result;
Expand All @@ -35,12 +35,13 @@ gvr::Mat4f MatrixMul(const glm::mat4x4 &m1, const gvr::Mat4f &m2) {
memcpy(reinterpret_cast<float*>(&ret.m), reinterpret_cast<float*>(&tm2[0]), 16 * sizeof(float));
return ret;
}

static const std::string toString(const glm::mat4x4 matrix){
std::stringstream ss;
ss<<"\n";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
ss<<matrix[i][j]<<" ";
ss<<matrix[i][j]<<",";
}
ss<<"\n";
}
Expand Down Expand Up @@ -81,7 +82,7 @@ void MatricesManager::calculateNewHeadPoseIfNeeded(gvr::GvrApi *gvr_api, const i
return;
}
gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow();
target_time.monotonic_system_time_nanos+=predictMS*1000*1000;
target_time.monotonic_system_time_nanos+=predictMS*NANO_TO_MS;
worldMatrices.lastHeadPos=gvr_api->GetHeadSpaceFromStartSpaceRotation(target_time);
//we only apply the neck model when we calculate the view M for 1PP
if(settingsVR.GHT_MODE==MODE_1PP){
Expand Down Expand Up @@ -112,21 +113,44 @@ void MatricesManager::calculateNewHeadPoseIfNeeded(gvr::GvrApi *gvr_api, const i
}*/
worldMatrices.leftEyeViewTracked=glm::translate(headView,glm::vec3(-settingsVR.VR_InterpupilaryDistance/2.0f,0,0));
worldMatrices.rightEyeViewTracked=glm::translate(headView,glm::vec3(settingsVR.VR_InterpupilaryDistance/2.0f,0,0));

//LOGD("%s",toString(worldMatrices.leftEyeViewTracked).c_str());
}

//The 360 degree mono renderer only uses the last known head translation as 'view' matrix
void MatricesManager::calculateNewHeadPose360(gvr::GvrApi *gvr_api, const int predictMS) {
gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow();
target_time.monotonic_system_time_nanos+=predictMS*1000*1000;
target_time.monotonic_system_time_nanos+=predictMS*NANO_TO_MS;
const gvr::Mat4f tmpHeadPose = gvr_api->GetHeadSpaceFromStartSpaceTransform(target_time);
gvr_api->ApplyNeckModel(tmpHeadPose,1);
worldMatrices.monoViewTracked=toGLM(tmpHeadPose);
//glm::mat4 tmpM2=toGLM(tmpM);
//tmpM = MatrixMul(worldMatrices.monoForward360, tmpM);
//glm::mat4x4 headView=glm::make_mat4(reinterpret_cast<const float*>(&tmpM.m));
//headView=glm::transpose(headView);
//worldMatrices.monoViewTracked=headView;
worldMatrices.monoViewTracked=glm::transpose(worldMatrices.monoViewTracked);

worldMatrices.monoViewTracked=worldMatrices.leftEyeViewTracked;

//float aaa[16]={1,-1.94429e-07,5.55111e-17,0,
// 1.94429e-07,1,1.66533e-16,0,
// -5.55112e-17,-1.66533e-16,1,0,
// -0.1,1.94429e-08,-5.55111e-18,1,};
//worldMatrices.monoViewTracked=glm::make_mat4x4(aaa);
}

/*float rot=0;
void MatricesManager::calculateNewHeadPose360_fixRot(gvr::GvrApi *gvr_api, const int predictMS) {
gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow();
target_time.monotonic_system_time_nanos+=predictMS*NANO_TO_MS;
gvr::Mat4f tmpM = gvr_api->GetHeadSpaceFromStartSpaceTransform(target_time);
tmpM = MatrixMul(worldMatrices.monoForward360, tmpM);
gvr_api->ApplyNeckModel(tmpM,1);
glm::mat4x4 headView=glm::make_mat4(reinterpret_cast<const float*>(&tmpM.m));
headView=glm::transpose(headView);
worldMatrices.monoViewTracked=glm::translate(headView,glm::vec3(0,0,0.0));
//worldMatrices.monoViewTracked=glm::mat4();
//worldMatrices.monoViewTracked=glm::rotate(worldMatrices.monoViewTracked,rot,glm::vec3(0,0,1));
//rot+=0.02f;
}*/

/*fov=100;
float left=fov/2.0f;
float right=fov/2.0f;
Expand Down
54 changes: 33 additions & 21 deletions app/src/main/java/constantin/fpv_vr/APlay/AMono360.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.vr.cardboard.DisplaySynchronizer;
import com.google.vr.ndk.base.GvrApi;
import com.google.vr.ndk.base.GvrLayout;

import constantin.fpv_vr.AirHeadTrackingSender;
import constantin.fpv_vr.GLRenderer.GLRMono360;
Expand All @@ -21,30 +22,42 @@
public class AMono360 extends AppCompatActivity {
private Context mContext;
private GLSurfaceView mGLView;
private GvrApi gvrApi;
private GLRMono360 mGLRenderer;
private AirHeadTrackingSender airHeadTrackingSender;
private TelemetryReceiver telemetryReceiver;
//either create gvr api directly or use gvr layout as wrapper - first one introduces bug, e.g. "wrong 360° video orientation"
private static final boolean useGvrLayout=true;
private GvrApi gvrApi;
private GvrLayout gvrLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext=this;
PerformanceHelper.enableSustainedPerformanceIfPossible(this);
mGLView = new GLSurfaceView(this);
mGLView.setEGLContextClientVersion(2);
mGLView.setEGLConfigChooser(new MyEGLConfigChooser(false, SJ.MultiSampleAntiAliasing(this),true));
mGLView.setEGLWindowSurfaceFactory(new MyEGLWindowSurfaceFactory());

//mGLView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
mGLView.setPreserveEGLContextOnPause(true);
gvrApi = new GvrApi(this, new DisplaySynchronizer(this,getWindowManager().getDefaultDisplay()));
if(SJ.EnableAHT(mContext)){
airHeadTrackingSender=new AirHeadTrackingSender(this,gvrApi);
if(useGvrLayout){
gvrLayout=new GvrLayout(this);
gvrLayout.setAsyncReprojectionEnabled(false);
gvrLayout.setStereoModeEnabled(false);
gvrLayout.setPresentationView(mGLView);
}else{
gvrApi = new GvrApi(this, new DisplaySynchronizer(this,getWindowManager().getDefaultDisplay()));
gvrApi.reconnectSensors();
gvrApi.clearError();
gvrApi.recenterTracking();
}
telemetryReceiver=new TelemetryReceiver(this);
mGLRenderer =new GLRMono360(mContext,telemetryReceiver,gvrApi);
mGLRenderer =new GLRMono360(mContext,telemetryReceiver,useGvrLayout ? gvrLayout.getGvrApi() : gvrApi);
mGLView.setRenderer(mGLRenderer);
setContentView(mGLView);
if(useGvrLayout){
setContentView(gvrLayout);
}else{
setContentView(mGLView);
}
}


Expand All @@ -56,33 +69,32 @@ protected void onResume() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
telemetryReceiver.startReceiving();
mGLView.onResume();
if(gvrApi!=null){
if(useGvrLayout){
gvrLayout.onResume();
}else{
gvrApi.resumeTracking();
}
if(airHeadTrackingSender!=null){
airHeadTrackingSender.startSendingDataIfEnabled();
}
}

@Override
protected void onPause() {
super.onPause();
//Log.d(TAG, "onPause");
if(gvrApi!=null){
gvrApi.pauseTracking();
}
if(airHeadTrackingSender!=null){
airHeadTrackingSender.stopSendingDataIfEnabled();
}
telemetryReceiver.stopReceiving();
mGLRenderer.onPause();
mGLView.onPause();
if(useGvrLayout){
gvrLayout.onPause();
}else{
gvrApi.pauseTracking();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if(gvrApi!=null){
if(useGvrLayout){
gvrLayout.shutdown();
}else{
gvrApi.shutdown();
}
mGLView=null;
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.1'
classpath 'com.google.gms:google-services:4.3.2'
classpath 'io.fabric.tools:gradle:1.31.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
Expand All @@ -35,7 +35,7 @@ allprojects {
materialVersion = '1.1.0-alpha10'
constraintlayoutVersion='1.1.3'
androidxpreferenceVersion='1.1.0'
mGvrVersion = '1.190.0'
mGvrVersion = '1.180.0'
}
}

Expand Down

0 comments on commit b3d2a56

Please sign in to comment.