From c5ca9f435e0077eaf1f2d301dcac8e20226376d5 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 4 Oct 2023 09:55:09 +0200 Subject: [PATCH] Wierd looking terrain --- client/src/main/java/core/engine/Engine.java | 5 +- .../main/java/core/engine/entity/Camera.java | 2 +- .../core/engine/renderer/TerrainRenderer.java | 11 +- .../engine/shader/terrainFragmentShader.glsl | 2 +- .../java/core/engine/terrain/Terrain.java | 12 +- .../java/core/engine/terrain/TerrainTile.java | 108 ++++++++---------- 6 files changed, 68 insertions(+), 72 deletions(-) diff --git a/client/src/main/java/core/engine/Engine.java b/client/src/main/java/core/engine/Engine.java index 64a6e07..ece4d85 100644 --- a/client/src/main/java/core/engine/Engine.java +++ b/client/src/main/java/core/engine/Engine.java @@ -124,7 +124,7 @@ public class Engine { Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1)); //Generate Simple, Flat Terrain - Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("grass"))); + Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("white"))); //Create Hexagon HexagonModel hexagonModel = new HexagonModel(); @@ -138,7 +138,8 @@ public class Engine { MasterRenderer renderer = new MasterRenderer(); input(camera, renderer); while ( !glfwWindowShouldClose(window) ) { - renderer.processEntity(hexagonEntity); + renderer.processTerrain(terrain); + //renderer.processEntity(hexagonEntity); renderer.render(light, camera); glfwSwapBuffers(window); // swap the color buffers // Poll for window events. The key callback above will only be diff --git a/client/src/main/java/core/engine/entity/Camera.java b/client/src/main/java/core/engine/entity/Camera.java index f853f22..2399148 100644 --- a/client/src/main/java/core/engine/entity/Camera.java +++ b/client/src/main/java/core/engine/entity/Camera.java @@ -4,7 +4,7 @@ import utils.vectors.Vector3f; public class Camera { - private Vector3f position = new Vector3f(0,2,0); + private Vector3f position = new Vector3f(0,37,-1); private float pitch = 90; private float yaw = 180; private float roll; diff --git a/client/src/main/java/core/engine/renderer/TerrainRenderer.java b/client/src/main/java/core/engine/renderer/TerrainRenderer.java index 02debc6..4cdac2b 100644 --- a/client/src/main/java/core/engine/renderer/TerrainRenderer.java +++ b/client/src/main/java/core/engine/renderer/TerrainRenderer.java @@ -31,19 +31,19 @@ public class TerrainRenderer { for(TerrainTile terrain : terrains) { prepareTerrain(terrain); loadModelMatrix(terrain); - GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); + GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); unbindTexturedModel(); } } - private void prepareTerrain(TerrainTile model) { - RawModel rawModel = model.getModel(); + private void prepareTerrain(TerrainTile terrainTile) { + RawModel rawModel = terrainTile.getModel().getRawModel(); GL30.glBindVertexArray(rawModel.getVaoID()); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); GL13.glActiveTexture(GL13.GL_TEXTURE0); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID()); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, terrainTile.getModel().getModelTexture().getTextureID()); } private void unbindTexturedModel() { @@ -54,7 +54,8 @@ public class TerrainRenderer { } private void loadModelMatrix(TerrainTile terrain) { - Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1); + Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(terrain.getPosition(), terrain.getRotX(), + terrain.getRotY(), terrain.getRotZ(), terrain.getScale()); shader.loadTransformationMatrix(transformationMatrix); } } diff --git a/client/src/main/java/core/engine/shader/terrainFragmentShader.glsl b/client/src/main/java/core/engine/shader/terrainFragmentShader.glsl index bd7f6d4..45026bc 100644 --- a/client/src/main/java/core/engine/shader/terrainFragmentShader.glsl +++ b/client/src/main/java/core/engine/shader/terrainFragmentShader.glsl @@ -17,5 +17,5 @@ void main(void) { float brightness = max(nDotl, 0.2); vec3 diffusde = brightness * lightColor; - out_Color = vec4(diffusde, 1.0) * texture(textureSampler, pass_textureCoords); + out_Color = texture(textureSampler, pass_textureCoords); } \ No newline at end of file diff --git a/client/src/main/java/core/engine/terrain/Terrain.java b/client/src/main/java/core/engine/terrain/Terrain.java index f04a728..1d21b4b 100644 --- a/client/src/main/java/core/engine/terrain/Terrain.java +++ b/client/src/main/java/core/engine/terrain/Terrain.java @@ -1,8 +1,11 @@ package core.engine.terrain; import core.engine.Loader; +import core.engine.model.HexagonModel; import core.engine.model.RawModel; +import core.engine.model.TexturedModel; import core.engine.textures.ModelTexture; +import utils.vectors.Vector3f; import java.util.ArrayList; import java.util.List; @@ -12,9 +15,12 @@ public class Terrain { private List terrainTiles = new ArrayList<>(); public Terrain(Loader loader, ModelTexture modelTexture) { - for(int i=0; i<32; i++) { - for(int j=0; j<32; j++) { - terrainTiles.add(new TerrainTile(i, j, loader, modelTexture)); + HexagonModel hexagonModel = new HexagonModel(); + RawModel rawModel = loader.loadHexagon(hexagonModel); + TexturedModel texturedModel = new TexturedModel(rawModel, modelTexture); + for(int j=22; j>0; j--) { + for(int i=-8; i<8; i++) { + terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(j - 0.75f *2,0, i* (float)(0.866*2)), 0, 30, 0, 1)); } } } diff --git a/client/src/main/java/core/engine/terrain/TerrainTile.java b/client/src/main/java/core/engine/terrain/TerrainTile.java index 18bb0de..42c9c65 100644 --- a/client/src/main/java/core/engine/terrain/TerrainTile.java +++ b/client/src/main/java/core/engine/terrain/TerrainTile.java @@ -4,81 +4,69 @@ import core.engine.Loader; import core.engine.model.RawModel; import core.engine.model.TexturedModel; import core.engine.textures.ModelTexture; +import utils.vectors.Vector3f; public class TerrainTile { - private static final float SIZE = 32; + private TexturedModel model; + private Vector3f position; + private float rotX, rotY, rotZ; + private float scale; - private static final int VERTEX_COUNT = 33; - - private float x; - private float z; - private RawModel model; - private ModelTexture texture; - - public TerrainTile(int gridX, int gridZ, Loader loader, ModelTexture texture) { - this.x = gridX; - this.z = gridZ; - this.texture = texture; - this.model = generateTerrain(loader); + public TerrainTile(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) { + this.model = model; + this.position = position; + this.rotX = rotX; + this.rotY = rotY; + this.rotZ = rotZ; + this.scale = scale; } - private RawModel generateTerrain(Loader loader) { - int count = VERTEX_COUNT * VERTEX_COUNT; - float[] vertices = new float[count * 3]; - float[] normals = new float[count * 3]; - float[] textureCoords = new float[count*2]; - int[] indices = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)]; - int vertexPointer = 0; - for(int i=0;i