Render TerrainTiles with nearest color (doesn't work entirly yet)
This commit is contained in:
parent
2464231d41
commit
209c38e367
@ -37,6 +37,8 @@ public class Engine {
|
||||
private static DoubleBuffer lastCursorX = BufferUtils.createDoubleBuffer(1);
|
||||
private static DoubleBuffer lastCursorY = BufferUtils.createDoubleBuffer(1);
|
||||
|
||||
private Terrain terrain;
|
||||
|
||||
|
||||
public Engine() {
|
||||
|
||||
@ -116,9 +118,9 @@ public class Engine {
|
||||
GL.createCapabilities();
|
||||
|
||||
// Set the clear color
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClearColor(1f, 1f, 1f, 1f);
|
||||
Camera camera = new Camera();
|
||||
Terrain terrain = Terrain.generateTerrain(loader);
|
||||
terrain = Terrain.generateTerrain(loader);
|
||||
|
||||
|
||||
Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1));
|
||||
@ -234,6 +236,10 @@ public class Engine {
|
||||
if(key == GLFW_KEY_Y && action == GLFW_PRESS) {
|
||||
masterRenderer.switchWireframe();
|
||||
}
|
||||
|
||||
if(key == GLFW_KEY_G && action == GLFW_PRESS) {
|
||||
terrain = Terrain.generateTerrain(loader);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -45,7 +45,7 @@ public class MasterRenderer {
|
||||
public void prepare() {
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
||||
GL11.glClearColor(0.49f, 89f, 0.98f, 1);
|
||||
GL11.glClearColor(1f, 1f, 1f, 1);
|
||||
}
|
||||
|
||||
public void render(Light light, Camera camera) {
|
||||
|
@ -9,12 +9,21 @@ import core.engine.textures.ModelTexture;
|
||||
import utils.vectors.Vector3f;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Terrain {
|
||||
|
||||
|
||||
private final List<TerrainTile> terrainTiles;
|
||||
private static Vector3f colors[] = new Vector3f[]{
|
||||
new Vector3f(1f, 0f, 0.86f),
|
||||
new Vector3f( 0.05f, 0.3f, 0f),
|
||||
//new Vector3f(1f, 0.5f, 0),
|
||||
//new Vector3f(0, 0.84f, 1f),
|
||||
//new Vector3f(1, 1, 0)
|
||||
};
|
||||
|
||||
private Terrain(List<TerrainTile> terrainTiles) {
|
||||
this.terrainTiles = terrainTiles;
|
||||
@ -26,23 +35,74 @@ public class Terrain {
|
||||
ModelTexture hexagonTexture = new ModelTexture(loader.loadTexture("white"));
|
||||
TexturedModel hexagontexturedModel = new TexturedModel(hexagonRawModel, hexagonTexture);
|
||||
|
||||
List<TerrainTile> generatedTerrainTiles = new ArrayList<>();
|
||||
ArrayList<TerrainTile> generatedTerrainTiles = new ArrayList<>();
|
||||
TerrainTile[][] terrainTilesGrid = new TerrainTile[18][];
|
||||
|
||||
for(int y = -9; y<9; y++) {
|
||||
for(int x=-16; x<16; x++) {
|
||||
float xPos = x * 1.7319988f;
|
||||
if(y % 2 == 0) {
|
||||
xPos -= 0.866f;
|
||||
double keepSample = Math.random();
|
||||
if(keepSample > 0.1) {
|
||||
float xPos = x * 1.7319988f;
|
||||
if(y % 2 == 0) {
|
||||
xPos -= 0.866f;
|
||||
}
|
||||
|
||||
TerrainTile terrainTile = new TerrainTile(hexagonRawModel, new Vector3f(xPos , 0, y * -1.4999994f), 0, 0, 0,1);
|
||||
generatedTerrainTiles.add(terrainTile);
|
||||
}
|
||||
Vector3f randomColor = new Vector3f((float) Math.random(), (float) Math.random(), (float) Math.random());
|
||||
generatedTerrainTiles.add(new TerrainTile(hexagonRawModel, new Vector3f(xPos , 0, y * -1.4999994f), 0, 0, 0,1, randomColor));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(31 * 1.7319988f);
|
||||
List<TerrainTile> selectedTerrainTiles = new ArrayList<>();
|
||||
List<Integer> usedIndices = new ArrayList<>();
|
||||
for(int i=0; i<colors.length; i++) {
|
||||
int randomIndex = (int) (Math.random() * generatedTerrainTiles.size());
|
||||
|
||||
while(usedIndices.contains(randomIndex)) {
|
||||
randomIndex = (int) (Math.random() * generatedTerrainTiles.size());
|
||||
}
|
||||
|
||||
usedIndices.add(randomIndex);
|
||||
generatedTerrainTiles.get(i).setColor(colors[i]);
|
||||
selectedTerrainTiles.add(generatedTerrainTiles.get(i));
|
||||
}
|
||||
|
||||
System.out.println(usedIndices);
|
||||
|
||||
for(int i=0; i<generatedTerrainTiles.size(); i++) {
|
||||
TerrainTile nearestTerrainTile = Terrain.getNearestTerrainTile(selectedTerrainTiles, generatedTerrainTiles.get(i));
|
||||
generatedTerrainTiles.get(i).setColor(nearestTerrainTile.getColor());
|
||||
}
|
||||
|
||||
for(int i=0; i<generatedTerrainTiles.size(); i++) {
|
||||
if(generatedTerrainTiles.get(i).getColor().length() == 0) {
|
||||
System.out.println("Null");
|
||||
}
|
||||
}
|
||||
return new Terrain(generatedTerrainTiles);
|
||||
}
|
||||
|
||||
private static TerrainTile getNearestTerrainTile(List<TerrainTile> terrainTiles, TerrainTile currentTerrainTile) {
|
||||
TerrainTile nearestTerrainTile = null;
|
||||
float nearestDistance = Float.MAX_VALUE;
|
||||
for(TerrainTile terrainTile : terrainTiles) {
|
||||
if(nearestTerrainTile == null) {
|
||||
nearestTerrainTile = terrainTile;
|
||||
nearestDistance = Vector3f.sub(terrainTile.getPosition(), currentTerrainTile.getPosition(), null).length();
|
||||
} else {
|
||||
float distance = Vector3f.sub(terrainTile.getPosition(), currentTerrainTile.getPosition(), null).length();
|
||||
if(distance < nearestDistance) {
|
||||
nearestDistance = distance;
|
||||
nearestTerrainTile = terrainTile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(nearestDistance);
|
||||
|
||||
return nearestTerrainTile;
|
||||
}
|
||||
|
||||
public List<TerrainTile> getTerrainTiles() {
|
||||
return terrainTiles;
|
||||
}
|
||||
|
@ -6,21 +6,26 @@ import core.engine.model.TexturedModel;
|
||||
import core.engine.textures.ModelTexture;
|
||||
import utils.vectors.Vector3f;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class TerrainTile {
|
||||
|
||||
private RawModel model;
|
||||
private final Vector3f position;
|
||||
private final float rotX, rotY, rotZ;
|
||||
private final float scale;
|
||||
private Vector3f color;
|
||||
private Vector3f color = new Vector3f();
|
||||
|
||||
public TerrainTile(RawModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale, Vector3f color) {
|
||||
public TerrainTile(RawModel 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;
|
||||
}
|
||||
|
||||
public void setColor(Vector3f color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@ -51,4 +56,17 @@ public class TerrainTile {
|
||||
public Vector3f getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
TerrainTile that = (TerrainTile) o;
|
||||
return Objects.equals(model, that.model) && Objects.equals(position, that.position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(model, position);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user