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

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

Issue 2668093002: VrShell background implemented in JS. (Closed)
Patch Set: Created 3 years, 10 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <string> 9 #include <string>
10 10
11 #include "chrome/browser/android/vr_shell/vr_gl_util.h" 11 #include "chrome/browser/android/vr_shell/vr_gl_util.h"
12 12
13 namespace { 13 namespace {
14 14
15 #define RECTANGULAR_TEXTURE_BUFFER(left, right, bottom, top) \ 15 #define RECTANGULAR_TEXTURE_BUFFER(left, right, bottom, top) \
16 { left, bottom, left, top, right, bottom, left, top, right, top, right, \ 16 { left, bottom, left, top, right, bottom, left, top, right, top, right, \
17 bottom } 17 bottom }
18 18
19 static constexpr float kHalfHeight = 0.5f; 19 static constexpr float kHalfSize = 0.5f;
20 static constexpr float kHalfWidth = 0.5f;
21 static constexpr float kTextureQuadPosition[18] = { 20 static constexpr float kTextureQuadPosition[18] = {
22 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, 21 -kHalfSize, kHalfSize, 0.0f, -kHalfSize, -kHalfSize, 0.0f,
23 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, 22 kHalfSize, kHalfSize, 0.0f, -kHalfSize, -kHalfSize, 0.0f,
24 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f}; 23 kHalfSize, -kHalfSize, 0.0f, kHalfSize, kHalfSize, 0.0f};
25 static constexpr int kPositionDataSize = 3; 24 static constexpr int kPositionDataSize = 3;
26 // Number of vertices passed to glDrawArrays(). 25 // Number of vertices passed to glDrawArrays().
27 static constexpr int kVerticesNumber = 6; 26 static constexpr int kVerticesNumber = 6;
28 27
29 static constexpr float kTexturedQuadTextureCoordinates[12] = 28 static constexpr float kTexturedQuadTextureCoordinates[12] =
30 RECTANGULAR_TEXTURE_BUFFER(0.0f, 1.0f, 0.0f, 1.0f); 29 RECTANGULAR_TEXTURE_BUFFER(0.0f, 1.0f, 0.0f, 1.0f);
31 30
32 static constexpr int kTextureCoordinateDataSize = 2; 31 static constexpr int kTextureCoordinateDataSize = 2;
33 32
34 static constexpr float kWebVrVertices[16] = { 33 static constexpr float kWebVrVertices[16] = {
(...skipping 13 matching lines...) Expand all
48 static constexpr float kMidRingOpacity = 0.22f; 47 static constexpr float kMidRingOpacity = 0.22f;
49 static constexpr float kReticleColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; 48 static constexpr float kReticleColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
50 49
51 // Laser constants 50 // Laser constants
52 static constexpr float kFadeEnd = 0.535; 51 static constexpr float kFadeEnd = 0.535;
53 static constexpr float kFadePoint = 0.5335; 52 static constexpr float kFadePoint = 0.5335;
54 static constexpr float kLaserColor[] = {1.0f, 1.0f, 1.0f, 0.5f}; 53 static constexpr float kLaserColor[] = {1.0f, 1.0f, 1.0f, 0.5f};
55 static constexpr int kLaserDataWidth = 48; 54 static constexpr int kLaserDataWidth = 48;
56 static constexpr int kLaserDataHeight = 1; 55 static constexpr int kLaserDataHeight = 1;
57 56
58 // Background constants.
59 static constexpr float kGroundTileSize = 2.5f;
60 static constexpr float kGroundMaxSize = 25.0f;
61 static constexpr float kGroundYPosition = -2.0f;
62
63 // Laser texture data, 48x1 RGBA. 57 // Laser texture data, 48x1 RGBA.
64 // TODO(mthiesse): As we add more resources for VR Shell, we should put them 58 // TODO(mthiesse): As we add more resources for VR Shell, we should put them
65 // in Chrome's resource files. 59 // in Chrome's resource files.
66 static const unsigned char kLaserData[] = 60 static const unsigned char kLaserData[] =
67 "\xff\xff\xff\x01\xff\xff\xff\x02\xbf\xbf\xbf\x04\xcc\xcc\xcc\x05\xdb\xdb" 61 "\xff\xff\xff\x01\xff\xff\xff\x02\xbf\xbf\xbf\x04\xcc\xcc\xcc\x05\xdb\xdb"
68 "\xdb\x07\xcc\xcc\xcc\x0a\xd8\xd8\xd8\x0d\xd2\xd2\xd2\x11\xce\xce\xce\x15" 62 "\xdb\x07\xcc\xcc\xcc\x0a\xd8\xd8\xd8\x0d\xd2\xd2\xd2\x11\xce\xce\xce\x15"
69 "\xce\xce\xce\x1a\xce\xce\xce\x1f\xcd\xcd\xcd\x24\xc8\xc8\xc8\x2a\xc9\xc9" 63 "\xce\xce\xce\x1a\xce\xce\xce\x1f\xcd\xcd\xcd\x24\xc8\xc8\xc8\x2a\xc9\xc9"
70 "\xc9\x2f\xc9\xc9\xc9\x34\xc9\xc9\xc9\x39\xc9\xc9\xc9\x3d\xc8\xc8\xc8\x41" 64 "\xc9\x2f\xc9\xc9\xc9\x34\xc9\xc9\xc9\x39\xc9\xc9\xc9\x3d\xc8\xc8\xc8\x41"
71 "\xcb\xcb\xcb\x44\xee\xee\xee\x87\xfa\xfa\xfa\xc8\xf9\xf9\xf9\xc9\xf9\xf9" 65 "\xcb\xcb\xcb\x44\xee\xee\xee\x87\xfa\xfa\xfa\xc8\xf9\xf9\xf9\xc9\xf9\xf9"
72 "\xf9\xc9\xfa\xfa\xfa\xc9\xfa\xfa\xfa\xc9\xf9\xf9\xf9\xc9\xf9\xf9\xf9\xc9" 66 "\xf9\xc9\xfa\xfa\xfa\xc9\xfa\xfa\xfa\xc9\xf9\xf9\xf9\xc9\xf9\xf9\xf9\xc9"
(...skipping 17 matching lines...) Expand all
90 uniform mat4 u_ModelViewProjMatrix; 84 uniform mat4 u_ModelViewProjMatrix;
91 attribute vec4 a_Position; 85 attribute vec4 a_Position;
92 attribute vec2 a_TexCoordinate; 86 attribute vec2 a_TexCoordinate;
93 varying vec2 v_TexCoordinate; 87 varying vec2 v_TexCoordinate;
94 88
95 void main() { 89 void main() {
96 v_TexCoordinate = a_TexCoordinate; 90 v_TexCoordinate = a_TexCoordinate;
97 gl_Position = u_ModelViewProjMatrix * a_Position; 91 gl_Position = u_ModelViewProjMatrix * a_Position;
98 } 92 }
99 /* clang-format on */); 93 /* clang-format on */);
100 case vr_shell::ShaderID::BACKGROUND_VERTEX_SHADER: 94 case vr_shell::ShaderID::GRADIENT_QUAD_VERTEX_SHADER:
95 case vr_shell::ShaderID::GRADIENT_GRID_VERTEX_SHADER:
101 return SHADER( 96 return SHADER(
102 /* clang-format off */ 97 /* clang-format off */
103 uniform mat4 u_ModelViewProjMatrix; 98 uniform mat4 u_ModelViewProjMatrix;
104 uniform float u_SceneRadius; 99 uniform float u_SceneRadius;
105 attribute vec4 a_Position; 100 attribute vec4 a_Position;
106 varying vec2 v_GridPosition; 101 varying vec2 v_GridPosition;
107 102
108 void main() { 103 void main() {
109 v_GridPosition = a_Position.xz / u_SceneRadius; 104 v_GridPosition = a_Position.xy / u_SceneRadius;
110 gl_Position = u_ModelViewProjMatrix * a_Position; 105 gl_Position = u_ModelViewProjMatrix * a_Position;
111 } 106 }
112 /* clang-format on */); 107 /* clang-format on */);
113 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER: 108 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER:
114 return OEIE_SHADER( 109 return OEIE_SHADER(
115 /* clang-format off */ 110 /* clang-format off */
116 precision highp float; 111 precision highp float;
117 uniform samplerExternalOES u_Texture; 112 uniform samplerExternalOES u_Texture;
118 uniform vec4 u_CopyRect; // rectangle 113 uniform vec4 u_CopyRect; // rectangle
119 varying vec2 v_TexCoordinate; 114 varying vec2 v_TexCoordinate;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 mediump float front_fade_factor = 1.0 - 195 mediump float front_fade_factor = 1.0 -
201 clamp(1.0 - (uv.y - fade_point) / (1.0 - fade_point), 0.0, 1.0); 196 clamp(1.0 - (uv.y - fade_point) / (1.0 - fade_point), 0.0, 1.0);
202 mediump float back_fade_factor = 197 mediump float back_fade_factor =
203 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); 198 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0);
204 mediump float total_fade = front_fade_factor * back_fade_factor; 199 mediump float total_fade = front_fade_factor * back_fade_factor;
205 lowp vec4 texture_color = texture2D(texture_unit, uv); 200 lowp vec4 texture_color = texture2D(texture_unit, uv);
206 lowp vec4 final_color = color * texture_color; 201 lowp vec4 final_color = color * texture_color;
207 gl_FragColor = vec4(final_color.xyz, final_color.w * total_fade); 202 gl_FragColor = vec4(final_color.xyz, final_color.w * total_fade);
208 } 203 }
209 /* clang-format on */); 204 /* clang-format on */);
210 case vr_shell::ShaderID::BACKGROUND_FRAGMENT_SHADER: 205 case vr_shell::ShaderID::GRADIENT_QUAD_FRAGMENT_SHADER:
211 return SHADER( 206 case vr_shell::ShaderID::GRADIENT_GRID_FRAGMENT_SHADER:
207 return OEIE_SHADER(
212 /* clang-format off */ 208 /* clang-format off */
213 precision highp float; 209 precision highp float;
214 varying vec2 v_GridPosition; 210 varying vec2 v_GridPosition;
215 uniform vec4 u_CenterColor; 211 uniform vec4 u_CenterColor;
216 uniform vec4 u_EdgeColor; 212 uniform vec4 u_EdgeColor;
217 213
218 void main() { 214 void main() {
219 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0); 215 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0);
220 float centerColorWeight = 1.0 - edgeColorWeight; 216 float centerColorWeight = 1.0 - edgeColorWeight;
221 gl_FragColor = u_CenterColor * centerColorWeight + 217 gl_FragColor = u_CenterColor * centerColorWeight +
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 glUniform1f(fade_end_handle_, kFadeEnd); 442 glUniform1f(fade_end_handle_, kFadeEnd);
447 443
448 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); 444 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
449 445
450 glDisableVertexAttribArray(position_handle_); 446 glDisableVertexAttribArray(position_handle_);
451 glDisableVertexAttribArray(tex_coord_handle_); 447 glDisableVertexAttribArray(tex_coord_handle_);
452 } 448 }
453 449
454 LaserRenderer::~LaserRenderer() = default; 450 LaserRenderer::~LaserRenderer() = default;
455 451
456 BackgroundRenderer::BackgroundRenderer() 452 GradientQuadRenderer::GradientQuadRenderer()
457 : BaseRenderer(BACKGROUND_VERTEX_SHADER, BACKGROUND_FRAGMENT_SHADER) { 453 : BaseRenderer(GRADIENT_QUAD_VERTEX_SHADER, GRADIENT_QUAD_FRAGMENT_SHADER) {
458 model_view_proj_matrix_handle_ = 454 model_view_proj_matrix_handle_ =
459 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); 455 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix");
460 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius"); 456 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius");
461 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor"); 457 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor");
462 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor"); 458 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor");
463
464 // Make ground grid.
465 int groundTilesNumber = kGroundMaxSize / kGroundTileSize;
466 scene_radius_ = groundTilesNumber * kGroundTileSize * 0.5f;
467 int groundLinesNumber = 2 * (groundTilesNumber + 1);
468 ground_grid_lines_.resize(groundLinesNumber);
469
470 for (int i = 0; i < groundLinesNumber - 1; i += 2) {
471 float position =
472 -scene_radius_ + (i / 2) * scene_radius_ * 2.0f / groundTilesNumber;
473
474 // Line parallel to the z axis.
475 Line3d& zLine = ground_grid_lines_[i];
476 // Line parallel to the x axis.
477 Line3d& xLine = ground_grid_lines_[i + 1];
478
479 zLine.start.x = position;
480 zLine.start.y = kGroundYPosition;
481 zLine.start.z = -scene_radius_;
482 zLine.end.x = position;
483 zLine.end.y = kGroundYPosition;
484 zLine.end.z = scene_radius_;
485 xLine.start.x = -scene_radius_;
486 xLine.start.y = kGroundYPosition;
487 xLine.start.z = position;
488 xLine.end.x = scene_radius_;
489 xLine.end.y = kGroundYPosition;
490 xLine.end.z = position;
491 }
492
493 // Make plane for ground and ceilings.
494 // clang-format off
495 ground_ceiling_plane_positions_ = {
496 // First triangle.
497 -scene_radius_, 0.0f, scene_radius_,
498 scene_radius_, 0.0f, scene_radius_,
499 -scene_radius_, 0.0f, -scene_radius_,
500 // Second triangle.
501 -scene_radius_, 0.0f, -scene_radius_,
502 scene_radius_, 0.0f, scene_radius_,
503 scene_radius_, 0.0f, -scene_radius_};
504 // clang-format on
505
506 // Make the transform to draw the plane either on the ground or the ceiling.
507 SetIdentityM(ground_plane_transform_mat_);
508 TranslateMRight(ground_plane_transform_mat_, ground_plane_transform_mat_,
509 0.0f, kGroundYPosition - 0.1f, 0.0f);
510
511 SetIdentityM(ceiling_plane_transform_mat_);
512 gvr::Quatf rotation_quat = QuatFromAxisAngle({1.0f, 0.0f, 0.0f}, M_PI);
513 ceiling_plane_transform_mat_ =
514 MatrixMul(ceiling_plane_transform_mat_, QuatToMatrix(rotation_quat));
515 TranslateMRight(ceiling_plane_transform_mat_, ceiling_plane_transform_mat_,
516 0.0f, kGroundYPosition - 0.1f, 0.0f);
517 } 459 }
518 460
519 void BackgroundRenderer::Draw(const gvr::Mat4f& view_proj_matrix) { 461 void GradientQuadRenderer::Draw(const gvr::Mat4f& view_proj_matrix,
520 glUseProgram(program_handle_); 462 const Colorf& edge_color,
463 const Colorf& center_color) {
464 PrepareToDraw(model_view_proj_matrix_handle_, view_proj_matrix);
465
466 // Tell shader the grid size so that it can calculate the fading.
467 glUniform1f(scene_radius_handle_, kHalfSize);
468
469 // Set the edge color to the fog color so that it seems to fade out.
470 glUniform4f(edge_color_handle_, edge_color.r, edge_color.g, edge_color.b,
471 edge_color.a);
472 glUniform4f(center_color_handle_, center_color.r, center_color.g,
473 center_color.b, center_color.a);
474
475 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
476
477 glDisableVertexAttribArray(position_handle_);
478 glDisableVertexAttribArray(tex_coord_handle_);
479 }
480
481 GradientQuadRenderer::~GradientQuadRenderer() = default;
482
483 GradientGridRenderer::GradientGridRenderer()
484 : BaseRenderer(GRADIENT_QUAD_VERTEX_SHADER, GRADIENT_QUAD_FRAGMENT_SHADER) {
485 model_view_proj_matrix_handle_ =
486 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix");
487 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius");
488 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor");
489 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor");
490 }
491
492 void GradientGridRenderer::Draw(const gvr::Mat4f& view_proj_matrix,
493 const Colorf& edge_color,
494 const Colorf& center_color,
495 unsigned int tile_number) {
496 // In case the tile number changed we have to regenerate the grid lines.
497 if (grid_lines_.size() != 2 * (tile_number + 1)) {
498 MakeGridLines(tile_number);
499 }
521 500
522 // Pass in model view project matrix. 501 // Pass in model view project matrix.
523 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false, 502 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false,
524 MatrixToGLArray(view_proj_matrix).data()); 503 MatrixToGLArray(view_proj_matrix).data());
525 504
526 // Tell shader the grid size so that it can calculate the fading. 505 // Tell shader the grid size so that it can calculate the fading.
527 int groundTilesNumber = kGroundMaxSize / kGroundTileSize; 506 glUniform1f(scene_radius_handle_, kHalfSize);
528 glUniform1f(scene_radius_handle_, scene_radius_);
529 507
530 // Set the edge color to the fog color so that it seems to fade out. 508 // Set the edge color to the fog color so that it seems to fade out.
531 glUniform4f(edge_color_handle_, kFogBrightness, kFogBrightness, 509 glUniform4f(edge_color_handle_, edge_color.r, edge_color.g, edge_color.b,
532 kFogBrightness, 1.0f); 510 edge_color.a);
511 glUniform4f(center_color_handle_, center_color.r, center_color.g,
512 center_color.b, center_color.a);
533 513
534 // Draw the ground grid. 514 // Draw the grid.
535 glEnableVertexAttribArray(position_handle_); 515 glEnableVertexAttribArray(position_handle_);
536 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, 516 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0,
537 (float*)ground_grid_lines_.data()); 517 (float*)grid_lines_.data());
538 glUniform4f(center_color_handle_, kGridBrightness, kGridBrightness, 518 int verticesNumber = 4 * (tile_number + 1);
539 kGridBrightness, 1.0f); 519 glDrawArrays(GL_LINES, 0, verticesNumber);
540 int groundVerticesNumber = 4 * (groundTilesNumber + 1);
541 glDrawArrays(GL_LINES, 0, groundVerticesNumber);
542
543 // Draw the ground plane.
544 gvr::Mat4f transformed_matrix =
545 MatrixMul(view_proj_matrix, ground_plane_transform_mat_);
546 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false,
547 MatrixToGLArray(transformed_matrix).data());
548 glUniform4f(center_color_handle_, kGroundCeilingBrightness,
549 kGroundCeilingBrightness, kGroundCeilingBrightness, 1.0f);
550 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0,
551 ground_ceiling_plane_positions_.data());
552 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
553
554 // Draw the ceiling plane.
555 transformed_matrix =
556 MatrixMul(view_proj_matrix, ceiling_plane_transform_mat_);
557 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false,
558 MatrixToGLArray(transformed_matrix).data());
559 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
560 520
561 glDisableVertexAttribArray(position_handle_); 521 glDisableVertexAttribArray(position_handle_);
562 } 522 }
563 523
564 BackgroundRenderer::~BackgroundRenderer() = default; 524 GradientGridRenderer::~GradientGridRenderer() = default;
525
526 void GradientGridRenderer::MakeGridLines(unsigned int tile_number) {
527 int linesNumber = 2 * (tile_number + 1);
528 grid_lines_.resize(linesNumber);
529
530 for (int i = 0; i < linesNumber - 1; i += 2) {
531 float position = -kHalfSize + (i / 2) * kHalfSize * 2.0f / tile_number;
532
533 // Line parallel to the z axis.
534 Line3d& zLine = grid_lines_[i];
535 // Line parallel to the x axis.
536 Line3d& xLine = grid_lines_[i + 1];
537
538 zLine.start.x = position;
539 zLine.start.y = kHalfSize;
540 zLine.start.z = 0.0f;
541 zLine.end.x = position;
542 zLine.end.y = -kHalfSize;
543 zLine.end.z = 0.0f;
544 xLine.start.x = -kHalfSize;
545 xLine.start.y = -position;
546 xLine.start.z = 0.0f;
547 xLine.end.x = kHalfSize;
548 xLine.end.y = -position;
549 xLine.end.z = 0.0f;
550 }
551 }
565 552
566 VrShellRenderer::VrShellRenderer() 553 VrShellRenderer::VrShellRenderer()
567 : textured_quad_renderer_(new TexturedQuadRenderer), 554 : textured_quad_renderer_(new TexturedQuadRenderer),
568 webvr_renderer_(new WebVrRenderer), 555 webvr_renderer_(new WebVrRenderer),
569 reticle_renderer_(new ReticleRenderer), 556 reticle_renderer_(new ReticleRenderer),
570 laser_renderer_(new LaserRenderer), 557 laser_renderer_(new LaserRenderer),
571 background_renderer_(new BackgroundRenderer) {} 558 gradient_quad_renderer_(new GradientQuadRenderer),
559 gradient_grid_renderer_(new GradientGridRenderer) {}
572 560
573 VrShellRenderer::~VrShellRenderer() = default; 561 VrShellRenderer::~VrShellRenderer() = default;
574 562
575 } // namespace vr_shell 563 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698