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 <array> |
| 6 #include <cmath> |
| 7 |
| 8 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 9 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g
vr_types.h" |
| 10 |
| 11 namespace vr_shell { |
| 12 |
| 13 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) { |
| 14 // Note that this performs a *transpose* to a column-major matrix array, as |
| 15 // expected by GL. The input matrix has translation components at [i][3] for |
| 16 // use with row vectors and premultiplied transforms. In the output, the |
| 17 // translation elements are at the end at positions 3*4+i. |
| 18 std::array<float, 16> result; |
| 19 for (int i = 0; i < 4; ++i) { |
| 20 for (int j = 0; j < 4; ++j) { |
| 21 result[j * 4 + i] = matrix.m[i][j]; |
| 22 } |
| 23 } |
| 24 return result; |
| 25 } |
| 26 |
| 27 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { |
| 28 gvr::Mat4f result; |
| 29 for (int i = 0; i < 4; ++i) { |
| 30 for (int k = 0; k < 4; ++k) { |
| 31 result.m[i][k] = mat.m[k][i]; |
| 32 } |
| 33 } |
| 34 return result; |
| 35 } |
| 36 |
| 37 gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) { |
| 38 gvr::Mat4f result; |
| 39 for (int i = 0; i < 4; ++i) { |
| 40 for (int j = 0; j < 4; ++j) { |
| 41 result.m[i][j] = 0.0f; |
| 42 for (int k = 0; k < 4; ++k) { |
| 43 result.m[i][j] += matrix1.m[i][k] * matrix2.m[k][j]; |
| 44 } |
| 45 } |
| 46 } |
| 47 return result; |
| 48 } |
| 49 |
| 50 gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov, |
| 51 float z_near, |
| 52 float z_far) { |
| 53 gvr::Mat4f result; |
| 54 const float x_left = -std::tan(fov.left * M_PI / 180.0f) * z_near; |
| 55 const float x_right = std::tan(fov.right * M_PI / 180.0f) * z_near; |
| 56 const float y_bottom = -std::tan(fov.bottom * M_PI / 180.0f) * z_near; |
| 57 const float y_top = std::tan(fov.top * M_PI / 180.0f) * z_near; |
| 58 |
| 59 assert(x_left < x_right && y_bottom < y_top && z_near < z_far && |
| 60 z_near > 0.0f && z_far > 0.0f); |
| 61 const float X = (2 * z_near) / (x_right - x_left); |
| 62 const float Y = (2 * z_near) / (y_top - y_bottom); |
| 63 const float A = (x_right + x_left) / (x_right - x_left); |
| 64 const float B = (y_top + y_bottom) / (y_top - y_bottom); |
| 65 const float C = (z_near + z_far) / (z_near - z_far); |
| 66 const float D = (2 * z_near * z_far) / (z_near - z_far); |
| 67 |
| 68 for (int i = 0; i < 4; ++i) { |
| 69 for (int j = 0; j < 4; ++j) { |
| 70 result.m[i][j] = 0.0f; |
| 71 } |
| 72 } |
| 73 result.m[0][0] = X; |
| 74 result.m[0][2] = A; |
| 75 result.m[1][1] = Y; |
| 76 result.m[1][2] = B; |
| 77 result.m[2][2] = C; |
| 78 result.m[2][3] = D; |
| 79 result.m[3][2] = -1; |
| 80 |
| 81 return result; |
| 82 } |
| 83 |
| 84 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) { |
| 85 gvr::Rectf result = {rect.left * width, rect.right * width, |
| 86 rect.bottom * height, rect.top * height}; |
| 87 return result; |
| 88 } |
| 89 |
| 90 gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size, |
| 91 const gvr::Rectf& texture_rect) { |
| 92 float width = static_cast<float>(texture_size.width); |
| 93 float height = static_cast<float>(texture_size.height); |
| 94 gvr::Rectf rect = ModulateRect(texture_rect, width, height); |
| 95 gvr::Recti result = { |
| 96 static_cast<int>(rect.left), static_cast<int>(rect.right), |
| 97 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; |
| 98 return result; |
| 99 } |
| 100 |
| 101 GLuint CompileShader(GLenum shader_type, |
| 102 const GLchar* shader_source, |
| 103 std::string& error) { |
| 104 GLuint shader_handle = glCreateShader(shader_type); |
| 105 if (shader_handle != 0) { |
| 106 // Pass in the shader source. |
| 107 int len = strlen(shader_source); |
| 108 glShaderSource(shader_handle, 1, &shader_source, &len); |
| 109 // Compile the shader. |
| 110 glCompileShader(shader_handle); |
| 111 // Get the compilation status. |
| 112 GLint status; |
| 113 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); |
| 114 if (status == GL_FALSE) { |
| 115 GLint info_log_length; |
| 116 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 117 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 118 glGetShaderInfoLog(shader_handle, info_log_length, NULL, str_info_log); |
| 119 error = "Error compiling shader: "; |
| 120 error += str_info_log; |
| 121 delete[] str_info_log; |
| 122 glDeleteShader(shader_handle); |
| 123 shader_handle = 0; |
| 124 } |
| 125 } |
| 126 |
| 127 return shader_handle; |
| 128 } |
| 129 |
| 130 GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, |
| 131 GLuint fragment_shader_handle, |
| 132 int num_attributes, |
| 133 const GLchar** attributes, |
| 134 std::string& error) { |
| 135 GLuint program_handle = glCreateProgram(); |
| 136 |
| 137 if (program_handle != 0) { |
| 138 // Bind the vertex shader to the program. |
| 139 glAttachShader(program_handle, vertext_shader_handle); |
| 140 |
| 141 // Bind the fragment shader to the program. |
| 142 glAttachShader(program_handle, fragment_shader_handle); |
| 143 |
| 144 // Bind attributes. This is optional, no need to supply them if |
| 145 // using glGetAttribLocation to look them up. Useful for a single |
| 146 // vertex array object (VAO) that is used with multiple shaders. |
| 147 if (attributes != nullptr) { |
| 148 for (int i = 0; i < num_attributes; i++) { |
| 149 glBindAttribLocation(program_handle, i, attributes[i]); |
| 150 } |
| 151 } |
| 152 |
| 153 // Link the two shaders together into a program. |
| 154 glLinkProgram(program_handle); |
| 155 |
| 156 // Get the link status. |
| 157 GLint link_status; |
| 158 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); |
| 159 |
| 160 // If the link failed, delete the program. |
| 161 if (link_status == GL_FALSE) { |
| 162 GLint info_log_length; |
| 163 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 164 |
| 165 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 166 glGetProgramInfoLog(program_handle, info_log_length, NULL, str_info_log); |
| 167 error = "Error compiling program: "; |
| 168 error += str_info_log; |
| 169 delete[] str_info_log; |
| 170 glDeleteProgram(program_handle); |
| 171 program_handle = 0; |
| 172 } |
| 173 } |
| 174 |
| 175 return program_handle; |
| 176 } |
| 177 |
| 178 } // namespace vr_shell |
OLD | NEW |