Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 mediump float back_fade_factor = | 218 mediump float back_fade_factor = |
| 219 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); | 219 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); |
| 220 mediump float total_fade = front_fade_factor * back_fade_factor; | 220 mediump float total_fade = front_fade_factor * back_fade_factor; |
| 221 lowp vec4 texture_color = texture2D(texture_unit, uv); | 221 lowp vec4 texture_color = texture2D(texture_unit, uv); |
| 222 lowp vec4 final_color = color * texture_color; | 222 lowp vec4 final_color = color * texture_color; |
| 223 lowp float final_opacity = final_color.w * total_fade * u_Opacity; | 223 lowp float final_opacity = final_color.w * total_fade * u_Opacity; |
| 224 gl_FragColor = vec4(final_color.xyz * final_opacity, final_opacity); | 224 gl_FragColor = vec4(final_color.xyz * final_opacity, final_opacity); |
| 225 } | 225 } |
| 226 /* clang-format on */); | 226 /* clang-format on */); |
| 227 case vr_shell::ShaderID::GRADIENT_QUAD_FRAGMENT_SHADER: | 227 case vr_shell::ShaderID::GRADIENT_QUAD_FRAGMENT_SHADER: |
| 228 case vr_shell::ShaderID::GRADIENT_GRID_FRAGMENT_SHADER: | 228 return SHADER( |
| 229 return OEIE_SHADER( | |
| 230 /* clang-format off */ | 229 /* clang-format off */ |
| 231 precision highp float; | 230 precision lowp float; |
| 232 varying vec2 v_GridPosition; | 231 varying vec2 v_GridPosition; |
| 233 uniform vec4 u_CenterColor; | 232 uniform vec4 u_CenterColor; |
| 234 uniform vec4 u_EdgeColor; | 233 uniform vec4 u_EdgeColor; |
| 235 uniform mediump float u_Opacity; | 234 uniform mediump float u_Opacity; |
| 236 | 235 |
| 237 void main() { | 236 void main() { |
| 238 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0); | 237 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0); |
| 239 float centerColorWeight = 1.0 - edgeColorWeight; | 238 float centerColorWeight = 1.0 - edgeColorWeight; |
| 240 vec4 color = u_CenterColor * centerColorWeight + | 239 vec4 color = u_CenterColor * centerColorWeight + |
| 241 u_EdgeColor * edgeColorWeight; | 240 u_EdgeColor * edgeColorWeight; |
| 242 gl_FragColor = vec4(color.xyz * color.w * u_Opacity, | 241 gl_FragColor = vec4(color.xyz * color.w * u_Opacity, |
| 243 color.w * u_Opacity); | 242 color.w * u_Opacity); |
| 244 } | 243 } |
| 245 /* clang-format on */); | 244 /* clang-format on */); |
| 245 case vr_shell::ShaderID::GRADIENT_GRID_FRAGMENT_SHADER: | |
| 246 return SHADER( | |
| 247 /* clang-format off */ | |
| 248 precision lowp float; | |
| 249 varying vec2 v_GridPosition; | |
| 250 uniform vec4 u_CenterColor; | |
| 251 uniform vec4 u_EdgeColor; | |
| 252 uniform vec4 u_GridColor; | |
| 253 uniform mediump float u_Opacity; | |
| 254 uniform int u_LinesCount; | |
| 255 | |
| 256 void main() { | |
| 257 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0); | |
| 258 float centerColorWeight = 1.0 - edgeColorWeight; | |
| 259 vec4 bg_color = u_CenterColor * centerColorWeight + | |
| 260 u_EdgeColor * edgeColorWeight; | |
| 261 bg_color = vec4(bg_color.xyz * bg_color.w, bg_color.w); | |
| 262 float linesCountF = float(u_LinesCount) / 2.0; | |
| 263 float pos_x = abs(v_GridPosition.x) * linesCountF; | |
| 264 float pos_y = abs(v_GridPosition.y) * linesCountF; | |
| 265 float diff_x = abs(pos_x - floor(pos_x + 0.5)); | |
| 266 float diff_y = abs(pos_y - floor(pos_y + 0.5)); | |
| 267 float diff = diff_x < diff_y ? diff_x : diff_y; | |
|
mthiesse
2017/05/31 14:41:01
nit: use min?
acondor_
2017/05/31 15:38:30
Done.
| |
| 268 if (diff < 0.01) { | |
| 269 float opacity = 1.0 - diff / 0.01; | |
| 270 opacity = opacity * opacity * centerColorWeight * u_GridColor.w; | |
| 271 vec3 grid_color = u_GridColor.xyz * opacity; | |
| 272 gl_FragColor = vec4(grid_color.xyz + bg_color.xyz * (1.0 - opacity ), | |
|
mthiesse
2017/05/31 14:41:01
nit: line length
acondor_
2017/05/31 15:38:30
Done.
| |
| 273 opacity + bg_color.w * (1.0 - opacity)); | |
| 274 } else { | |
| 275 gl_FragColor = bg_color; | |
| 276 } | |
| 277 } | |
| 278 /* clang-format on */); | |
| 246 case vr_shell::ShaderID::CONTROLLER_FRAGMENT_SHADER: | 279 case vr_shell::ShaderID::CONTROLLER_FRAGMENT_SHADER: |
| 247 return SHADER( | 280 return SHADER( |
| 248 /* clang-format off */ | 281 /* clang-format off */ |
| 249 precision mediump float; | 282 precision mediump float; |
| 250 uniform sampler2D u_texture; | 283 uniform sampler2D u_texture; |
| 251 varying vec2 v_TexCoordinate; | 284 varying vec2 v_TexCoordinate; |
| 252 uniform mediump float u_Opacity; | 285 uniform mediump float u_Opacity; |
| 253 | 286 |
| 254 void main() { | 287 void main() { |
| 255 lowp vec4 texture_color = texture2D(u_texture, v_TexCoordinate); | 288 lowp vec4 texture_color = texture2D(u_texture, v_TexCoordinate); |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 712 | 745 |
| 713 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); | 746 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 714 | 747 |
| 715 glDisableVertexAttribArray(position_handle_); | 748 glDisableVertexAttribArray(position_handle_); |
| 716 glDisableVertexAttribArray(tex_coord_handle_); | 749 glDisableVertexAttribArray(tex_coord_handle_); |
| 717 } | 750 } |
| 718 | 751 |
| 719 GradientQuadRenderer::~GradientQuadRenderer() = default; | 752 GradientQuadRenderer::~GradientQuadRenderer() = default; |
| 720 | 753 |
| 721 GradientGridRenderer::GradientGridRenderer() | 754 GradientGridRenderer::GradientGridRenderer() |
| 722 : BaseRenderer(GRADIENT_QUAD_VERTEX_SHADER, GRADIENT_QUAD_FRAGMENT_SHADER) { | 755 : BaseQuadRenderer(GRADIENT_QUAD_VERTEX_SHADER, |
| 756 GRADIENT_GRID_FRAGMENT_SHADER) { | |
| 723 model_view_proj_matrix_handle_ = | 757 model_view_proj_matrix_handle_ = |
| 724 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); | 758 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); |
| 725 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius"); | 759 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius"); |
| 726 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor"); | 760 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor"); |
| 727 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor"); | 761 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor"); |
| 762 grid_color_handle_ = glGetUniformLocation(program_handle_, "u_GridColor"); | |
| 728 opacity_handle_ = glGetUniformLocation(program_handle_, "u_Opacity"); | 763 opacity_handle_ = glGetUniformLocation(program_handle_, "u_Opacity"); |
| 764 lines_count_handle_ = glGetUniformLocation(program_handle_, "u_LinesCount"); | |
| 729 } | 765 } |
| 730 | 766 |
| 731 void GradientGridRenderer::Draw(const vr::Mat4f& view_proj_matrix, | 767 void GradientGridRenderer::Draw(const vr::Mat4f& view_proj_matrix, |
| 732 SkColor edge_color, | 768 SkColor edge_color, |
| 733 SkColor center_color, | 769 SkColor center_color, |
| 770 SkColor grid_color, | |
| 734 int gridline_count, | 771 int gridline_count, |
| 735 float opacity) { | 772 float opacity) { |
| 736 // In case the tile number changed we have to regenerate the grid lines. | 773 PrepareToDraw(model_view_proj_matrix_handle_, view_proj_matrix); |
| 737 if (grid_lines_.size() != static_cast<size_t>(2 * (gridline_count + 1))) { | |
| 738 MakeGridLines(gridline_count); | |
| 739 } | |
| 740 | |
| 741 glUseProgram(program_handle_); | |
| 742 | |
| 743 // Pass in model view project matrix. | |
| 744 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false, | |
| 745 MatrixToGLArray(view_proj_matrix).data()); | |
| 746 | 774 |
| 747 // Tell shader the grid size so that it can calculate the fading. | 775 // Tell shader the grid size so that it can calculate the fading. |
| 748 glUniform1f(scene_radius_handle_, kHalfSize); | 776 glUniform1f(scene_radius_handle_, kHalfSize); |
| 777 glUniform1i(lines_count_handle_, gridline_count); | |
| 749 | 778 |
| 750 // Set the edge color to the fog color so that it seems to fade out. | 779 // Set the edge color to the fog color so that it seems to fade out. |
| 751 SetColorUniform(edge_color_handle_, edge_color); | 780 SetColorUniform(edge_color_handle_, edge_color); |
| 752 SetColorUniform(center_color_handle_, center_color); | 781 SetColorUniform(center_color_handle_, center_color); |
| 782 SetColorUniform(grid_color_handle_, grid_color); | |
| 753 glUniform1f(opacity_handle_, opacity); | 783 glUniform1f(opacity_handle_, opacity); |
| 754 | 784 |
| 755 // Draw the grid. | 785 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 756 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 757 | |
| 758 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, | |
| 759 VOID_OFFSET(0)); | |
| 760 glEnableVertexAttribArray(position_handle_); | |
| 761 glEnable(GL_BLEND); | |
| 762 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | |
| 763 int verticesNumber = 4 * (gridline_count + 1); | |
| 764 glDrawArrays(GL_LINES, 0, verticesNumber); | |
| 765 | 786 |
| 766 glDisableVertexAttribArray(position_handle_); | 787 glDisableVertexAttribArray(position_handle_); |
| 788 glDisableVertexAttribArray(tex_coord_handle_); | |
| 767 } | 789 } |
| 768 | 790 |
| 769 GradientGridRenderer::~GradientGridRenderer() = default; | 791 GradientGridRenderer::~GradientGridRenderer() = default; |
| 770 | 792 |
| 771 void GradientGridRenderer::MakeGridLines(int gridline_count) { | |
| 772 int linesNumber = 2 * (gridline_count + 1); | |
| 773 grid_lines_.resize(linesNumber); | |
| 774 | |
| 775 for (int i = 0; i < linesNumber - 1; i += 2) { | |
| 776 float position = -kHalfSize + (i / 2) * kHalfSize * 2.0f / gridline_count; | |
| 777 | |
| 778 // Line parallel to the z axis. | |
| 779 Line3d& zLine = grid_lines_[i]; | |
| 780 // Line parallel to the x axis. | |
| 781 Line3d& xLine = grid_lines_[i + 1]; | |
| 782 | |
| 783 zLine.start.x = position; | |
| 784 zLine.start.y = kHalfSize; | |
| 785 zLine.start.z = 0.0f; | |
| 786 zLine.end.x = position; | |
| 787 zLine.end.y = -kHalfSize; | |
| 788 zLine.end.z = 0.0f; | |
| 789 xLine.start.x = -kHalfSize; | |
| 790 xLine.start.y = -position; | |
| 791 xLine.start.z = 0.0f; | |
| 792 xLine.end.x = kHalfSize; | |
| 793 xLine.end.y = -position; | |
| 794 xLine.end.z = 0.0f; | |
| 795 } | |
| 796 | |
| 797 size_t vertex_buffer_size = sizeof(Line3d) * linesNumber; | |
| 798 | |
| 799 glGenBuffersARB(1, &vertex_buffer_); | |
| 800 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 801 glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, grid_lines_.data(), | |
| 802 GL_STATIC_DRAW); | |
| 803 } | |
| 804 | |
| 805 VrShellRenderer::VrShellRenderer() | 793 VrShellRenderer::VrShellRenderer() |
| 806 : external_textured_quad_renderer_( | 794 : external_textured_quad_renderer_( |
| 807 base::MakeUnique<ExternalTexturedQuadRenderer>()), | 795 base::MakeUnique<ExternalTexturedQuadRenderer>()), |
| 808 textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()), | 796 textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()), |
| 809 webvr_renderer_(base::MakeUnique<WebVrRenderer>()), | 797 webvr_renderer_(base::MakeUnique<WebVrRenderer>()), |
| 810 reticle_renderer_(base::MakeUnique<ReticleRenderer>()), | 798 reticle_renderer_(base::MakeUnique<ReticleRenderer>()), |
| 811 laser_renderer_(base::MakeUnique<LaserRenderer>()), | 799 laser_renderer_(base::MakeUnique<LaserRenderer>()), |
| 812 controller_renderer_(base::MakeUnique<ControllerRenderer>()), | 800 controller_renderer_(base::MakeUnique<ControllerRenderer>()), |
| 813 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), | 801 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), |
| 814 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { | 802 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 855 GradientGridRenderer* VrShellRenderer::GetGradientGridRenderer() { | 843 GradientGridRenderer* VrShellRenderer::GetGradientGridRenderer() { |
| 856 Flush(); | 844 Flush(); |
| 857 return gradient_grid_renderer_.get(); | 845 return gradient_grid_renderer_.get(); |
| 858 } | 846 } |
| 859 | 847 |
| 860 void VrShellRenderer::Flush() { | 848 void VrShellRenderer::Flush() { |
| 861 textured_quad_renderer_->Flush(); | 849 textured_quad_renderer_->Flush(); |
| 862 } | 850 } |
| 863 | 851 |
| 864 } // namespace vr_shell | 852 } // namespace vr_shell |
| OLD | NEW |