finished game-engine-terrain-generation #2
@ -119,12 +119,10 @@ public class Engine {
|
|||||||
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
||||||
Camera camera = new Camera();
|
Camera camera = new Camera();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1));
|
Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1));
|
||||||
|
|
||||||
//Generate Simple, Flat Terrain
|
//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
|
//Create Hexagon
|
||||||
HexagonModel hexagonModel = new HexagonModel();
|
HexagonModel hexagonModel = new HexagonModel();
|
||||||
|
@ -9,28 +9,32 @@ import utils.vectors.Vector3f;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Terrain {
|
public class Terrain {
|
||||||
|
|
||||||
private List<TerrainTile> terrainTiles = new ArrayList<>();
|
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();
|
HexagonModel hexagonModel = new HexagonModel();
|
||||||
RawModel rawModel = loader.loadHexagon(hexagonModel);
|
RawModel rawModel = loader.loadHexagon(hexagonModel);
|
||||||
TexturedModel texturedModel = new TexturedModel(rawModel, modelTexture);
|
TexturedModel texturedModel = new TexturedModel(rawModel, modelTexture);
|
||||||
for(int row = 0; row < rows; row++) {
|
for(int row = 0; row < rows; row++) {
|
||||||
for(int column = 0; column < columns; column++) {
|
for(int column = 0; column < columns; column++) {
|
||||||
if(row % 2 == 1) {
|
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 {
|
} 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() {
|
public List<TerrainTile> getTerrainTiles() {
|
||||||
return terrainTiles;
|
return terrainTiles;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -12,17 +12,21 @@ public class TerrainTile {
|
|||||||
private Vector3f position;
|
private Vector3f position;
|
||||||
private float rotX, rotY, rotZ;
|
private float rotX, rotY, rotZ;
|
||||||
private float scale;
|
private float scale;
|
||||||
|
private int row;
|
||||||
|
private int column;
|
||||||
|
|
||||||
private Vector3f color;
|
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.model = model;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.rotX = rotX;
|
this.rotX = rotX;
|
||||||
this.rotY = rotY;
|
this.rotY = rotY;
|
||||||
this.rotZ = rotZ;
|
this.rotZ = rotZ;
|
||||||
this.scale = scale;
|
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() {
|
public TexturedModel getModel() {
|
||||||
|
Loading…
Reference in New Issue
Block a user