Fix gaps in TerrainTiles
This commit is contained in:
parent
4932b3f6a0
commit
e0afd6e93a
@ -118,7 +118,7 @@ public class Engine {
|
|||||||
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();
|
||||||
|
|
||||||
input(camera);
|
|
||||||
|
|
||||||
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);
|
RawModel model = OBJLoader.loadOBJModel("dragon", loader);
|
||||||
@ -127,11 +127,12 @@ public class Engine {
|
|||||||
Entity entity = new Entity(texturedModel, new Vector3f(0,0,-50), 0,0,0,1);
|
Entity entity = new Entity(texturedModel, new Vector3f(0,0,-50), 0,0,0,1);
|
||||||
|
|
||||||
//Generate Simple, Flat Terrain
|
//Generate Simple, Flat Terrain
|
||||||
Terrain terrain = new Terrain(0,0, loader, new ModelTexture(loader.loadTexture("grass")));
|
Terrain terrain = new Terrain( loader, new ModelTexture(loader.loadTexture("grass")));
|
||||||
|
|
||||||
// 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);
|
||||||
while ( !glfwWindowShouldClose(window) ) {
|
while ( !glfwWindowShouldClose(window) ) {
|
||||||
//entity.increaseRotation(0,1,0);
|
//entity.increaseRotation(0,1,0);
|
||||||
renderer.processTerrain(terrain);
|
renderer.processTerrain(terrain);
|
||||||
@ -146,7 +147,7 @@ public class Engine {
|
|||||||
loader.cleanUp();
|
loader.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void input(Camera camera) {
|
private void input(Camera camera, MasterRenderer masterRenderer) {
|
||||||
|
|
||||||
GLFWScrollCallback glfwScrollCallback = new GLFWScrollCallback() {
|
GLFWScrollCallback glfwScrollCallback = new GLFWScrollCallback() {
|
||||||
@Override
|
@Override
|
||||||
@ -226,6 +227,10 @@ public class Engine {
|
|||||||
if(key == GLFW_KEY_S) {
|
if(key == GLFW_KEY_S) {
|
||||||
camera.moveBackward();
|
camera.moveBackward();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(key == GLFW_KEY_Y && action == GLFW_PRESS) {
|
||||||
|
masterRenderer.switchWireframe();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -31,6 +31,8 @@ public class MasterRenderer {
|
|||||||
private List<TerrainTile> terrains = new ArrayList<>();
|
private List<TerrainTile> terrains = new ArrayList<>();
|
||||||
private Matrix4f projectionMatrix;
|
private Matrix4f projectionMatrix;
|
||||||
|
|
||||||
|
private boolean wireframe = false;
|
||||||
|
|
||||||
public MasterRenderer() {
|
public MasterRenderer() {
|
||||||
GL11.glEnable(GL11.GL_CULL_FACE);
|
GL11.glEnable(GL11.GL_CULL_FACE);
|
||||||
GL11.glCullFace(GL11.GL_BACK);
|
GL11.glCullFace(GL11.GL_BACK);
|
||||||
@ -98,4 +100,14 @@ public class MasterRenderer {
|
|||||||
projectionMatrix.m33 = 0;
|
projectionMatrix.m33 = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void switchWireframe() {
|
||||||
|
this.wireframe = !this.wireframe;
|
||||||
|
|
||||||
|
if(this.wireframe) {
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
} else {
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,40 +9,18 @@ import java.util.List;
|
|||||||
|
|
||||||
public class Terrain {
|
public class Terrain {
|
||||||
|
|
||||||
private static final float SIZE = 100;
|
|
||||||
|
|
||||||
private static final int VERTEX_COUNT = 128;
|
|
||||||
|
|
||||||
private float x;
|
|
||||||
private float z;
|
|
||||||
|
|
||||||
private List<TerrainTile> terrainTiles = new ArrayList<>();
|
private List<TerrainTile> terrainTiles = new ArrayList<>();
|
||||||
|
|
||||||
public Terrain(int gridX, int gridZ, Loader loader, ModelTexture modelTexture) {
|
public Terrain(Loader loader, ModelTexture modelTexture) {
|
||||||
this.x = gridX * SIZE;
|
for(int i=0; i<32; i++) {
|
||||||
this.z = gridZ * SIZE;
|
for(int j=0; j<32; j++) {
|
||||||
|
terrainTiles.add(new TerrainTile(i, j, loader, modelTexture));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getZ() {
|
|
||||||
return z;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<TerrainTile> getTerrainTiles() {
|
public List<TerrainTile> getTerrainTiles() {
|
||||||
return terrainTiles;
|
return terrainTiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTerrainTiles(List<TerrainTile> terrainTiles) {
|
|
||||||
this.terrainTiles = terrainTiles;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package core.engine.terrain;
|
|
||||||
|
|
||||||
public class TerrainGenerator {
|
|
||||||
|
|
||||||
}
|
|
@ -2,101 +2,83 @@ package core.engine.terrain;
|
|||||||
|
|
||||||
import core.engine.Loader;
|
import core.engine.Loader;
|
||||||
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;
|
||||||
|
|
||||||
public class TerrainTile {
|
public class TerrainTile {
|
||||||
|
|
||||||
private RawModel model;
|
private static final float SIZE = 32;
|
||||||
private ModelTexture texture;
|
|
||||||
|
private static final int VERTEX_COUNT = 33;
|
||||||
|
|
||||||
private float x;
|
private float x;
|
||||||
private float z;
|
private float z;
|
||||||
|
private RawModel model;
|
||||||
|
private ModelTexture texture;
|
||||||
|
|
||||||
public TerrainTile(Loader loader, ModelTexture texture, int row, int column, int VERTEX_COUNT, float size) {
|
public TerrainTile(int gridX, int gridZ, Loader loader, ModelTexture texture) {
|
||||||
float[] vertices = new float[12]; // 4 vertices * 3 coordinates
|
this.x = gridX;
|
||||||
float[] normals = new float[12]; // 4 normals * 3 coordinates
|
this.z = gridZ;
|
||||||
float[] textureCoords = new float[8]; // 4 texture coords * 2 coordinates
|
this.texture = texture;
|
||||||
int[] indices = new int[6]; // 2 triangles * 3 indices
|
this.model = generateTerrain(loader);
|
||||||
|
|
||||||
//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
|
private RawModel generateTerrain(Loader loader) {
|
||||||
indices[0] = 0;
|
int count = VERTEX_COUNT * VERTEX_COUNT;
|
||||||
indices[1] = 1;
|
float[] vertices = new float[count * 3];
|
||||||
indices[2] = 2;
|
float[] normals = new float[count * 3];
|
||||||
indices[3] = 2;
|
float[] textureCoords = new float[count*2];
|
||||||
indices[4] = 1;
|
int[] indices = new int[6*(VERTEX_COUNT-1)*(VERTEX_COUNT-1)];
|
||||||
indices[5] = 3;
|
int vertexPointer = 0;
|
||||||
|
for(int i=0;i<VERTEX_COUNT;i++){
|
||||||
this.model = loader.loadToVAO(vertices, textureCoords, normals, indices);
|
for(int j=0;j<VERTEX_COUNT;j++){
|
||||||
this.texture = texture;
|
vertices[vertexPointer*3] = (float)j/((float)VERTEX_COUNT - 1) * SIZE;
|
||||||
|
vertices[vertexPointer*3+1] = 0;
|
||||||
this.x = x0;
|
vertices[vertexPointer*3+2] = (float)i/((float)VERTEX_COUNT - 1) * SIZE;
|
||||||
this.z = z0;
|
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 RawModel getModel() {
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setModel(RawModel model) {
|
|
||||||
this.model = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModelTexture getTexture() {
|
public ModelTexture getTexture() {
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTexture(ModelTexture texture) {
|
|
||||||
this.texture = texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getX() {
|
public float getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setX(float x) {
|
|
||||||
this.x = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getZ() {
|
public float getZ() {
|
||||||
return z;
|
return z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setZ(float z) {
|
|
||||||
this.z = z;
|
|
||||||
}
|
// Getters and setters for the model, x, and z
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user