Skip to content

Commit f41b72b

Browse files
committed
More Optimization
1 parent 0b3c455 commit f41b72b

10 files changed

+112
-96
lines changed

client/src/block/blockMeshGeneration.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void BlockMesh_ResetIndexes(void) {
5353
BFH_indicesI[1] = 0;
5454
}
5555

56-
void BlockMesh_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block b, int translucent, int light) {
56+
void BlockMesh_AddFace(unsigned char *vertices, unsigned short *indices, unsigned short *texcoords, unsigned char *colors, BlockFace face, Vector3 pos, Block b, int translucent, int light) {
5757

5858
Vector3 *facesPosition;
5959

@@ -109,39 +109,39 @@ void BlockMesh_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block b, in
109109

110110
int verticeIndexD3 = BFH_verticesI[translucent] / 3;
111111

112-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3;
113-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 1;
114-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 2;
115-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 1;
116-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3;
117-
mesh->indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 3;
112+
indices[BFH_indicesI[translucent]++] = verticeIndexD3;
113+
indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 1;
114+
indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 2;
115+
indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 1;
116+
indices[BFH_indicesI[translucent]++] = verticeIndexD3;
117+
indices[BFH_indicesI[translucent]++] = verticeIndexD3 + 3;
118118

119119
for(int i = 0; i < 4; i++) {
120120
int faceIndex = i + faceX4;
121121

122-
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].x / 16 + pos.x) * 15);
123-
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].y / 16 + pos.y) * 15);
124-
mesh->vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].z / 16 + pos.z) * 15);
122+
vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].x / 16 + pos.x) * 15);
123+
vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].y / 16 + pos.y) * 15);
124+
vertices[BFH_verticesI[translucent]++] = (unsigned char)((facesPosition[faceIndex].z / 16 + pos.z) * 15);
125125

126126
if(b.modelType != BlockModelType_Sprite) {
127127
switch(face) {
128128
case BlockFace_Bottom:
129-
mesh->colors[BFH_colorsI[translucent]++] = fmin(100, fmax(16, 100 - light));
129+
colors[BFH_colorsI[translucent]++] = fmin(100, fmax(16, 100 - light));
130130
break;
131131
case BlockFace_Left:
132132
case BlockFace_Right:
133-
mesh->colors[BFH_colorsI[translucent]++] = fmin(150, fmax(16, 150 - light));
133+
colors[BFH_colorsI[translucent]++] = fmin(150, fmax(16, 150 - light));
134134
break;
135135
case BlockFace_Front:
136136
case BlockFace_Back:
137-
mesh->colors[BFH_colorsI[translucent]++] = fmin(200, fmax(16, 200 - light));
137+
colors[BFH_colorsI[translucent]++] = fmin(200, fmax(16, 200 - light));
138138
break;
139139
default:
140-
mesh->colors[BFH_colorsI[translucent]++] = fmin(255, fmax(16, 255 - light));
140+
colors[BFH_colorsI[translucent]++] = fmin(255, fmax(16, 255 - light));
141141
break;
142142
}
143143
} else {
144-
mesh->colors[BFH_colorsI[translucent]++] = fmax(16, 255 - light);
144+
colors[BFH_colorsI[translucent]++] = fmax(16, 255 - light);
145145
}
146146

147147
}
@@ -170,16 +170,16 @@ void BlockMesh_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block b, in
170170
}
171171
}
172172

173-
mesh->texcoords[BFH_texI[translucent]++] = iMinX;
174-
mesh->texcoords[BFH_texI[translucent]++] = iMaxY;
173+
texcoords[BFH_texI[translucent]++] = iMinX;
174+
texcoords[BFH_texI[translucent]++] = iMaxY;
175175

176-
mesh->texcoords[BFH_texI[translucent]++] = iMaxX;
177-
mesh->texcoords[BFH_texI[translucent]++] = iMinY;
176+
texcoords[BFH_texI[translucent]++] = iMaxX;
177+
texcoords[BFH_texI[translucent]++] = iMinY;
178178

179-
mesh->texcoords[BFH_texI[translucent]++] = iMinX;
180-
mesh->texcoords[BFH_texI[translucent]++] = iMinY;
179+
texcoords[BFH_texI[translucent]++] = iMinX;
180+
texcoords[BFH_texI[translucent]++] = iMinY;
181181

182-
mesh->texcoords[BFH_texI[translucent]++] = iMaxX;
183-
mesh->texcoords[BFH_texI[translucent]++] = iMaxY;
182+
texcoords[BFH_texI[translucent]++] = iMaxX;
183+
texcoords[BFH_texI[translucent]++] = iMaxY;
184184
}
185185

client/src/block/blockMeshGeneration.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
void BlockMesh_ResetIndexes(void);
1818

1919
//Add a block face to a given mesh.
20-
void BlockMesh_AddFace(ChunkMesh *mesh, BlockFace face, Vector3 pos, Block blockDef, int translucent, int light);
20+
void BlockMesh_AddFace(unsigned char *vertices, unsigned short *indices, unsigned short *texcoords, unsigned char *colors, BlockFace face, Vector3 pos, Block b, int translucent, int light);
2121

2222
//Get facing direction of a block face.
2323
Vector3 BlockMesh_GetDirection(BlockFace face);

client/src/chunk/chunk.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "chunkMeshGeneration.h"
1515
#include "world.h"
1616
#include "chunkMesh.h"
17-
#include "stb_perlin.h"
1817
#include "worldgenerator.h"
1918
#include "networkhandler.h"
2019
#include "../block/block.h"
@@ -82,19 +81,18 @@ void Chunk_Unload(Chunk *chunk) {
8281

8382
void Chunk_Generate(Chunk *chunk) {
8483
if(!chunk->isLightGenerated) {
85-
8684
if(!chunk->fromFile) {
8785
//Map Generation
8886
for(int i = CHUNK_SIZE - 1; i >= 0; i--) {
8987
Vector3 npos = Vector3Add(Chunk_IndexToPos(i), chunk->blockPosition);
9088
chunk->data[i] = WorldGenerator_Generate(chunk, npos, i);
9189
}
9290
}
91+
9392
chunk->isMapGenerated = true;
9493
Chunk_DoSunlight(chunk);
9594
Chunk_DoLightSources(chunk);
9695
chunk->isLightGenerated = true;
97-
9896
}
9997
}
10098

@@ -283,3 +281,13 @@ QueuedChunk *Chunk_PopFromQueue(QueuedChunk *queue) {
283281
return node;
284282
}
285283

284+
QueuedChunk *Chunk_RemoveFromQueue(QueuedChunk *head, QueuedChunk* previous, QueuedChunk* chunk) {
285+
if(head == NULL) return NULL;
286+
287+
QueuedChunk *next = chunk->next;
288+
MemFree(chunk);
289+
if(chunk == head) return next;
290+
previous->next = next;
291+
292+
return head;
293+
}

client/src/chunk/chunk.h

+1
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,6 @@ Vector3 Chunk_IndexToPos(int index);
9191
QueuedChunk *Chunk_AddToQueue(QueuedChunk *queue, Chunk* chunk);
9292
//Remove a chunk from a queue.
9393
QueuedChunk *Chunk_PopFromQueue(QueuedChunk *queue);
94+
QueuedChunk *Chunk_RemoveFromQueue(QueuedChunk *head, QueuedChunk* previous, QueuedChunk* chunk);
9495

9596
#endif

client/src/chunk/chunkMesh.c

+5-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "chunkmesh.h"
1313
#include "world.h"
1414

15-
void ChunkMesh_Upload(ChunkMesh *mesh) {
15+
void ChunkMesh_Upload(ChunkMesh *mesh, unsigned char *vertices, unsigned short *indices, unsigned short *texcoords, unsigned char *colors) {
1616

1717
mesh->drawVertexCount = mesh->vertexCount;
1818
mesh->drawTriangleCount = mesh->triangleCount;
@@ -31,19 +31,19 @@ void ChunkMesh_Upload(ChunkMesh *mesh) {
3131
int vertXchar = mesh->vertexCount * sizeof(unsigned char);
3232
int vertXShort = mesh->vertexCount * sizeof(unsigned short);
3333

34-
mesh->vboId[0] = rlLoadVertexBuffer(mesh->vertices, vertXchar * 3, false);
34+
mesh->vboId[0] = rlLoadVertexBuffer(vertices, vertXchar * 3, false);
3535
rlSetVertexAttribute(0, 3, RL_UNSIGNED_BYTE, 0, 0, 0);
3636
rlEnableVertexAttribute(0);
3737

38-
mesh->vboId[1] = rlLoadVertexBuffer(mesh->texcoords, vertXShort * 2, false);
38+
mesh->vboId[1] = rlLoadVertexBuffer(texcoords, vertXShort * 2, false);
3939
rlSetVertexAttribute(1, 2, 0x1403, 0, 0, 0);
4040
rlEnableVertexAttribute(1);
4141

42-
mesh->vboId[2] = rlLoadVertexBuffer(mesh->colors, vertXchar, false);
42+
mesh->vboId[2] = rlLoadVertexBuffer(colors, vertXchar, false);
4343
rlSetVertexAttribute(3, 1, RL_UNSIGNED_BYTE, 0, 0, 0);
4444
rlEnableVertexAttribute(3);
4545

46-
mesh->vboId[3] = rlLoadVertexBufferElement(mesh->indices, mesh->drawTriangleCount*3*sizeof(unsigned short), false);
46+
mesh->vboId[3] = rlLoadVertexBufferElement(indices, mesh->drawTriangleCount*3*sizeof(unsigned short), false);
4747

4848
rlDisableVertexArray();
4949
}
@@ -55,15 +55,6 @@ void ChunkMesh_Unload(ChunkMesh *mesh) {
5555

5656
RL_FREE(mesh->vboId);
5757

58-
RL_FREE(mesh->vertices);
59-
RL_FREE(mesh->texcoords);
60-
RL_FREE(mesh->colors);
61-
RL_FREE(mesh->indices);
62-
63-
mesh->vertices = NULL;
64-
mesh->texcoords = NULL;
65-
mesh->colors = NULL;
66-
mesh->indices = NULL;
6758
}
6859

6960
void ChunkMesh_PrepareDrawing(Material mat) {

client/src/chunk/chunkMesh.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@ typedef struct ChunkMesh {
1616
int drawTriangleCount;
1717
int triangleCount;
1818

19-
unsigned char *vertices;
20-
unsigned short *indices;
21-
unsigned short *texcoords;
22-
unsigned char *colors;
23-
2419
unsigned int vaoId;
2520
unsigned int *vboId;
2621
} ChunkMesh;
2722

28-
void ChunkMesh_Upload(ChunkMesh *mesh);
23+
void ChunkMesh_Upload(ChunkMesh *mesh, unsigned char *vertices, unsigned short *indices, unsigned short *texcoords, unsigned char *colors);
2924
void ChunkMesh_Unload(ChunkMesh *mesh);
3025
void ChunkMesh_PrepareDrawing(Material mat);
3126
void ChunkMesh_FinishDrawing(void);

client/src/chunk/chunkMeshGeneration.c

+33-28
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,30 @@
1515
int Chunk_triangleCounter = 0;
1616
int Chunk_triangleCounterTransparent = 0;
1717

18-
void Chunk_AllocateMeshData(ChunkMesh *mesh, int triangleCount) {
19-
20-
mesh->vertexCount = triangleCount * 2;
21-
mesh->triangleCount = triangleCount;
22-
23-
mesh->vertices = MemAlloc(mesh->vertexCount * 3);
24-
mesh->texcoords = MemAlloc(mesh->vertexCount * 2 * sizeof(unsigned short));
25-
mesh->colors = MemAlloc(mesh->vertexCount);
26-
mesh->indices = MemAlloc(mesh->triangleCount * 3 * sizeof(unsigned short));
27-
}
28-
29-
void Chunk_ReAllocateMeshData(ChunkMesh *mesh, int triangleCount) {
30-
mesh->vertexCount = triangleCount * 2;
31-
mesh->triangleCount = triangleCount;
32-
33-
mesh->vertices = MemRealloc(mesh->vertices, mesh->vertexCount * 3);
34-
mesh->texcoords = MemRealloc(mesh->texcoords, mesh->vertexCount * 2 * sizeof(unsigned short));
35-
mesh->colors = MemRealloc(mesh->colors, mesh->vertexCount);
36-
mesh->indices = MemRealloc(mesh->indices ,mesh->triangleCount * 3 * sizeof(unsigned short));
18+
unsigned char *vertices;
19+
unsigned short *indices;
20+
unsigned short *texcoords;
21+
unsigned char *colors;
22+
23+
unsigned char *verticesT;
24+
unsigned short *indicesT;
25+
unsigned short *texcoordsT;
26+
unsigned char *colorsT;
27+
28+
void Chunk_MeshGenerationInit(void) {
29+
30+
int vertexCount = 2 * 6 * CHUNK_SIZE * 2;
31+
int triangleCount = 2 * 6 * CHUNK_SIZE;
32+
33+
vertices = MemAlloc(vertexCount * 3);
34+
texcoords = MemAlloc(vertexCount * 2 * sizeof(unsigned short));
35+
colors = MemAlloc(vertexCount);
36+
indices = MemAlloc(triangleCount * 3 * sizeof(unsigned short));
37+
38+
verticesT = MemAlloc(vertexCount * 3);
39+
texcoordsT = MemAlloc(vertexCount * 2 * sizeof(unsigned short));
40+
colorsT = MemAlloc(vertexCount);
41+
indicesT = MemAlloc(triangleCount * 3 * sizeof(unsigned short));
3742
}
3843

3944

@@ -44,9 +49,6 @@ void Chunk_BuildMesh(Chunk *chunk) {
4449
ChunkMesh_Unload(chunk->meshTransparent);
4550
}
4651

47-
Chunk_AllocateMeshData(chunk->mesh, 2 * 6 * CHUNK_SIZE);
48-
Chunk_AllocateMeshData(chunk->meshTransparent, 2 * 6 * CHUNK_SIZE);
49-
5052
BlockMesh_ResetIndexes();
5153
Chunk_triangleCounter = 0;
5254
Chunk_triangleCounterTransparent = 0;
@@ -66,11 +68,14 @@ void Chunk_BuildMesh(Chunk *chunk) {
6668
}
6769
}
6870

69-
Chunk_ReAllocateMeshData(chunk->mesh, Chunk_triangleCounter);
70-
Chunk_ReAllocateMeshData(chunk->meshTransparent, Chunk_triangleCounterTransparent);
71+
chunk->mesh->vertexCount = Chunk_triangleCounter * 2;
72+
chunk->mesh->triangleCount = Chunk_triangleCounter;
73+
74+
chunk->meshTransparent->vertexCount = Chunk_triangleCounterTransparent * 2;
75+
chunk->meshTransparent->triangleCount = Chunk_triangleCounterTransparent;
7176

72-
ChunkMesh_Upload(chunk->mesh);
73-
ChunkMesh_Upload(chunk->meshTransparent);
77+
ChunkMesh_Upload(chunk->mesh, vertices, indices, texcoords, colors);
78+
ChunkMesh_Upload(chunk->meshTransparent, verticesT, indicesT, texcoordsT, colorsT);
7479

7580
chunk->isBuilt = true;
7681
}
@@ -131,10 +136,10 @@ void Chunk_AddFace(Chunk *chunk, ChunkMesh *mesh, Vector3 pos, BlockFace face, B
131136
if(bTest || sprite) {
132137
if(renderType == BlockRenderType_Translucent) {
133138
Chunk_triangleCounterTransparent += 2;
134-
BlockMesh_AddFace(mesh, face, pos, blockDef, 1, lightLevel);
139+
BlockMesh_AddFace(verticesT, indicesT, texcoordsT, colorsT, face, pos, blockDef, 1, lightLevel);
135140
} else {
136141
Chunk_triangleCounter += 2;
137-
BlockMesh_AddFace(mesh, face, pos, blockDef, 0, lightLevel);
142+
BlockMesh_AddFace(vertices, indices, texcoords, colors, face, pos, blockDef, 0, lightLevel);
138143
}
139144

140145
}

client/src/chunk/chunkMeshGeneration.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
#include "chunk.h"
1313
#include "../block/block.h"
1414

15-
//Allocate memory for a mesh.
16-
void Chunk_AllocateMeshData(ChunkMesh *mesh, int triangleCount);
17-
//Reallocate memory for a mesh.
18-
void Chunk_ReAllocateMeshData(ChunkMesh *mesh, int triangleCount);
19-
15+
void Chunk_MeshGenerationInit(void);
2016
//Build/Refresh a chunk's mesh.
2117
void Chunk_BuildMesh(Chunk *chunk);
2218
void Chunk_AddCube(Chunk *chunk, ChunkMesh *mesh, Vector3 pos, Block blockDef);

client/src/gui/screens.c

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ void Screen_MakeOptions(void) {
154154
if(world.drawDistance > 6) {
155155
world.drawDistance = 2;
156156
World_Reload();
157+
} else {
158+
World_LoadChunks();
157159
}
158160
}
159161

0 commit comments

Comments
 (0)