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_gl.h" |
| 6 |
| 7 #include <array> |
| 8 #include <cmath> |
| 9 |
| 10 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g
vr_types.h" |
| 11 |
| 12 namespace vr_shell { |
| 13 |
| 14 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) { |
| 15 // Note that this performs a *transpose* to a column-major matrix array, as |
| 16 // expected by GL. The input matrix has translation components at [i][3] for |
| 17 // use with row vectors and premultiplied transforms. In the output, the |
| 18 // translation elements are at the end at positions 3*4+i. |
| 19 std::array<float, 16> result; |
| 20 for (int i = 0; i < 4; ++i) { |
| 21 for (int j = 0; j < 4; ++j) { |
| 22 result[j * 4 + i] = matrix.m[i][j]; |
| 23 } |
| 24 } |
| 25 return result; |
| 26 } |
| 27 |
| 28 gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size, |
| 29 const gvr::Rectf& texture_rect) { |
| 30 float width = static_cast<float>(texture_size.width); |
| 31 float height = static_cast<float>(texture_size.height); |
| 32 gvr::Rectf rect = ModulateRect(texture_rect, width, height); |
| 33 gvr::Recti result = { |
| 34 static_cast<int>(rect.left), static_cast<int>(rect.right), |
| 35 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; |
| 36 return result; |
| 37 } |
| 38 |
| 39 GLuint CompileShader(GLenum shader_type, |
| 40 const GLchar* shader_source, |
| 41 std::string& error) { |
| 42 GLuint shader_handle = glCreateShader(shader_type); |
| 43 if (shader_handle != 0) { |
| 44 // Pass in the shader source. |
| 45 int len = strlen(shader_source); |
| 46 glShaderSource(shader_handle, 1, &shader_source, &len); |
| 47 // Compile the shader. |
| 48 glCompileShader(shader_handle); |
| 49 // Get the compilation status. |
| 50 GLint status; |
| 51 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); |
| 52 if (status == GL_FALSE) { |
| 53 GLint info_log_length; |
| 54 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 55 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 56 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log); |
| 57 error = "Error compiling shader: "; |
| 58 error += str_info_log; |
| 59 delete[] str_info_log; |
| 60 glDeleteShader(shader_handle); |
| 61 shader_handle = 0; |
| 62 } |
| 63 } |
| 64 |
| 65 return shader_handle; |
| 66 } |
| 67 |
| 68 GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, |
| 69 GLuint fragment_shader_handle, |
| 70 int num_attributes, |
| 71 const GLchar** attributes, |
| 72 std::string& error) { |
| 73 GLuint program_handle = glCreateProgram(); |
| 74 |
| 75 if (program_handle != 0) { |
| 76 // Bind the vertex shader to the program. |
| 77 glAttachShader(program_handle, vertext_shader_handle); |
| 78 |
| 79 // Bind the fragment shader to the program. |
| 80 glAttachShader(program_handle, fragment_shader_handle); |
| 81 |
| 82 // Bind attributes. This is optional, no need to supply them if |
| 83 // using glGetAttribLocation to look them up. Useful for a single |
| 84 // vertex array object (VAO) that is used with multiple shaders. |
| 85 if (attributes != nullptr) { |
| 86 for (int i = 0; i < num_attributes; i++) { |
| 87 glBindAttribLocation(program_handle, i, attributes[i]); |
| 88 } |
| 89 } |
| 90 |
| 91 // Link the two shaders together into a program. |
| 92 glLinkProgram(program_handle); |
| 93 |
| 94 // Get the link status. |
| 95 GLint link_status; |
| 96 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); |
| 97 |
| 98 // If the link failed, delete the program. |
| 99 if (link_status == GL_FALSE) { |
| 100 GLint info_log_length; |
| 101 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 102 |
| 103 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 104 glGetProgramInfoLog(program_handle, info_log_length, nullptr, |
| 105 str_info_log); |
| 106 error = "Error compiling program: "; |
| 107 error += str_info_log; |
| 108 delete[] str_info_log; |
| 109 glDeleteProgram(program_handle); |
| 110 program_handle = 0; |
| 111 } |
| 112 } |
| 113 |
| 114 return program_handle; |
| 115 } |
| 116 |
| 117 } // namespace vr_shell |
OLD | NEW |