OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 6 |
| 7 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 8 |
| 9 namespace vr_shell { |
| 10 |
| 11 namespace { |
| 12 |
| 13 const float kHalfHeight = 0.5f; |
| 14 const float kHalfWidth = 0.5f; |
| 15 const float kTextureQuadPosition[18] = { |
| 16 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, |
| 17 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, |
| 18 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f |
| 19 }; |
| 20 const int kPositionDataSize = 3; |
| 21 // Number of vertices passed to glDrawArrays(). |
| 22 const int kVerticesNumber = 6; |
| 23 |
| 24 const float kTexturedQuadTextureCoordinates[12] = { |
| 25 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, |
| 26 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f |
| 27 }; |
| 28 const int kTextureCoordinateDataSize = 2; |
| 29 |
| 30 const GLchar* kTexturedQuadVertexShader = |
| 31 "uniform mat4 u_CombinedMatrix;\n" |
| 32 "attribute vec4 a_Position;\n" |
| 33 "attribute vec2 a_TexCoordinate;\n" |
| 34 "varying vec2 v_TexCoordinate;\n" |
| 35 "\n" |
| 36 "void main() {\n" |
| 37 " v_TexCoordinate = a_TexCoordinate;\n" |
| 38 " gl_Position = u_CombinedMatrix * a_Position;\n" |
| 39 "}\n"; |
| 40 |
| 41 const GLchar* kTexturedQuadFragmentShader = |
| 42 "#extension GL_OES_EGL_image_external : require\n" |
| 43 "precision highp float;\n" |
| 44 "uniform samplerExternalOES u_Texture;\n" |
| 45 "varying vec2 v_TexCoordinate;\n" |
| 46 "\n" |
| 47 "void main() {\n" |
| 48 " vec4 texture = texture2D(u_Texture, v_TexCoordinate);\n" |
| 49 " gl_FragColor = vec4(texture.r, texture.g, texture.b, 1.0);\n" |
| 50 "}\n"; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 TexturedQuadRenderer::TexturedQuadRenderer() { |
| 55 std::string error; |
| 56 vertex_shader_handle_ = |
| 57 CompileShader(GL_VERTEX_SHADER, kTexturedQuadVertexShader, error); |
| 58 if (vertex_shader_handle_ == 0) { |
| 59 LOG(ERROR) << error; |
| 60 exit(1); |
| 61 } |
| 62 fragment_shader_handle_ = |
| 63 CompileShader(GL_FRAGMENT_SHADER, kTexturedQuadFragmentShader, error); |
| 64 if (fragment_shader_handle_ == 0) { |
| 65 LOG(ERROR) << error; |
| 66 exit(1); |
| 67 } |
| 68 |
| 69 program_handle_ = CreateAndLinkProgram( |
| 70 vertex_shader_handle_, fragment_shader_handle_, 4, nullptr, error); |
| 71 if (program_handle_ == 0) { |
| 72 LOG(ERROR) << error; |
| 73 exit(1); |
| 74 } |
| 75 combined_matrix_handle_ = |
| 76 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); |
| 77 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 78 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); |
| 79 texture_coordinate_handle_ = |
| 80 glGetAttribLocation(program_handle_, "a_TexCoordinate"); |
| 81 } |
| 82 |
| 83 void TexturedQuadRenderer::Draw(int texture_data_handle, |
| 84 const gvr::Mat4f& combined_matrix) { |
| 85 glUseProgram(program_handle_); |
| 86 |
| 87 // Pass in model view project matrix. |
| 88 glUniformMatrix4fv(combined_matrix_handle_, 1, false, |
| 89 MatrixToGLArray(combined_matrix).data()); |
| 90 |
| 91 // Pass in texture coordinate. |
| 92 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, |
| 93 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); |
| 94 glEnableVertexAttribArray(texture_coordinate_handle_); |
| 95 |
| 96 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, |
| 97 kTextureQuadPosition); |
| 98 glEnableVertexAttribArray(position_handle_); |
| 99 |
| 100 // Link texture data with texture unit. |
| 101 glActiveTexture(GL_TEXTURE0); |
| 102 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); |
| 103 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 104 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 105 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 106 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 107 glUniform1i(texture_uniform_handle_, 0); |
| 108 |
| 109 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 110 |
| 111 glDisableVertexAttribArray(position_handle_); |
| 112 glDisableVertexAttribArray(texture_coordinate_handle_); |
| 113 } |
| 114 |
| 115 TexturedQuadRenderer::~TexturedQuadRenderer() { |
| 116 glDeleteShader(vertex_shader_handle_); |
| 117 glDeleteShader(fragment_shader_handle_); |
| 118 } |
| 119 |
| 120 VrShellRenderer::VrShellRenderer() |
| 121 : textured_quad_renderer_(new TexturedQuadRenderer) {} |
| 122 |
| 123 VrShellRenderer::~VrShellRenderer() {} |
| 124 |
| 125 } // namespace vr_shell |
OLD | NEW |