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