Render Single HexagonModel as TerrainTile
This commit is contained in:
		
							parent
							
								
									466a15f160
								
							
						
					
					
						commit
						ac3e0a1b0e
					
				
							
								
								
									
										200007
									
								
								client/res/dragon.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										200007
									
								
								client/res/dragon.obj
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								client/res/stall.blend
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								client/res/stall.blend
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										1696
									
								
								client/res/stall.obj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1696
									
								
								client/res/stall.obj
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								client/res/stallTexture.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								client/res/stallTexture.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 49 KiB  | 
@ -118,34 +118,17 @@ public class Engine {
 | 
			
		||||
        // Set the clear color
 | 
			
		||||
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
 | 
			
		||||
        Camera camera = new Camera();
 | 
			
		||||
 | 
			
		||||
        Terrain terrain = Terrain.generateTerrain(loader);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
        // the window or has pressed the ESCAPE key.
 | 
			
		||||
        MasterRenderer renderer = new MasterRenderer();
 | 
			
		||||
        input(camera, renderer);
 | 
			
		||||
        while ( !glfwWindowShouldClose(window) ) {
 | 
			
		||||
            renderer.processEntity(hexagonEntity);
 | 
			
		||||
            //entity.increaseRotation(0,1,0);
 | 
			
		||||
            //renderer.processTerrain(terrain);
 | 
			
		||||
            //renderer.processEntity(entity);
 | 
			
		||||
            renderer.processTerrain(terrain);
 | 
			
		||||
 | 
			
		||||
            renderer.render(light, camera);
 | 
			
		||||
            glfwSwapBuffers(window); // swap the color buffers
 | 
			
		||||
            // 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) {
 | 
			
		||||
                if(key == GLFW_KEY_A) {
 | 
			
		||||
                    //move camera to left
 | 
			
		||||
                    camera.moveLeft();
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if(key == GLFW_KEY_D) {
 | 
			
		||||
 | 
			
		||||
@ -10,43 +10,43 @@ public class Camera {
 | 
			
		||||
    private float roll;
 | 
			
		||||
 | 
			
		||||
    public void moveLeft() {
 | 
			
		||||
        position.x += 0.5f;
 | 
			
		||||
        position.x += 0.25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void moveForward() {
 | 
			
		||||
        position.z -= .5f;
 | 
			
		||||
        position.z -= .25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void moveBackward() {
 | 
			
		||||
        position.z += .5f;
 | 
			
		||||
        position.z += .25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void moveRight() {
 | 
			
		||||
        position.x -= .5f;
 | 
			
		||||
        position.x -= .25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void zoomOut() {
 | 
			
		||||
        position.y += 0.5f;
 | 
			
		||||
        position.y += 0.25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void zoomIn() {
 | 
			
		||||
        position.y -= 0.5f;
 | 
			
		||||
        position.y -= 0.25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void increasePitch() {
 | 
			
		||||
        this.pitch += .5f;
 | 
			
		||||
        this.pitch += .25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void decreasePitch() {
 | 
			
		||||
        this.pitch -= .5f;
 | 
			
		||||
        this.pitch -= .25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void increaseYaw() {
 | 
			
		||||
        this.yaw += 0.5f;
 | 
			
		||||
        this.yaw += 0.25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void decreaseYaw() {
 | 
			
		||||
        this.yaw -= 0.5f;
 | 
			
		||||
        this.yaw -= 0.25f;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Vector3f getPosition() {
 | 
			
		||||
 | 
			
		||||
@ -58,31 +58,18 @@ public class HexagonModel {
 | 
			
		||||
        return vertices;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setVertices(float[] vertices) {
 | 
			
		||||
        this.vertices = vertices;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public float[] getNormals() {
 | 
			
		||||
        return normals;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setNormals(float[] normals) {
 | 
			
		||||
        this.normals = normals;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public float[] getTextureCoords() {
 | 
			
		||||
        return textureCoords;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setTextureCoords(float[] textureCoords) {
 | 
			
		||||
        this.textureCoords = textureCoords;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int[] getIndices() {
 | 
			
		||||
        return indices;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setIndices(int[] indices) {
 | 
			
		||||
        this.indices = indices;
 | 
			
		||||
    }
 | 
			
		||||
    /*public float[] moveVertices(float dx, float dz) {
 | 
			
		||||
        
 | 
			
		||||
    }*/
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -65,9 +65,12 @@ public class MasterRenderer {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void processTerrain(Terrain terrain) {
 | 
			
		||||
        terrains.addAll(terrain.getTerrainTiles());
 | 
			
		||||
        for(TerrainTile terrainTile : terrain.getTerrainTiles()) {
 | 
			
		||||
           terrains.add(terrainTile);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void processEntity(Entity entity) {
 | 
			
		||||
        TexturedModel entityModel = entity.getModel();
 | 
			
		||||
        List<Entity> batch = entities.get(entityModel);
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ package core.engine.renderer;
 | 
			
		||||
import core.engine.entity.Entity;
 | 
			
		||||
import core.engine.model.RawModel;
 | 
			
		||||
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;
 | 
			
		||||
@ -15,35 +16,43 @@ import utils.vectors.Matrix4f;
 | 
			
		||||
import utils.vectors.Vector3f;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
public class TerrainRenderer {
 | 
			
		||||
 | 
			
		||||
    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) {
 | 
			
		||||
        this.shader = shader;
 | 
			
		||||
 | 
			
		||||
        shader.start();
 | 
			
		||||
        shader.loadProjectionMatrix(projectionMatrix);
 | 
			
		||||
        shader.stop();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
            unbindTexturedModel();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public void render(List<TerrainTile> terrainTiles) {
 | 
			
		||||
       for(TerrainTile terrainTile : terrainTiles) {
 | 
			
		||||
           prepareTexturedModel(terrainTile.getModel());
 | 
			
		||||
           prepareInstance(terrainTile);
 | 
			
		||||
           GL11.glDrawElements(GL11.GL_TRIANGLES, terrainTile.getModel().getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
 | 
			
		||||
       }
 | 
			
		||||
       unbindTexturedModel();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void prepareTerrain(TerrainTile model) {
 | 
			
		||||
        RawModel rawModel = model.getModel();
 | 
			
		||||
    private void prepareTexturedModel(TexturedModel model) {
 | 
			
		||||
        RawModel rawModel = model.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, model.getModelTexture().getTextureID());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void unbindTexturedModel() {
 | 
			
		||||
@ -53,8 +62,10 @@ public class TerrainRenderer {
 | 
			
		||||
        GL30.glBindVertexArray(0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loadModelMatrix(TerrainTile terrain) {
 | 
			
		||||
        Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()), 0,0,0,1);
 | 
			
		||||
    private void prepareInstance(TerrainTile entity) {
 | 
			
		||||
        Matrix4f transformationMatrix = MatrixGraphicUtils.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
 | 
			
		||||
                entity.getRotY(), entity.getRotZ(), entity.getScale());
 | 
			
		||||
        shader.loadTransformationMatrix(transformationMatrix);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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 = vec4(1,1,1,1);
 | 
			
		||||
}
 | 
			
		||||
@ -1,8 +1,12 @@
 | 
			
		||||
package core.engine.terrain;
 | 
			
		||||
 | 
			
		||||
import core.engine.Loader;
 | 
			
		||||
import core.engine.entity.Entity;
 | 
			
		||||
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;
 | 
			
		||||
@ -10,14 +14,23 @@ import java.util.List;
 | 
			
		||||
public class Terrain {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private List<TerrainTile> terrainTiles = new ArrayList<>();
 | 
			
		||||
    private final List<TerrainTile> terrainTiles;
 | 
			
		||||
 | 
			
		||||
    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));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    private Terrain(List<TerrainTile> terrainTiles) {
 | 
			
		||||
        this.terrainTiles = terrainTiles;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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() {
 | 
			
		||||
 | 
			
		||||
@ -4,81 +4,45 @@ 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 final Vector3f position;
 | 
			
		||||
    private final float rotX, rotY, rotZ;
 | 
			
		||||
    private final 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 Vector3f getPosition() {
 | 
			
		||||
        return position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public float getX() {
 | 
			
		||||
        return x;
 | 
			
		||||
    public float getRotX() {
 | 
			
		||||
        return rotX;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public float getZ() {
 | 
			
		||||
        return z;
 | 
			
		||||
    public float getRotY() {
 | 
			
		||||
        return rotY;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public float getRotZ() {
 | 
			
		||||
        return rotZ;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // Getters and setters for the model, x, and z
 | 
			
		||||
    public float getScale() {
 | 
			
		||||
        return scale;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user