vr_gl_util.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "device/vr/vr_gl_util.h"
  5. #include "ui/gfx/geometry/transform.h"
  6. namespace vr {
  7. // This code is adapted from the GVR Treasure Hunt demo source.
  8. std::array<float, 16> MatrixToGLArray(const gfx::Transform& transform) {
  9. std::array<float, 16> result;
  10. transform.matrix().getColMajor(result.data());
  11. return result;
  12. }
  13. GLuint CompileShader(GLenum shader_type,
  14. const std::string& shader_source,
  15. std::string& error) {
  16. GLuint shader_handle = glCreateShader(shader_type);
  17. if (shader_handle != 0) {
  18. // Pass in the shader source. No need to pass in a length for
  19. // null-terminated input.
  20. const char* source = shader_source.c_str();
  21. glShaderSource(shader_handle, 1, &source, nullptr);
  22. // Compile the shader.
  23. glCompileShader(shader_handle);
  24. // Get the compilation status.
  25. GLint status = GL_FALSE;
  26. glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
  27. if (status == GL_FALSE) {
  28. GLint info_log_length = 0;
  29. glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
  30. GLchar* str_info_log = new GLchar[info_log_length + 1];
  31. glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log);
  32. error = "Error compiling shader: ";
  33. error += str_info_log;
  34. delete[] str_info_log;
  35. glDeleteShader(shader_handle);
  36. shader_handle = 0;
  37. }
  38. } else {
  39. error = "Could not create a shader handle (did not attempt compilation).";
  40. }
  41. return shader_handle;
  42. }
  43. GLuint CreateAndLinkProgram(GLuint vertext_shader_handle,
  44. GLuint fragment_shader_handle,
  45. std::string& error) {
  46. GLuint program_handle = glCreateProgram();
  47. if (program_handle != 0) {
  48. // Bind the vertex shader to the program.
  49. glAttachShader(program_handle, vertext_shader_handle);
  50. // Bind the fragment shader to the program.
  51. glAttachShader(program_handle, fragment_shader_handle);
  52. // Link the two shaders together into a program.
  53. glLinkProgram(program_handle);
  54. // Get the link status.
  55. GLint link_status = GL_FALSE;
  56. glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
  57. // If the link failed, delete the program.
  58. if (link_status == GL_FALSE) {
  59. GLint info_log_length = 0;
  60. glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
  61. GLchar* str_info_log = new GLchar[info_log_length + 1];
  62. glGetProgramInfoLog(program_handle, info_log_length, nullptr,
  63. str_info_log);
  64. error = "Error compiling program: ";
  65. error += str_info_log;
  66. delete[] str_info_log;
  67. glDeleteProgram(program_handle);
  68. program_handle = 0;
  69. }
  70. }
  71. return program_handle;
  72. }
  73. void SetTexParameters(GLenum texture_type) {
  74. glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  75. glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  76. glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  77. glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  78. }
  79. void SetColorUniform(GLuint handle, SkColor c) {
  80. glUniform4f(handle, SkColorGetR(c) / 255.0, SkColorGetG(c) / 255.0,
  81. SkColorGetB(c) / 255.0, SkColorGetA(c) / 255.0);
  82. }
  83. void SetOpaqueColorUniform(GLuint handle, SkColor c) {
  84. glUniform3f(handle, SkColorGetR(c) / 255.0, SkColorGetG(c) / 255.0,
  85. SkColorGetB(c) / 255.0);
  86. }
  87. } // namespace vr