Skip to content

Commit f2234b1

Browse files
committed
optimizations & readme
1 parent 696747b commit f2234b1

File tree

5 files changed

+98
-51
lines changed

5 files changed

+98
-51
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# MCRewrite
2+
An open source Minecraft Java Edition rewrite in C++ using raylib.
3+
This branch version is rd-132211.
4+
5+
## TODO for rd-132211
6+
- [ ] Lighting (probably using a depth buffer)
7+
- [ ] Optimizations for camera ray hitting
8+
- [ ] Maybe change the camera FOV as we are using just a random value rn :skull:
9+
10+
## What do we want it to be?
11+
This is meant to be a 1:1 user experience rewrite/decompilation (the game look and gameplay must match) but the code may and does not match with the original as we are using modern C++ features and trying to improve the original code structure.
12+
13+
## How did we do that?
14+
We just decompiled a Java Minecraft binary and tried to rewrite it in C++ but with our own modifications.
15+
16+
## Is it better?
17+
Yeah, rd-132211 runs at ~2500-3000 fps on my Windows 10 instead of 300 and you can ideally see no difference between this and the Java version.
18+
19+
## Running
20+
Keep in mind that we are not publishing the game's assets so you have to find them yourself. You should place them into the `assets` directory which should be near the executable.
21+
22+
You can download a binary from the releases tab or build it yourself.
23+
24+
### Building
25+
```
26+
git clone https://github.com/JaanDev/MCRewrite.git --recursive
27+
cd MCRewrite
28+
cmake -S . -B build -D CMAKE_BUILD_TYPE=Release
29+
cmake --build build
30+
```
31+
Or just open the folder in VS Code/Visual Studio (don't forget the CMake extension for VS Code) and build it from there.
32+
33+
### Contributions
34+
The contributions are always welcomed! :<zero-width space>)
35+
36+
### Our contacts
37+
Discord:
38+
* Jaan#2897
39+
* Kolyah35#0107

src/Chunk.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ChunkPos Chunk::getPos() {
1818

1919
uint32_t Chunk::getFaceCount() {
2020
TIME_MEASURE_BEGIN(GetFaceCount)
21+
2122
uint32_t faces = 0;
2223

2324
auto chunkXP = m_level->getChunk(ChunkPos {m_pos.x + 1, m_pos.z}); // x+
@@ -33,11 +34,11 @@ uint32_t Chunk::getFaceCount() {
3334

3435
if (isSolidTile(localPos)) {
3536
// y+
36-
if (!isSolidTile({x, y + 1, z}))
37+
if (y == chunkHeight - 1 || !isSolidTile({x, y + 1, z}))
3738
faces++;
3839

3940
// y-
40-
if (!isSolidTile({x, y - 1, z}))
41+
if (y == 0 || !isSolidTile({x, y - 1, z}))
4142
faces++;
4243

4344
// z-
@@ -87,6 +88,7 @@ uint32_t Chunk::getFaceCount() {
8788
}
8889
}
8990
}
91+
9092
TIME_MEASURE_END(GetFaceCount)
9193
TIME_MEASURE_DBG(GetFaceCount)
9294

@@ -180,6 +182,11 @@ void Chunk::generateMesh() {
180182
col = {static_cast<uint8_t>(br * 255.f), static_cast<uint8_t>(br * 255.f), static_cast<uint8_t>(br * 255.f), 255}; \
181183
}
182184

185+
auto chunkXP = m_level->getChunk(ChunkPos {m_pos.x + 1, m_pos.z}); // x+
186+
auto chunkXM = m_level->getChunk(ChunkPos {m_pos.x - 1, m_pos.z}); // x-
187+
auto chunkZP = m_level->getChunk(ChunkPos {m_pos.x, m_pos.z + 1}); // z+
188+
auto chunkZM = m_level->getChunk(ChunkPos {m_pos.x, m_pos.z - 1}); // z-
189+
183190
TIME_MEASURE_BEGIN(GenMeshLoop)
184191

185192
for (uint8_t x = 0; x < chunkSize; x++) {
@@ -195,7 +202,7 @@ void Chunk::generateMesh() {
195202
auto uv = AssetManager::sharedState()->uvForBlockType(getBlock(pos));
196203
// auto uv = UV {0, 0, 1, 1};
197204

198-
if (!isSolidTile({x, y + 1, z})) { // y+
205+
if (y == chunkHeight - 1 || !isSolidTile({x, y + 1, z})) { // y+
199206
CALC_COL(0, 1, 0)
200207
normal = {0, 1, 0};
201208
ADD_VERTEX(0, 1, 0, uv.startX, uv.startY)
@@ -207,7 +214,7 @@ void Chunk::generateMesh() {
207214
ADD_VERTEX(1, 1, 0, uv.endX, uv.startY)
208215
}
209216

210-
if (!isSolidTile({x, y - 1, z})) { // y-
217+
if (y == 0 || !isSolidTile({x, y - 1, z})) { // y-
211218
CALC_COL(0, -1, 0)
212219
normal = {0, -1, 0};
213220
ADD_VERTEX(1, 0, 0, uv.endX, uv.startY)
@@ -219,7 +226,8 @@ void Chunk::generateMesh() {
219226
ADD_VERTEX(0, 0, 1, uv.startX, uv.endY)
220227
}
221228

222-
if (!m_level->isSolidTile(globalPos + BlockPos {0, 0, 1})) { // z+
229+
if ((z == chunkSize - 1 && (!chunkZP || !chunkZP->isSolidTile({x, y, 0}))) ||
230+
(z != chunkSize - 1 && !isSolidTile({x, y, z + 1}))) { // z+
223231
CALC_COL(0, 0, 1)
224232
normal = {0, 0, 1};
225233
ADD_VERTEX(0, 0, 1, uv.startX, uv.endY)
@@ -231,7 +239,8 @@ void Chunk::generateMesh() {
231239
ADD_VERTEX(0, 1, 1, uv.startX, uv.startY)
232240
}
233241

234-
if (!m_level->isSolidTile(globalPos - BlockPos {0, 0, 1})) { // z-
242+
if ((z == 0 && (!chunkZM || !chunkZM->isSolidTile({x, y, chunkSize - 1}))) ||
243+
(z != 0 && !isSolidTile({x, y, z - 1}))) { // z-
235244
CALC_COL(0, 0, -1)
236245
normal = {0, 0, -1};
237246
ADD_VERTEX(1, 1, 0, uv.startX, uv.startY)
@@ -243,7 +252,8 @@ void Chunk::generateMesh() {
243252
ADD_VERTEX(0, 0, 0, uv.endX, uv.endY)
244253
}
245254

246-
if (!m_level->isSolidTile(globalPos + BlockPos {1, 0, 0})) { // x+
255+
if ((x == chunkSize - 1 && (!chunkXP || !chunkXP->isSolidTile({0, y, z}))) ||
256+
(x != chunkSize - 1 && !isSolidTile({x + 1, y, z}))) { // x+
247257
CALC_COL(1, 0, 0)
248258
normal = {1, 0, 0};
249259
ADD_VERTEX(1, 0, 0, uv.endX, uv.endY)
@@ -255,7 +265,8 @@ void Chunk::generateMesh() {
255265
ADD_VERTEX(1, 0, 1, uv.startX, uv.endY)
256266
}
257267

258-
if (!m_level->isSolidTile(globalPos - BlockPos {1, 0, 0})) { // x-
268+
if ((x == 0 && (!chunkXM || !chunkXM->isSolidTile({chunkSize - 1, y, z}))) ||
269+
(x != 0 && !isSolidTile({x - 1, y, z}))) { // x-
259270
CALC_COL(-1, 0, 0)
260271
normal = {-1, 0, 0};
261272
ADD_VERTEX(0, 0, 0, uv.startX, uv.endY)
@@ -336,9 +347,25 @@ Model* Chunk::getModel() {
336347
return &m_model;
337348
}
338349

339-
void Chunk::cameraLook(Ray ray, HitResult& coll) {
350+
void Chunk::cameraLook(Ray ray, HitResult& coll, const BlockPos& playerPos) {
340351
coll.coll = GetRayCollisionMesh(ray, m_model.meshes[0], MatrixTranslate(m_pos.x * chunkSize, 0.f, m_pos.z * chunkSize));
341352
if (coll.coll.hit) {
353+
auto pos = coll.coll.point;
354+
coll.blockPos = {static_cast<int>(pos.x), static_cast<int>(pos.y), static_cast<int>(pos.z)};
355+
356+
if (coll.face == Faces::Up)
357+
coll.blockPos.y--;
358+
if (coll.face == Faces::Back)
359+
coll.blockPos.z--;
360+
if (coll.face == Faces::Left)
361+
coll.blockPos.x--;
362+
363+
if (std::abs(playerPos.x - coll.blockPos.x) > 4 || std::abs(playerPos.y - coll.blockPos.y) > 4 ||
364+
std::abs(playerPos.z - coll.blockPos.z) > 4) {
365+
coll.coll.hit = false;
366+
return;
367+
}
368+
342369
auto normal = coll.coll.normal;
343370
if (normal == Vector3 {0, 1, 0})
344371
coll.face = Faces::Up;
@@ -352,15 +379,5 @@ void Chunk::cameraLook(Ray ray, HitResult& coll) {
352379
coll.face = Faces::Back;
353380
else if (normal == Vector3 {0, 0, -1})
354381
coll.face = Faces::Front;
355-
356-
auto pos = coll.coll.point;
357-
coll.blockPos = {static_cast<int>(pos.x), static_cast<int>(pos.y), static_cast<int>(pos.z)};
358-
359-
if (coll.face == Faces::Up)
360-
coll.blockPos.y--;
361-
if (coll.face == Faces::Back)
362-
coll.blockPos.z--;
363-
if (coll.face == Faces::Left)
364-
coll.blockPos.x--;
365382
}
366383
}

src/Chunk.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Chunk {
2121
void calcLightDepths();
2222
Mesh* getMesh();
2323
Model* getModel();
24-
void cameraLook(Ray ray, HitResult& coll);
24+
void cameraLook(Ray ray, HitResult& coll, const BlockPos& playerPos);
2525

2626
private:
2727
void generate();

src/main.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int main() {
1818
// SetConfigFlags(FLAG_MSAA_4X_HINT);
1919
InitWindow(winW, winH, "Minecraft rd-132211");
2020
// SetTargetFPS(60);
21-
SetTargetFPS(2600); // so it isnt too much bc then my pc starts making a high frequency noise which isnt good i suppose
21+
// SetTargetFPS(2600); // so it isnt too much bc then my pc starts making a high frequency noise which isnt good i suppose
2222

2323
BeginDrawing();
2424
ClearBackground(RAYWHITE);
@@ -30,8 +30,10 @@ int main() {
3030
glShadeModel(GL_SMOOTH);
3131
glClearColor(.5f, .8f, 1.f, 0.f);
3232
glClearDepth(1.0);
33-
glEnable(GL_DEPTH_TEST);
3433
glDepthFunc(GL_LEQUAL);
34+
glEnable(GL_NORMALIZE);
35+
glEnable(GL_POINT_SMOOTH);
36+
glEnable(GL_DEPTH_TEST);
3537
// glMatrixMode(5889);
3638
// glLoadIdentity();
3739
// glMatrixMode(5888);
@@ -45,13 +47,8 @@ int main() {
4547
auto player = std::make_shared<Player>(lvl);
4648
auto timer = std::make_shared<Timer>(60.f);
4749

48-
// RayCollision collision;
4950
HitResult coll;
5051

51-
auto plane = GenMeshPlane(1.f, 1.f, 1, 1);
52-
UploadMesh(&plane, false);
53-
auto planeModel = LoadModelFromMesh(plane);
54-
5552
auto lastTime = system_clock::now();
5653
size_t frames = 0;
5754

@@ -75,19 +72,13 @@ int main() {
7572
cam.setTarget(cam.GetCamera().position + target);
7673

7774
// block selection
78-
79-
// auto chunk = lvl->getChunk({(int)playerPos.x, (int)playerPos.y, (int)playerPos.z});
80-
// if (chunk) {
81-
// chunk->cameraLook(cam.GetViewRay(), coll);
82-
// } else {
83-
// coll.coll.hit = false;
84-
// }
85-
8675
auto camRay = cam.GetViewRay();
8776

8877
coll.coll.hit = false;
8978
auto plBlockPos = BlockPos {(int)playerPos.x, (int)playerPos.y, (int)playerPos.z};
9079
auto chunkPos = plBlockPos.chunkPos();
80+
81+
// TODO: rework this ._.
9182
vector<std::shared_ptr<Chunk>> chunksAroundPlayer;
9283
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {0, 0}));
9384
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {1, 0}));
@@ -99,11 +90,11 @@ int main() {
9990
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {1, -1}));
10091
chunksAroundPlayer.push_back(lvl->getChunk(chunkPos + ChunkPos {-1, 1}));
10192
for (const auto& chunk : chunksAroundPlayer) {
102-
if (chunk){
103-
chunk->cameraLook(camRay, coll);
104-
if (coll.coll.hit) break;
93+
if (chunk) {
94+
chunk->cameraLook(camRay, coll, plBlockPos);
95+
if (coll.coll.hit)
96+
break;
10597
}
106-
10798
}
10899

109100
if (coll.coll.hit) {
@@ -119,23 +110,20 @@ int main() {
119110

120111
BeginDrawing();
121112

122-
ClearBackground({127, 204, 255});
123-
113+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124114
cam.BeginMode3D();
125-
115+
glEnable(GL_CULL_FACE);
126116
glEnable(GL_FOG);
127117
glFogi(GL_FOG_MODE, 2048);
128118
glFogf(GL_FOG_DENSITY, .2f);
129119
glFogfv(GL_FOG_COLOR, fogColor);
130120

131121
lvl->render();
132122

133-
glDisable(GL_FOG);
134-
135-
DrawGrid(10, 1);
136-
DrawRay({{0, 0, 0}, {1, 0, 0}}, RED); // x
137-
DrawRay({{0, 0, 0}, {0, 1, 0}}, GREEN); // y
138-
DrawRay({{0, 0, 0}, {0, 0, 1}}, BLUE); // z
123+
// DrawGrid(10, 1);
124+
// DrawRay({{0, 0, 0}, {1, 0, 0}}, RED); // x
125+
// DrawRay({{0, 0, 0}, {0, 1, 0}}, GREEN); // y
126+
// DrawRay({{0, 0, 0}, {0, 0, 1}}, BLUE); // z
139127

140128
if (coll.coll.hit) {
141129
auto millis =
@@ -144,9 +132,12 @@ int main() {
144132
drawFace(coll.blockPos, coll.face, col);
145133
}
146134

135+
glDisable(GL_FOG);
136+
147137
cam.EndMode3D();
148138

149139
DrawFPS(0, 0);
140+
150141
EndDrawing();
151142

152143
frames++;
@@ -157,7 +148,6 @@ int main() {
157148
}
158149
}
159150

160-
UnloadModel(planeModel);
161151
CloseWindow();
162152
return 0;
163153
}

src/utils.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
const int chunkSize = 16; // x and z
66
const int chunkHeight = 256; // y
7-
const int chunksCount = 16;
7+
const int chunksCount = 4;
8+
// const int chunksCount = 16;
89

910
const int surfaceLevel = 170; // surface level
1011

@@ -187,7 +188,7 @@ inline void drawFace(const BlockPos& blockPos, Faces face, const Color& col) {
187188
}
188189
}
189190

190-
// #define DO_TIME_MEASURING
191+
#define DO_TIME_MEASURING
191192

192193
#ifdef DO_TIME_MEASURING
193194
#define TIME_MEASURE_BEGIN(name) auto name##_begin = std::chrono::system_clock::now();

0 commit comments

Comments
 (0)