1
1

common.d 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. module three.common;
  2. public import derelict.opengl3.gl3;
  3. public import derelict.glfw3.glfw3;
  4. public import derelict.anttweakbar.anttweakbar;
  5. public import derelict.freeimage.freeimage;
  6. public import derelict.freetype.ft;
  7. public import derelict.assimp3.assimp;
  8. public import std.experimental.logger;
  9. alias SoA(T) = T[];
  10. import std.traits : ReturnType;
  11. ReturnType!func glCheck(alias func, string file = __FILE__, size_t line = __LINE__, string mod = __MODULE__, string funcd = __FUNCTION__, string pretty = __PRETTY_FUNCTION__, Args...)(Args args) nothrow {
  12. import std.stdio;
  13. import std.stdio : stderr;
  14. import std.array : join;
  15. import std.range : repeat;
  16. import std.string : format;
  17. try{
  18. debug scope(exit) {
  19. GLenum err = glGetError();
  20. if(err != GL_NO_ERROR) {
  21. stderr.writeln("\n===============================");
  22. stderr.writeln("File: ", file, "\nLine: ", line, "\nModule: ",mod, "\nFunction: ",funcd, "\n",pretty);
  23. stderr.writeln("-------------------------------");
  24. stderr.writefln(`OpenGL function "%s(%s)" failed: "%s."`, func.stringof, format("%s".repeat(Args.length).join(", "), args), glErrorString(err));
  25. stderr.writeln("=============================== \n");
  26. assert(false);
  27. }
  28. }
  29. }
  30. catch(Exception e){
  31. }
  32. debug if(func is null) {
  33. try{
  34. stderr.writefln("%s is null! OpenGL loaded? Required OpenGL version not supported?".format(func.stringof));
  35. }
  36. catch(Exception e){
  37. assert(false);
  38. }
  39. assert(false);
  40. }
  41. return func(args);
  42. }
  43. string glErrorString(GLenum error) pure @safe nothrow @nogc {
  44. final switch(error) {
  45. case GL_NO_ERROR: return "no error";
  46. case GL_INVALID_ENUM: return "invalid enum";
  47. case GL_INVALID_VALUE: return "invalid value";
  48. case GL_INVALID_OPERATION: return "invalid operation";
  49. //case GL_STACK_OVERFLOW: return "stack overflow";
  50. //case GL_STACK_UNDERFLOW: return "stack underflow";
  51. case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffer operation";
  52. case GL_OUT_OF_MEMORY: return "out of memory";
  53. }
  54. assert(false, "invalid enum");
  55. }
  56. //==============================================================================
  57. ///
  58. template toGlType(T) {
  59. static if(is(T == byte)) {
  60. enum toGlType = GL_BYTE;
  61. } else static if(is(T == ubyte)) {
  62. enum toGlType = GL_UNSIGNED_BYTE;
  63. } else static if(is(T == short)) {
  64. enum toGlType = GL_SHORT;
  65. } else static if(is(T == ushort)) {
  66. enum toGlType = GL_UNSIGNED_SHORT;
  67. } else static if(is(T == int)) {
  68. enum toGlType = GL_INT;
  69. } else static if(is(T == uint)) {
  70. enum toGlType = GL_UNSIGNED_INT;
  71. } else static if(is(T == float)) {
  72. enum toGlType = GL_FLOAT;
  73. } else static if(is(T == double)) {
  74. enum toGlType = GL_DOUBLE;
  75. } else {
  76. static assert(false, T.stringof ~ " cannot be represented as GLenum");
  77. }
  78. }
  79. //==============================================================================
  80. ///
  81. template sizeofGlType(GLenum t) {
  82. static if(t == GL_BYTE) {
  83. enum sizeofGlType = byte.sizeof;
  84. } else static if(t == GL_UNSIGNED_BYTE) {
  85. enum sizeofGlType = ubyte.sizeof;
  86. } else static if(t == GL_SHORT) {
  87. enum sizeofGlType = short.sizeof;
  88. } else static if(t == GL_UNSIGNED_SHORT) {
  89. enum sizeofGlType = ushort.sizeof;
  90. } else static if(t == GL_INT) {
  91. enum sizeofGlType = int.sizeof;
  92. } else static if(t == GL_UNSIGNED_INT) {
  93. enum sizeofGlType = uint.sizeof;
  94. } else static if(t == GL_FLOAT) {
  95. enum sizeofGlType = float.sizeof;
  96. } else static if(t == GL_DOUBLE) {
  97. enum sizeofGlType = double.sizeof;
  98. } else {
  99. static assert(false, T.stringof ~ " cannot be represented as D-Type");
  100. }
  101. }