Strijar

Untitled

Jan 13th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package ru.strijar.vs;
  2.  
  3. import com.jme3.bullet.control.RigidBodyControl;
  4. import com.jme3.bullet.control.SoftBodyControl;
  5. import com.jme3.math.Vector3f;
  6. import com.jme3.scene.Mesh;
  7. import com.jme3.scene.VertexBuffer.Type;
  8. import com.jme3.util.BufferUtils;
  9. import java.nio.FloatBuffer;
  10. import java.nio.IntBuffer;
  11. import java.util.ArrayList;
  12. import java.util.HashSet;
  13.  
  14. class TriMesh extends Mesh {
  15.     private int             SUB = 5;
  16.     private final int                   vertexCount;
  17.     private final ArrayList<Vector3f>   vertex = new ArrayList<Vector3f>();
  18.     private final ArrayList<Integer>    index = new ArrayList<Integer>();
  19.     private final HashSet<Integer>      sideAB = new HashSet<Integer>();
  20.    
  21.     public TriMesh(Vector3f a, Vector3f b, Vector3f c) {
  22.         vertex.add(a);          sideAB.add(0);
  23.         vertex.add(b);          sideAB.add(1);
  24.         vertex.add(c);
  25.  
  26.         createTri(a, b, c, 0, 1, 2, true, SUB);
  27.  
  28.         // Vertex
  29.        
  30.         vertexCount = vertex.size();
  31.  
  32.         FloatBuffer vertBuf = BufferUtils.createVector3Buffer(vertexCount);
  33.         setBuffer(Type.Position, 3, vertBuf);
  34.        
  35.         int i = 0;
  36.        
  37.         for (Vector3f item : vertex)
  38.             BufferUtils.setInBuffer(item, vertBuf, i++);
  39.        
  40.         vertex.clear();
  41.            
  42.         // Index
  43.        
  44.         IntBuffer indexBuf = BufferUtils.createIntBuffer(index.size());
  45.         setBuffer(Type.Index, 3, indexBuf);
  46.  
  47.         for (Integer item : index)
  48.             indexBuf.put(item);
  49.  
  50.         index.clear();
  51.  
  52.         // Color
  53.        
  54.         FloatBuffer colorBuf = BufferUtils.createFloatBuffer(vertexCount * 4);
  55.         setBuffer(Type.Color, 4, colorBuf);
  56.        
  57.         for (i = 0; i < vertexCount; i++) {
  58.             if (sideAB.contains(i)) {
  59.                 if (i < 64) {
  60.                     colorBuf.put(1f);
  61.                     colorBuf.put(0f);
  62.                     colorBuf.put(0f);
  63.                 } else {
  64.                     colorBuf.put(1f);
  65.                     colorBuf.put(1f);
  66.                     colorBuf.put(1f);
  67.                 }
  68.             } else {
  69.                 colorBuf.put(0f);
  70.                 colorBuf.put(0f);
  71.                 colorBuf.put(1f);
  72.             }
  73.             colorBuf.put(1f);
  74.         }
  75.        
  76.         // Normal
  77.        
  78.         FloatBuffer normBuf = BufferUtils.createVector3Buffer(vertexCount);
  79.         setBuffer(Type.Normal, 3, normBuf);
  80.        
  81.         for (i = 0; i < vertexCount; i++)
  82.             BufferUtils.setInBuffer(Vector3f.UNIT_X, normBuf, i);
  83.        
  84.         updateBound();
  85.     }
  86.  
  87.     public void attachAB(RigidBodyControl line, SoftBodyControl control) {
  88.         for (Integer item : sideAB) {
  89.             control.appendAnchor(item, line, false, 1f);
  90.         }
  91.     }
  92.    
  93.     private void createTri(Vector3f a, Vector3f b, Vector3f c, int ai, int bi, int ci, boolean abFlag, int sub) {
  94.         if (sub == 0) {
  95.             index.add(ai);
  96.             index.add(bi);
  97.             index.add(ci);
  98.  
  99.             if (abFlag) {
  100.                 sideAB.add(ai);
  101.                 sideAB.add(bi);
  102.             }
  103.         } else {
  104.             Vector3f d = new Vector3f();
  105.             Vector3f e = new Vector3f();
  106.             Vector3f f = new Vector3f();
  107.  
  108.             int ei = vertex.size();
  109.             int fi = ei + 1;
  110.             int di = ei + 2;
  111.            
  112.             d.interpolateLocal(a, c, 0.5f);
  113.             e.interpolateLocal(a, b, 0.5f);
  114.             f.interpolateLocal(b, c, 0.5f);
  115.  
  116.             vertex.add(e);
  117.             vertex.add(f);
  118.             vertex.add(d);
  119.            
  120.             createTri(a, e, d, ai, ei, di, abFlag, sub-1);
  121.             createTri(e, b, f, ei, bi, fi, abFlag, sub-1);
  122.             createTri(d, e, f, di, ei, fi, false, sub-1);
  123.             createTri(d, f, c, di, fi, ci, false, sub-1);
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment