package com.aaaaahhhhhhh.bananapuncher714.minietest; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.bukkit.util.Vector; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.ChunkLocation; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.Facet; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.MeshBuilder; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.Plane; public class MeshTest { private static final File BASE = new File( System.getProperty( "user.dir" ) ); private static final File CHUNK_DIR = new File( BASE, "chunks" ); public static void main( String[] args ) { if ( CHUNK_DIR.exists() && CHUNK_DIR.isDirectory() ) { System.out.println( "Found " + CHUNK_DIR.list().length + " files" ); for ( File file : CHUNK_DIR.listFiles() ) { doStuff( file ); break; } } else { System.err.println( "No such directory exists: " + CHUNK_DIR ); } } private static void doStuff( File file ) { String name = file.getName(); String[] split = name.split( "," ); ChunkLocation location = new ChunkLocation( split[ 0 ], Integer.parseInt( split[ 1 ] ), Integer.parseInt( split[ 2 ] ) ); System.out.println( "Parsing chunk " + location ); // First parse the file to get a list of all bounding boxes that we can use List< AABB > boxes = new ArrayList< AABB >(); try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) ) { String line; while ( ( line = reader.readLine() ) != null ) { if ( !line.isEmpty() ) { String[] values = line.split( "," ); double minX = Double.parseDouble( values[ 0 ] ); double minY = Double.parseDouble( values[ 1 ] ); double minZ = Double.parseDouble( values[ 2 ] ); double maxX = Double.parseDouble( values[ 3 ] ); double maxY = Double.parseDouble( values[ 4 ] ); double maxZ = Double.parseDouble( values[ 5 ] ); boxes.add( new AABB( minX, minY, minZ, maxX, maxY, maxZ ) ); } } } catch ( FileNotFoundException e ) { e.printStackTrace(); return; } catch ( IOException e ) { e.printStackTrace(); return; } System.out.println( "Found " + boxes.size() + " boxes" ); // Now that we have a bunch of bounding boxes, do whatever MeshBuilder builder = new MeshBuilder(); int i = 0; for ( AABB box : boxes ) { for ( Facet facet : generateFacetsFor( box ) ) { builder.addFacet( facet ); } } System.out.println( "Planes: " + builder.planes.size() ); for ( Plane plane : builder.planes ) { System.out.println( "Plane" ); System.out.println( "Norm:\t" + plane.normal ); System.out.println( "Ref:\t" + plane.point ); System.out.println( "Size:\t" + plane.polygons.size() ); } /* * The general plan is as follows: * - Create a new mesh object * - Insert faces one by one until we have all the shapes we want * - In this case, it happens to be perfect AABB * - Process the faces * - Sort them into their correct plane of existence, along with their normal * - In this case, their unique plane can be defined by their normal, and a point * - Use the first point of the polygon to compare * - Use the smallest point we can find as the reference point * - O(1) operation per face, I suppose * - Need to check point and normal with tolerance which is why * - Now that we have faces each in their assorted group, we can start to tessellate each one * - Generate even/odd regions and remove any colinear edges * - Save this as the pre-mesh * - Now trianglate each group and put that into a mesh collision shape or gimpact shape or whatnot * - Use an IndexedMesh with the two array constructor * - Put into game and test */ } private static List< Facet > generateFacetsFor( AABB box ) { List< Facet > facets = new ArrayList< Facet >(); Vector p1 = new Vector( box.xmin, box.ymin, box.zmin ); Vector p2 = new Vector( box.xmin, box.ymin, box.zmax ); Vector p3 = new Vector( box.xmin, box.ymax, box.zmin ); Vector p4 = new Vector( box.xmin, box.ymax, box.zmax ); Vector p5 = new Vector( box.xmax, box.ymin, box.zmin ); Vector p6 = new Vector( box.xmax, box.ymin, box.zmax ); Vector p7 = new Vector( box.xmax, box.ymax, box.zmin ); Vector p8 = new Vector( box.xmax, box.ymax, box.zmax ); { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p2 ); facet.points.add( p4 ); facet.points.add( p3 ); facet.normal = new Vector( -1, 0, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p5 ); facet.points.add( p6 ); facet.points.add( p8 ); facet.points.add( p7 ); facet.normal = new Vector( 1, 0, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p2 ); facet.points.add( p6 ); facet.points.add( p5 ); facet.normal = new Vector( 0, -1, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p3 ); facet.points.add( p4 ); facet.points.add( p7 ); facet.points.add( p8 ); facet.normal = new Vector( 0, 1, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p3 ); facet.points.add( p7 ); facet.points.add( p5 ); facet.normal = new Vector( 0, 0, -1 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p2 ); facet.points.add( p4 ); facet.points.add( p8 ); facet.points.add( p6 ); facet.normal = new Vector( 0, 0, 1 ); facets.add( facet ); } return facets; } static class AABB { double xmin, ymin, zmin; double xmax, ymax, zmax; AABB( double xmin, double ymin, double zmin, double xmax, double ymax, double zmax ) { this.xmin = xmin; this.ymax = ymin; this.zmin = zmin; this.xmax = xmax; this.ymax = ymax; this.zmax = zmax; } } }