Render Single HexagonModel as TerrainTile

This commit is contained in:
Sebastian Böckelmann 2024-03-21 16:28:50 +01:00
parent 466a15f160
commit ac3e0a1b0e
12 changed files with 201793 additions and 129 deletions

200007
client/res/dragon.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
client/res/stall.blend Normal file

Binary file not shown.

1696
client/res/stall.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
client/res/stallTexture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

View File

@ -118,34 +118,17 @@ public class Engine {
// Set the clear color // Set the clear color
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();
Terrain terrain = Terrain.generateTerrain(loader);
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));
RawModel model = OBJLoader.loadOBJModel("dragon", loader);
ModelTexture modelTexture = new ModelTexture(loader.loadTexture("white"));
TexturedModel texturedModel = new TexturedModel(model, modelTexture);
Entity entity = new Entity(texturedModel, new Vector3f(0,0,-50), 0,0,0,1);
//Generate Simple, Flat Terrain
Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("grass")));
//Create Hexagon
HexagonModel hexagonModel = new HexagonModel();
RawModel hexagonRawModel = loader.loadToVAO(hexagonModel.getVertices(), hexagonModel.getTextureCoords(), hexagonModel.getNormals(), hexagonModel.getIndices());
ModelTexture hexagonTexture = new ModelTexture(loader.loadTexture("white"));
TexturedModel hexagontexturedModel = new TexturedModel(hexagonRawModel, hexagonTexture);
Entity hexagonEntity = new Entity(hexagontexturedModel, new Vector3f(0,1,0), 0,0,0,1);
// Run the rendering loop until the user has attempted to close // Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key. // the window or has pressed the ESCAPE key.
MasterRenderer renderer = new MasterRenderer(); MasterRenderer renderer = new MasterRenderer();
input(camera, renderer); input(camera, renderer);
while ( !glfwWindowShouldClose(window) ) { while ( !glfwWindowShouldClose(window) ) {
renderer.processEntity(hexagonEntity); renderer.processTerrain(terrain);
//entity.increaseRotation(0,1,0);
//renderer.processTerrain(terrain);
//renderer.processEntity(entity);
renderer.render(light, camera); renderer.render(light, camera);
glfwSwapBuffers(window); // swap the color buffers glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be // Poll for window events. The key callback above will only be
@ -221,7 +204,7 @@ public class Engine {
public void invoke(long window, int key, int scancode, int action, int mods) { public void invoke(long window, int key, int scancode, int action, int mods) {
if(key == GLFW_KEY_A) { if(key == GLFW_KEY_A) {
//move camera to left //move camera to left
camera.moveLeft();
} }
if(key == GLFW_KEY_D) { if(key == GLFW_KEY_D) {

View File

@ -10,43 +10,43 @@ public class Camera {
private float roll; private float roll;
public void moveLeft() { public void moveLeft() {
position.x += 0.5f; position.x += 0.25f;
} }
public void moveForward() { public void moveForward() {
position.z -= .5f; position.z -= .25f;
} }
public void moveBackward() { public void moveBackward() {
position.z += .5f; position.z += .25f;
} }
public void moveRight() { public void moveRight() {
position.x -= .5f; position.x -= .25f;
} }
public void zoomOut() { public void zoomOut() {
position.y += 0.5f; position.y += 0.25f;
} }
public void zoomIn() { public void zoomIn() {
position.y -= 0.5f; position.y -= 0.25f;
} }
public void increasePitch() { public void increasePitch() {
this.pitch += .5f; this.pitch += .25f;
} }
public void decreasePitch() { public void decreasePitch() {
this.pitch -= .5f; this.pitch -= .25f;
} }
public void increaseYaw() { public void increaseYaw() {
this.yaw += 0.5f; this.yaw += 0.25f;
} }
public void decreaseYaw() { public void decreaseYaw() {
this.yaw -= 0.5f; this.yaw -= 0.25f;
} }
public Vector3f getPosition() { public Vector3f getPosition() {

View File

@ -58,31 +58,18 @@ public class HexagonModel {
return vertices; return vertices;
} }
public void setVertices(float[] vertices) {
this.vertices = vertices;
}
public float[] getNormals() { public float[] getNormals() {
return normals; return normals;
} }
public void setNormals(float[] normals) {
this.normals = normals;
}
public float[] getTextureCoords() { public float[] getTextureCoords() {
return textureCoords; return textureCoords;
} }
public void setTextureCoords(float[] textureCoords) {
this.textureCoords = textureCoords;
}
public int[] getIndices() { public int[] getIndices() {
return indices; return indices;
} }
public void setIndices(int[] indices) { /*public float[] moveVertices(float dx, float dz) {
this.indices = indices;
} }*/
} }

View File

@ -65,9 +65,12 @@ public class MasterRenderer {
} }
public void processTerrain(Terrain terrain) { public void processTerrain(Terrain terrain) {
terrains.addAll(terrain.getTerrainTiles()); for(TerrainTile terrainTile : terrain.getTerrainTiles()) {
terrains.add(terrainTile);
}
} }
public void processEntity(Entity entity) { public void processEntity(Entity entity) {
TexturedModel entityModel = entity.getModel(); TexturedModel entityModel = entity.getModel();
List<Entity> batch = entities.get(entityModel); List<Entity> batch = entities.get(entityModel);

View File

@ -3,6 +3,7 @@ package core.engine.renderer;
import core.engine.entity.Entity; import core.engine.entity.Entity;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.model.TexturedModel; import core.engine.model.TexturedModel;
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 core.engine.terrain.TerrainTile;
@ -15,35 +16,43 @@ import utils.vectors.Matrix4f;
import utils.vectors.Vector3f; import utils.vectors.Vector3f;
import java.util.List; import java.util.List;
import java.util.Map;
public class TerrainRenderer { public class TerrainRenderer {
private TerrainShader shader; private TerrainShader shader;
private static final float FOV = 70;
private static final float NEAR_PLEANE = 0.1f;
private static final float FAR_PLANE = 1000;
public TerrainRenderer(TerrainShader shader, Matrix4f projectionMatrix) { public TerrainRenderer(TerrainShader shader, Matrix4f projectionMatrix) {
this.shader = shader; this.shader = shader;
shader.start(); shader.start();
shader.loadProjectionMatrix(projectionMatrix); shader.loadProjectionMatrix(projectionMatrix);
shader.stop(); shader.stop();
} }
public void render(List<TerrainTile> terrains) {
for(TerrainTile terrain : terrains) {
prepareTerrain(terrain); public void render(List<TerrainTile> terrainTiles) {
loadModelMatrix(terrain); for(TerrainTile terrainTile : terrainTiles) {
GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); prepareTexturedModel(terrainTile.getModel());
unbindTexturedModel(); prepareInstance(terrainTile);
} GL11.glDrawElements(GL11.GL_TRIANGLES, terrainTile.getModel().getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
}
unbindTexturedModel();
} }
private void prepareTerrain(TerrainTile model) { private void prepareTexturedModel(TexturedModel model) {
RawModel rawModel = model.getModel(); RawModel rawModel = model.getRawModel();
GL30.glBindVertexArray(rawModel.getVaoID()); GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2); GL20.glEnableVertexAttribArray(2);
GL13.glActiveTexture(GL13.GL_TEXTURE0); GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID()); GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getModelTexture().getTextureID());
} }
private void unbindTexturedModel() { private void unbindTexturedModel() {
@ -53,8 +62,10 @@ public class TerrainRenderer {
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }
private void loadModelMatrix(TerrainTile terrain) { private void prepareInstance(TerrainTile entity) {
Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1); Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix); shader.loadTransformationMatrix(transformationMatrix);
} }
} }

View File

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

View File

@ -1,8 +1,12 @@
package core.engine.terrain; package core.engine.terrain;
import core.engine.Loader; import core.engine.Loader;
import core.engine.entity.Entity;
import core.engine.model.HexagonModel;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.model.TexturedModel;
import core.engine.textures.ModelTexture; import core.engine.textures.ModelTexture;
import utils.vectors.Vector3f;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -10,14 +14,23 @@ import java.util.List;
public class Terrain { public class Terrain {
private List<TerrainTile> terrainTiles = new ArrayList<>(); private final List<TerrainTile> terrainTiles;
public Terrain(Loader loader, ModelTexture modelTexture) { private Terrain(List<TerrainTile> terrainTiles) {
for(int i=0; i<32; i++) { this.terrainTiles = terrainTiles;
for(int j=0; j<32; j++) { }
terrainTiles.add(new TerrainTile(i, j, loader, modelTexture));
} public static Terrain generateTerrain(Loader loader) {
} HexagonModel hexagonModel = new HexagonModel();
RawModel hexagonRawModel = loader.loadToVAO(hexagonModel.getVertices(), hexagonModel.getTextureCoords(), hexagonModel.getNormals(), hexagonModel.getIndices());
ModelTexture hexagonTexture = new ModelTexture(loader.loadTexture("white"));
TexturedModel hexagontexturedModel = new TexturedModel(hexagonRawModel, hexagonTexture);
TerrainTile terrainTile = new TerrainTile(hexagontexturedModel, new Vector3f(1.3f,0,0.68f), 0,0,0,0.075f);
List<TerrainTile> generatedTerrainTiles = new ArrayList<>();
generatedTerrainTiles.add(terrainTile);
return new Terrain(generatedTerrainTiles);
} }
public List<TerrainTile> getTerrainTiles() { public List<TerrainTile> getTerrainTiles() {

View File

@ -4,81 +4,45 @@ import core.engine.Loader;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.model.TexturedModel; import core.engine.model.TexturedModel;
import core.engine.textures.ModelTexture; import core.engine.textures.ModelTexture;
import utils.vectors.Vector3f;
public class TerrainTile { public class TerrainTile {
private static final float SIZE = 32; private TexturedModel model;
private final Vector3f position;
private final float rotX, rotY, rotZ;
private final float scale;
private static final int VERTEX_COUNT = 33; public TerrainTile(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) {
this.model = model;
private float x; this.position = position;
private float z; this.rotX = rotX;
private RawModel model; this.rotY = rotY;
private ModelTexture texture; this.rotZ = rotZ;
this.scale = scale;
public TerrainTile(int gridX, int gridZ, Loader loader, ModelTexture texture) {
this.x = gridX;
this.z = gridZ;
this.texture = texture;
this.model = generateTerrain(loader);
} }
private RawModel generateTerrain(Loader loader) { public TexturedModel getModel() {
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() {
return model; return model;
} }
public ModelTexture getTexture() { public Vector3f getPosition() {
return texture; return position;
} }
public float getRotX() {
public float getX() { return rotX;
return x;
} }
public float getZ() { public float getRotY() {
return z; return rotY;
} }
public float getRotZ() {
return rotZ;
}
public float getScale() {
// Getters and setters for the model, x, and z return scale;
}
} }