1 /**
2 Copyright: Copyright (c) 2014-2017 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module voxelman.graphics.cubeutils;
8 
9 import voxelman.log;
10 
11 import voxelman.container.buffer;
12 
13 import voxelman.geometry.cube : CubeSide, cubeFaces;
14 import voxelman.math;
15 import voxelman.graphics;
16 
17 
18 void putFilledBlock(V)(ref Buffer!V output, vec3 pos, vec3 size, Color4ub color)
19 {
20 	output.reserve(6 * 6); // 6 faces, 6 points per edge
21 
22 	for (size_t i = 0; i!=18*6; i+=3)
23 	{
24 		auto v = V(
25 			cubeFaces[i  ]*size.x + pos.x,
26 			cubeFaces[i+1]*size.y + pos.y,
27 			cubeFaces[i+2]*size.z + pos.z,
28 			color);
29 		output.put(v);
30 	}
31 }
32 
33 void putLineBlock(V)(ref Buffer!V output, vec3 pos, vec3 size, Color4ub color)
34 {
35 	output.reserve(12 * 2); // 12 edges, 2 points per edge
36 
37 	for (size_t i = 0; i!=12*2*3; i+=3)
38 	{
39 		auto v = V(
40 			cubeLines[i  ]*size.x + pos.x,
41 			cubeLines[i+1]*size.y + pos.y,
42 			cubeLines[i+2]*size.z + pos.z,
43 			color);
44 		output.put(v);
45 	}
46 }
47 
48 void putFilledSide(V)(ref Buffer!V output, vec3 pos, vec3 size, CubeSide side, Color4ub color)
49 {
50 	output.reserve(6);
51 
52 	for (size_t i = side * 18; i!=side*18+18; i+=3)
53 	{
54 		auto v = V(
55 			cubeFaces[i  ]*size.x + pos.x,
56 			cubeFaces[i+1]*size.y + pos.y,
57 			cubeFaces[i+2]*size.z + pos.z,
58 			color);
59 		output.put(v);
60 	}
61 }
62 
63 void putLineSide(V)(ref Buffer!V output, vec3 pos, vec3 size, CubeSide side, Color4ub color)
64 {
65 	output.reserve(8); // 4 edges, 2 points per edge
66 
67 	for (size_t i = side * 24; i!=side*24+24; i+=3)
68 	{
69 		auto v = V(
70 			cubeLineSides[i  ]*size.x + pos.x,
71 			cubeLineSides[i+1]*size.y + pos.y,
72 			cubeLineSides[i+2]*size.z + pos.z,
73 			color);
74 		output.put(v);
75 	}
76 }
77 
78 immutable ubyte[] cubeLines =
79 [
80 	0, 0, 0,  1, 0, 0,
81 	1, 0, 0,  1, 0, 1,
82 	1, 0, 1,  0, 0, 1,
83 	0, 0, 1,  0, 0, 0,
84 
85 	0, 1, 0,  1, 1, 0,
86 	1, 1, 0,  1, 1, 1,
87 	1, 1, 1,  0, 1, 1,
88 	0, 1, 1,  0, 1, 0,
89 
90 	0, 0, 0,  0, 1, 0,
91 	1, 0, 0,  1, 1, 0,
92 	1, 0, 1,  1, 1, 1,
93 	0, 0, 1,  0, 1, 1,
94 ];
95 
96 immutable ubyte[] cubeLineSides =
97 [
98 	0, 0, 0, // zneg
99 	1, 0, 0,
100 	0, 1, 0,
101 	1, 1, 0,
102 	0, 0, 0,
103 	0, 1, 0,
104 	1, 0, 0,
105 	1, 1, 0,
106 
107 	0, 0, 1, // zpos
108 	1, 0, 1,
109 	0, 1, 1,
110 	1, 1, 1,
111 	0, 0, 1,
112 	0, 1, 1,
113 	1, 0, 1,
114 	1, 1, 1,
115 
116 	1, 0, 0, // xpos
117 	1, 0, 1,
118 	1, 1, 0,
119 	1, 1, 1,
120 	1, 0, 0,
121 	1, 1, 0,
122 	1, 0, 1,
123 	1, 1, 1,
124 
125 	0, 0, 0, // xneg
126 	0, 0, 1,
127 	0, 1, 0,
128 	0, 1, 1,
129 	0, 0, 0,
130 	0, 1, 0,
131 	0, 0, 1,
132 	0, 1, 1,
133 
134 	1, 1, 1, // ypos
135 	0, 1, 1,
136 	1, 1, 0,
137 	0, 1, 0,
138 	1, 1, 1,
139 	1, 1, 0,
140 	0, 1, 1,
141 	0, 1, 0,
142 
143 	1, 0, 1, // yneg
144 	0, 0, 1,
145 	1, 0, 0,
146 	0, 0, 0,
147 	1, 0, 1,
148 	1, 0, 0,
149 	0, 0, 1,
150 	0, 0, 0,
151 ];