1 /** 2 Copyright: Copyright (c) 2016 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module voxelman.model.vertex; 8 9 import std.meta; 10 import derelict.opengl3.gl3; 11 import voxelman.math; 12 13 align(4) struct VertexPosColor(PosType, ColType) 14 { 15 this(Pos, Col)(Pos x, Pos y, Pos z, Col color) 16 { 17 this.position = Vector!(PosType, 3)(x, y, z); 18 this.color = Vector!(ColType, 3)(color); 19 } 20 21 this(Vector!(PosType, 3) p, Vector!(ColType, 3) color) 22 { 23 this.position = p; 24 this.color = color; 25 } 26 27 align(4): 28 Vector!(PosType, 3) position; 29 Vector!(ColType, 3) color; 30 31 void toString()(scope void delegate(const(char)[]) sink) const 32 { 33 import std.format : formattedWrite; 34 sink.formattedWrite("V!(%s,%s)(%s, %s)", stringof(PosType), stringof(ColType), position, color); 35 } 36 37 static void setAttributes() { 38 enum Size = typeof(this).sizeof; 39 40 // position 41 glEnableVertexAttribArray(0); 42 enum uint posGlType = glTypeOf!PosType; 43 enum posOffset = position.offsetof; 44 enum bool doPosNomalization = GL_FALSE; 45 glVertexAttribPointer(0, 3, posGlType, doPosNomalization, Size, null); 46 47 // color 48 glEnableVertexAttribArray(1); 49 enum uint colGlType = glTypeOf!ColType; 50 enum colOffset = color.offsetof; 51 enum bool doColNomalization = normalizeColorType!ColType; 52 glVertexAttribPointer(1, 3, colGlType, doColNomalization, Size, cast(void*)colOffset); 53 } 54 } 55 56 template glTypeOf(T) { 57 enum glTypeOf = glTypes[glTypeIndex!T]; 58 } 59 60 template normalizeColorType(T) { 61 enum normalizeColorType = normalizeColorTable[glTypeIndex!T]; 62 } 63 64 alias glTypeIndex(T) = staticIndexOf!(T, 65 byte, // GL_BYTE 66 ubyte, // GL_UNSIGNED_BYTE 67 short, // GL_SHORT 68 ushort,// GL_UNSIGNED_SHORT 69 int, // GL_INT 70 uint, // GL_UNSIGNED_INT 71 half, // GL_HALF_FLOAT 72 float, // GL_FLOAT 73 double,// GL_DOUBLE 74 ); 75 76 static immutable uint[] glTypes = [ 77 GL_BYTE, 78 GL_UNSIGNED_BYTE, 79 GL_SHORT, 80 GL_UNSIGNED_SHORT, 81 GL_INT, 82 GL_UNSIGNED_INT, 83 GL_HALF_FLOAT, 84 GL_FLOAT, 85 GL_DOUBLE, 86 ]; 87 88 static immutable bool[] normalizeColorTable = [ 89 true, // GL_BYTE 90 true, // GL_UNSIGNED_BYTE 91 true, // GL_SHORT 92 true, // GL_UNSIGNED_SHORT 93 true, // GL_INT 94 true, // GL_UNSIGNED_INT 95 false, // GL_HALF_FLOAT 96 false, // GL_FLOAT 97 false, // GL_DOUBLE 98 ];