1
1
#include <stdlib.h>
2
+ #include <string.h>
2
3
#include "raylib.h"
3
4
#include "rlgl.h"
4
5
#include "block.h"
5
6
#include "math.h"
6
7
#include "chunkmesh.h"
7
8
#include "blockfacehelper.h"
8
9
9
- int BFH_verticesI = 0 ;
10
- int BFH_texI = 0 ;
11
- int BFH_colorsI = 0 ;
10
+ int BFH_verticesI [] = { 0 , 0 } ;
11
+ int BFH_texI [] = { 0 , 0 } ;
12
+ int BFH_colorsI [] = { 0 , 0 } ;
12
13
13
14
float BFH_texCoords [12 ] = {
14
15
1 , 1 , 0 , 0 , 1 , 0 ,
15
16
0 , 0 , 1 , 1 , 0 , 1
16
17
17
18
};
18
19
19
- Vector3 BFH_facesPosition [36 ] = {
20
- //left
21
- (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {0 , 1 , 1 },
22
- (Vector3 ) {0 , 1 , 0 }, (Vector3 ) {0 , 1 , 1 },
23
- (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {0 , 0 , 1 },
24
- //right
25
- (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {1 , 1 , 0 },
26
- (Vector3 ) {1 , 1 , 1 }, (Vector3 ) {1 , 1 , 0 },
27
- (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {1 , 0 , 0 },
28
- //top
29
- (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 0 },
30
- (Vector3 ) {0 , 1 , 0 }, (Vector3 ) {1 , 1 , 0 },
31
- (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 1 },
32
- //bottom
33
- (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 0 , 1 },
34
- (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 0 , 1 },
35
- (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 0 , 0 },
36
- //front
37
- (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 1 , 1 },
38
- (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 1 },
39
- (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 0 , 1 },
40
- //back
41
- (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 1 , 0 },
42
- (Vector3 ) {1 , 1 , 0 }, (Vector3 ) {0 , 1 , 0 },
43
- (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 0 , 0 }
44
- };
20
+ void BFH_GetFacesPosition (Block b , Vector3 * facesPosition ) {
21
+
22
+ if (b .modelType == BlockModelType_Sprite ) {
23
+
24
+ Vector3 nFacesPosition [24 ] = {
25
+ //left
26
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 1 , 1 },
27
+ (Vector3 ) {0 , 1 , 0 }, (Vector3 ) {1 , 1 , 1 },
28
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 0 , 1 },
29
+ //right
30
+ (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {0 , 1 , 0 },
31
+ (Vector3 ) {1 , 1 , 1 }, (Vector3 ) {0 , 1 , 0 },
32
+ (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {0 , 0 , 0 },
33
+ //front
34
+ (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 1 , 0 },
35
+ (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 0 },
36
+ (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 0 , 0 },
37
+ //back
38
+ (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 1 , 1 },
39
+ (Vector3 ) {1 , 1 , 0 }, (Vector3 ) {0 , 1 , 1 },
40
+ (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 0 , 1 }
41
+ };
42
+
43
+ memcpy (facesPosition , nFacesPosition , 24 * sizeof (Vector3 ));
44
+
45
+ } else {
46
+
47
+ Vector3 nFacesPosition [36 ] = {
48
+ //left
49
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {0 , 1 , 1 },
50
+ (Vector3 ) {0 , 1 , 0 }, (Vector3 ) {0 , 1 , 1 },
51
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {0 , 0 , 1 },
52
+ //right
53
+ (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {1 , 1 , 0 },
54
+ (Vector3 ) {1 , 1 , 1 }, (Vector3 ) {1 , 1 , 0 },
55
+ (Vector3 ) {1 , 0 , 1 }, (Vector3 ) {1 , 0 , 0 },
56
+ //top
57
+ (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 0 },
58
+ (Vector3 ) {0 , 1 , 0 }, (Vector3 ) {1 , 1 , 0 },
59
+ (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 1 },
60
+ //bottom
61
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 0 , 1 },
62
+ (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 0 , 1 },
63
+ (Vector3 ) {0 , 0 , 0 }, (Vector3 ) {1 , 0 , 0 },
64
+ //front
65
+ (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 1 , 1 },
66
+ (Vector3 ) {0 , 1 , 1 }, (Vector3 ) {1 , 1 , 1 },
67
+ (Vector3 ) {0 , 0 , 1 }, (Vector3 ) {1 , 0 , 1 },
68
+ //back
69
+ (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 1 , 0 },
70
+ (Vector3 ) {1 , 1 , 0 }, (Vector3 ) {0 , 1 , 0 },
71
+ (Vector3 ) {1 , 0 , 0 }, (Vector3 ) {0 , 0 , 0 }
72
+ };
73
+
74
+ memcpy (facesPosition , nFacesPosition , 36 * sizeof (Vector3 ));
75
+
76
+ }
77
+
78
+ }
45
79
46
80
Vector3 BFH_GetDirection (BlockFace face ) {
47
81
switch (face ) {
@@ -62,14 +96,20 @@ Vector3 BFH_GetDirection(BlockFace face) {
62
96
}
63
97
64
98
void BFH_ResetIndexes (void ) {
65
- BFH_verticesI = 0 ;
66
- BFH_texI = 0 ;
67
- BFH_colorsI = 0 ;
99
+ BFH_verticesI [0 ] = 0 ;
100
+ BFH_texI [0 ] = 0 ;
101
+ BFH_colorsI [0 ] = 0 ;
102
+
103
+ BFH_verticesI [1 ] = 0 ;
104
+ BFH_texI [1 ] = 0 ;
105
+ BFH_colorsI [1 ] = 0 ;
68
106
}
69
107
70
- void BFH_AddFace (ChunkMesh * mesh , BlockFace face , Vector3 pos , int blockID ) {
108
+ void BFH_AddFace (ChunkMesh * mesh , BlockFace face , Vector3 pos , Block blockDef , int translucent , int light ) {
71
109
72
- int texID = Block_definition [blockID ].textures [(int )face ];
110
+ Vector3 facesPosition [36 ] = {0 };
111
+ BFH_GetFacesPosition (blockDef , facesPosition );
112
+ int texID = blockDef .textures [(int )face ];
73
113
74
114
int texI = 0 ;
75
115
int textureX = texID % 16 ;
@@ -79,29 +119,34 @@ void BFH_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, int blockID) {
79
119
80
120
for (int i = 0 ; i < 6 ; i ++ ) {
81
121
int faceIndex = i + faceX6 ;
82
- mesh -> vertices [BFH_verticesI ++ ] = (unsigned char )((BFH_facesPosition [faceIndex ].x + pos .x ) * 15.9f );
83
- mesh -> vertices [BFH_verticesI ++ ] = (unsigned char )((BFH_facesPosition [faceIndex ].y + pos .y ) * 15.9f );
84
- mesh -> vertices [BFH_verticesI ++ ] = (unsigned char )((BFH_facesPosition [faceIndex ].z + pos .z ) * 15.9f );
85
122
86
- switch (face ) {
87
- case BlockFace_Bottom :
88
- mesh -> colors [BFH_colorsI ++ ] = 100 ;
89
- break ;
90
- case BlockFace_Left :
91
- case BlockFace_Right :
92
- mesh -> colors [BFH_colorsI ++ ] = 150 ;
93
- break ;
94
- case BlockFace_Front :
95
- case BlockFace_Back :
96
- mesh -> colors [BFH_colorsI ++ ] = 200 ;
97
- break ;
98
- default :
99
- mesh -> colors [BFH_colorsI ++ ] = 255 ;
100
- break ;
123
+ mesh -> vertices [BFH_verticesI [translucent ]++ ] = (unsigned char )((facesPosition [faceIndex ].x + pos .x ) * 15.99f );
124
+ mesh -> vertices [BFH_verticesI [translucent ]++ ] = (unsigned char )((facesPosition [faceIndex ].y + pos .y ) * 15.99f );
125
+ mesh -> vertices [BFH_verticesI [translucent ]++ ] = (unsigned char )((facesPosition [faceIndex ].z + pos .z ) * 15.99f );
126
+
127
+ if (blockDef .modelType != BlockModelType_Sprite ) {
128
+ switch (face ) {
129
+ case BlockFace_Bottom :
130
+ mesh -> colors [BFH_colorsI [translucent ]++ ] = fmin (100 , fmax (16 , 100 - light ));
131
+ break ;
132
+ case BlockFace_Left :
133
+ case BlockFace_Right :
134
+ mesh -> colors [BFH_colorsI [translucent ]++ ] = fmin (150 , fmax (16 , 150 - light ));
135
+ break ;
136
+ case BlockFace_Front :
137
+ case BlockFace_Back :
138
+ mesh -> colors [BFH_colorsI [translucent ]++ ] = fmin (200 , fmax (16 , 200 - light ));
139
+ break ;
140
+ default :
141
+ mesh -> colors [BFH_colorsI [translucent ]++ ] = fmin (255 , fmax (16 , 255 - light ));
142
+ break ;
143
+ }
144
+ } else {
145
+ mesh -> colors [BFH_colorsI [translucent ]++ ] = fmax (16 , 255 - light );
101
146
}
102
147
103
- mesh -> texcoords [BFH_texI ++ ] = (unsigned char )((BFH_texCoords [texI ++ ] + textureX ));
104
- mesh -> texcoords [BFH_texI ++ ] = (unsigned char )((BFH_texCoords [texI ++ ] + textureY ));
148
+ mesh -> texcoords [BFH_texI [ translucent ] ++ ] = (unsigned char )((BFH_texCoords [texI ++ ] + textureX ));
149
+ mesh -> texcoords [BFH_texI [ translucent ] ++ ] = (unsigned char )((BFH_texCoords [texI ++ ] + textureY ));
105
150
}
106
151
107
152
}
0 commit comments