batch 2: fixing chunk rendering & frustum culling; rewrote most of it, but now it works. next step is to fix the "highlight" effect & add the optimizations i wrote on optifromhell.txt

This commit is contained in:
JuiceyDev 2026-03-27 00:36:18 +01:00 committed by Tropical
parent fbb7b304c8
commit 09e8a8f981
6 changed files with 69 additions and 69 deletions

View file

@ -1,4 +1,4 @@
// Linux_UIController.cpp // should we keep Linux_UIController.cpp?*
#include "../../../Minecraft.World/Platform/stdafx.h" #include "../../../Minecraft.World/Platform/stdafx.h"
#include "Linux_UIController.h" #include "Linux_UIController.h"

View file

@ -22,8 +22,10 @@ float Camera::xa2 = 0.0f;
float Camera::za2 = 0.0f; float Camera::za2 = 0.0f;
void Camera::prepare(std::shared_ptr<Player> player, bool mirror) { void Camera::prepare(std::shared_ptr<Player> player, bool mirror) {
glGetFloat(GL_MODELVIEW_MATRIX, modelview); memcpy(modelview->_getDataPointer(),
glGetFloat(GL_PROJECTION_MATRIX, projection); RenderManager.MatrixGet(GL_MODELVIEW_MATRIX), 16 * sizeof(float));
memcpy(projection->_getDataPointer(),
RenderManager.MatrixGet(GL_PROJECTION_MATRIX), 16 * sizeof(float));
/* Original java code for reference /* Original java code for reference
glGetInteger(GL_VIEWPORT, viewport); glGetInteger(GL_VIEWPORT, viewport);

View file

@ -1,9 +1,11 @@
#include "../Platform/stdafx.h" #include "../Platform/stdafx.h"
#include "../../Minecraft.World/IO/Streams/FloatBuffer.h" #include "../../Minecraft.World/IO/Streams/FloatBuffer.h"
#include "Frustum.h" #include "Frustum.h"
#include "Camera.h"
Frustum* Frustum::frustum = new Frustum(); Frustum* Frustum::frustum = new Frustum();
// those are now unused but i still gotta do testing.
Frustum::Frustum() { Frustum::Frustum() {
_proj = MemoryTracker::createFloatBuffer(16); _proj = MemoryTracker::createFloatBuffer(16);
_modl = MemoryTracker::createFloatBuffer(16); _modl = MemoryTracker::createFloatBuffer(16);
@ -43,63 +45,38 @@ void Frustum::normalizePlane(float** frustum, int side) {
} }
void Frustum::calculateFrustum() { void Frustum::calculateFrustum() {
_proj->clear(); // 4jcraft: GL 3.3 core removed GL_MODELVIEW_MATRIX / GL_PROJECTION_MATRIX
_modl->clear(); // queries.
_clip->clear(); // Camera::prepare() already captures both matrices every frame :)
// i spent an ungodly amount of time on this simple fix.
memcpy(proj.data, RenderManager.MatrixGet(GL_PROJECTION_MATRIX),
16 * sizeof(float));
memcpy(modl.data, RenderManager.MatrixGet(GL_MODELVIEW_MATRIX),
16 * sizeof(float));
// glGetFloatv() is used to extract information about our OpenGL world. float* p = proj.data;
// Below, we pass in GL_PROJECTION_MATRIX to abstract our projection matrix. float* m = modl.data;
// It then stores the matrix into an array of [16]. float* c = clip.data;
glGetFloat(GL_PROJECTION_MATRIX, _proj);
// By passing in GL_MODELVIEW_MATRIX, we can abstract our model view matrix. c[0] = m[0] * p[0] + m[1] * p[4] + m[2] * p[8] + m[3] * p[12];
// This also stores it in an array of [16]. c[1] = m[0] * p[1] + m[1] * p[5] + m[2] * p[9] + m[3] * p[13];
glGetFloat(GL_MODELVIEW_MATRIX, _modl); c[2] = m[0] * p[2] + m[1] * p[6] + m[2] * p[10] + m[3] * p[14];
c[3] = m[0] * p[3] + m[1] * p[7] + m[2] * p[11] + m[3] * p[15];
_proj->flip()->limit(16); c[4] = m[4] * p[0] + m[5] * p[4] + m[6] * p[8] + m[7] * p[12];
_proj->get(&proj); c[5] = m[4] * p[1] + m[5] * p[5] + m[6] * p[9] + m[7] * p[13];
_modl->flip()->limit(16); c[6] = m[4] * p[2] + m[5] * p[6] + m[6] * p[10] + m[7] * p[14];
_modl->get(&modl); c[7] = m[4] * p[3] + m[5] * p[7] + m[6] * p[11] + m[7] * p[15];
// Now that we have our modelview and projection matrix, if we combine these c[8] = m[8] * p[0] + m[9] * p[4] + m[10] * p[8] + m[11] * p[12];
// 2 matrices, it will give us our clipping planes. To combine 2 matrices, c[9] = m[8] * p[1] + m[9] * p[5] + m[10] * p[9] + m[11] * p[13];
// we multiply them. c[10] = m[8] * p[2] + m[9] * p[6] + m[10] * p[10] + m[11] * p[14];
c[11] = m[8] * p[3] + m[9] * p[7] + m[10] * p[11] + m[11] * p[15];
clip[0] = modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + c[12] = m[12] * p[0] + m[13] * p[4] + m[14] * p[8] + m[15] * p[12];
modl[3] * proj[12]; c[13] = m[12] * p[1] + m[13] * p[5] + m[14] * p[9] + m[15] * p[13];
clip[1] = modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + c[14] = m[12] * p[2] + m[13] * p[6] + m[14] * p[10] + m[15] * p[14];
modl[3] * proj[13]; c[15] = m[12] * p[3] + m[13] * p[7] + m[14] * p[11] + m[15] * p[15];
clip[2] = modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] +
modl[3] * proj[14];
clip[3] = modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] +
modl[3] * proj[15];
clip[4] = modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] +
modl[7] * proj[12];
clip[5] = modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] +
modl[7] * proj[13];
clip[6] = modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] +
modl[7] * proj[14];
clip[7] = modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] +
modl[7] * proj[15];
clip[8] = modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] +
modl[11] * proj[12];
clip[9] = modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] +
modl[11] * proj[13];
clip[10] = modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] +
modl[11] * proj[14];
clip[11] = modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] +
modl[11] * proj[15];
clip[12] = modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] +
modl[15] * proj[12];
clip[13] = modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] +
modl[15] * proj[13];
clip[14] = modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] +
modl[15] * proj[14];
clip[15] = modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] +
modl[15] * proj[15];
// Now we actually want to get the sides of the frustum. To do this we take // Now we actually want to get the sides of the frustum. To do this we take
// the clipping planes we received above and extract the sides from them. // the clipping planes we received above and extract the sides from them.

View file

@ -1,21 +1,22 @@
#include "../Platform/stdafx.h" #include "../Platform/stdafx.h"
#include "FrustumData.h" #include "FrustumData.h"
float** m_Frustum; // float** m_Frustum;
FrustumData::FrustumData() { FrustumData::FrustumData() {
m_Frustum = new float*[16]; this->m_Frustum = new float*[6];
for (int i = 0; i < 16; i++) m_Frustum[i] = new float[16]; for (int i = 0; i < 6; i++) {
this->m_Frustum[i] = new float[4];
}
proj = floatArray(16); proj = floatArray(16);
modl = floatArray(16); modl = floatArray(16);
clip = floatArray(16); clip = floatArray(16);
} }
FrustumData::~FrustumData() { FrustumData::~FrustumData() {
delete[] proj.data; for (int i = 0; i < 6; i++) {
delete[] modl.data; delete[] m_Frustum[i];
delete[] clip.data; }
for (int i = 0; i < 16; i++) delete[] m_Frustum[i];
delete[] m_Frustum; delete[] m_Frustum;
} }

View file

@ -856,6 +856,10 @@ int LevelRenderer::renderChunks(int from, int to, int layer, double alpha) {
if ((globalChunkFlags[pClipChunk->globalIdx] & emptyFlag) == emptyFlag) if ((globalChunkFlags[pClipChunk->globalIdx] & emptyFlag) == emptyFlag)
continue; // Check that this particular layer isn't empty continue; // Check that this particular layer isn't empty
glPushMatrix();
glTranslatef((float)pClipChunk->chunk->x, (float)pClipChunk->chunk->y,
(float)pClipChunk->chunk->z);
// List can be calculated directly from the chunk's global idex // List can be calculated directly from the chunk's global idex
int list = pClipChunk->globalIdx * 2 + layer; int list = pClipChunk->globalIdx * 2 + layer;
list += chunkLists; list += chunkLists;
@ -863,6 +867,8 @@ int LevelRenderer::renderChunks(int from, int to, int layer, double alpha) {
if (RenderManager.CBuffCall(list, first)) { if (RenderManager.CBuffCall(list, first)) {
first = false; first = false;
} }
glPopMatrix();
count++; count++;
} }
@ -2248,9 +2254,13 @@ bool LevelRenderer::updateDirtyChunks() {
// int64_t startTime = System::currentTimeMillis(); // int64_t startTime = System::currentTimeMillis();
// app.DebugPrintf("Rebuilding permaChunk %d\n", index); // app.DebugPrintf("Rebuilding permaChunk %d\n", index);
Tesselator::getInstance()->offset(-permaChunk[index].x,
-permaChunk[index].y,
-permaChunk[index].z);
permaChunk[index].rebuild(); permaChunk[index].rebuild();
Tesselator::getInstance()->offset(0, 0, 0);
if (index != 0) if (index != 0)
s_rebuildCompleteEvents->Set( s_rebuildCompleteEvents->Set(
index - 1); // MGH - this rebuild happening on the main index - 1); // MGH - this rebuild happening on the main
@ -2303,7 +2313,10 @@ bool LevelRenderer::updateDirtyChunks() {
// static int64_t totalTime = 0; // static int64_t totalTime = 0;
// static int64_t countTime = 0; // static int64_t countTime = 0;
// int64_t startTime = System::currentTimeMillis(); // int64_t startTime = System::currentTimeMillis();
Tesselator::getInstance()->offset(
-permaChunk[index].x, -permaChunk[index].y, -permaChunk[index].z);
permaChunk.rebuild(); permaChunk.rebuild();
Tesselator::getInstance()->offset(0, 0, 0);
// int64_t endTime = System::currentTimeMillis(); // int64_t endTime = System::currentTimeMillis();
// totalTime += (endTime - startTime); // totalTime += (endTime - startTime);
// countTime++; // countTime++;
@ -4039,10 +4052,12 @@ int LevelRenderer::rebuildChunkThreadProc(void* lpParam) {
while (true) { while (true) {
s_activationEventA[index]->WaitForSignal(INFINITE); s_activationEventA[index]->WaitForSignal(INFINITE);
Tesselator* t = Tesselator::getInstance();
Tesselator::getInstance()->offset(
-permaChunk[index].x, -permaChunk[index].y, -permaChunk[index].z);
// app.DebugPrintf("Rebuilding permaChunk %d\n", index + 1); // app.DebugPrintf("Rebuilding permaChunk %d\n", index + 1);
permaChunk[index + 1].rebuild(); permaChunk[index + 1].rebuild();
Tesselator::getInstance()->offset(0, 0, 0);
// Inform the producer thread that we are done with this chunk // Inform the producer thread that we are done with this chunk
s_rebuildCompleteEvents->Set(index); s_rebuildCompleteEvents->Set(index);
} }

View file

@ -215,10 +215,15 @@ void Tesselator::end() {
} }
#endif #endif
} }
glDisableClientState(GL_VERTEX_ARRAY); // 4jcraft: gldisableclientstate breaks gl compat, commenting those lead
if (hasTexture) glDisableClientState(GL_TEXTURE_COORD_ARRAY); // to some weird glitches with input but.. somehow stopped one day so..
if (hasColor) glDisableClientState(GL_COLOR_ARRAY); // just keep an eye on these incase mouse locking stops working outta
if (hasNormal) glDisableClientState(GL_NORMAL_ARRAY); // nowhere (i blame opengl not me)
//
// glDisableClientState(GL_VERTEX_ARRAY); if (hasTexture)
// glDisableClientState(GL_TEXTURE_COORD_ARRAY); if (hasColor)
// glDisableClientState(GL_COLOR_ARRAY); if (hasNormal)
// glDisableClientState(GL_NORMAL_ARRAY);
} }
clear(); clear();