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