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_icons.h" |
5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 6 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
6 | 7 |
7 #include "chrome/browser/android/vr_shell/vr_gl_util.h" | 8 #include "chrome/browser/android/vr_shell/vr_gl_util.h" |
8 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 #define RECTANGULAR_TEXTURE_BUFFER(left, right, bottom, top) \ | 13 #define RECTANGULAR_TEXTURE_BUFFER(left, right, bottom, top) \ |
13 { left, bottom, left, top, right, bottom, left, top, right, top, right, \ | 14 { left, bottom, left, top, right, bottom, left, top, right, top, right, \ |
14 bottom } | 15 bottom } |
(...skipping 19 matching lines...) Expand all Loading... |
34 -1.f, -1.f, 0.f, 1.f, | 35 -1.f, -1.f, 0.f, 1.f, |
35 0.f, -1.f, 1.f, 1.f, | 36 0.f, -1.f, 1.f, 1.f, |
36 0.f, 1.f, 1.f, 0.f, | 37 0.f, 1.f, 1.f, 0.f, |
37 | 38 |
38 0.f, 1.f, 0.f, 0.f, // Right Eye | 39 0.f, 1.f, 0.f, 0.f, // Right Eye |
39 0.f, -1.f, 0.f, 1.f, | 40 0.f, -1.f, 0.f, 1.f, |
40 1.f, -1.f, 1.f, 1.f, | 41 1.f, -1.f, 1.f, 1.f, |
41 1.f, 1.f, 1.f, 0.f }; | 42 1.f, 1.f, 1.f, 0.f }; |
42 const int kWebVrVerticesSize = sizeof(float) * 32; | 43 const int kWebVrVerticesSize = sizeof(float) * 32; |
43 | 44 |
| 45 const float kOverlayIconVertices[32] = { |
| 46 // x y u, v |
| 47 -1.f, 1.f, 0.f, 0.f, |
| 48 -1.f, -1.f, 0.f, 1.f, |
| 49 1.f, -1.f, 1.f, 1.f, |
| 50 1.f, 1.f, 1.f, 0.f}; |
| 51 const int kOverlayIconVerticesSize = sizeof(float) * 16; |
| 52 |
44 // Reticle constants | 53 // Reticle constants |
45 static constexpr float kRingDiameter = 1.0f; | 54 static constexpr float kRingDiameter = 1.0f; |
46 static constexpr float kInnerHole = 0.0f; | 55 static constexpr float kInnerHole = 0.0f; |
47 static constexpr float kInnerRingEnd = 0.177f; | 56 static constexpr float kInnerRingEnd = 0.177f; |
48 static constexpr float kInnerRingThickness = 0.14f; | 57 static constexpr float kInnerRingThickness = 0.14f; |
49 static constexpr float kMidRingEnd = 0.177f; | 58 static constexpr float kMidRingEnd = 0.177f; |
50 static constexpr float kMidRingOpacity = 0.22f; | 59 static constexpr float kMidRingOpacity = 0.22f; |
51 static constexpr float kReticleColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; | 60 static constexpr float kReticleColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; |
52 | 61 |
53 // Laser constants | 62 // Laser constants |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 mediump vec2 uv = v_TexCoordinate; | 176 mediump vec2 uv = v_TexCoordinate; |
168 mediump float front_fade_factor = 1.0 - | 177 mediump float front_fade_factor = 1.0 - |
169 clamp(1.0 - (uv.y - fade_point) / (1.0 - fade_point), 0.0, 1.0); | 178 clamp(1.0 - (uv.y - fade_point) / (1.0 - fade_point), 0.0, 1.0); |
170 mediump float back_fade_factor = | 179 mediump float back_fade_factor = |
171 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); | 180 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); |
172 mediump float total_fade = front_fade_factor * back_fade_factor; | 181 mediump float total_fade = front_fade_factor * back_fade_factor; |
173 lowp vec4 texture_color = texture2D(texture_unit, uv); | 182 lowp vec4 texture_color = texture2D(texture_unit, uv); |
174 lowp vec4 final_color = color * texture_color; | 183 lowp vec4 final_color = color * texture_color; |
175 gl_FragColor = vec4(final_color.xyz, final_color.w * total_fade); | 184 gl_FragColor = vec4(final_color.xyz, final_color.w * total_fade); |
176 }); | 185 }); |
| 186 case vr_shell::ShaderID::OVERLAY_ICON_VERTEX_SHADER: |
| 187 return SHADER( |
| 188 uniform mat4 u_CombinedMatrix; |
| 189 attribute vec2 a_Position; |
| 190 attribute vec2 a_TexCoordinate; |
| 191 uniform vec4 u_SrcRect; |
| 192 varying vec2 v_TexCoordinate; |
| 193 |
| 194 void main() { |
| 195 v_TexCoordinate = u_SrcRect.xy + (a_TexCoordinate * u_SrcRect.zw); |
| 196 gl_Position = u_CombinedMatrix * vec4(a_Position, 0.0, 1.0); |
| 197 }); |
| 198 case vr_shell::ShaderID::OVERLAY_ICON_FRAGMENT_SHADER: |
| 199 return SHADER( |
| 200 precision highp float; |
| 201 uniform sampler2D u_Texture; |
| 202 varying vec2 v_TexCoordinate; |
| 203 |
| 204 void main() { |
| 205 gl_FragColor = texture2D(u_Texture, v_TexCoordinate); |
| 206 }); |
177 default: | 207 default: |
178 LOG(ERROR) << "Shader source requested for unknown shader"; | 208 LOG(ERROR) << "Shader source requested for unknown shader"; |
179 return ""; | 209 return ""; |
180 } | 210 } |
181 } | 211 } |
182 | 212 |
183 #undef RECTANGULAR_TEXTURE_BUFFER | 213 #undef RECTANGULAR_TEXTURE_BUFFER |
184 } // namespace | 214 } // namespace |
185 | 215 |
186 namespace vr_shell { | 216 namespace vr_shell { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 glUniform1f(fade_end_handle_, kFadeEnd); | 462 glUniform1f(fade_end_handle_, kFadeEnd); |
433 | 463 |
434 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); | 464 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
435 | 465 |
436 glDisableVertexAttribArray(position_handle_); | 466 glDisableVertexAttribArray(position_handle_); |
437 glDisableVertexAttribArray(tex_coord_handle_); | 467 glDisableVertexAttribArray(tex_coord_handle_); |
438 } | 468 } |
439 | 469 |
440 LaserRenderer::~LaserRenderer() = default; | 470 LaserRenderer::~LaserRenderer() = default; |
441 | 471 |
| 472 void MakeTexImage(const icon_rle_image_t& icon, |
| 473 GLuint texture_data_handles[], |
| 474 int icon_id) { |
| 475 // Helper function to decode a bitmap from Gimp ".c file" |
| 476 // run length encoded format and store it as a texture. |
| 477 // TODO(klausw): remove the Gimp decoding bits once we |
| 478 // can create the content from text. |
| 479 |
| 480 int image_pixels = icon.width * icon.height; |
| 481 int image_bytes = image_pixels * icon.bytes_per_pixel; |
| 482 |
| 483 char* image_data = (char*)malloc(image_bytes); |
| 484 if (!image_data) { |
| 485 LOG(ERROR) << "Failed to allocate memory for image " << icon_id; |
| 486 exit(1); |
| 487 } |
| 488 ICON_RUN_LENGTH_DECODE(image_data, icon.rle_pixel_data, |
| 489 image_pixels, icon.bytes_per_pixel); |
| 490 |
| 491 glBindTexture(GL_TEXTURE_2D, texture_data_handles[icon_id]); |
| 492 |
| 493 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, icon.width, icon.height, |
| 494 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data); |
| 495 |
| 496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 497 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 498 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 499 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 500 } |
| 501 |
| 502 OverlayIconRenderer::OverlayIconRenderer() { |
| 503 bounds_ = { 0.0f, 0.0f, 1.0f, 1.0f }; |
| 504 |
| 505 std::string error; |
| 506 GLuint vertex_shader_handle = CompileShader( |
| 507 GL_VERTEX_SHADER, GetShaderSource(OVERLAY_ICON_VERTEX_SHADER), error); |
| 508 CHECK(vertex_shader_handle) << error; |
| 509 |
| 510 GLuint fragment_shader_handle = CompileShader( |
| 511 GL_FRAGMENT_SHADER, GetShaderSource(OVERLAY_ICON_FRAGMENT_SHADER), error); |
| 512 CHECK(fragment_shader_handle) << error; |
| 513 |
| 514 program_handle_ = CreateAndLinkProgram( |
| 515 vertex_shader_handle, fragment_shader_handle, error); |
| 516 CHECK(program_handle_) << error; |
| 517 |
| 518 // Once the program is linked the shader objects are no longer needed |
| 519 glDeleteShader(vertex_shader_handle); |
| 520 glDeleteShader(fragment_shader_handle); |
| 521 |
| 522 combined_matrix_handle_ = |
| 523 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); |
| 524 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 525 src_rect_uniform_handle_ = glGetUniformLocation(program_handle_, "u_SrcRect"); |
| 526 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); |
| 527 texcoord_handle_ = glGetAttribLocation(program_handle_, "a_TexCoordinate"); |
| 528 |
| 529 // TODO(bajones): Figure out why this need to be restored. |
| 530 GLint old_buffer; |
| 531 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &old_buffer); |
| 532 |
| 533 glGenBuffersARB(1, &vertex_buffer_); |
| 534 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 535 glBufferData(GL_ARRAY_BUFFER, kOverlayIconVerticesSize, kOverlayIconVertices, |
| 536 GL_STATIC_DRAW); |
| 537 |
| 538 glBindBuffer(GL_ARRAY_BUFFER, old_buffer); |
| 539 |
| 540 glGenTextures(ICON_ID_MAX, texture_data_handles_); |
| 541 |
| 542 MakeTexImage(icon_not_secure_small, texture_data_handles_, |
| 543 ICON_INSECURE_PERMANENT); |
| 544 MakeTexImage(icon_not_secure_verbose, texture_data_handles_, |
| 545 ICON_INSECURE_TRANSIENT); |
| 546 } |
| 547 |
| 548 void OverlayIconRenderer::Draw(const gvr::Mat4f& combined_matrix, |
| 549 int icon_num) { |
| 550 // TODO(bajones): Figure out why this need to be restored. |
| 551 GLint old_buffer; |
| 552 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &old_buffer); |
| 553 |
| 554 glUseProgram(program_handle_); |
| 555 |
| 556 // Bind vertex attributes |
| 557 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 558 |
| 559 glEnableVertexAttribArray(position_handle_); |
| 560 glEnableVertexAttribArray(texcoord_handle_); |
| 561 |
| 562 // Pass in model view project matrix. |
| 563 glUniformMatrix4fv(combined_matrix_handle_, 1, false, |
| 564 MatrixToGLArray(combined_matrix).data()); |
| 565 |
| 566 glVertexAttribPointer(position_handle_, POSITION_ELEMENTS, GL_FLOAT, false, |
| 567 VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET)); |
| 568 glVertexAttribPointer(texcoord_handle_, TEXCOORD_ELEMENTS, GL_FLOAT, false, |
| 569 VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET)); |
| 570 |
| 571 // Bind texture. |
| 572 glActiveTexture(GL_TEXTURE0); |
| 573 glBindTexture(GL_TEXTURE_2D, texture_data_handles_[icon_num]); |
| 574 glUniform1i(tex_uniform_handle_, 0); |
| 575 |
| 576 glEnable(GL_BLEND); |
| 577 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 578 |
| 579 glUniform4fv(src_rect_uniform_handle_, 1, (float*)(&bounds_)); |
| 580 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 581 |
| 582 glDisableVertexAttribArray(position_handle_); |
| 583 glDisableVertexAttribArray(texcoord_handle_); |
| 584 |
| 585 glBindBuffer(GL_ARRAY_BUFFER, old_buffer); |
| 586 } |
| 587 |
| 588 OverlayIconRenderer::~OverlayIconRenderer() = default; |
| 589 |
442 VrShellRenderer::VrShellRenderer() | 590 VrShellRenderer::VrShellRenderer() |
443 : textured_quad_renderer_(new TexturedQuadRenderer), | 591 : textured_quad_renderer_(new TexturedQuadRenderer), |
444 webvr_renderer_(new WebVrRenderer), | 592 webvr_renderer_(new WebVrRenderer), |
445 reticle_renderer_(new ReticleRenderer), | 593 reticle_renderer_(new ReticleRenderer), |
446 laser_renderer_(new LaserRenderer) {} | 594 laser_renderer_(new LaserRenderer), |
| 595 overlay_icon_renderer_(new OverlayIconRenderer) {} |
447 | 596 |
448 VrShellRenderer::~VrShellRenderer() = default; | 597 VrShellRenderer::~VrShellRenderer() = default; |
449 | 598 |
450 } // namespace vr_shell | 599 } // namespace vr_shell |
OLD | NEW |