Commit 1ce4ff3d authored by cjgrant's avatar cjgrant Committed by Commit bot

Add VR Shell animation classes and unit test.

This change includes the animation and easing (intermediate state
calculation) capabilities developed by mthiesse@, and adds initial unit
tests for animation.

BUG=

Review-Url: https://codereview.chromium.org/2335643002
Cr-Commit-Position: refs/heads/master@{#419449}
parent c7397ecf
......@@ -4,19 +4,26 @@
import("//build/config/android/rules.gni")
import("//chrome/common/features.gni")
import("//testing/test.gni")
assert(enable_vr_shell && android_java_ui)
static_library("vr_shell") {
sources = [
"animation.cc",
"animation.h",
"easing.cc",
"easing.h",
"ui_elements.cc",
"ui_elements.h",
"vr_gl_util.cc",
"vr_gl_util.h",
"vr_math.cc",
"vr_math.h",
"vr_shell.cc",
"vr_shell.h",
"vr_shell_renderer.cc",
"vr_shell_renderer.h",
"vr_util.cc",
"vr_util.h",
]
deps = [
......@@ -39,3 +46,17 @@ generate_jni("vr_shell_jni_headers") {
]
jni_package = "vr_shell"
}
test("vr_shell_unittests") {
sources = [
"ui_elements_unittest.cc",
]
deps = [
":vr_shell",
"//base/test:run_all_unittests",
"//base/test:test_support",
"//testing/gtest",
"//ui/gfx/geometry",
]
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/vr_shell/animation.h"
namespace vr_shell {
Animation::Animation(int id,
Property property,
std::unique_ptr<easing::Easing> easing,
std::vector<float> from,
std::vector<float> to,
int64_t start,
int64_t duration)
: id(id),
property(property),
easing(std::move(easing)),
from(from),
to(to),
start(start),
duration(duration) {}
Animation::~Animation() {}
} // namespace vr_shell
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_VR_SHELL_ANIMATION_H_
#define CHROME_BROWSER_ANDROID_VR_SHELL_ANIMATION_H_
#include <memory>
#include <vector>
#include "chrome/browser/android/vr_shell/easing.h"
namespace vr_shell {
// Describes the characteristics of a transition from an initial set of values
// to a final set, with timing and interpolation information. Other classes use
// this information to animate UI element location, size, and other properties.
class Animation {
public:
enum Property {
COPYRECT = 0,
SIZE,
TRANSLATION,
UNUSED, // No longer used, available for a future type.
ROTATION,
};
Animation(int id,
Property property,
std::unique_ptr<easing::Easing> easing,
std::vector<float> from,
std::vector<float> to,
int64_t start,
int64_t duration);
~Animation();
int id;
Property property;
std::unique_ptr<easing::Easing> easing;
std::vector<float> from;
std::vector<float> to;
int64_t start;
int64_t duration;
};
} // namespace vr_shell
#endif // CHROME_BROWSER_ANDROID_VR_SHELL_ANIMATION_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/vr_shell/easing.h"
#include <cmath>
#include "base/logging.h"
namespace vr_shell {
namespace easing {
double Easing::CalculateValue(double input) {
DCHECK(input >= 0.0 && input <= 1.0);
return CalculateValueImpl(input);
}
CubicBezier::CubicBezier(double p1x, double p1y, double p2x, double p2y)
: bezier_(p1x, p1y, p2x, p2y) {}
double CubicBezier::CalculateValueImpl(double state) {
return bezier_.Solve(state);
}
EaseIn::EaseIn(double power) : power_(power) {}
double EaseIn::CalculateValueImpl(double state) {
return pow(state, power_);
}
EaseOut::EaseOut(double power) : power_(power) {}
double EaseOut::CalculateValueImpl(double state) {
return 1.0 - pow(1.0 - state, power_);
}
double Linear::CalculateValueImpl(double state) {
return state;
}
} // namespace easing
} // namespace vr_shell
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_VR_SHELL_EASING_H_
#define CHROME_BROWSER_ANDROID_VR_SHELL_EASING_H_
#include "ui/gfx/geometry/cubic_bezier.h"
namespace vr_shell {
namespace easing {
// Abstract base class for custom interpolators, mapping linear input between
// 0 and 1 to custom values between those two points.
class Easing {
public:
// Compute an output value, given an input between 0 and 1. Output will
// equal input at (at least) points 0 and 1.
double CalculateValue(double input);
virtual ~Easing() {}
protected:
Easing() {}
virtual double CalculateValueImpl(double input) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(Easing);
};
// Linear interpolation generates output equal to input.
class Linear : public Easing {
public:
Linear() = default;
double CalculateValueImpl(double input) override;
private:
DISALLOW_COPY_AND_ASSIGN(Linear);
};
// Computes a cubic-bezier transition based on two control points.
class CubicBezier : public Easing {
public:
CubicBezier(double p1x, double p1y, double p2x, double p2y);
double CalculateValueImpl(double input) override;
private:
gfx::CubicBezier bezier_;
DISALLOW_COPY_AND_ASSIGN(CubicBezier);
};
// Computes |input|^|power|.
class EaseIn : public Easing {
public:
explicit EaseIn(double power);
double CalculateValueImpl(double input) override;
private:
double power_;
DISALLOW_COPY_AND_ASSIGN(EaseIn);
};
// Computes 1 - |input|^|power|.
class EaseOut : public Easing {
public:
explicit EaseOut(double power);
double CalculateValueImpl(double input) override;
private:
double power_;
DISALLOW_COPY_AND_ASSIGN(EaseOut);
};
} // namespace easing
} // namespace vr_shell
#endif // CHROME_BROWSER_ANDROID_VR_SHELL_EASING_H_
......@@ -5,7 +5,8 @@
#include "chrome/browser/android/vr_shell/ui_elements.h"
#include <cmath>
#include <vector>
#include "chrome/browser/android/vr_shell/animation.h"
namespace vr_shell {
......@@ -86,5 +87,95 @@ ContentRectangle::ContentRectangle() = default;
ContentRectangle::~ContentRectangle() = default;
void ContentRectangle::Animate(int64_t time) {
for (auto& it : animations) {
Animation& animation = *it;
if (time < animation.start)
continue;
// If |from| is not specified, start at the current values.
if (animation.from.size() == 0) {
switch (animation.property) {
case Animation::COPYRECT:
animation.from.push_back(copy_rect.x);
animation.from.push_back(copy_rect.y);
animation.from.push_back(copy_rect.width);
animation.from.push_back(copy_rect.height);
break;
case Animation::SIZE:
animation.from.push_back(size.x);
animation.from.push_back(size.y);
animation.from.push_back(size.z);
break;
case Animation::TRANSLATION:
animation.from.push_back(translation.x);
animation.from.push_back(translation.y);
animation.from.push_back(translation.z);
break;
case Animation::ROTATION:
animation.from.push_back(rotation_axis_angle[0]);
animation.from.push_back(rotation_axis_angle[1]);
animation.from.push_back(rotation_axis_angle[2]);
animation.from.push_back(rotation_axis_angle[3]);
break;
case Animation::UNUSED:
break;
}
}
DCHECK_EQ(animation.from.size(), animation.to.size());
std::vector<float> values(animation.from.size());
for (std::size_t i = 0; i < animation.from.size(); ++i) {
if (animation.to[i] == animation.from[i] ||
time >= (animation.start + animation.duration)) {
values[i] = animation.to[i];
continue;
}
double value = animation.easing->CalculateValue(
(double)(time - animation.start) / (double)animation.duration);
values[i] =
animation.from[i] + (value * (animation.to[i] - animation.from[i]));
}
switch (animation.property) {
case Animation::COPYRECT:
DCHECK_EQ(animation.from.size(), 4u);
copy_rect.x = values[0];
copy_rect.y = values[1];
copy_rect.width = values[2];
copy_rect.height = values[3];
break;
case Animation::SIZE:
DCHECK_EQ(animation.from.size(), 3u);
size.x = values[0];
size.y = values[1];
size.z = values[2];
break;
case Animation::TRANSLATION:
DCHECK_EQ(animation.from.size(), 3u);
translation.x = values[0];
translation.y = values[1];
translation.z = values[2];
break;
case Animation::ROTATION:
DCHECK_EQ(animation.from.size(), 4u);
rotation_axis_angle[0] = values[0];
rotation_axis_angle[1] = values[1];
rotation_axis_angle[2] = values[2];
rotation_axis_angle[3] = values[3];
break;
case Animation::UNUSED:
break;
}
}
for (auto it = animations.begin(); it != animations.end();) {
const Animation& animation = **it;
if (time >= (animation.start + animation.duration)) {
it = animations.erase(it);
} else {
++it;
}
}
}
} // namespace vr_shell
......@@ -9,11 +9,13 @@
#include <vector>
#include "base/logging.h"
#include "chrome/browser/android/vr_shell/vr_util.h"
#include "chrome/browser/android/vr_shell/vr_math.h"
#include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr_types.h"
namespace vr_shell {
class Animation;
enum XAnchoring {
XLEFT = 0,
XRIGHT,
......@@ -61,18 +63,22 @@ struct ContentRectangle : public WorldRectangle {
ContentRectangle();
~ContentRectangle();
int id;
void Animate(int64_t time);
int id = 0;
// samplerExternalOES texture data for desktop content image.
int content_texture_handle;
Rectf copy_rect;
Recti window_rect;
gvr::Vec3f size;
gvr::Vec3f translation;
XAnchoring x_anchoring;
YAnchoring y_anchoring;
bool anchor_z;
int content_texture_handle = -1;
Rectf copy_rect = {0.0f, 0.0f, 0.0f, 0.0f};
Recti window_rect = {0.0f, 0.0f, 0.0f, 0.0f};
gvr::Vec3f size = {0.0f, 0.0f, 0.0f};
gvr::Vec3f translation = {0.0f, 0.0f, 0.0f};
XAnchoring x_anchoring = XAnchoring::XNONE;
YAnchoring y_anchoring = YAnchoring::YNONE;
bool anchor_z = false;
std::vector<float> orientation_axis_angle;
std::vector<float> rotation_axis_angle;
std::vector<std::unique_ptr<Animation>> animations;
private:
DISALLOW_COPY_AND_ASSIGN(ContentRectangle);
......
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/vr_shell/ui_elements.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/android/vr_shell/animation.h"
#include "chrome/browser/android/vr_shell/easing.h"
#include "testing/gtest/include/gtest/gtest.h"
#define EXPECT_VEC3F_EQ(a, b) \
EXPECT_FLOAT_EQ(a.x, b.x); \
EXPECT_FLOAT_EQ(a.y, b.y); \
EXPECT_FLOAT_EQ(a.z, b.z);
#define EXPECT_RECTF_EQ(a, b) \
EXPECT_FLOAT_EQ(a.x, b.x); \
EXPECT_FLOAT_EQ(a.y, b.y); \
EXPECT_FLOAT_EQ(a.width, b.width); \
EXPECT_FLOAT_EQ(a.height, b.height);
#define EXPECT_VECTOR_FLOAT_4_EQ(a, b) \
EXPECT_EQ(a.size(), 4u); \
EXPECT_EQ(b.size(), 4u); \
EXPECT_FLOAT_EQ(a[0], b[0]); \
EXPECT_FLOAT_EQ(a[1], b[1]); \
EXPECT_FLOAT_EQ(a[2], b[2]); \
EXPECT_FLOAT_EQ(a[3], b[3]);
namespace vr_shell {
TEST(UiElements, AnimateCopyRect) {
ContentRectangle rect;
rect.copy_rect = {10, 100, 1000, 10000};
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::COPYRECT,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {20, 200, 2000, 20000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_RECTF_EQ(rect.copy_rect, Rectf({10, 100, 1000, 10000}));
rect.Animate(60000);
EXPECT_RECTF_EQ(rect.copy_rect, Rectf({20, 200, 2000, 20000}));
}
TEST(UiElements, AnimateSize) {
ContentRectangle rect;
rect.size = {10, 100, 1000};
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::SIZE,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_VEC3F_EQ(rect.size, gvr::Vec3f({10, 100, 1000}));
rect.Animate(60000);
EXPECT_VEC3F_EQ(rect.size, gvr::Vec3f({20, 200, 2000}));
}
TEST(UiElements, AnimateTranslation) {
ContentRectangle rect;
rect.translation = {10, 100, 1000};
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({10, 100, 1000}));
rect.Animate(60000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({20, 200, 2000}));
}
TEST(UiElements, AnimateRotation) {
ContentRectangle rect;
rect.rotation_axis_angle = {10, 100, 1000, 10000};
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::ROTATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {20, 200, 2000, 20000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_VECTOR_FLOAT_4_EQ(rect.rotation_axis_angle,
std::vector<float>({10, 100, 1000, 10000}));
rect.Animate(60000);
EXPECT_VECTOR_FLOAT_4_EQ(rect.rotation_axis_angle,
std::vector<float>({20, 200, 2000, 20000}));
}
TEST(UiElements, AnimationHasNoEffectBeforeScheduledStart) {
ContentRectangle rect;
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{10, 100, 1000}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(49999);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({0, 0, 0}));
}
TEST(UiElements, AnimationPurgedWhenDone) {
ContentRectangle rect;
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{10, 100, 1000}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(60000);
EXPECT_EQ(0u, rect.animations.size());
}
TEST(UiElements, AnimationLinearEasing) {
ContentRectangle rect;
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{10, 100, 1000}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({10, 100, 1000}));
rect.Animate(55000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({15, 150, 1500}));
rect.Animate(60000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({20, 200, 2000}));
}
TEST(UiElements, AnimationStartFromSpecifiedLocation) {
ContentRectangle rect;
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{10, 100, 1000}, {20, 200, 2000}, 50000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.Animate(50000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({10, 100, 1000}));
rect.Animate(60000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({20, 200, 2000}));
}
// Ensure that when a new animation overlaps another of the same type, the
// newly added animation overrides the original. For example:
// Animation 1: ? .......... 20
// Animation 2: ? .......... 50
// Result: 0 ... 10 ... 30 ... 50
TEST(UiElements, AnimationOverlap) {
ContentRectangle rect;
std::unique_ptr<Animation> animation(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {20, 200, 2000}, 50000, 10000));
std::unique_ptr<Animation> animation2(new Animation(
0, Animation::Property::TRANSLATION,
std::unique_ptr<easing::Easing>(new easing::Linear()),
{}, {50, 500, 5000}, 55000, 10000));
rect.animations.emplace_back(std::move(animation));
rect.animations.emplace_back(std::move(animation2));
rect.Animate(55000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({10, 100, 1000}));
rect.Animate(60000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({30, 300, 3000}));
rect.Animate(65000);
EXPECT_VEC3F_EQ(rect.translation, gvr::Vec3f({50, 500, 5000}));
}
} // namespace vr_shell
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/vr_shell/vr_gl_util.h"
#include <array>
#include <cmath>
namespace vr_shell {
// This code is adapted from the GVR Treasure Hunt demo source.
std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) {
// Note that this performs a *transpose* to a column-major matrix array, as
// expected by GL. The input matrix has translation components at [i][3] for
// use with row vectors and premultiplied transforms. In the output, the
// translation elements are at the end at positions 3*4+i.
std::array<float, 16> result;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
result[j * 4 + i] = matrix.m[i][j];
}
}
return result;
}
// This code is adapted from the GVR Treasure Hunt demo source.
gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) {
gvr::Rectf result = {rect.left * width, rect.right * width,
rect.bottom * height, rect.top * height};
return result;
}
// This code is adapted from the GVR Treasure Hunt demo source.
gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size,
const gvr::Rectf& texture_rect) {
float width = static_cast<float>(texture_size.width);
float height = static_cast<float>(texture_size.height);
gvr::Rectf rect = ModulateRect(texture_rect, width, height);
gvr::Recti result = {
static_cast<int>(rect.left), static_cast<int>(rect.right),
static_cast<int>(rect.bottom), static_cast<int>(rect.top)};
return result;
}
GLuint CompileShader(GLenum shader_type,
const GLchar* shader_source,
std::string& error) {
GLuint shader_handle = glCreateShader(shader_type);
if (shader_handle != 0) {
// Pass in the shader source.
int len = strlen(shader_source);
glShaderSource(shader_handle, 1, &shader_source, &len);
// Compile the shader.
glCompileShader(shader_handle);
// Get the compilation status.
GLint status;
glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint info_log_length;
glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* str_info_log = new GLchar[info_log_length + 1];
glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log);
error = "Error compiling shader: ";
error += str_info_log;
delete[] str_info_log;
glDeleteShader(shader_handle);
shader_handle = 0;
}
}
return shader_handle;
}
GLuint CreateAndLinkProgram(GLuint vertext_shader_handle,
GLuint fragment_shader_handle,
int num_attributes,
const GLchar** attributes,
std::string& error) {
GLuint program_handle = glCreateProgram();
if (program_handle != 0) {
// Bind the vertex shader to the program.
glAttachShader(program_handle, vertext_shader_handle);
// Bind the fragment shader to the program.
glAttachShader(program_handle, fragment_shader_handle);
// Bind attributes. This is optional, no need to supply them if
// using glGetAttribLocation to look them up. Useful for a single
// vertex array object (VAO) that is used with multiple shaders.
if (attributes != nullptr) {
for (int i = 0; i < num_attributes; i++) {
glBindAttribLocation(program_handle, i, attributes[i]);
}
}
// Link the two shaders together into a program.
glLinkProgram(program_handle);
// Get the link status.
GLint link_status;
glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
// If the link failed, delete the program.
if (link_status == GL_FALSE) {
GLint info_log_length;
glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* str_info_log = new GLchar[info_log_length + 1];
glGetProgramInfoLog(program_handle, info_log_length, nullptr,
str_info_log);
error = "Error compiling program: ";
error += str_info_log;
delete[] str_info_log;
glDeleteProgram(program_handle);
program_handle = 0;
}
}
return program_handle;
}
} // namespace vr_shell
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_GL_UTIL_H_
#define CHROME_BROWSER_ANDROID_VR_SHELL_VR_GL_UTIL_H_
#include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr_types.h"
#include "ui/gl/gl_bindings.h"
namespace vr_shell {
std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix);
gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height);
gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size,
const gvr::Rectf& texture_rect);
// Compile a shader.
GLuint CompileShader(GLenum shader_type,
const GLchar* shader_source,
std::string& error);
// Compile and link a program.
GLuint CreateAndLinkProgram(GLuint vertex_shader_handle,
GLuint fragment_shader_handle,
int num_attributes,
const GLchar** attributes,
std::string& error);
} // namespace vr_shell
#endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_GL_UTIL_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/vr_shell/vr_util.h"
#include "chrome/browser/android/vr_shell/vr_math.h"
#include <array>
#include <cmath>
......@@ -105,20 +105,6 @@ void ScaleMRight(gvr::Mat4f& tmat, const gvr::Mat4f& mat,
}
}
std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) {
// Note that this performs a *transpose* to a column-major matrix array, as
// expected by GL. The input matrix has translation components at [i][3] for
// use with row vectors and premultiplied transforms. In the output, the
// translation elements are at the end at positions 3*4+i.
std::array<float, 16> result;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
result[j * 4 + i] = matrix.m[i][j];
}
}
return result;
}
gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) {
gvr::Mat4f result;
for (int i = 0; i < 4; ++i) {
......@@ -221,23 +207,6 @@ gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov,
return result;
}
gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) {
gvr::Rectf result = {rect.left * width, rect.right * width,
rect.bottom * height, rect.top * height};
return result;
}
gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size,
const gvr::Rectf& texture_rect) {
float width = static_cast<float>(texture_size.width);
float height = static_cast<float>(texture_size.height);
gvr::Rectf rect = ModulateRect(texture_rect, width, height);
gvr::Recti result = {
static_cast<int>(rect.left), static_cast<int>(rect.right),
static_cast<int>(rect.bottom), static_cast<int>(rect.top)};
return result;
}
gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) {
gvr::Vec3f forward;
float* fp = &forward.x;
......@@ -263,84 +232,6 @@ gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) {
return translation;
}
GLuint CompileShader(GLenum shader_type,
const GLchar* shader_source,
std::string& error) {
GLuint shader_handle = glCreateShader(shader_type);
if (shader_handle != 0) {
// Pass in the shader source.
int len = strlen(shader_source);
glShaderSource(shader_handle, 1, &shader_source, &len);
// Compile the shader.
glCompileShader(shader_handle);
// Get the compilation status.
GLint status;
glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint info_log_length;
glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* str_info_log = new GLchar[info_log_length + 1];
glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log);
error = "Error compiling shader: ";
error += str_info_log;
delete[] str_info_log;
glDeleteShader(shader_handle);
shader_handle = 0;
}
}
return shader_handle;
}
GLuint CreateAndLinkProgram(GLuint vertext_shader_handle,
GLuint fragment_shader_handle,
int num_attributes,
const GLchar** attributes,
std::string& error) {
GLuint program_handle = glCreateProgram();
if (program_handle != 0) {
// Bind the vertex shader to the program.
glAttachShader(program_handle, vertext_shader_handle);
// Bind the fragment shader to the program.
glAttachShader(program_handle, fragment_shader_handle);
// Bind attributes. This is optional, no need to supply them if
// using glGetAttribLocation to look them up. Useful for a single
// vertex array object (VAO) that is used with multiple shaders.
if (attributes != nullptr) {
for (int i = 0; i < num_attributes; i++) {
glBindAttribLocation(program_handle, i, attributes[i]);
}
}
// Link the two shaders together into a program.
glLinkProgram(program_handle);
// Get the link status.
GLint link_status;
glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
// If the link failed, delete the program.
if (link_status == GL_FALSE) {
GLint info_log_length;
glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* str_info_log = new GLchar[info_log_length + 1];
glGetProgramInfoLog(program_handle, info_log_length, nullptr,
str_info_log);
error = "Error compiling program: ";
error += str_info_log;
delete[] str_info_log;
glDeleteProgram(program_handle);
program_handle = 0;
}
}
return program_handle;
}
float VectorLength(const gvr::Vec3f& vec) {
return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
}
......
......@@ -2,13 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_UTIL_H_
#define CHROME_BROWSER_ANDROID_VR_SHELL_VR_UTIL_H_
#ifndef CHROME_BROWSER_ANDROID_VR_SHELL_VR_MATH_H_
#define CHROME_BROWSER_ANDROID_VR_SHELL_VR_MATH_H_
#include <string>
#include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr_types.h"
#include "ui/gl/gl_bindings.h"
namespace vr_shell {
......@@ -41,8 +40,6 @@ void ScaleM(gvr::Mat4f& tmat, const gvr::Mat4f& mat, float x, float y, float z);
void ScaleMRight(gvr::Mat4f& tmat, const gvr::Mat4f& mat,
float x, float y, float z);
std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix);
// Util functions that are copied from the treasure_hunt NDK demo in
// third_party/gvr-andoir-sdk/ folder.
gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat);
......@@ -56,9 +53,6 @@ gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2);
gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov,
float z_near,
float z_far);
gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height);
gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size,
const gvr::Rectf& texture_rect);
// Provides the direction the head is looking towards as a 3x1 unit vector.
gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix);
......@@ -66,18 +60,6 @@ gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix);
// Provides the relative translation of the head as a 3x1 vector.
gvr::Vec3f getTranslation(const gvr::Mat4f& matrix);
// Compile a shader.
GLuint CompileShader(GLenum shader_type,
const GLchar* shader_source,
std::string& error);
// Compile and link a program.
GLuint CreateAndLinkProgram(GLuint vertex_shader_handle,
GLuint fragment_shader_handle,
int num_attributes,
const GLchar** attributes,
std::string& error);
gvr::Quatf QuatMultiply(const gvr::Quatf& a, const gvr::Quatf& b);
gvr::Mat4f QuatToMatrix(const gvr::Quatf& quat);
......@@ -94,4 +76,4 @@ gvr::Quatf QuatFromAxisAngle(float x, float y, float z, float angle);
} // namespace vr_shell
#endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_UTIL_H_
#endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_MATH_H_
......@@ -6,8 +6,9 @@
#include <thread>
#include "chrome/browser/android/vr_shell/vr_gl_util.h"
#include "chrome/browser/android/vr_shell/vr_math.h"
#include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
#include "chrome/browser/android/vr_shell/vr_util.h"
#include "jni/VrShell_jni.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/init/gl_factory.h"
......
......@@ -4,7 +4,7 @@
#include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
#include "chrome/browser/android/vr_shell/vr_util.h"
#include "chrome/browser/android/vr_shell/vr_gl_util.h"
#include "ui/gl/gl_bindings.h"
namespace {
......
......@@ -10,9 +10,10 @@
#include <vector>
#include "base/macros.h"
#include "chrome/browser/android/vr_shell/vr_util.h"
#include "chrome/browser/android/vr_shell/vr_math.h"
#include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr.h"
#include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/gvr_types.h"
#include "ui/gl/gl_bindings.h"
namespace vr_shell {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment