From 77b5c9173713ac5cc4dd7db8764834e0f34821d7 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:36:53 +1000 Subject: [PATCH] Textures work --- .../cortex/zenith/client/core/VoxelCore.java | 7 +++++ .../client/core/model/ModelManager.java | 12 ++++++-- .../client/core/model/ModelTextureBakery.java | 29 ++++++++++--------- .../core/rendering/Gl46FarWorldRenderer.java | 4 +++ .../assets/zenith/shaders/lod/gl46/quads.frag | 8 +++-- .../assets/zenith/shaders/lod/gl46/quads.vert | 17 +++++++++++ 6 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/main/java/me/cortex/zenith/client/core/VoxelCore.java b/src/main/java/me/cortex/zenith/client/core/VoxelCore.java index bc2bf13a..1a798377 100644 --- a/src/main/java/me/cortex/zenith/client/core/VoxelCore.java +++ b/src/main/java/me/cortex/zenith/client/core/VoxelCore.java @@ -84,6 +84,12 @@ public class VoxelCore { this.world.getMapper().setCallbacks(a->{}, a->{}); System.out.println("Voxel core initialized"); + + + for (var state : this.world.getMapper().getStateEntries()) { + this.renderer.getModelManager().updateEntry(state.id, state.state); + } + //this.renderer.getModelManager().updateEntry(0, Blocks.GRASS_BLOCK.getDefaultState()); } @@ -107,6 +113,7 @@ public class VoxelCore { matrices.translate(-cameraX, -cameraY, -cameraZ); DebugUtil.setPositionMatrix(matrices); matrices.pop(); + //this.renderer.getModelManager().updateEntry(0, Blocks.DIRT_PATH.getDefaultState()); //this.renderer.getModelManager().updateEntry(0, Blocks.COMPARATOR.getDefaultState()); //this.renderer.getModelManager().updateEntry(0, Blocks.OAK_LEAVES.getDefaultState()); diff --git a/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java b/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java index 2165e331..9fd5a253 100644 --- a/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java +++ b/src/main/java/me/cortex/zenith/client/core/model/ModelManager.java @@ -1,5 +1,6 @@ package me.cortex.zenith.client.core.model; +import com.mojang.blaze3d.platform.GlConst; import com.mojang.blaze3d.platform.GlStateManager; import me.cortex.zenith.client.core.gl.GlBuffer; import me.cortex.zenith.client.core.gl.GlTexture; @@ -88,6 +89,8 @@ public class ModelManager { int aaaa = 1; + this.putTextures(id, textureData); + //Model data contains, the quad size and offset of each face and whether the face needs to be resolved with a colour modifier // sourced from the quad data and reverse indexed into the section data (meaning there will be a maxiumum number of colours) @@ -104,9 +107,14 @@ public class ModelManager { int X = (id&0xFF) * this.modelTextureSize*3; int Y = ((id>>8)&0xFF) * this.modelTextureSize*2; for (int subTex = 0; subTex < 6; subTex++) { + int x = X + (subTex%3)*this.modelTextureSize; + int y = Y + (subTex/3)*this.modelTextureSize; - - //glTextureSubImage2D(this.textures.id, 0, ); + 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); + glTextureSubImage2D(this.textures.id, 0, x, y, this.modelTextureSize, this.modelTextureSize, GL_RGBA, GL_UNSIGNED_BYTE, textures[subTex].colour()); } } diff --git a/src/main/java/me/cortex/zenith/client/core/model/ModelTextureBakery.java b/src/main/java/me/cortex/zenith/client/core/model/ModelTextureBakery.java index edcf1e3e..3987c7db 100644 --- a/src/main/java/me/cortex/zenith/client/core/model/ModelTextureBakery.java +++ b/src/main/java/me/cortex/zenith/client/core/model/ModelTextureBakery.java @@ -37,13 +37,13 @@ public class ModelTextureBakery { private static final List FACE_VIEWS = new ArrayList<>(); static { - addView(-90,0);//Direction.DOWN - addView(90,0);//Direction.UP - addView(0,180);//Direction.NORTH - addView(0,0);//Direction.SOUTH + addView(-90,0, 0);//Direction.DOWN + addView(90,0, 0);//Direction.UP + addView(0,180, 0);//Direction.NORTH + addView(0,0, 0);//Direction.SOUTH //TODO: check these arnt the wrong way round - addView(0,90);//Direction.EAST - addView(0,270);//Direction.WEST + addView(0,90, -90);//Direction.EAST + addView(0,270, -90);//Direction.WEST } public ModelTextureBakery(int width, int height) { @@ -54,9 +54,10 @@ public class ModelTextureBakery { this.framebuffer = new GlFramebuffer().bind(GL_COLOR_ATTACHMENT0, this.colourTex).bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthTex).verify(); } - private static void addView(float pitch, float yaw) { + private static void addView(float pitch, float yaw, float rotation) { var stack = new MatrixStack(); stack.translate(0.5f,0.5f,0.5f); + stack.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(rotation)); stack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(pitch)); stack.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(yaw)); stack.translate(-0.5f,-0.5f,-0.5f); @@ -80,6 +81,11 @@ public class ModelTextureBakery { glBindFramebuffer(GL_FRAMEBUFFER, this.framebuffer.id); + var renderLayer = RenderLayers.getBlockLayer(state); + if (renderLayer == RenderLayer.getTranslucent()) { + //TODO: TRANSLUCENT, must sort the quad first + } + renderLayer.startDrawing(); RenderSystem.depthMask(true); RenderSystem.enableBlend(); RenderSystem.enableDepthTest(); @@ -93,20 +99,17 @@ public class ModelTextureBakery { //TODO: render fluid } - var renderLayer = RenderLayers.getBlockLayer(state); - if (renderLayer == RenderLayer.getTranslucent()) { - //TODO: TRANSLUCENT, must sort the quad first - } - var faces = new ColourDepthTextureData[FACE_VIEWS.size()]; for (int i = 0; i < faces.length; i++) { faces[i] = captureView(state, model, FACE_VIEWS.get(i), randomValue); } + renderLayer.endDrawing(); + RenderSystem.setProjectionMatrix(oldProjection, VertexSorter.BY_DISTANCE); glBindFramebuffer(GL_FRAMEBUFFER, oldFB); GL11C.glViewport(GlStateManager.Viewport.getX(), GlStateManager.Viewport.getY(), GlStateManager.Viewport.getWidth(), GlStateManager.Viewport.getHeight()); - glBlitNamedFramebuffer(this.framebuffer.id, oldFB, 0,0,16,16,0,0,256,256, GL_COLOR_BUFFER_BIT, GL_NEAREST); + //glBlitNamedFramebuffer(this.framebuffer.id, oldFB, 0,0,16,16,0,0,256,256, GL_COLOR_BUFFER_BIT, GL_NEAREST); return faces; } diff --git a/src/main/java/me/cortex/zenith/client/core/rendering/Gl46FarWorldRenderer.java b/src/main/java/me/cortex/zenith/client/core/rendering/Gl46FarWorldRenderer.java index 2cd8b431..cf9f5bb7 100644 --- a/src/main/java/me/cortex/zenith/client/core/rendering/Gl46FarWorldRenderer.java +++ b/src/main/java/me/cortex/zenith/client/core/rendering/Gl46FarWorldRenderer.java @@ -7,6 +7,7 @@ import me.cortex.zenith.client.core.gl.shader.Shader; import me.cortex.zenith.client.core.gl.shader.ShaderType; import me.cortex.zenith.client.core.rendering.util.UploadStream; import me.cortex.zenith.client.mixin.joml.AccessFrustumIntersection; +import net.minecraft.block.Blocks; import net.minecraft.client.render.RenderLayer; import net.minecraft.client.util.math.MatrixStack; import org.joml.Matrix4f; @@ -80,6 +81,9 @@ public class Gl46FarWorldRenderer extends AbstractFarWorldRenderer { if (this.geometry.getSectionCount() == 0) { return; } + + //this.getModelManager().updateEntry(this.frameId%(1<<15), Blocks.STONE.getDefaultState()); + RenderLayer.getCutoutMipped().startDrawing(); int oldActiveTexture = glGetInteger(GL_ACTIVE_TEXTURE); int oldBoundTexture = glGetInteger(GL_TEXTURE_BINDING_2D); diff --git a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag index c27e93b4..a2a4f69c 100644 --- a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag +++ b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.frag @@ -1,9 +1,13 @@ #version 460 core layout(binding = 0) uniform sampler2D blockModelAtlas; + layout(location = 0) in vec2 uv; +layout(location = 1) in flat vec2 baseUV; +layout(location = 2) in flat vec4 colourTinting; + layout(location = 0) out vec4 outColour; void main() { - vec2 uv = mod(uv, vec2(1)); + vec2 uv = mod(uv, vec2(1))*(1f/(vec2(3,2)*256f)); - outColour = texture(blockModelAtlas, uv); + outColour = texture(blockModelAtlas, uv + baseUV) * colourTinting; } \ No newline at end of file diff --git a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.vert b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.vert index 3110efc4..f71f3a0d 100644 --- a/src/main/resources/assets/zenith/shaders/lod/gl46/quads.vert +++ b/src/main/resources/assets/zenith/shaders/lod/gl46/quads.vert @@ -5,6 +5,8 @@ #import layout(location = 0) out vec2 uv; +layout(location = 1) out flat vec2 baseUV; +layout(location = 2) out flat vec4 colourTinting; uint extractLodLevel() { return uint(gl_BaseInstance)>>29; @@ -66,6 +68,21 @@ void main() { uint biomeId = extractBiomeId(quad); uv = vec2(sizePreLod); + + vec2 modelUV = vec2(stateId&0xFF, (stateId>>8)&0xFF)*(1f/(256f)); + + //TODO: make the face orientated by 2x3 so that division is not a integer div and modulo isnt needed + // as these are very slow ops + baseUV = modelUV + (vec2(face%3, face/3) * (1f/(vec2(3,2)*256f))); + + colourTinting = getLighting(extractLightId(quad)); + //Apply face tint + if (face == 0) { + colourTinting.xyz *= vec3(0.75, 0.75, 0.75); + } else if (face != 1) { + colourTinting.xyz *= vec3((float(face-2)/4)*0.6 + 0.4); + } + } //gl_Position = MVP * vec4(vec3(((cornerIdx)&1)+10,10,((cornerIdx>>1)&1)+10),1); //uint i = uint(quad>>32);