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