| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting.cardboard; | 5 package org.chromium.chromoting.cardboard; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.graphics.Point; | 8 import android.graphics.Point; |
| 9 import android.graphics.PointF; | 9 import android.graphics.PointF; |
| 10 import android.opengl.GLES20; | 10 import android.opengl.GLES20; |
| 11 import android.opengl.Matrix; | 11 import android.opengl.Matrix; |
| 12 | 12 |
| 13 import com.google.vrtoolkit.cardboard.CardboardView; | 13 import com.google.vrtoolkit.cardboard.CardboardView; |
| 14 import com.google.vrtoolkit.cardboard.Eye; | 14 import com.google.vrtoolkit.cardboard.Eye; |
| 15 import com.google.vrtoolkit.cardboard.HeadTransform; | 15 import com.google.vrtoolkit.cardboard.HeadTransform; |
| 16 import com.google.vrtoolkit.cardboard.Viewport; | 16 import com.google.vrtoolkit.cardboard.Viewport; |
| 17 | 17 |
| 18 import org.chromium.chromoting.jni.Client; | 18 import org.chromium.chromoting.jni.JniInterface; |
| 19 | 19 |
| 20 import javax.microedition.khronos.egl.EGLConfig; | 20 import javax.microedition.khronos.egl.EGLConfig; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Renderer for Cardboard view. | 23 * Renderer for Cardboard view. |
| 24 */ | 24 */ |
| 25 public class CardboardRenderer implements CardboardView.StereoRenderer { | 25 public class CardboardRenderer implements CardboardView.StereoRenderer { |
| 26 private static final String TAG = "cr.CardboardRenderer"; | 26 private static final String TAG = "cr.CardboardRenderer"; |
| 27 | 27 |
| 28 private static final int BYTE_PER_FLOAT = 4; | 28 private static final int BYTE_PER_FLOAT = 4; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 58 // the camera position. | 58 // the camera position. |
| 59 // If the user triggers the button while looking faraway, this will cause th
e | 59 // If the user triggers the button while looking faraway, this will cause th
e |
| 60 // desktop to be re-positioned in the center of the view. | 60 // desktop to be re-positioned in the center of the view. |
| 61 private static final float FARAWAY_ANGLE_RATIO = 1.6777f; | 61 private static final float FARAWAY_ANGLE_RATIO = 1.6777f; |
| 62 | 62 |
| 63 // Small number used to avoid division-overflow or other problems with | 63 // Small number used to avoid division-overflow or other problems with |
| 64 // floating-point imprecision. | 64 // floating-point imprecision. |
| 65 private static final float EPSILON = 1e-5f; | 65 private static final float EPSILON = 1e-5f; |
| 66 | 66 |
| 67 private final Activity mActivity; | 67 private final Activity mActivity; |
| 68 private final Client mClient; | |
| 69 | 68 |
| 70 private float mCameraPosition; | 69 private float mCameraPosition; |
| 71 | 70 |
| 72 // Lock to allow multithreaded access to mCameraPosition. | 71 // Lock to allow multithreaded access to mCameraPosition. |
| 73 private final Object mCameraPositionLock = new Object(); | 72 private final Object mCameraPositionLock = new Object(); |
| 74 | 73 |
| 75 private float[] mCameraMatrix; | 74 private float[] mCameraMatrix; |
| 76 private float[] mViewMatrix; | 75 private float[] mViewMatrix; |
| 77 private float[] mProjectionMatrix; | 76 private float[] mProjectionMatrix; |
| 78 | 77 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 97 private Photosphere mPhotosphere; | 96 private Photosphere mPhotosphere; |
| 98 private Cursor mCursor; | 97 private Cursor mCursor; |
| 99 | 98 |
| 100 // Lock for eye position related operations. | 99 // Lock for eye position related operations. |
| 101 // This protects access to mEyeDesktopPosition. | 100 // This protects access to mEyeDesktopPosition. |
| 102 private final Object mEyeDesktopPositionLock = new Object(); | 101 private final Object mEyeDesktopPositionLock = new Object(); |
| 103 | 102 |
| 104 // Flag to indicate whether to show menu bar. | 103 // Flag to indicate whether to show menu bar. |
| 105 private boolean mMenuBarVisible; | 104 private boolean mMenuBarVisible; |
| 106 | 105 |
| 107 public CardboardRenderer(Activity activity, Client client) { | 106 public CardboardRenderer(Activity activity) { |
| 108 mActivity = activity; | 107 mActivity = activity; |
| 109 mClient = client; | |
| 110 mCameraPosition = 0.0f; | 108 mCameraPosition = 0.0f; |
| 111 | 109 |
| 112 mCameraMatrix = new float[16]; | 110 mCameraMatrix = new float[16]; |
| 113 mViewMatrix = new float[16]; | 111 mViewMatrix = new float[16]; |
| 114 mProjectionMatrix = new float[16]; | 112 mProjectionMatrix = new float[16]; |
| 115 mDesktopModelMatrix = new float[16]; | 113 mDesktopModelMatrix = new float[16]; |
| 116 mDesktopCombinedMatrix = new float[16]; | 114 mDesktopCombinedMatrix = new float[16]; |
| 117 mEyePointModelMatrix = new float[16]; | 115 mEyePointModelMatrix = new float[16]; |
| 118 mEyePointCombinedMatrix = new float[16]; | 116 mEyePointCombinedMatrix = new float[16]; |
| 119 mPhotosphereCombinedMatrix = new float[16]; | 117 mPhotosphereCombinedMatrix = new float[16]; |
| 120 | 118 |
| 121 mForwardVector = new float[3]; | 119 mForwardVector = new float[3]; |
| 122 } | 120 } |
| 123 | 121 |
| 124 private void initializeRedrawCallback() { | 122 private void initializeRedrawCallback() { |
| 125 mActivity.runOnUiThread(new Runnable() { | 123 mActivity.runOnUiThread(new Runnable() { |
| 126 public void run() { | 124 public void run() { |
| 127 mClient.provideRedrawCallback(new Runnable() { | 125 JniInterface.provideRedrawCallback(new Runnable() { |
| 128 @Override | 126 @Override |
| 129 public void run() { | 127 public void run() { |
| 130 mDesktop.reloadTexture(); | 128 mDesktop.reloadTexture(); |
| 131 mCursor.reloadTexture(); | 129 mCursor.reloadTexture(); |
| 132 } | 130 } |
| 133 }); | 131 }); |
| 134 | 132 |
| 135 mClient.redrawGraphics(); | 133 JniInterface.redrawGraphics(); |
| 136 } | 134 } |
| 137 }); | 135 }); |
| 138 } | 136 } |
| 139 | 137 |
| 140 @Override | 138 @Override |
| 141 public void onSurfaceCreated(EGLConfig config) { | 139 public void onSurfaceCreated(EGLConfig config) { |
| 142 // Set the background clear color to black. | 140 // Set the background clear color to black. |
| 143 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | 141 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 144 | 142 |
| 145 // Use culling to remove back faces. | 143 // Use culling to remove back faces. |
| 146 GLES20.glEnable(GLES20.GL_CULL_FACE); | 144 GLES20.glEnable(GLES20.GL_CULL_FACE); |
| 147 | 145 |
| 148 // Enable depth testing. | 146 // Enable depth testing. |
| 149 GLES20.glEnable(GLES20.GL_DEPTH_TEST); | 147 GLES20.glEnable(GLES20.GL_DEPTH_TEST); |
| 150 | 148 |
| 151 mDesktop = new Desktop(mClient); | 149 mDesktop = new Desktop(); |
| 152 mMenuBar = new MenuBar(mActivity); | 150 mMenuBar = new MenuBar(mActivity); |
| 153 mPhotosphere = new Photosphere(mActivity); | 151 mPhotosphere = new Photosphere(mActivity); |
| 154 mCursor = new Cursor(mClient); | 152 mCursor = new Cursor(); |
| 155 | 153 |
| 156 initializeRedrawCallback(); | 154 initializeRedrawCallback(); |
| 157 } | 155 } |
| 158 | 156 |
| 159 @Override | 157 @Override |
| 160 public void onSurfaceChanged(int width, int height) { | 158 public void onSurfaceChanged(int width, int height) { |
| 161 } | 159 } |
| 162 | 160 |
| 163 @Override | 161 @Override |
| 164 public void onNewFrame(HeadTransform headTransform) { | 162 public void onNewFrame(HeadTransform headTransform) { |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 synchronized (mCameraPositionLock) { | 422 synchronized (mCameraPositionLock) { |
| 425 theta = Math.atan(mDesktop.getHalfWidth() | 423 theta = Math.atan(mDesktop.getHalfWidth() |
| 426 / (DESKTOP_POSITION_Z - mCameraPosition)); | 424 / (DESKTOP_POSITION_Z - mCameraPosition)); |
| 427 } | 425 } |
| 428 | 426 |
| 429 // Calculate current looking angle. | 427 // Calculate current looking angle. |
| 430 double phi = Math.atan(mForwardVector[0] / mForwardVector[2]); | 428 double phi = Math.atan(mForwardVector[0] / mForwardVector[2]); |
| 431 | 429 |
| 432 return Math.abs(phi) > FARAWAY_ANGLE_RATIO * Math.abs(theta); | 430 return Math.abs(phi) > FARAWAY_ANGLE_RATIO * Math.abs(theta); |
| 433 } | 431 } |
| 434 } | 432 } |
| OLD | NEW |