From 5a0825115a12cbee109a59c76af26ede2c99b979 Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Sun, 8 Mar 2020 21:06:35 -0700 Subject: [PATCH] Goodbye Litecraft! --- .../github/halotroop/litecraft/Litecraft.java | 7 +- .../litecraft/render/BlockRenderer.java | 3 +- .../github/hydos/ginger/VulkanExample.java | 1 + .../common/elements/objects/Player.java | 100 ------------------ .../engine/opengl/render/GLRenderManager.java | 22 ++-- .../render/renderers/GLObjectRenderer.java | 3 +- .../ginger/engine/opengl/utils/GLLoader.java | 40 ------- 7 files changed, 20 insertions(+), 156 deletions(-) delete mode 100644 src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java diff --git a/src/main/java/com/github/halotroop/litecraft/Litecraft.java b/src/main/java/com/github/halotroop/litecraft/Litecraft.java index 42eb545..77ab22b 100644 --- a/src/main/java/com/github/halotroop/litecraft/Litecraft.java +++ b/src/main/java/com/github/halotroop/litecraft/Litecraft.java @@ -2,6 +2,7 @@ package com.github.halotroop.litecraft; import org.joml.*; +import com.github.halotroop.litecraft.render.BlockRenderer; import com.github.halotroop.litecraft.save.LitecraftSave; import com.github.halotroop.litecraft.screens.*; import com.github.halotroop.litecraft.types.block.Blocks; @@ -19,7 +20,7 @@ import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.obj.ModelLoader; import com.github.hydos.ginger.engine.opengl.api.GingerGL; import com.github.hydos.ginger.engine.opengl.postprocessing.PostProcessing; -import com.github.hydos.ginger.engine.opengl.render.GLRenderManager; +import com.github.hydos.ginger.engine.opengl.render.*; import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.utils.*; @@ -35,6 +36,7 @@ public class Litecraft extends Game public int fps, ups, tps; public Vector4i dbgStats = new Vector4i(); private long frameTimer; + private BlockRenderer blockRenderer; public Litecraft(int windowWidth, int windowHeight, float frameLimit) { @@ -141,6 +143,7 @@ public class Litecraft extends Game //Set the player model GLTexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png"); FontType font = new FontType(GLLoader.loadFontAtlas("candara.png"), "candara.fnt"); + this.blockRenderer = new BlockRenderer(GingerRegister.getInstance().masterRenderer.getEntityShader(), GingerRegister.getInstance().masterRenderer.getProjectionMatrix()); this.player = new PlayerEntity(playerModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f)); this.camera = new FirstPersonCamera(player); this.data = new GameData(this.player, this.camera, 20); @@ -210,5 +213,5 @@ public class Litecraft extends Game @Override public void renderScene() - { world.render(GingerRegister.getInstance().masterRenderer.blockRenderer); } + { world.render(this.blockRenderer); } } \ No newline at end of file diff --git a/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java b/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java index 1066fae..951c74d 100644 --- a/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java +++ b/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java @@ -12,7 +12,6 @@ import com.github.hydos.ginger.engine.common.math.Maths; import com.github.hydos.ginger.engine.opengl.render.Renderer; import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader; -import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class BlockRenderer extends Renderer implements WorldGenConstants { @@ -25,7 +24,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants shader.start(); shader.loadProjectionMatrix(projectionMatrix); shader.stop(); - this.atlasID = GLLoader.createBlockAtlas(); + this.atlasID = VoxelLoader.createBlockAtlas(); } private void prepBlockInstance(RenderObject entity) diff --git a/src/main/java/com/github/hydos/ginger/VulkanExample.java b/src/main/java/com/github/hydos/ginger/VulkanExample.java index 4372d4b..0e5e96b 100644 --- a/src/main/java/com/github/hydos/ginger/VulkanExample.java +++ b/src/main/java/com/github/hydos/ginger/VulkanExample.java @@ -1406,6 +1406,7 @@ public class VulkanExample return indices.isComplete() && extensionsSupported && swapChainAdequate && anisotropySupported; } + @SuppressWarnings("unlikely-arg-type") private boolean checkDeviceExtensionSupport(VkPhysicalDevice device) { try (MemoryStack stack = stackPush()) diff --git a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java b/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java deleted file mode 100644 index aec8922..0000000 --- a/src/main/java/com/github/hydos/ginger/engine/common/elements/objects/Player.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.github.hydos.ginger.engine.common.elements.objects; - -import org.joml.Vector3f; - -import com.github.halotroop.litecraft.Litecraft; -import com.github.halotroop.litecraft.util.RelativeDirection; -import com.github.halotroop.litecraft.world.gen.WorldGenConstants; -import com.github.hydos.ginger.engine.common.Constants; -import com.github.hydos.ginger.engine.common.api.GingerRegister; -import com.github.hydos.ginger.engine.common.io.Window; -import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel; - -public class Player extends RenderObject implements WorldGenConstants -{ - private boolean isInAir = false; - private double upwardsSpeed; - private boolean noWeight = true; // because the force of gravity on an object's mass is called WEIGHT, not gravity - private int chunkX, chunkY, chunkZ; - - public Player(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale) - { - super(model, position, rotX, rotY, rotZ, scale); - this.chunkX = (int) position.x >> POS_SHIFT; - this.chunkY = (int) position.y >> POS_SHIFT; - this.chunkZ = (int) position.z >> POS_SHIFT; - } - - public void move(RelativeDirection direction) - { - float ry = (float) Math.toRadians(GingerRegister.getInstance().game.data.camera.getYaw()); - switch (direction) - { - case FORWARD: - default: - position.z -= Math.cos(ry) * Constants.movementSpeed; - position.x += Math.sin(ry) * Constants.movementSpeed; - break; - case BACKWARD: - position.z += Math.cos(ry) * Constants.movementSpeed; - position.x -= Math.sin(ry) * Constants.movementSpeed; - break; - case LEFT: - ry -= RIGHT_ANGLE; - position.z -= Math.cos(ry) * Constants.movementSpeed; - position.x += Math.sin(ry) * Constants.movementSpeed; - break; - case RIGHT: - ry += RIGHT_ANGLE; - position.z -= Math.cos(ry) * Constants.movementSpeed; - position.x += Math.sin(ry) * Constants.movementSpeed; - break; - case UP: - if (this.noWeight) - position.y += Constants.movementSpeed; - else this.jump(); - break; - case DOWN: - position.y -= Constants.movementSpeed; - break; - } - } - - private static final float RIGHT_ANGLE = (float) (Math.PI / 2f); - - private void jump() - { - if (!isInAir) - { - isInAir = true; - this.upwardsSpeed = Constants.jumpPower; - } - } - - public int getChunkX() - { return this.chunkX; } - - public int getChunkY() - { return this.chunkY; } - - public int getChunkZ() - { return this.chunkZ; } - - public void updateMovement() - { - super.increasePosition(0, (float) (upwardsSpeed * (Window.getTime())), 0); - upwardsSpeed += Constants.gravity.y() * Window.getTime(); // TODO: Implement 3D gravity - isInAir = false; - upwardsSpeed = 0; - int newChunkX = (int) position.x >> POS_SHIFT; - int newChunkY = (int) position.y >> POS_SHIFT; - int newChunkZ = (int) position.z >> POS_SHIFT; - if (newChunkX != this.chunkX || newChunkY != this.chunkY || newChunkZ != this.chunkZ) - { - Litecraft.getInstance().getWorld().updateLoadedChunks(newChunkX, newChunkY, newChunkZ); - this.chunkX = newChunkX; - this.chunkY = newChunkY; - this.chunkZ = newChunkZ; - } - } -} diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/GLRenderManager.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/GLRenderManager.java index ed1a485..1c72ffd 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/GLRenderManager.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/GLRenderManager.java @@ -6,7 +6,6 @@ import java.util.*; import org.joml.*; import org.lwjgl.opengl.*; -import com.github.halotroop.litecraft.render.BlockRenderer; import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.common.elements.GuiTexture; @@ -32,7 +31,6 @@ public class GLRenderManager // GL11.glCullFace(GL11.GL_BACK); } - public BlockRenderer blockRenderer; private StaticShader entityShader; public GLObjectRenderer entityRenderer; private GuiShader guiShader; @@ -48,8 +46,7 @@ public class GLRenderManager { createProjectionMatrix(); entityShader = new StaticShader(); - blockRenderer = new BlockRenderer(entityShader, projectionMatrix); - entityRenderer = new GLObjectRenderer(entityShader, projectionMatrix); + entityRenderer = new GLObjectRenderer(getEntityShader(), projectionMatrix); guiShader = new GuiShader(); guiRenderer = new GLGuiRenderer(guiShader); normalRenderer = new GLNormalMappingRenderer(projectionMatrix); @@ -58,7 +55,7 @@ public class GLRenderManager public void cleanUp() { - entityShader.cleanUp(); + getEntityShader().cleanUp(); guiRenderer.cleanUp(); shadowMapRenderer.cleanUp(); normalRenderer.cleanUp(); @@ -130,12 +127,12 @@ public class GLRenderManager for (RenderObject entity : entities) { processEntity(entity); } entityRenderer.prepare(); - entityShader.start(); - entityShader.loadSkyColour(Window.getColour()); - entityShader.loadLights(lights); - entityShader.loadViewMatrix(camera); + getEntityShader().start(); + getEntityShader().loadSkyColour(Window.getColour()); + getEntityShader().loadLights(lights); + getEntityShader().loadViewMatrix(camera); entityRenderer.render(this.entities); - entityShader.stop(); + getEntityShader().stop(); this.entities.clear(); } @@ -172,4 +169,9 @@ public class GLRenderManager shadowMapRenderer.render(entities, sun); entities.clear(); } + + public StaticShader getEntityShader() + { + return entityShader; + } } diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GLObjectRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GLObjectRenderer.java index 6f3469b..3399278 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GLObjectRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GLObjectRenderer.java @@ -5,7 +5,6 @@ import java.util.*; import org.joml.Matrix4f; import org.lwjgl.opengl.*; -import com.github.halotroop.litecraft.types.block.BlockInstance; import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; import com.github.hydos.ginger.engine.common.io.Window; @@ -84,7 +83,7 @@ public class GLObjectRenderer extends Renderer GL30.glBindVertexArray(0); } - public void render(List renderList) + public void render(List renderList) { prepare(); shader.start(); diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java index a98aaf6..edc9fd0 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java @@ -6,7 +6,6 @@ import java.util.*; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.*; -import com.github.halotroop.litecraft.types.block.*; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; import com.github.hydos.ginger.engine.opengl.render.texture.Image; @@ -105,45 +104,6 @@ public class GLLoader public static int loadTexture(String path) { return loadTextureDirectly("/textures/" + path); } - public static int createBlockAtlas() - { - int width = 16; - int height = 16; - //Prepare the atlas texture and gen it - int atlasId = GL11.glGenTextures(); - //Bind it to openGL - GL11.glBindTexture(GL11.GL_TEXTURE_2D, atlasId); - //Apply the settings for the texture - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); - //Fill the image with blank image data - GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width * 2, height * 2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); - long maxX = Math.round(Math.sqrt(Blocks.blocks.size())); - int currentX = 0; - int currentY = 0; - for (Block block : Blocks.blocks) - { - //just in case - if (!block.texture.equals("DONTLOAD")) - { - System.out.println(block.texture); - block.updateBlockModelData(); - if (currentX > maxX) - { - currentX = 0; - currentY--; - } - GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, - currentX * width, currentY * height, - width, height, - GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, - block.model.getTexture().getTexture().getImage()); - currentX++; - } - } - return atlasId; - } - public static int loadTextureDirectly(String path) { int textureID = GL11.glGenTextures();