Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TheMaze.generation;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Mesh;
- import com.jme3.scene.VertexBuffer.Type;
- import com.jme3.util.BufferUtils;
- import java.util.ArrayList;
- /**
- * Creates a custom mesh from a matrix of 1 and 0
- * represented through a 2D Array.
- * It automaticly skips invisible Faces.
- * @author Makkuro Na
- */
- public class MazeToMesh {
- private ArrayList<Vector3f> vertices;
- private ArrayList<Vector2f> texCoords;
- private ArrayList<Integer> indexes;
- Mesh maze;
- Mesh ground;
- int[][] grid;
- int vPos=0;
- /**
- * Initialites all needed stuff and creates a mesh from the given matrix.
- */
- public MazeToMesh(int[][] M) {
- grid=M;
- maze=new Mesh();
- ground=new Mesh();
- vertices=new ArrayList<Vector3f>();
- texCoords=new ArrayList<Vector2f>();
- indexes=new ArrayList<Integer>();
- build();
- setBuffers();
- buildGround();
- }
- private void build() {
- // Fill the grid.
- for (int x=0;x<grid.length;x++) {
- for (int y=0;y<grid[0].length;y++) {
- if (grid[x][y]==1) {
- buildBlock(x, y);
- }
- }
- }
- }
- private void buildBlock(int x, int y) {
- vertices.add(new Vector3f(x,4,y));
- vertices.add(new Vector3f(x,0,y));
- vertices.add(new Vector3f(x+1,0,y));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(0,1));
- texCoords.add(new Vector2f(0,0));
- texCoords.add(new Vector2f(1,0));
- vertices.add(new Vector3f(x+1,0,y));
- vertices.add(new Vector3f(x+1,4,y));
- vertices.add(new Vector3f(x,4,y));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(1,0));
- texCoords.add(new Vector2f(1,1));
- texCoords.add(new Vector2f(0,1));
- vertices.add(new Vector3f(x+1,4,y-1));
- vertices.add(new Vector3f(x+1,0,y-1));
- vertices.add(new Vector3f(x,0,y-1));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(0,1));
- texCoords.add(new Vector2f(0,0));
- texCoords.add(new Vector2f(1,0));
- vertices.add(new Vector3f(x,0,y-1));
- vertices.add(new Vector3f(x,4,y-1));
- vertices.add(new Vector3f(x+1,4,y-1));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(1,0));
- texCoords.add(new Vector2f(1,1));
- texCoords.add(new Vector2f(0,1));
- vertices.add(new Vector3f(x,4,y-1));
- vertices.add(new Vector3f(x,0,y-1));
- vertices.add(new Vector3f(x,0,y));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(0,1));
- texCoords.add(new Vector2f(0,0));
- texCoords.add(new Vector2f(1,0));
- vertices.add(new Vector3f(x,0,y));
- vertices.add(new Vector3f(x,4,y));
- vertices.add(new Vector3f(x,4,y-1));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(1,0));
- texCoords.add(new Vector2f(1,1));
- texCoords.add(new Vector2f(0,1));
- vertices.add(new Vector3f(x+1,4,y));
- vertices.add(new Vector3f(x+1,0,y));
- vertices.add(new Vector3f(x+1,0,y-1));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(0,1));
- texCoords.add(new Vector2f(0,0));
- texCoords.add(new Vector2f(1,0));
- vertices.add(new Vector3f(x+1,0,y-1));
- vertices.add(new Vector3f(x+1,4,y-1));
- vertices.add(new Vector3f(x+1,4,y));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(1,0));
- texCoords.add(new Vector2f(1,1));
- texCoords.add(new Vector2f(0,1));
- // And set a top on it... XD
- vertices.add(new Vector3f(x,4,y-1));
- vertices.add(new Vector3f(x,4,y));
- vertices.add(new Vector3f(x+1,4,y));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(0,1));
- texCoords.add(new Vector2f(0,0));
- texCoords.add(new Vector2f(1,0));
- vertices.add(new Vector3f(x+1,4,y));
- vertices.add(new Vector3f(x+1,4,y-1));
- vertices.add(new Vector3f(x,4,y-1));
- indexes.add(vPos);
- indexes.add(vPos+1);
- indexes.add(vPos+2);
- vPos+=3;
- texCoords.add(new Vector2f(1,0));
- texCoords.add(new Vector2f(1,1));
- texCoords.add(new Vector2f(0,1));
- }
- private void setBuffers() {
- // Create the needed arrays from the arraylists
- Vector3f[] V=new Vector3f[vertices.size()];
- for (int i=0;i<vertices.size();i++)
- V[i]=vertices.get(i);
- Vector2f[] T=new Vector2f[texCoords.size()];
- for (int i=0;i<texCoords.size();i++)
- T[i]=texCoords.get(i);
- int[] I=new int[indexes.size()];
- for (int i=0;i<indexes.size();i++)
- I[i]=indexes.get(i);
- maze.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(V));
- maze.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(T));
- maze.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(I));
- maze.updateBound();
- }
- public Mesh getMesh() {
- return maze;
- }
- public Mesh getGround() {
- return ground;
- }
- private void buildGround() {
- ArrayList<Vector3f> V=new ArrayList<Vector3f>();
- ArrayList<Vector2f> T=new ArrayList<Vector2f>();
- ArrayList<Integer> I=new ArrayList<Integer>();
- vPos=0;
- for (int x=0;x<grid.length;x++) {
- for (int y=0;y<grid[0].length;y++) {
- if (grid[x][y]==0) {
- V.add(new Vector3f(x,0,y-1));
- V.add(new Vector3f(x,0,y));
- V.add(new Vector3f(x+1,0,y));
- I.add(vPos);
- I.add(vPos+1);
- I.add(vPos+2);
- vPos+=3;
- T.add(new Vector2f(0,1));
- T.add(new Vector2f(0,0));
- T.add(new Vector2f(1,0));
- V.add(new Vector3f(x+1,0,y));
- V.add(new Vector3f(x+1,0,y-1));
- V.add(new Vector3f(x,0,y-1));
- I.add(vPos);
- I.add(vPos+1);
- I.add(vPos+2);
- vPos+=3;
- T.add(new Vector2f(1,0));
- T.add(new Vector2f(1,1));
- T.add(new Vector2f(0,1));
- }
- }
- }
- // Create the needed arrays from the arraylists
- Vector3f[] V2=new Vector3f[V.size()];
- for (int i=0;i<V.size();i++)
- V2[i]=V.get(i);
- Vector2f[] T2=new Vector2f[T.size()];
- for (int i=0;i<T.size();i++)
- T2[i]=T.get(i);
- int[] I2=new int[I.size()];
- for (int i=0;i<I.size();i++)
- I2[i]=I.get(i);
- ground.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(V2));
- ground.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(T2));
- ground.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(I2));
- ground.updateBound();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment