finished game-engine-terrain-generation #2
@ -8,6 +8,7 @@ import core.engine.model.TexturedModel;
|
||||
import core.engine.shader.StaticShader;
|
||||
import core.engine.shader.TerrainShader;
|
||||
import core.engine.terrain.Terrain;
|
||||
import core.engine.terrain.TerrainTile;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import utils.vectors.Matrix4f;
|
||||
|
||||
@ -27,7 +28,7 @@ public class MasterRenderer {
|
||||
private EntityRenderer renderer;
|
||||
private TerrainRenderer terrainRenderer;
|
||||
private Map<TexturedModel, List<Entity>> entities = new HashMap<>();
|
||||
private List<Terrain> terrains = new ArrayList<>();
|
||||
private List<TerrainTile> terrains = new ArrayList<>();
|
||||
private Matrix4f projectionMatrix;
|
||||
|
||||
public MasterRenderer() {
|
||||
@ -62,7 +63,7 @@ public class MasterRenderer {
|
||||
}
|
||||
|
||||
public void processTerrain(Terrain terrain) {
|
||||
terrains.add(terrain);
|
||||
terrains.addAll(terrain.getTerrainTiles());
|
||||
}
|
||||
|
||||
public void processEntity(Entity entity) {
|
||||
|
@ -5,6 +5,7 @@ import core.engine.model.RawModel;
|
||||
import core.engine.model.TexturedModel;
|
||||
import core.engine.shader.TerrainShader;
|
||||
import core.engine.terrain.Terrain;
|
||||
import core.engine.terrain.TerrainTile;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
@ -26,8 +27,8 @@ public class TerrainRenderer {
|
||||
shader.stop();
|
||||
}
|
||||
|
||||
public void render(List<Terrain> terrains) {
|
||||
for(Terrain terrain : terrains) {
|
||||
public void render(List<TerrainTile> terrains) {
|
||||
for(TerrainTile terrain : terrains) {
|
||||
prepareTerrain(terrain);
|
||||
loadModelMatrix(terrain);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
@ -35,7 +36,7 @@ public class TerrainRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareTerrain(Terrain model) {
|
||||
private void prepareTerrain(TerrainTile model) {
|
||||
RawModel rawModel = model.getModel();
|
||||
GL30.glBindVertexArray(rawModel.getVaoID());
|
||||
GL20.glEnableVertexAttribArray(0);
|
||||
@ -52,7 +53,7 @@ public class TerrainRenderer {
|
||||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
private void loadModelMatrix(Terrain terrain) {
|
||||
private void loadModelMatrix(TerrainTile terrain) {
|
||||
Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1);
|
||||
shader.loadTransformationMatrix(transformationMatrix);
|
||||
}
|
||||
|
@ -4,60 +4,30 @@ import core.engine.Loader;
|
||||
import core.engine.model.RawModel;
|
||||
import core.engine.textures.ModelTexture;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Terrain {
|
||||
|
||||
private static final float WIDTH = 100;
|
||||
private static final float DEPTH = 100;
|
||||
private static final float SIZE = 100;
|
||||
|
||||
private static final int VERTEX_COUNT = 128;
|
||||
|
||||
private float x;
|
||||
private float z;
|
||||
private RawModel model;
|
||||
private ModelTexture texture;
|
||||
|
||||
private List<TerrainTile> terrainTiles = new ArrayList<>();
|
||||
|
||||
public Terrain(int gridX, int gridZ, Loader loader, ModelTexture modelTexture) {
|
||||
this.texture = modelTexture;
|
||||
this.x = gridX * WIDTH;
|
||||
this.z = gridZ * DEPTH;
|
||||
this.model = generateTerrain(loader);
|
||||
}
|
||||
this.x = gridX * SIZE;
|
||||
this.z = gridZ * SIZE;
|
||||
|
||||
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<VERTEX_COUNT;i++){
|
||||
for(int j=0;j<VERTEX_COUNT;j++){
|
||||
vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * WIDTH;
|
||||
vertices[vertexPointer*3+1] = 0;
|
||||
vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * DEPTH;
|
||||
normals[vertexPointer*3] = 0;
|
||||
normals[vertexPointer*3+1] = 1;
|
||||
normals[vertexPointer*3+2] = 0;
|
||||
textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1);
|
||||
textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1);
|
||||
vertexPointer++;
|
||||
for (int row = 0; row < VERTEX_COUNT - 1; row++) {
|
||||
for (int column = 0; column < VERTEX_COUNT - 1; column++) {
|
||||
TerrainTile terrainTile = new TerrainTile(loader, modelTexture, row, column, VERTEX_COUNT, SIZE);
|
||||
terrainTiles.add(terrainTile);
|
||||
}
|
||||
}
|
||||
int pointer = 0;
|
||||
for(int gz=0;gz<VERTEX_COUNT-1;gz++){
|
||||
for(int gx=0;gx<VERTEX_COUNT-1;gx++){
|
||||
int topLeft = (gz*VERTEX_COUNT)+gx;
|
||||
int topRight = topLeft + 1;
|
||||
int bottomLeft = ((gz+1)*VERTEX_COUNT)+gx;
|
||||
int bottomRight = bottomLeft + 1;
|
||||
indices[pointer++] = topLeft;
|
||||
indices[pointer++] = bottomLeft;
|
||||
indices[pointer++] = topRight;
|
||||
indices[pointer++] = topRight;
|
||||
indices[pointer++] = bottomLeft;
|
||||
indices[pointer++] = bottomRight;
|
||||
}
|
||||
}
|
||||
return loader.loadToVAO(vertices, textureCoords, normals, indices);
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
@ -68,11 +38,11 @@ public class Terrain {
|
||||
return z;
|
||||
}
|
||||
|
||||
public RawModel getModel() {
|
||||
return model;
|
||||
public List<TerrainTile> getTerrainTiles() {
|
||||
return terrainTiles;
|
||||
}
|
||||
|
||||
public ModelTexture getTexture() {
|
||||
return texture;
|
||||
public void setTerrainTiles(List<TerrainTile> terrainTiles) {
|
||||
this.terrainTiles = terrainTiles;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
package core.engine.terrain;
|
||||
|
||||
public class TerrainGenerator {
|
||||
|
||||
}
|
102
client/src/main/java/core/engine/terrain/TerrainTile.java
Normal file
102
client/src/main/java/core/engine/terrain/TerrainTile.java
Normal file
@ -0,0 +1,102 @@
|
||||
package core.engine.terrain;
|
||||
|
||||
import core.engine.Loader;
|
||||
import core.engine.model.RawModel;
|
||||
import core.engine.textures.ModelTexture;
|
||||
|
||||
public class TerrainTile {
|
||||
|
||||
private RawModel model;
|
||||
private ModelTexture texture;
|
||||
|
||||
private float x;
|
||||
private float z;
|
||||
|
||||
public TerrainTile(Loader loader, ModelTexture texture, int row, int column, int VERTEX_COUNT, float size) {
|
||||
float[] vertices = new float[12]; // 4 vertices * 3 coordinates
|
||||
float[] normals = new float[12]; // 4 normals * 3 coordinates
|
||||
float[] textureCoords = new float[8]; // 4 texture coords * 2 coordinates
|
||||
int[] indices = new int[6]; // 2 triangles * 3 indices
|
||||
|
||||
//Calculate positions for the four vertices of this terrain tile
|
||||
float x0 = (float) column / (VERTEX_COUNT - 1) * size;
|
||||
float x1 = (float) (column + 1) / (VERTEX_COUNT - 1) * size;
|
||||
float z0 = (float) row / (VERTEX_COUNT - 1) * size;
|
||||
float z1 = (float) (row + 1) / (VERTEX_COUNT - 1) * size;
|
||||
|
||||
// Populate vertices, normals, and textureCoords arrays based on the calculated positions
|
||||
//Vertices
|
||||
// Vertex 0
|
||||
vertices[0] = x0;
|
||||
vertices[1] = 0;
|
||||
vertices[2] = z0;
|
||||
|
||||
// Vertex 1
|
||||
vertices[3] = x0;
|
||||
vertices[4] = 0;
|
||||
vertices[5] = z1;
|
||||
|
||||
// Vertex 2
|
||||
vertices[6] = x1;
|
||||
vertices[7] = 0;
|
||||
vertices[8] = z0;
|
||||
|
||||
// Vertex 3
|
||||
vertices[9] = x1;
|
||||
vertices[10] = 0;
|
||||
vertices[11] = z1;
|
||||
|
||||
//Normals
|
||||
for(int i=0; i<normals.length/3; i++) {
|
||||
normals[i*3] = 0;
|
||||
normals[i*3+1] = 1;
|
||||
normals[i*3+2] = 2;
|
||||
}
|
||||
|
||||
// Define indices for the triangles
|
||||
indices[0] = 0;
|
||||
indices[1] = 1;
|
||||
indices[2] = 2;
|
||||
indices[3] = 2;
|
||||
indices[4] = 1;
|
||||
indices[5] = 3;
|
||||
|
||||
this.model = loader.loadToVAO(vertices, textureCoords, normals, indices);
|
||||
this.texture = texture;
|
||||
|
||||
this.x = x0;
|
||||
this.z = z0;
|
||||
}
|
||||
|
||||
public RawModel getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(RawModel model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public ModelTexture getTexture() {
|
||||
return texture;
|
||||
}
|
||||
|
||||
public void setTexture(ModelTexture texture) {
|
||||
this.texture = texture;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setZ(float z) {
|
||||
this.z = z;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user