1 /**
2 Copyright: Copyright (c) 2014-2016 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.core.basicblocks;
7 
8 import voxelman.core.block;
9 import voxelman.core.config;
10 
11 
12 immutable Block unknownBlock = Block(0, "", [0,0,0], false,
13 			&sideIsNotTransparent, &makeNullMesh);
14 immutable Block airBlock = Block(1, "", [0,0,0], false,
15 			&sideIsTransparent, &makeNullMesh);
16 immutable Block grassBlock = makeColoredSolidBlock(2, [0, 255, 0]);
17 immutable Block dirtBlock = makeColoredSolidBlock(3, [120, 72, 0]);
18 immutable Block stoneBlock = makeColoredSolidBlock(4, [128, 128, 128]);
19 immutable Block sandBlock = makeColoredSolidBlock(5, [225, 169, 95]);
20 
21 
22 Block makeSolidBlock(BlockType id)
23 {
24 	return Block(id, "", [255, 255, 255], true,
25 		&sideIsNotTransparent,	&makeColoredBlockMesh);
26 }
27 
28 Block makeColoredSolidBlock(BlockType id, ubyte[3] color)
29 {
30 	Block result = makeSolidBlock(id);
31 	result.color = color;
32 	return result;
33 }
34 
35 bool sideIsNotTransparent(Side side){ return false; }
36 bool sideIsTransparent(Side side){ return true; }
37 
38 ubyte[] makeNullMesh(const Block block,
39 	ubyte bx, ubyte by, ubyte bz, ubyte sides, ubyte sidesnum)
40 {
41 	return null;
42 }
43 
44 ubyte[] makeColoredBlockMesh(const Block block,
45 		ubyte bx, ubyte by, ubyte bz, ubyte sides, ubyte sidesnum)
46 {
47 	import std.random;
48 	static immutable(float)[] shadowMultipliers = [
49 		0.7, 0.75, 0.6, 0.5, 0.85, 0.4,
50 	];
51 	ubyte[] data;
52 	data.reserve(sidesnum * 48);
53 
54 	float randomTint = uniform(0.92f, 1.0f);
55 
56 	foreach(ubyte i; 0..6)
57 	{
58 		if (sides & (2^^i))
59 		{
60 			for (size_t v = 0; v!=18; v+=3)
61 			{
62 				data ~= cast(ubyte)(faces[18*i+v] + bx);
63 				data ~= cast(ubyte)(faces[18*i+v+1] + by);
64 				data ~= cast(ubyte)(faces[18*i+v+2] + bz);
65 				data ~= 0;
66 				data ~= cast(ubyte)(shadowMultipliers[i] * block.color[0] * randomTint);
67 				data ~= cast(ubyte)(shadowMultipliers[i] * block.color[1] * randomTint);
68 				data ~= cast(ubyte)(shadowMultipliers[i] * block.color[2] * randomTint);
69 				data ~= 0;
70 			} // for v
71 		} // if
72 	} // for i
73 
74 	return data;
75 }
76 
77 // mesh for single block
78 immutable ubyte[18 * 6] faces =
79 [
80 	0, 0, 0, // triangle 1 : begin // north
81 	1, 0, 0,
82 	1, 1, 0, // triangle 1 : end
83 	0, 0, 0, // triangle 2 : begin
84 	1, 1, 0,
85 	0, 1, 0, // triangle 2 : end
86 
87 	1, 0, 1, // south
88 	0, 0, 1,
89 	0, 1, 1,
90 	1, 0, 1,
91 	0, 1, 1,
92 	1, 1, 1,
93 
94 	1, 0, 0, // east
95 	1, 0, 1,
96 	1, 1, 1,
97 	1, 0, 0,
98 	1, 1, 1,
99 	1, 1, 0,
100 
101 	0, 0, 1, // west
102 	0, 0, 0,
103 	0, 1, 0,
104 	0, 0, 1,
105 	0, 1, 0,
106 	0, 1, 1,
107 
108 	1, 1, 1, // top
109 	0, 1, 1,
110 	0, 1, 0,
111 	1, 1, 1,
112 	0, 1, 0,
113 	1, 1, 0,
114 
115 	0, 0, 1, // bottom
116 	1, 0, 1,
117 	1, 0, 0,
118 	0, 0, 1,
119 	1, 0, 0,
120 	0, 0, 0,
121 ];