Skip to content

Commit

Permalink
Square render distance
Browse files Browse the repository at this point in the history
  • Loading branch information
uis246 committed Dec 22, 2020
1 parent 0482e52 commit 0388092
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight,
Render::~Render() {
Settings::Write("username", fieldUsername);
Settings::Write("serverAddr", fieldServerAddr);
Settings::WriteDouble("renderDistance", fieldDistance);
Settings::WriteInt("renderDistance", fieldDistance);
Settings::WriteDouble("targetFps", fieldTargetFps);
Settings::WriteDouble("mouseSensetivity", fieldSensetivity);
Settings::WriteBool("vsync", fieldVsync);
Expand Down Expand Up @@ -619,7 +619,7 @@ void Render::RenderGui() {
}
ImGui::Separator();

ImGui::SliderFloat("Render distance", &fieldDistance, 1.0f, 16.0f);
ImGui::SliderInt("Render distance", &fieldDistance, 1, 16);

ImGui::SliderFloat("Brightness", &fieldBrightness, 0.0f, 1.0f);

Expand Down
2 changes: 1 addition & 1 deletion src/Render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Render {
std::string stateString;
char fieldUsername[512];
char fieldServerAddr[512];
float fieldDistance;
int fieldDistance;
float fieldSensetivity;
float fieldTargetFps;
bool fieldWireframe;
Expand Down
55 changes: 29 additions & 26 deletions src/RendererWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() {

void RendererWorld::UpdateAllSections(VectorF playerPos) {
OPTICK_EVENT();
Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16));

std::vector<Vector> suitableChunks;
auto chunks = GetGameState()->GetWorld().GetSectionsList();
for (auto& it : chunks) {
double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength();
if (distance > MaxRenderingDistance)
continue;
suitableChunks.push_back(it);
}
Vector playerSection(std::floor(playerPos.x / 16), std::floor(playerPos.y / 16.0), std::floor(playerPos.z / 16));
Vector2I32 playerChunk(playerSection.x, playerSection.z);

std::vector<Vector> suitableChunks;
auto chunks = GetGameState()->GetWorld().GetSectionsList();
for (auto& it : chunks) {
Vector2I32 diff = (Vector2I32(it.x, it.z) - playerChunk).abs();
int distance = std::max(diff.x, diff.z);
if (distance > MaxRenderingDistance)
continue;
suitableChunks.push_back(it);
}

std::vector<Vector> toRemove;

Expand All @@ -169,16 +171,15 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) {
PUSH_EVENT("DeleteSectionRender", it);
}

playerChunk.y = std::floor(GetGameState()->GetPlayer()->pos.y / 16.0);
std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) {
double leftLengthToPlayer = (playerChunk - lhs).GetLength();
double rightLengthToPlayer = (playerChunk - rhs).GetLength();
std::sort(suitableChunks.begin(), suitableChunks.end(), [playerSection](Vector lhs, Vector rhs) {
double leftLengthToPlayer = (playerSection - lhs).GetLength();
double rightLengthToPlayer = (playerSection - rhs).GetLength();
return leftLengthToPlayer < rightLengthToPlayer;
});

for (auto& it : suitableChunks) {
PUSH_EVENT("ChunkChanged", it);
}
}
}

RendererWorld::RendererWorld() {
Expand Down Expand Up @@ -233,33 +234,35 @@ RendererWorld::RendererWorld() {
}
});

listener->RegisterHandler("ChunkChanged", [this](const Event& eventData) {
listener->RegisterHandler("ChunkChanged", [this](const Event& eventData) {
OPTICK_EVENT("EV_ChunkChanged");
auto vec = eventData.get<Vector>();
if (vec == Vector())
return;

Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16));
Vector2I32 playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), std::floor(GetGameState()->GetPlayer()->pos.z / 16));

double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength();
if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) {
return;
}
Vector2I32 diff = (Vector2I32(vec.x, vec.z) - playerChunk).abs();
int distanceToChunk = std::max(diff.x, diff.z);
if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) {
return;
}

parseQueue.push(vec);

parseQueueNeedRemoveUnnecessary = true;
});
});

listener->RegisterHandler("ChunkChangedForce", [this](const Event& eventData) {
OPTICK_EVENT("EV_ChunkChangedForce");
auto vec = eventData.get<Vector>();
if (vec == Vector())
return;

Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16));
Vector2I32 playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), std::floor(GetGameState()->GetPlayer()->pos.z / 16));

double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength();
Vector2I32 diff = (Vector2I32(vec.x, vec.z) - playerChunk).abs();
int distanceToChunk = std::max(diff.x, diff.z);
if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) {
return;
}
Expand Down Expand Up @@ -329,9 +332,9 @@ void RendererWorld::Render(RenderState & renderState) {
glm::mat4 view = GetGameState()->GetViewMatrix();
glm::mat4 projView = projection * view;

{//Set listener position
Entity *player = GetGameState()->GetPlayer();

{//Set listener position
float playerYaw = Entity::DecodeYaw(player->yaw);
float playerYawR = playerYaw * (M_PI / 180.f);

Expand Down Expand Up @@ -410,7 +413,7 @@ void RendererWorld::Render(RenderState & renderState) {
skyShader->Activate();
skyShader->SetUniform("projView", projection);
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm());
model = glm::translate(model, player->pos.glm());
const float scale = 1000000.0f;
model = glm::scale(model, glm::vec3(scale, scale, scale));
float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f;
Expand Down
2 changes: 1 addition & 1 deletion src/RendererWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RendererWorld {
void Render(RenderState& renderState);
void PrepareRender();

double MaxRenderingDistance;
int MaxRenderingDistance;

void Update(double timeToUpdate);

Expand Down
4 changes: 4 additions & 0 deletions src/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ struct AC_API Vector2 {
return dot(rhs) / GetLength() / rhs.GetLength();
}

Vector2 abs() {
return Vector2(std::abs(x), std::abs(z));
}

Vector2<double> normalize() {
auto length = GetLength();

Expand Down

0 comments on commit 0388092

Please sign in to comment.