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 // This code is adapted from the GVR Treasure Hunt demo source. |
| 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 // This code is adapted from the GVR Treasure Hunt demo source. |
| 28 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) { |
| 29 gvr::Rectf result = {rect.left * width, rect.right * width, |
| 30 rect.bottom * height, rect.top * height}; |
| 31 return result; |
| 32 } |
| 33 |
| 34 // This code is adapted from the GVR Treasure Hunt demo source. |
| 35 gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size, |
| 36 const gvr::Rectf& texture_rect) { |
| 37 float width = static_cast<float>(texture_size.width); |
| 38 float height = static_cast<float>(texture_size.height); |
| 39 gvr::Rectf rect = ModulateRect(texture_rect, width, height); |
| 40 gvr::Recti result = { |
| 41 static_cast<int>(rect.left), static_cast<int>(rect.right), |
| 42 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; |
| 43 return result; |
| 44 } |
| 45 |
| 46 GLuint CompileShader(GLenum shader_type, |
| 47 const GLchar* shader_source, |
| 48 std::string& error) { |
| 49 GLuint shader_handle = glCreateShader(shader_type); |
| 50 if (shader_handle != 0) { |
| 51 // Pass in the shader source. |
| 52 int len = strlen(shader_source); |
| 53 glShaderSource(shader_handle, 1, &shader_source, &len); |
| 54 // Compile the shader. |
| 55 glCompileShader(shader_handle); |
| 56 // Get the compilation status. |
| 57 GLint status; |
| 58 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); |
| 59 if (status == GL_FALSE) { |
| 60 GLint info_log_length; |
| 61 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 62 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 63 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log); |
| 64 error = "Error compiling shader: "; |
| 65 error += str_info_log; |
| 66 delete[] str_info_log; |
| 67 glDeleteShader(shader_handle); |
| 68 shader_handle = 0; |
| 69 } |
| 70 } |
| 71 |
| 72 return shader_handle; |
| 73 } |
| 74 |
| 75 GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, |
| 76 GLuint fragment_shader_handle, |
| 77 int num_attributes, |
| 78 const GLchar** attributes, |
| 79 std::string& error) { |
| 80 GLuint program_handle = glCreateProgram(); |
| 81 |
| 82 if (program_handle != 0) { |
| 83 // Bind the vertex shader to the program. |
| 84 glAttachShader(program_handle, vertext_shader_handle); |
| 85 |
| 86 // Bind the fragment shader to the program. |
| 87 glAttachShader(program_handle, fragment_shader_handle); |
| 88 |
| 89 // Bind attributes. This is optional, no need to supply them if |
| 90 // using glGetAttribLocation to look them up. Useful for a single |
| 91 // vertex array object (VAO) that is used with multiple shaders. |
| 92 if (attributes != nullptr) { |
| 93 for (int i = 0; i < num_attributes; i++) { |
| 94 glBindAttribLocation(program_handle, i, attributes[i]); |
| 95 } |
| 96 } |
| 97 |
| 98 // Link the two shaders together into a program. |
| 99 glLinkProgram(program_handle); |
| 100 |
| 101 // Get the link status. |
| 102 GLint link_status; |
| 103 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); |
| 104 |
| 105 // If the link failed, delete the program. |
| 106 if (link_status == GL_FALSE) { |
| 107 GLint info_log_length; |
| 108 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); |
| 109 |
| 110 GLchar* str_info_log = new GLchar[info_log_length + 1]; |
| 111 glGetProgramInfoLog(program_handle, info_log_length, nullptr, |
| 112 str_info_log); |
| 113 error = "Error compiling program: "; |
| 114 error += str_info_log; |
| 115 delete[] str_info_log; |
| 116 glDeleteProgram(program_handle); |
| 117 program_handle = 0; |
| 118 } |
| 119 } |
| 120 |
| 121 return program_handle; |
| 122 } |
| 123 |
| 124 } // namespace vr_shell |
OLD | NEW |