Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: chrome/browser/android/vr_shell/vr_controller_model.cc

Issue 2775283004: Rendering Daydream controller in a fixed position. (Closed)
Patch Set: Moving code to more appropriate locations. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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_controller_model.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/android/vr_shell/gltf_parser.h"
12 #include "components/component_updater/component_updater_paths.h"
13 #include "ui/gfx/codec/png_codec.h"
14
15 namespace vr_shell {
16
17 namespace {
18
19 enum {
20 ELEMENTS_BUFFER_ID = 0,
21 INDICES_BUFFER_ID = 1,
22 };
23
24 constexpr char kPosition[] = "POSITION";
25 constexpr char kTexCoord[] = "TEXCOORD_0";
26
27 } // namespace
28
29 constexpr char const VrControllerModel::kComponentName[];
30 constexpr char const VrControllerModel::kDefaultVersion[];
31 constexpr char const VrControllerModel::kModelsDirectory[];
32 constexpr char const VrControllerModel::kModelFilename[];
33 constexpr char const VrControllerModel::kTexturesDirectory[];
34 constexpr char const* VrControllerModel::kTextureFilenames[];
35
36 VrControllerModel::VrControllerModel(
37 std::unique_ptr<gltf::Asset> gltf_asset,
38 std::vector<std::unique_ptr<gltf::Buffer>> buffers)
39 : gltf_asset_(std::move(gltf_asset)),
40 texture_bitmaps_(State::STATE_COUNT),
41 buffers_(std::move(buffers)) {}
42
43 VrControllerModel::~VrControllerModel() = default;
44
45 const GLvoid* VrControllerModel::ElementsBuffer() const {
46 const gltf::BufferView* buffer_view =
47 gltf_asset_->GetBufferView(ELEMENTS_BUFFER_ID);
48 DCHECK(buffer_view && buffer_view->target == GL_ARRAY_BUFFER);
49 const char* buffer = Buffer();
50 return buffer ? buffer + buffer_view->byte_offset : nullptr;
51 }
52
53 GLsizeiptr VrControllerModel::ElementsBufferSize() const {
54 const gltf::BufferView* buffer_view =
55 gltf_asset_->GetBufferView(ELEMENTS_BUFFER_ID);
56 DCHECK(buffer_view && buffer_view->target == GL_ARRAY_BUFFER);
57 return buffer_view->byte_length;
58 }
59
60 const GLvoid* VrControllerModel::IndicesBuffer() const {
61 const gltf::BufferView* buffer_view =
62 gltf_asset_->GetBufferView(INDICES_BUFFER_ID);
63 DCHECK(buffer_view && buffer_view->target == GL_ELEMENT_ARRAY_BUFFER);
64 const char* buffer = Buffer();
65 return buffer ? buffer + buffer_view->byte_offset : nullptr;
66 }
67
68 GLsizeiptr VrControllerModel::IndicesBufferSize() const {
69 const gltf::BufferView* buffer_view =
70 gltf_asset_->GetBufferView(INDICES_BUFFER_ID);
71 DCHECK(buffer_view && buffer_view->target == GL_ELEMENT_ARRAY_BUFFER);
72 return buffer_view->byte_length;
73 }
74
75 GLenum VrControllerModel::DrawMode() const {
76 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0);
77 DCHECK(mesh && mesh->primitives.size());
78 return mesh->primitives[0]->mode;
79 }
80
81 const gltf::Accessor* VrControllerModel::IndicesAccessor() const {
82 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0);
83 DCHECK(mesh && mesh->primitives.size());
84 return mesh->primitives[0]->indices;
85 }
86
87 const gltf::Accessor* VrControllerModel::PositionAccessor() const {
88 return Accessor(kPosition);
89 }
90
91 const gltf::Accessor* VrControllerModel::TextureCoordinateAccessor() const {
92 return Accessor(kTexCoord);
93 }
94
95 void VrControllerModel::SetTexture(int state,
96 std::unique_ptr<SkBitmap> bitmap) {
97 DCHECK(state >= 0 && state < STATE_COUNT);
98 texture_bitmaps_[state] = std::move(bitmap);
99 }
100
101 const SkBitmap* VrControllerModel::GetTexture(int state) const {
102 DCHECK(state >= 0 && state < STATE_COUNT);
103 return texture_bitmaps_[state].get();
104 }
105
106 const char* VrControllerModel::Buffer() const {
107 if (buffers_.empty())
108 return nullptr;
109 return buffers_[0]->data();
110 }
111
112 const gltf::Accessor* VrControllerModel::Accessor(
113 const std::string& key) const {
114 const gltf::Mesh* mesh = gltf_asset_->GetMesh(0);
115 DCHECK(mesh && mesh->primitives.size());
116 auto it = mesh->primitives[0]->attributes.find(key);
117 DCHECK(it != mesh->primitives[0]->attributes.begin());
118 return it->second;
119 }
120
121 std::unique_ptr<VrControllerModel> VrControllerModel::LoadFromComponent() {
122 base::FilePath models_path;
123 PathService::Get(component_updater::DIR_COMPONENT_USER, &models_path);
124 models_path = models_path.Append(VrControllerModel::kComponentName)
125 .Append(VrControllerModel::kDefaultVersion)
126 .Append(VrControllerModel::kModelsDirectory);
127 auto model_path = models_path.Append(VrControllerModel::kModelFilename);
128
129 // No further action if model file is not present
130 if (!base::PathExists(model_path)) {
131 LOG(WARNING) << "Controller model files not found";
132 return nullptr;
133 }
134
135 GltfParser gltf_parser;
136 std::vector<std::unique_ptr<gltf::Buffer>> buffers;
137 auto asset = gltf_parser.Parse(model_path, &buffers);
138 if (!asset) {
139 LOG(ERROR) << "Failed to read controller model";
140 return nullptr;
141 }
142
143 auto controller_model =
144 base::MakeUnique<VrControllerModel>(std::move(asset), std::move(buffers));
145
146 auto textures_path =
147 models_path.Append(VrControllerModel::kTexturesDirectory);
148
149 for (int i = 0; i < VrControllerModel::STATE_COUNT; i++) {
150 auto texture_path =
151 textures_path.Append(VrControllerModel::kTextureFilenames[i]);
152 std::string data;
153 auto bitmap = base::MakeUnique<SkBitmap>();
154 if (!base::ReadFileToString(texture_path, &data) ||
155 !gfx::PNGCodec::Decode(
156 reinterpret_cast<const unsigned char*>(data.data()), data.size(),
157 bitmap.get()) ||
158 bitmap->colorType() != kRGBA_8888_SkColorType) {
159 LOG(ERROR) << "Failed to read controller texture";
160 return nullptr;
161 }
162 controller_model->SetTexture(i, std::move(bitmap));
163 }
164
165 return controller_model;
166 }
167
168 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/vr_controller_model.h ('k') | chrome/browser/android/vr_shell/vr_shell.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698