OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/vr_shell/vr_shell.h" |
| 6 |
| 7 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 8 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 9 #include "jni/VrShell_jni.h" |
| 10 #include "ui/gl/gl_bindings.h" |
| 11 #include "ui/gl/init/gl_factory.h" |
| 12 |
| 13 namespace vr_shell { |
| 14 |
| 15 namespace { |
| 16 // Constant taken from treasure_hunt demo. |
| 17 const long kPredictionTimeWithoutVsyncNanos = 50000000; |
| 18 |
| 19 const float kZNear = 0.1f; |
| 20 const float kZFar = 1000.0f; |
| 21 |
| 22 // Content rect in world coordinates. Height and width are currently supplied |
| 23 // as DrawFrame arguments. |
| 24 const gvr::Vec3f kContentRectPositionDefault = {0.0f, 0.0f, -1.0f}; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 ContentRect::ContentRect() { |
| 29 SetIdentity(); |
| 30 } |
| 31 |
| 32 ContentRect::~ContentRect() {} |
| 33 |
| 34 void ContentRect::SetIdentity() { |
| 35 transfrom_to_world.m[0][0] = 1; |
| 36 transfrom_to_world.m[0][1] = 0; |
| 37 transfrom_to_world.m[0][2] = 0; |
| 38 transfrom_to_world.m[0][3] = 0; |
| 39 transfrom_to_world.m[1][0] = 0; |
| 40 transfrom_to_world.m[1][1] = 1; |
| 41 transfrom_to_world.m[1][2] = 0; |
| 42 transfrom_to_world.m[1][3] = 0; |
| 43 transfrom_to_world.m[2][0] = 0; |
| 44 transfrom_to_world.m[2][1] = 0; |
| 45 transfrom_to_world.m[2][2] = 1; |
| 46 transfrom_to_world.m[2][3] = 0; |
| 47 transfrom_to_world.m[3][0] = 0; |
| 48 transfrom_to_world.m[3][1] = 0; |
| 49 transfrom_to_world.m[3][2] = 0; |
| 50 transfrom_to_world.m[3][3] = 1; |
| 51 } |
| 52 |
| 53 void ContentRect::Translate(float x, float y, float z) { |
| 54 transfrom_to_world.m[0][3] += x; |
| 55 transfrom_to_world.m[1][3] += y; |
| 56 transfrom_to_world.m[2][3] += z; |
| 57 } |
| 58 |
| 59 VrShell::VrShell(JNIEnv* env, jobject obj) { |
| 60 j_vr_shell_.Reset(env, obj); |
| 61 } |
| 62 |
| 63 void VrShell::Destroy(JNIEnv* env, |
| 64 const base::android::JavaParamRef<jobject>& obj) { |
| 65 delete this; |
| 66 } |
| 67 |
| 68 bool RegisterVrShell(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); |
| 70 } |
| 71 |
| 72 VrShell::~VrShell() {} |
| 73 |
| 74 void VrShell::GvrInit(JNIEnv* env, |
| 75 const base::android::JavaParamRef<jobject>& obj, |
| 76 jlong native_gvr_api) { |
| 77 gvr_api_ = |
| 78 gvr::GvrApi::WrapNonOwned(reinterpret_cast<gvr_context*>(native_gvr_api)); |
| 79 } |
| 80 |
| 81 void VrShell::InitializeGl(JNIEnv* env, |
| 82 const base::android::JavaParamRef<jobject>& obj, |
| 83 jint texture_data_handle) { |
| 84 gl::init::InitializeGLOneOff(); |
| 85 gvr_api_->InitializeGl(); |
| 86 std::vector<gvr::BufferSpec> specs; |
| 87 specs.push_back(gvr_api_->CreateBufferSpec()); |
| 88 render_size_ = specs[0].GetSize(); |
| 89 swap_chain_.reset(new gvr::SwapChain(gvr_api_->CreateSwapchain(specs))); |
| 90 content_rect_.reset(new ContentRect()); |
| 91 content_rect_->content_texture_handle = |
| 92 reinterpret_cast<int>(texture_data_handle); |
| 93 vr_shell_renderer_.reset(new VrShellRenderer()); |
| 94 buffer_viewport_list_.reset( |
| 95 new gvr::BufferViewportList(gvr_api_->CreateEmptyBufferViewportList())); |
| 96 buffer_viewport_.reset( |
| 97 new gvr::BufferViewport(gvr_api_->CreateBufferViewport())); |
| 98 } |
| 99 |
| 100 void VrShell::DrawFrame(JNIEnv* env, |
| 101 const base::android::JavaParamRef<jobject>& obj) { |
| 102 buffer_viewport_list_->SetToRecommendedBufferViewports(); |
| 103 gvr::Frame frame = swap_chain_->AcquireFrame(); |
| 104 gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow(); |
| 105 target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos; |
| 106 head_pose_ = gvr_api_->GetHeadPoseInStartSpace(target_time); |
| 107 |
| 108 // Content area positioning. |
| 109 content_rect_->SetIdentity(); |
| 110 content_rect_->Translate(kContentRectPositionDefault.x, |
| 111 kContentRectPositionDefault.y, |
| 112 kContentRectPositionDefault.z); |
| 113 |
| 114 gvr::Mat4f left_eye_view_matrix = |
| 115 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_LEFT_EYE), head_pose_); |
| 116 gvr::Mat4f right_eye_view_matrix = |
| 117 MatrixMul(gvr_api_->GetEyeFromHeadMatrix(GVR_RIGHT_EYE), head_pose_); |
| 118 |
| 119 // Bind back to the default framebuffer. |
| 120 frame.BindBuffer(0); |
| 121 |
| 122 // Use culling to remove back faces. |
| 123 glEnable(GL_CULL_FACE); |
| 124 |
| 125 // Enable depth testing. |
| 126 glEnable(GL_DEPTH_TEST); |
| 127 glEnable(GL_SCISSOR_TEST); |
| 128 |
| 129 glClearColor(0.1f, 0.1f, 0.1f, 1.0f); |
| 130 |
| 131 // Enable transparency. |
| 132 glEnable(GL_BLEND); |
| 133 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 134 |
| 135 buffer_viewport_list_->GetBufferViewport(GVR_LEFT_EYE, |
| 136 buffer_viewport_.get()); |
| 137 DrawEye(left_eye_view_matrix, *buffer_viewport_); |
| 138 buffer_viewport_list_->GetBufferViewport(GVR_RIGHT_EYE, |
| 139 buffer_viewport_.get()); |
| 140 DrawEye(right_eye_view_matrix, *buffer_viewport_); |
| 141 |
| 142 frame.Unbind(); |
| 143 frame.Submit(*buffer_viewport_list_, head_pose_); |
| 144 } |
| 145 |
| 146 void VrShell::DrawEye(const gvr::Mat4f& view_matrix, |
| 147 const gvr::BufferViewport& params) { |
| 148 gvr::Recti pixel_rect = |
| 149 CalculatePixelSpaceRect(render_size_, params.GetSourceUv()); |
| 150 glViewport(pixel_rect.left, pixel_rect.bottom, |
| 151 pixel_rect.right - pixel_rect.left, |
| 152 pixel_rect.top - pixel_rect.bottom); |
| 153 glScissor(pixel_rect.left, pixel_rect.bottom, |
| 154 pixel_rect.right - pixel_rect.left, |
| 155 pixel_rect.top - pixel_rect.bottom); |
| 156 |
| 157 view_matrix_ = view_matrix; |
| 158 |
| 159 projection_matrix_ = |
| 160 PerspectiveMatrixFromView(params.GetSourceFov(), kZNear, kZFar); |
| 161 |
| 162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 163 DrawContentRect(); |
| 164 } |
| 165 |
| 166 void VrShell::DrawContentRect() { |
| 167 gvr::Mat4f content_rect_combined_matrix = |
| 168 MatrixMul(view_matrix_, content_rect_->transfrom_to_world); |
| 169 content_rect_combined_matrix = |
| 170 MatrixMul(projection_matrix_, content_rect_combined_matrix); |
| 171 vr_shell_renderer_->GetTexturedQuadRenderer()->Draw( |
| 172 content_rect_->content_texture_handle, content_rect_combined_matrix); |
| 173 } |
| 174 |
| 175 void VrShell::OnPause(JNIEnv* env, |
| 176 const base::android::JavaParamRef<jobject>& obj) { |
| 177 if (gvr_api_ == nullptr) |
| 178 return; |
| 179 gvr_api_->PauseTracking(); |
| 180 } |
| 181 |
| 182 void VrShell::OnResume(JNIEnv* env, |
| 183 const base::android::JavaParamRef<jobject>& obj) { |
| 184 if (gvr_api_ == nullptr) |
| 185 return; |
| 186 gvr_api_->RefreshViewerProfile(); |
| 187 gvr_api_->ResumeTracking(); |
| 188 } |
| 189 |
| 190 // ---------------------------------------------------------------------------- |
| 191 // Native JNI methods |
| 192 // ---------------------------------------------------------------------------- |
| 193 |
| 194 jlong Init(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj) { |
| 195 VrShell* vrShell = new VrShell(env, obj); |
| 196 return reinterpret_cast<intptr_t>(vrShell); |
| 197 } |
| 198 |
| 199 } // namespace vr_shell |
OLD | NEW |