1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/graphics/opengl/traits.d, _traits.d)
6  * Documentation:
7  * Coverage:
8  */
9 module liberty.graphics.opengl.traits;
10 version (__OpenGL__) :
11 import derelict.opengl;
12 import std.string, std.typetuple, std.typecons, std.traits;
13 import liberty.math.vector : Vector;
14 ///
15 bool isVideoIntegerType(uint t) pure nothrow @safe @nogc {
16 	return (t == GL_BYTE || t == GL_UNSIGNED_BYTE || t == GL_SHORT || t == GL_UNSIGNED_SHORT || t == GL_INT || t == GL_UNSIGNED_INT);
17 }
18 ///
19 alias VideoVectorTypes = TypeTuple!(byte, ubyte, short, ushort, int, uint, float, double);
20 ///
21 enum uint[] GLVectorTypes = [GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_DOUBLE];
22 ///
23 template isSupportedScalarType(T) {
24 	enum isSupportedScalarType = staticIndexOf!(Unqual!T, GLVectorTypes) != -1;
25 }
26 ///
27 template typeToGLScalar(T) {
28 	alias U = Unqual!T;
29 	enum index = staticIndexOf!(U, VectorTypes);
30 	static if (index == -1) {
31         static assert(0, "Could not use " ~ T.stringof ~ " in a vertex description!");
32     } else {
33         enum typeToGLScalar = VectorTypesGL[index];
34     }
35 }
36 ///
37 void toGLTypeAndSize(T)(out uint type, out int n) {
38     static if (isSupportedScalarType!T) {
39         type = typeToGLScalar!T;
40         n = 1;
41     } else static if (isStaticArray!T) {
42         type = typeToGLScalar!(typeof(T.init[0]));
43         n = T.length;
44     } else {
45         alias U = Unqual!T;
46         foreach(int t, S ; VideoVectorTypes) {
47             static if (is (U == Vector!(S, 2))) {
48                 type = GLVectorTypes[t];
49                 n = 2;
50                 return;
51             }
52             static if (is (U == Vector!(S, 3))) {
53                 type = GLVectorTypes[t];
54                 n = 3;
55                 return;
56             }
57             static if (is (U == Vector!(S, 4))) {
58                 type = GLVectorTypes[t];
59                 n = 4;
60                 return;
61             }
62         }
63         assert(false, "Could not use " ~ T.stringof ~ " in a vertex description.");
64     }
65 }