utility.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. static auto glrSize(uint size) -> uint {
  2. return size;
  3. //return bit::round(size); //return nearest power of two
  4. }
  5. static auto glrFormat(const string& format) -> GLuint {
  6. if(format == "r32i" ) return GL_R32I;
  7. if(format == "r32ui" ) return GL_R32UI;
  8. if(format == "rgba8" ) return GL_RGBA8;
  9. if(format == "rgb10a2") return GL_RGB10_A2;
  10. if(format == "rgba12" ) return GL_RGBA12;
  11. if(format == "rgba16" ) return GL_RGBA16;
  12. if(format == "rgba16f") return GL_RGBA16F;
  13. if(format == "rgba32f") return GL_RGBA32F;
  14. return GL_RGBA8;
  15. }
  16. static auto glrFilter(const string& filter) -> GLuint {
  17. if(filter == "nearest") return GL_NEAREST;
  18. if(filter == "linear" ) return GL_LINEAR;
  19. return GL_LINEAR;
  20. }
  21. static auto glrWrap(const string& wrap) -> GLuint {
  22. if(wrap == "border") return GL_CLAMP_TO_BORDER;
  23. if(wrap == "edge" ) return GL_CLAMP_TO_EDGE;
  24. if(wrap == "repeat") return GL_REPEAT;
  25. return GL_CLAMP_TO_BORDER;
  26. }
  27. static auto glrModulo(uint modulo) -> uint {
  28. if(modulo) return modulo;
  29. return 300; //divisible by 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 100, 150
  30. }
  31. static auto glrProgram() -> GLuint {
  32. GLuint program = 0;
  33. glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&program);
  34. return program;
  35. }
  36. static auto glrUniform1i(const string& name, GLint value) -> void {
  37. GLint location = glGetUniformLocation(glrProgram(), name);
  38. glUniform1i(location, value);
  39. }
  40. static auto glrUniform4f(const string& name, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3) -> void {
  41. GLint location = glGetUniformLocation(glrProgram(), name);
  42. glUniform4f(location, value0, value1, value2, value3);
  43. }
  44. static auto glrUniformMatrix4fv(const string& name, GLfloat* values) -> void {
  45. GLint location = glGetUniformLocation(glrProgram(), name);
  46. glUniformMatrix4fv(location, 1, GL_FALSE, values);
  47. }
  48. static auto glrParameters(GLuint filter, GLuint wrap) -> void {
  49. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
  50. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
  51. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
  52. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
  53. }
  54. static auto glrCreateShader(GLuint program, GLuint type, const char* source) -> GLuint {
  55. GLuint shader = glCreateShader(type);
  56. glShaderSource(shader, 1, &source, 0);
  57. glCompileShader(shader);
  58. GLint result = GL_FALSE;
  59. glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
  60. if(result == GL_FALSE) {
  61. GLint length = 0;
  62. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
  63. char text[length + 1];
  64. glGetShaderInfoLog(shader, length, &length, text);
  65. text[length] = 0;
  66. print("[ruby::OpenGL: shader compiler error]\n", (const char*)text, "\n\n");
  67. return 0;
  68. }
  69. glAttachShader(program, shader);
  70. return shader;
  71. }
  72. static auto glrLinkProgram(GLuint program) -> void {
  73. glLinkProgram(program);
  74. GLint result = GL_FALSE;
  75. glGetProgramiv(program, GL_LINK_STATUS, &result);
  76. if(result == GL_FALSE) {
  77. GLint length = 0;
  78. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
  79. char text[length + 1];
  80. glGetProgramInfoLog(program, length, &length, text);
  81. text[length] = 0;
  82. print("[ruby::OpenGL: shader linker error]\n", (const char*)text, "\n\n");
  83. }
  84. glValidateProgram(program);
  85. result = GL_FALSE;
  86. glGetProgramiv(program, GL_VALIDATE_STATUS, &result);
  87. if(result == GL_FALSE) {
  88. GLint length = 0;
  89. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
  90. char text[length + 1];
  91. glGetProgramInfoLog(program, length, &length, text);
  92. text[length] = 0;
  93. print("[ruby::OpenGL: shader validation error]\n", (const char*)text, "\n\n");
  94. }
  95. }