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
6 changed files with 68 additions and 72 deletions
Showing only changes of commit c5ca9f435e - Show all commits

View File

@ -124,7 +124,7 @@ public class Engine {
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("grass")));
Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("white")));
//Create Hexagon
HexagonModel hexagonModel = new HexagonModel();
@ -138,7 +138,8 @@ public class Engine {
MasterRenderer renderer = new MasterRenderer();
input(camera, renderer);
while ( !glfwWindowShouldClose(window) ) {
renderer.processEntity(hexagonEntity);
renderer.processTerrain(terrain);
//renderer.processEntity(hexagonEntity);
renderer.render(light, camera);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be

View File

@ -4,7 +4,7 @@ import utils.vectors.Vector3f;
public class Camera {
private Vector3f position = new Vector3f(0,2,0);
private Vector3f position = new Vector3f(0,37,-1);
private float pitch = 90;
private float yaw = 180;
private float roll;

View File

@ -31,19 +31,19 @@ public class TerrainRenderer {
for(TerrainTile terrain : terrains) {
prepareTerrain(terrain);
loadModelMatrix(terrain);
GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
unbindTexturedModel();
}
}
private void prepareTerrain(TerrainTile model) {
RawModel rawModel = model.getModel();
private void prepareTerrain(TerrainTile terrainTile) {
RawModel rawModel = terrainTile.getModel().getRawModel();
GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, terrainTile.getModel().getModelTexture().getTextureID());
}
private void unbindTexturedModel() {
@ -54,7 +54,8 @@ public class TerrainRenderer {
}
private void loadModelMatrix(TerrainTile terrain) {
Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1);
Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(terrain.getPosition(), terrain.getRotX(),
terrain.getRotY(), terrain.getRotZ(), terrain.getScale());
shader.loadTransformationMatrix(transformationMatrix);
}
}

View File

@ -17,5 +17,5 @@ void main(void) {
float brightness = max(nDotl, 0.2);
vec3 diffusde = brightness * lightColor;
out_Color = vec4(diffusde, 1.0) * texture(textureSampler, pass_textureCoords);
out_Color = texture(textureSampler, pass_textureCoords);
}

View File

@ -1,8 +1,11 @@
package core.engine.terrain;
import core.engine.Loader;
import core.engine.model.HexagonModel;
import core.engine.model.RawModel;
import core.engine.model.TexturedModel;
import core.engine.textures.ModelTexture;
import utils.vectors.Vector3f;
import java.util.ArrayList;
import java.util.List;
@ -12,9 +15,12 @@ public class Terrain {
private List<TerrainTile> terrainTiles = new ArrayList<>();
public Terrain(Loader loader, ModelTexture modelTexture) {
for(int i=0; i<32; i++) {
for(int j=0; j<32; j++) {
terrainTiles.add(new TerrainTile(i, j, loader, modelTexture));
HexagonModel hexagonModel = new HexagonModel();
RawModel rawModel = loader.loadHexagon(hexagonModel);
TexturedModel texturedModel = new TexturedModel(rawModel, modelTexture);
for(int j=22; j>0; j--) {
for(int i=-8; i<8; i++) {
terrainTiles.add(new TerrainTile(texturedModel, new Vector3f(j - 0.75f *2,0, i* (float)(0.866*2)), 0, 30, 0, 1));
}
}
}

View File

@ -4,81 +4,69 @@ import core.engine.Loader;
import core.engine.model.RawModel;
import core.engine.model.TexturedModel;
import core.engine.textures.ModelTexture;
import utils.vectors.Vector3f;
public class TerrainTile {
private static final float SIZE = 32;
private TexturedModel model;
private Vector3f position;
private float rotX, rotY, rotZ;
private float scale;
private static final int VERTEX_COUNT = 33;
private float x;
private float z;
private RawModel model;
private ModelTexture texture;
public TerrainTile(int gridX, int gridZ, Loader loader, ModelTexture texture) {
this.x = gridX;
this.z = gridZ;
this.texture = texture;
this.model = generateTerrain(loader);
public TerrainTile(TexturedModel 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;
}
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) * SIZE;
vertices[vertexPointer*3+1] = 0;
vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
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 RawModel getModel() {
public TexturedModel getModel() {
return model;
}
public ModelTexture getTexture() {
return texture;
public void setModel(TexturedModel model) {
this.model = model;
}
public float getX() {
return x;
public Vector3f getPosition() {
return position;
}
public float getZ() {
return z;
public void setPosition(Vector3f position) {
this.position = position;
}
public float getRotX() {
return rotX;
}
public void setRotX(float rotX) {
this.rotX = rotX;
}
// Getters and setters for the model, x, and z
public float getRotY() {
return rotY;
}
public void setRotY(float rotY) {
this.rotY = rotY;
}
public float getRotZ() {
return rotZ;
}
public void setRotZ(float rotZ) {
this.rotZ = rotZ;
}
public float getScale() {
return scale;
}
public void setScale(float scale) {
this.scale = scale;
}
}