Skip to content

Commit

Permalink
Removed greedy meshing
Browse files Browse the repository at this point in the history
Trying rendering smaller bricks all at ones. Something hella weird is happening though. Existing minecraft textures are drawn, sometimes even screenshots!
  • Loading branch information
Goby56 committed Aug 9, 2024
1 parent 4bcc89c commit 754eaf6
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 193 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/goby56/wakes/mixin/DebugHudMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ protected void getLeftText(CallbackInfoReturnable<List<String>> info) {
int q = WakesDebugInfo.quadsRendered;
info.getReturnValue().add(String.format("[Wakes] Rendering %d quads for %d wake nodes", q, WakeHandler.getInstance().getTotal()));
info.getReturnValue().add(String.format("[Wakes] Node logic: %.2fms/t", 10e-6 * WakesDebugInfo.nodeLogicTime));
info.getReturnValue().add(String.format("[Wakes] Mesh gen: %.2fms/t", 10e-6 * WakesDebugInfo.meshGenerationTime));
info.getReturnValue().add(String.format("[Wakes] Texturing: %.2fms/t", q * 10e-6 * WakesDebugInfo.texturingTime));
info.getReturnValue().add(String.format("[Wakes] Rendering: %.3fms/f", 10e-6 * WakesDebugInfo.wakeRenderingTime.stream().reduce(0L, Long::sum) / WakesDebugInfo.wakeRenderingTime.size()));
info.getReturnValue().add(String.format(" - Texturing: %.2fms/t", q * 10e-6 * WakesDebugInfo.texturingTime.stream().reduce(0L, Long::sum) / WakesDebugInfo.texturingTime.size()));
info.getReturnValue().add(String.format(" - Drawing: %.2fms/t", q * 10e-6 * WakesDebugInfo.drawingTime.stream().reduce(0L, Long::sum) / WakesDebugInfo.drawingTime.size()));
}
}
Expand Down
60 changes: 0 additions & 60 deletions src/main/java/com/goby56/wakes/render/WakeQuad.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/goby56/wakes/render/WakeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void afterTranslucent(WorldRenderContext context) {
if (wakeHandler == null || wakeHandler.resolutionResetScheduled) return;


ArrayList<WakeQuad> quads = wakeHandler.getVisible(context.frustum());
ArrayList<Brick> bricks = wakeHandler.getVisible(context.frustum(), Brick.class);

Matrix4f matrix = context.matrixStack().peek().getPositionMatrix();
RenderSystem.enableBlend();
Expand All @@ -46,8 +46,8 @@ public void afterTranslucent(WorldRenderContext context) {
if (resolution.res != WakeNode.res) return;
int n = 0;
long tRendering = System.nanoTime();
for (var quad : quads) {
wakeTextures.get(resolution).render(matrix, context.camera(), quad, context.world());
for (var brick : bricks) {
wakeTextures.get(resolution).render(matrix, context.camera(), brick);
n++;
}
WakesDebugInfo.wakeRenderingTime.add(System.nanoTime() - tRendering);
Expand Down
55 changes: 22 additions & 33 deletions src/main/java/com/goby56/wakes/render/WakeTexture.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.goby56.wakes.render;

import com.goby56.wakes.render.enums.RenderType;
import com.goby56.wakes.simulation.Brick;
import com.goby56.wakes.simulation.QuadTree;
import com.goby56.wakes.simulation.WakeNode;
import com.goby56.wakes.utils.WakesDebugInfo;
import com.mojang.blaze3d.platform.GlConst;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.TextureUtil;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.render.*;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.joml.Matrix4f;
Expand All @@ -16,15 +20,15 @@
import org.lwjgl.opengl.GL14;
import org.lwjgl.system.MemoryUtil;

import java.nio.IntBuffer;

public class WakeTexture {
public int res;
public int glTexId;
public long imgPtr;

public WakeTexture(int res) {
this.res = res;
this.glTexId = TextureUtil.generateTextureId();
this.imgPtr = MemoryUtil.nmemAlloc((long) 32 * res * 32 * res * 4);

GlStateManager._bindTexture(glTexId);
GlStateManager._texParameter(GlConst.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 0);
Expand All @@ -35,67 +39,52 @@ public WakeTexture(int res) {
GlStateManager._texParameter(GlConst.GL_TEXTURE_2D, GL12.GL_TEXTURE_MIN_FILTER, GL12.GL_NEAREST);
GlStateManager._texParameter(GlConst.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAG_FILTER, GL12.GL_NEAREST);

GlStateManager._texImage2D(GlConst.GL_TEXTURE_2D, 0, GlConst.GL_RGBA, 32 * res, 32 * res, 0, GlConst.GL_RGBA, GlConst.GL_UNSIGNED_BYTE, null);
GlStateManager._texImage2D(GlConst.GL_TEXTURE_2D, 0, GlConst.GL_RGBA, QuadTree.BRICK_WIDTH * res, QuadTree.BRICK_WIDTH * res, 0, GlConst.GL_RGBA, GlConst.GL_UNSIGNED_BYTE, null);
}

public void render(Matrix4f matrix, Camera camera, WakeQuad quad, World world) {
long tTexture = System.nanoTime();
quad.populatePixels(this, world);
WakesDebugInfo.texturingTime.add(System.nanoTime() - tTexture);

public void render(Matrix4f matrix, Camera camera, Brick brick) {
long tDrawing = System.nanoTime();
GlStateManager._bindTexture(glTexId);
GlStateManager._pixelStore(GlConst.GL_UNPACK_ROW_LENGTH, 0);
GlStateManager._pixelStore(GlConst.GL_UNPACK_SKIP_PIXELS, 0);
GlStateManager._pixelStore(GlConst.GL_UNPACK_SKIP_ROWS, 0);
GlStateManager._pixelStore(GlConst.GL_UNPACK_ALIGNMENT, 4);
GlStateManager._texSubImage2D(GlConst.GL_TEXTURE_2D, 0, 0, 0, 32 * res, 32 * res, GlConst.GL_RGBA, GlConst.GL_UNSIGNED_BYTE, imgPtr);
GlStateManager._texSubImage2D(GlConst.GL_TEXTURE_2D, 0,0,0,brick.dim * brick.texRes, brick.dim * brick.texRes, GlConst.GL_RGBA, GlConst.GL_UNSIGNED_BYTE, brick.imgPtr);

RenderSystem.setShaderTexture(0, glTexId);
RenderSystem.setShader(RenderType.getProgram());
RenderSystem.enableDepthTest(); // Is it THIS simple? https://github.com/Goby56/wakes/issues/46

BufferBuilder buffer = Tessellator.getInstance().getBuffer();
buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL);

WakeNode[][] nodes = quad.nodes;
int X = nodes.length - 1;
int Z = nodes[0].length - 1;
float u = quad.x / 32f;
float v = quad.z / 32f;
float uOffset = quad.w / 32f;
float vOffset = quad.h / 32f;
Vector3f pos = new Vec3d(quad.x, quad.y, quad.z).add(camera.getPos().negate()).toVector3f();

Vector3f pos = brick.pos.add(camera.getPos().negate()).toVector3f();
buffer.vertex(matrix, pos.x, pos.y, pos.z)
.color(1f, 1f, 1f, 1f)
.texture(u, v)
.texture(0, 0)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light(nodes[0][0], world))
.light(14680064)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x, pos.y, pos.z + quad.h)
buffer.vertex(matrix, pos.x, pos.y, pos.z + brick.dim)
.color(1f, 1f, 1f, 1f)
.texture(u, v + vOffset)
.texture(0, 1)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light(nodes[0][Z], world))
.light(14680064)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x + quad.w, pos.y, pos.z + quad.h)
buffer.vertex(matrix, pos.x + brick.dim, pos.y, pos.z + brick.dim)
.color(1f, 1f, 1f, 1f)
.texture(u + uOffset, v + vOffset)
.texture(1, 1)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light(nodes[X][Z], world))
.light(14680064)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x + quad.w, pos.y, pos.z)
buffer.vertex(matrix, pos.x + brick.dim, pos.y, pos.z)
.color(1f, 1f, 1f, 1f)
.texture(u + uOffset, v)
.texture(1, 0)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light(nodes[X][0], world))
.light(14680064)
.normal(0f, 1f, 0f).next();

Tessellator.getInstance().draw();
WakesDebugInfo.drawingTime.add(System.nanoTime() - tDrawing);
}

private static int light(WakeNode node, World world) {
return WorldRenderer.getLightmapCoordinates(world, node.blockPos());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.simulation.WakeNode;
import com.goby56.wakes.utils.WakesDebugInfo;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
Expand All @@ -18,9 +19,9 @@ public class WakeDebugRenderer implements WorldRenderEvents.DebugRender {
public void beforeDebugRender(WorldRenderContext context) {
WakeHandler wakeHandler = WakeHandler.getInstance();
if (WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
for (var quad : wakeHandler.getVisible(context.frustum())) {
Box box = new Box(quad.x, quad.y - 0.1f, quad.z, quad.x + quad.w, quad.y - 0.2f, quad.z + quad.h);
var col = Color.getHSBColor(new Random(quad.hashCode()).nextFloat(), 1f, 1f).getRGBColorComponents(null);
for (var node : wakeHandler.getVisible(context.frustum(), WakeNode.class)) {
Box box = new Box(node.x, node.height - 0.1f, node.z, node.x + 1, node.height - 0.2f, node.z + 1);
var col = Color.getHSBColor(new Random(node.hashCode()).nextFloat(), 1f, 1f).getRGBColorComponents(null);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(),
box.offset(context.camera().getPos().negate()),
col[0], col[1], col[2], 0.5f);
Expand Down
Loading

0 comments on commit 754eaf6

Please sign in to comment.