finished game-engine-terrain-generation #2

Merged
sebastian merged 9 commits from game-engine-terrain-generation into game-engine 2023-10-05 14:00:29 +02:00
4 changed files with 52 additions and 9 deletions
Showing only changes of commit 354c5ce8be - Show all commits

View File

@ -119,12 +119,10 @@ public class Engine {
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
Camera camera = new Camera();
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("white")), 16, 32);
Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("white")), 16, 32, 7);
//Create Hexagon
HexagonModel hexagonModel = new HexagonModel();

View File

@ -9,28 +9,32 @@ import utils.vectors.Vector3f;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Terrain {
private List<TerrainTile> terrainTiles = new ArrayList<>();
public Terrain(Loader loader, ModelTexture modelTexture, int rows, int columns) {
public Terrain(Loader loader, ModelTexture modelTexture, int rows, int columns, int numberPlayers) {
TerrainGenerator terrainGenerator = new TerrainGenerator();
HexagonModel hexagonModel = new HexagonModel();
RawModel rawModel = loader.loadHexagon(hexagonModel);
TexturedModel texturedModel = new TexturedModel(rawModel, modelTexture);
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++) {
if(row % 2 == 1) {
terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(column * 2* 0.866f, 0, row * 1.5f),0,0,0,1));
terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(column * 2* 0.866f, 0, row * 1.5f),0,0,0,1, row, column, terrainGenerator.generateColor(column, row)));
} else {
terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(column *2 * 0.866f - 0.866f, 0, row * 1.5f),0,0,0,1));
terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(column *2 * 0.866f - 0.866f, 0, row * 1.5f),0,0,0,1, row, column, terrainGenerator.generateColor(column, row)));
}
}
}
}
public List<TerrainTile> getTerrainTiles() {
return terrainTiles;
}

View File

@ -0,0 +1,37 @@
package core.engine.terrain;
import utils.vectors.Vector3f;
import java.util.Random;
public class TerrainGenerator {
private final Random random = new Random();
private final int seed_red;
private final int seed_blue;
private final int seed_green;
public TerrainGenerator() {
this.seed_red = random.nextInt(1000000000);
this.seed_blue = random.nextInt(1000000000);
this.seed_green = random.nextInt(1000000000);
}
public Vector3f generateColor(int x, int z) {
return new Vector3f(getSmoothNoise(x, z, seed_red), getSmoothNoise(x, z, seed_green), getSmoothNoise(x, z, seed_blue));
}
private float getSmoothNoise(int x, int z, int seed) {
float corners = (getNoise(x -1, z-1, seed) + getNoise(x+1, z-1, seed) +
getNoise(x-1, z+1, seed) + getNoise(x+1, z+1, seed)) / 16f;
float sides = (getNoise(x-1, z, seed) + getNoise(x+1, z, seed) +
getNoise(x, z-1, seed) + getNoise(x, z+1, seed)) / 8f;
float center = getNoise(x, z, seed) / 4f;
return corners + sides + center;
}
private float getNoise(int x, int z, int seed) {
random.setSeed(x * 49632L + z * 325176L + seed);
return random.nextFloat();
}
}

View File

@ -12,17 +12,21 @@ public class TerrainTile {
private Vector3f position;
private float rotX, rotY, rotZ;
private float scale;
private int row;
private int column;
private Vector3f color;
public TerrainTile(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) {
public TerrainTile(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale, int row, int column, Vector3f color) {
this.model = model;
this.position = position;
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
this.scale = scale;
this.color = new Vector3f((float) Math.random(), (float) Math.random(), (float) Math.random());
this.row = row;
this.column = column;
this.color = color;
}
public TexturedModel getModel() {