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
5 changed files with 132 additions and 53 deletions
Showing only changes of commit 4932b3f6a0 - Show all commits

View File

@ -8,6 +8,7 @@ import core.engine.model.TexturedModel;
import core.engine.shader.StaticShader; import core.engine.shader.StaticShader;
import core.engine.shader.TerrainShader; import core.engine.shader.TerrainShader;
import core.engine.terrain.Terrain; import core.engine.terrain.Terrain;
import core.engine.terrain.TerrainTile;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import utils.vectors.Matrix4f; import utils.vectors.Matrix4f;
@ -27,7 +28,7 @@ public class MasterRenderer {
private EntityRenderer renderer; private EntityRenderer renderer;
private TerrainRenderer terrainRenderer; private TerrainRenderer terrainRenderer;
private Map<TexturedModel, List<Entity>> entities = new HashMap<>(); private Map<TexturedModel, List<Entity>> entities = new HashMap<>();
private List<Terrain> terrains = new ArrayList<>(); private List<TerrainTile> terrains = new ArrayList<>();
private Matrix4f projectionMatrix; private Matrix4f projectionMatrix;
public MasterRenderer() { public MasterRenderer() {
@ -62,7 +63,7 @@ public class MasterRenderer {
} }
public void processTerrain(Terrain terrain) { public void processTerrain(Terrain terrain) {
terrains.add(terrain); terrains.addAll(terrain.getTerrainTiles());
} }
public void processEntity(Entity entity) { public void processEntity(Entity entity) {

View File

@ -5,6 +5,7 @@ import core.engine.model.RawModel;
import core.engine.model.TexturedModel; import core.engine.model.TexturedModel;
import core.engine.shader.TerrainShader; import core.engine.shader.TerrainShader;
import core.engine.terrain.Terrain; import core.engine.terrain.Terrain;
import core.engine.terrain.TerrainTile;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL20;
@ -26,8 +27,8 @@ public class TerrainRenderer {
shader.stop(); shader.stop();
} }
public void render(List<Terrain> terrains) { public void render(List<TerrainTile> terrains) {
for(Terrain terrain : terrains) { for(TerrainTile terrain : terrains) {
prepareTerrain(terrain); prepareTerrain(terrain);
loadModelMatrix(terrain); loadModelMatrix(terrain);
GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); 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(); RawModel rawModel = model.getModel();
GL30.glBindVertexArray(rawModel.getVaoID()); GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(0);
@ -52,7 +53,7 @@ public class TerrainRenderer {
GL30.glBindVertexArray(0); 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); Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1);
shader.loadTransformationMatrix(transformationMatrix); shader.loadTransformationMatrix(transformationMatrix);
} }

View File

@ -4,60 +4,30 @@ import core.engine.Loader;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.textures.ModelTexture; import core.engine.textures.ModelTexture;
import java.util.ArrayList;
import java.util.List;
public class Terrain { public class Terrain {
private static final float WIDTH = 100; private static final float SIZE = 100;
private static final float DEPTH = 100;
private static final int VERTEX_COUNT = 128; private static final int VERTEX_COUNT = 128;
private float x; private float x;
private float z; 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) { public Terrain(int gridX, int gridZ, Loader loader, ModelTexture modelTexture) {
this.texture = modelTexture; this.x = gridX * SIZE;
this.x = gridX * WIDTH; this.z = gridZ * SIZE;
this.z = gridZ * DEPTH;
this.model = generateTerrain(loader);
}
private RawModel generateTerrain(Loader loader){ for (int row = 0; row < VERTEX_COUNT - 1; row++) {
int count = VERTEX_COUNT * VERTEX_COUNT; for (int column = 0; column < VERTEX_COUNT - 1; column++) {
float[] vertices = new float[count * 3]; TerrainTile terrainTile = new TerrainTile(loader, modelTexture, row, column, VERTEX_COUNT, SIZE);
float[] normals = new float[count * 3]; terrainTiles.add(terrainTile);
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++;
} }
} }
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() { public float getX() {
@ -68,11 +38,11 @@ public class Terrain {
return z; return z;
} }
public RawModel getModel() { public List<TerrainTile> getTerrainTiles() {
return model; return terrainTiles;
} }
public ModelTexture getTexture() { public void setTerrainTiles(List<TerrainTile> terrainTiles) {
return texture; this.terrainTiles = terrainTiles;
} }
} }

View File

@ -0,0 +1,5 @@
package core.engine.terrain;
public class TerrainGenerator {
}

View 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;
}
}