rendering: move some classes to packages, use AssetsManager for textures

This commit is contained in:
Bixilon 2021-02-04 17:43:28 +01:00
parent 2605a35f68
commit 4cfcd6519a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
10 changed files with 55 additions and 53 deletions

View File

@ -1,5 +1,6 @@
package de.bixilon.minosoft.gui.rendering
import de.bixilon.minosoft.gui.rendering.shader.Shader
import glm_.glm
import glm_.mat4x4.Mat4
import glm_.vec3.Vec3

View File

@ -107,6 +107,7 @@ object ChunkPreparer {
"minecraft:dirt" -> 1
"minecraft:stone" -> 2
"minecraft:glass" -> 3
"minecraft:red_wool" -> 4
else -> 2
}
}

View File

@ -1,31 +0,0 @@
package de.bixilon.minosoft.gui.rendering;
import de.bixilon.minosoft.data.mappings.blocks.Block;
import de.bixilon.minosoft.data.world.Chunk;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import java.util.HashMap;
public class DummyData {
private static final Block BEDROCK = new Block("bedrock");
private static final Block DIRT = new Block("dirt");
private static final Block STONE = new Block("stone");
public static Chunk getDummyChunk() {
Chunk chunk = new Chunk(new HashMap<>());
for (int y = 0; y < ProtocolDefinition.SECTION_HEIGHT_Y * ProtocolDefinition.SECTIONS_PER_CHUNK; y++) {
for (int x = 0; x < ProtocolDefinition.SECTION_WIDTH_X; x++) {
for (int z = 0; z < ProtocolDefinition.SECTION_WIDTH_Z; z++) {
if (y == 0) {
chunk.setBlock(x, y, z, BEDROCK);
} else if (y < 8) {
chunk.setBlock(x, y, z, DIRT);
} else if (y < 60) {
chunk.setBlock(x, y, z, STONE);
}
}
}
}
return chunk;
}
}

View File

@ -1,6 +1,7 @@
package de.bixilon.minosoft.gui.rendering;
import de.bixilon.minosoft.data.world.ChunkLocation;
import de.bixilon.minosoft.gui.rendering.shader.Shader;
import glm_.vec3.Vec3;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;

View File

@ -1,6 +1,8 @@
package de.bixilon.minosoft.gui.rendering
import de.bixilon.minosoft.data.world.ChunkLocation
import de.bixilon.minosoft.gui.rendering.shader.Shader
import de.bixilon.minosoft.gui.rendering.textures.TextureArray
import de.bixilon.minosoft.protocol.network.Connection
import org.lwjgl.*
import org.lwjgl.glfw.Callbacks
@ -111,7 +113,7 @@ class RenderWindow(private val connection: Connection) {
}
fun startLoop() {
texture0 = TextureArray(arrayOf("/textures/bedrock.png", "/textures/dirt.png", "/textures/stone.png", "/textures/glass.png"))
texture0 = TextureArray(connection.version.assetsManager, arrayOf("block/bedrock", "block/dirt", "block/stone", "block/glass", "block/red_wool"))
texture0.load()
shader = Shader("vertex.glsl", "fragment.glsl")

View File

@ -1,6 +1,7 @@
package de.bixilon.minosoft.gui.rendering;
package de.bixilon.minosoft.gui.rendering.shader;
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException;
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil;
import glm_.mat4x4.Mat4;
import glm_.vec3.Vec3;
import org.lwjgl.BufferUtils;

View File

@ -1,6 +1,7 @@
package de.bixilon.minosoft.gui.rendering;
package de.bixilon.minosoft.gui.rendering.shader;
import de.bixilon.minosoft.gui.rendering.exceptions.ShaderLoadingException;
import de.bixilon.minosoft.gui.rendering.util.OpenGLUtil;
import de.bixilon.minosoft.util.Util;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;

View File

@ -1,20 +1,23 @@
package de.bixilon.minosoft.gui.rendering;
package de.bixilon.minosoft.gui.rendering.textures;
import de.matthiasmann.twl.utils.PNGDecoder;
import org.lwjgl.BufferUtils;
import de.bixilon.minosoft.data.assets.AssetsManager;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Map;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY;
import static org.lwjgl.opengl.GL30.glGenerateMipmap;
public class TextureArray {
private final AssetsManager assetsManager;
private final String[] texturePaths;
private String[] textureIndexArray;
private int textureId;
public TextureArray(String[] texturePaths) {
public TextureArray(AssetsManager assetsManager, String[] texturePaths) {
this.assetsManager = assetsManager;
this.texturePaths = texturePaths;
}
@ -26,26 +29,22 @@ public class TextureArray {
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// load and generate the texture
var textures = TextureLoader.Companion.loadTextureArray(this.assetsManager, this.texturePaths);
boolean sizeSet = false;
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 16, 16, textures.size(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
for (int i = 0; i < this.texturePaths.length; i++) {
PNGDecoder decoder = new PNGDecoder(OpenGLUtil.class.getResourceAsStream(this.texturePaths[i]));
ByteBuffer buffer = BufferUtils.createByteBuffer(decoder.getWidth() * decoder.getHeight() * 4);
decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
buffer.flip();
if (!sizeSet) {
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), this.texturePaths.length, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
sizeSet = true;
}
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, decoder.getWidth(), decoder.getHeight(), 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
this.textureIndexArray = new String[textures.size()];
int i = 0;
for (Map.Entry<String, ByteBuffer> entry : textures.entrySet()) {
this.textureIndexArray[i] = entry.getKey();
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i++, 16, 16, 1, GL_RGBA, GL_UNSIGNED_BYTE, entry.getValue());
}
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glGenerateMipmap(GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
return this.textureId;
}
@ -57,4 +56,8 @@ public class TextureArray {
glActiveTexture(textureMode); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, this.textureId);
}
public String[] getTextureIndexArray() {
return this.textureIndexArray;
}
}

View File

@ -0,0 +1,23 @@
package de.bixilon.minosoft.gui.rendering.textures
import de.bixilon.minosoft.data.assets.AssetsManager
import de.matthiasmann.twl.utils.PNGDecoder
import org.lwjgl.BufferUtils
import java.nio.ByteBuffer
class TextureLoader {
companion object {
fun loadTextureArray(assetsManager: AssetsManager, textures: Array<String>): Map<String, ByteBuffer> {
val result: MutableMap<String, ByteBuffer> = mutableMapOf()
for (texture in textures) {
val decoder = PNGDecoder(assetsManager.readAssetAsStream("minecraft/textures/${texture}.png"))
val buffer = BufferUtils.createByteBuffer(decoder.width * decoder.height * 4)
decoder.decode(buffer, decoder.width * 4, PNGDecoder.Format.RGBA)
buffer.flip()
result[texture] = buffer
}
return result.toMap()
}
}
}

View File

@ -1,4 +1,4 @@
package de.bixilon.minosoft.gui.rendering;
package de.bixilon.minosoft.gui.rendering.util;
import org.lwjgl.opengl.ARBShaderObjects;