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