static short createVertexBuffer(ByteBuffer buffer, BGFXVertexDecl decl, Object[]... vertices) { for (Object[] objects : vertices) { for (Object object : objects) { if (object instanceof Tuple2f) buffer.putFloat(((Tuple2f) object).getX()).putFloat(((Tuple2f) object).getY()); else if (object instanceof Tuple3f) buffer.putFloat(((Tuple3f) object).getX()).putFloat(((Tuple3f) object).getY()).putFloat(((Tuple3f) object).getZ()); else if (object instanceof Tuple4f) buffer.putFloat(((Tuple4f) object).getX()).putFloat(((Tuple4f) object).getY()).putFloat(((Tuple4f) object).getZ()).putFloat(((Tuple4f) object).getW()); else if (object instanceof Integer) buffer.putInt(((int) object)); else throw new IllegalArgumentException("Unknown type of vertex: " + object.getClass().getName() + ", valid type are:" + "javax.vecmath.Tuple2f/3f/4f & java.lang.Integer"); } } if (buffer.remaining() != 0) { throw new RuntimeException("ByteBuffer size and number of arguments do not match"); } buffer.flip(); return createVertexBuffer(buffer, decl); }
private String format(float floatT, String strT, Tuple3f ptT) { if (!Float.isNaN(floatT)) { return TextFormat.format(floatT, width, precision, alignLeft, zeroPad); } else if (strT != null) { return TextFormat.format(strT, width, precision, alignLeft, zeroPad); } else if (ptT != null) { if (width == 0 && precision == Integer.MAX_VALUE) { width = 6; precision = 2; } return TextFormat.format(ptT.x, width, precision, false, false) + TextFormat.format(ptT.y, width, precision, false, false) + TextFormat.format(ptT.z, width, precision, false, false); } else { return text; } }
protected int[] getNormalMap(Tuple3f[] normals, int nNormals, BitSet bsValid, List<String> vNormals) { Map<String, Integer> htNormals = new Hashtable<String, Integer>(); int[] normalMap = new int[nNormals]; for (int i = 0; i < nNormals; i++) { String s; if (bsValid != null && !bsValid.get(i) || Float.isNaN(normals[i].x)){ if (bsValid != null) bsValid.clear(i); continue; } s = (round(normals[i].x) + " " + round(normals[i].y) + " " + round(normals[i].z) + "\n"); if (htNormals.containsKey(s)) { normalMap[i] = htNormals.get(s).intValue(); } else { normalMap[i] = vNormals.size(); vNormals.add(s); htNormals.put(s, Integer.valueOf(normalMap[i])); } } return normalMap; }
public void clear(int bufferMask, Tuple3f color) { if ((bufferMask & COLOR_BUFFER) != 0) { int r = (int)(color.x*255f); int g = (int)(color.y*255f); int b = (int)(color.z*255f); int c = (r << 16) | (g << 8) | b; for (int i=0; i<zbuffer.length; i++) { pixels[i] = c; } } if ((bufferMask & DEPTH_BUFFER) != 0) { for (int i=0; i<zbuffer.length; i++) { zbuffer[i] = Float.MAX_VALUE; } } }
public FacialPoint(FacialPointType type, Tuple3f coords) { this.pos = new Vector3f(coords); this.type = type; this.name = FpTexter.getInstance().getFPname(type); this.info = FpTexter.getInstance().getFPinfo(type); }
public static Point3d getIntersectionBetweenLineAndPlane(PlaneEquation eq, Tuple3f one, Tuple3f two) { double Xa = one.x; double Ya = one.y; double Za = one.z; double k; //A,B,C,D from Ax+By+Cz+d=0 double A = eq.getA(); double B = eq.getB(); double C = eq.getC(); double D = eq.getD(); //case when plane and polygon have the common tangent //ignore this? if (isPointWithinPlane(eq, one) && isPointWithinPlane(eq, two)) { return null; } try { //for parallel it'will be an exception k = -(A * Xa + B * Ya + C * Za + D) / (A * (two.x - one.x) + B * (two.y - one.y) + C * (two.z - one.z)); } catch (Exception e) { return null; } if (Math.abs(k) > 1 || k < 0) { return null; } double x0 = k * (two.x - one.x) + Xa; double y0 = k * (two.y - one.y) + Ya; double z0 = k * (two.z - one.z) + Za; Point3d resPoint = new Point3d(x0, y0, z0); return resPoint; }
public static boolean isPointWithinPlane(PlaneEquation eq, Tuple3f point) { double value = (eq.getA() * point.x + eq.getB() * point.y + eq.getC() * point.z + eq.getD()); return (Math.abs(value) / eq.getMAX() < THRESHOLD2); }
private static Tuple3f approx0(Tuple3f pt) { if (pt != null) { if (Math.abs(pt.x) < 0.0001f) pt.x = 0; if (Math.abs(pt.y) < 0.0001f) pt.y = 0; if (Math.abs(pt.z) < 0.0001f) pt.z = 0; } return pt; }
private static Tuple3f approx(Tuple3f pt) { if (pt != null) { pt.x = approx(pt.x); pt.y = approx(pt.y); pt.z = approx(pt.z); } return pt; }
@SuppressWarnings("unchecked") public static String escape(Object x) { if (x instanceof String) return escape((String) x); if (x instanceof List<?>) return escape((ArrayList<ScriptVariable>) x); if (x instanceof BitSet) return escape((BitSet) x, true); if (x instanceof Matrix3f) return ((Matrix3f) x).toString(); if (x instanceof Matrix4f) return ((Matrix4f) x).toString(); if (x instanceof Tuple3f) return escape((Tuple3f) x); if (x instanceof Point4f) { Point4f xyzw = (Point4f) x; return "{" + xyzw.x + " " + xyzw.y + " " + xyzw.z + " " + xyzw.w + "}"; } if (x instanceof AxisAngle4f) { AxisAngle4f a = (AxisAngle4f) x; return "{" + a.x + " " + a.y + " " + a.z + " " + (float) (a.angle * 180d/Math.PI) + "}"; } if (x instanceof String[]) return escape((String[]) x, true); if (x instanceof int[] || x instanceof int[][] || x instanceof float[] || x instanceof float[][] || x instanceof float[][][]) return toJSON(null, x); if (x instanceof Point3f[]) return escapeArray(x); if (x instanceof Map) return escape((Map<String, Object>) x); return (x == null ? "null" : x.toString()); }
public MeshSurface(int[][] polygonIndexes, Tuple3f[] vertices, int nVertices, Tuple3f[] normals, int nNormals) { this.polygonIndexes = polygonIndexes; if (vertices instanceof Point3f[]) this.vertices = (Point3f[]) vertices; else this.altVertices = vertices; this.vertexCount = (nVertices == 0 ? vertices.length : nVertices); this.normals = normals; this.normalCount = (nNormals == 0 && normals != null ? normals.length : nNormals); }
/** * q = (cos(theta/2), sin(theta/2) * n) * * @param pt * @param theta */ public void set(Tuple3f pt, float theta) { if (pt.x == 0 && pt.y == 0 && pt.z == 0) { q0 = 1; return; } double fact = (Math.sin(theta / 2 * Math.PI / 180) / Math.sqrt(pt.x * pt.x + pt.y * pt.y + pt.z * pt.z)); q0 = (float) (Math.cos(theta / 2 * Math.PI / 180)); q1 = (float) (pt.x * fact); q2 = (float) (pt.y * fact); q3 = (float) (pt.z * fact); }
public static final Quaternion getQuaternionFrame(Point3f center, Tuple3f x, Tuple3f xy) { Vector3f vA = new Vector3f(x); vA.sub(center); Vector3f vB = new Vector3f(xy); vB.sub(center); return getQuaternionFrame(vA, vB, null, false); }
/** * create the v or vn list * * @param pts * @param nPts * @param m * @param prefix * @param bsValid TODO */ private void outputList(Tuple3f[] pts, int nPts, Matrix4f m, String prefix, BitSet bsValid) { for (int i = 0; i < nPts; i++) { if (bsValid != null && !bsValid.get(i)) continue; ptTemp.set(pts[i]); if (m != null) m.transform(ptTemp); output(prefix + ptTemp.x + " " + ptTemp.y + " " + ptTemp.z + "\n"); } }
private void checkPoint(Tuple3f pt) { if (pt.x < ptMin.x) ptMin.x = pt.x; if (pt.y < ptMin.y) ptMin.y = pt.y; if (pt.z < ptMin.z) ptMin.z = pt.z; if (pt.x > ptMax.x) ptMax.x = pt.x; if (pt.y > ptMax.y) ptMax.y = pt.y; if (pt.z > ptMax.z) ptMax.z = pt.z; }
private String getMeshData(String type, int[][] indices, Tuple3f[] vertexes, Tuple3f[] normals) { int nFaces = indices.length; int vertexCount = vertexes.length; int normalCount = normals.length; StringBuffer sb = new StringBuffer(); getMeshHeader(type, nFaces, vertexCount, normalCount, 0, sb); StringBuffer sb1 = new StringBuffer(); for (int i = 0; i < indices.length; i++) { sb1.append(indices[i][0]).append(" "); sb1.append(indices[i][1]).append(" "); sb1.append(indices[i][2]).append(" "); } sb.append("MESH_FACE_POSITION_LIST { "); sb.append(sb1); sb.append("}\n"); sb.append("MESH_FACE_NORMAL_LIST { "); sb.append(sb1); sb.append("}\n"); sb.append("MESH_FACE_SHADING_LIST { "); for (int i = 0; i < nFaces; i++) sb.append("0 "); sb.append("}\n"); sb.append("MODEL_POSITION_LIST { "); for (int i = 0; i < vertexCount; i++) output(vertexes[i], sb, false); sb.append("}\n"); sb.append("MODEL_NORMAL_LIST { "); for (int i = 0; i < normalCount; i++) output(normals[i], sb, false); sb.append("}\n}}}\n"); return sb.toString(); }
protected int getCoordinateMap(Tuple3f[] vertices, int[] coordMap, BitSet bsValid) { int n = 0; for (int i = 0; i < coordMap.length; i++) { if (bsValid != null && !bsValid.get(i) || Float.isNaN(vertices[i].x)) { if (bsValid != null) bsValid.clear(i); continue; } coordMap[i] = n++; } return n; }
@Override public <T extends Tuple3f> T getVertex(int idx, T out) { int off = idx*vertexStride; out.x = vertexData.getFloat(off+4*0); out.y = vertexData.getFloat(off+4*1); out.z = vertexData.getFloat(off+4*2); return out; }
/** * A method for calculating the points of a face for an axis-aligned cuboid, with a given center and radius. */ public static Point3f[] getPointsForFace(EnumFacing face, Tuple3f center, Tuple3f radius) { Point3f centerOfFace = new Point3f(center); Point3f faceAdd = new Point3f(face.getFrontOffsetX() * radius.x, face.getFrontOffsetY() * radius.y, face.getFrontOffsetZ() * radius.z); centerOfFace.add(faceAdd); Vector3f faceRadius = new Vector3f(radius); if (face.getAxisDirection() == AxisDirection.POSITIVE) { faceRadius.sub(faceAdd); } else { faceRadius.add(faceAdd); } return getPoints(centerOfFace, faceRadius); }
/** * Inner method for {@link #getPointsForFace(EnumFacing, Tuple3f, Tuple3f)} that requires centerFace and faceRadius to be specific for the face. */ public static Point3f[] getPoints(Point3f centerFace, Tuple3f faceRadius) { Point3f[] array = { new Point3f(centerFace), new Point3f(centerFace), new Point3f(centerFace), new Point3f(centerFace) }; array[0].add(addOrNegate(faceRadius, false, false)); array[1].add(addOrNegate(faceRadius, false, true)); array[2].add(addOrNegate(faceRadius, true, true)); array[3].add(addOrNegate(faceRadius, true, false)); return array; }
public void putIntoBakedQuadBuilder(UnpackedBakedQuad.Builder builder, TextureAtlasSprite sprite) { Vertex transformed = getTransformedVertex(); Tuple4f positionBuffer = new Vector4f(); transformed.getPosition(positionBuffer); Tuple3f normalBuffer = new Vector3f(); transformed.getNormal(normalBuffer); Tuple2f uvBuffer = new Vector2f(); transformed.getUV(uvBuffer); VertexFormat vertexFormat = builder.getVertexFormat(); int elementCount = vertexFormat.getElementCount(); for (int e = 0; e < elementCount; e++) { VertexFormatElement element = vertexFormat.getElement(e); switch (element.getUsage()) { case POSITION: builder.put(e, positionBuffer.x, positionBuffer.z, -positionBuffer.y, positionBuffer.w); break; case NORMAL: builder.put(e, normalBuffer.x, normalBuffer.z, -normalBuffer.y, 0); break; case UV: if (element.getIndex() != 0) break; builder.put( e, sprite.getInterpolatedU(uvBuffer.x * 16), sprite.getInterpolatedV(uvBuffer.y * 16), 0, 1); break; case COLOR: builder.put(e, 1, 1, 1, 1); break; default: builder.put(e); } } }
private Tuple3f get(float param, List<? extends Tuple3f> list, Tuple3f result) { int index = findInsertionIndex(param); if (index < 0) { index = -(index + 1); checkInsertionIndex(index); float lower = params.get(index - 1); float upper = params.get(index); float f = (param - lower) / (upper - lower); result.interpolate(list.get(index - 1), list.get(index), f); } else { result.set(list.get(index)); } return result; }
public static void checkReal(Tuple3f p) { if (Double.isNaN(p.x) || Double.isNaN(p.y) || Double.isNaN(p.z)) { throw new IllegalArgumentException("tuple has NaN values: " + p); } if (Double.isInfinite(p.x) || Double.isInfinite(p.y) || Double.isInfinite(p.z)) { throw new IllegalArgumentException("tuple has Infinite values: " + p); } }
public static <T extends Tuple3f> float[] toFloatArray3(Collection<T> tuples, float[] buffer) { if (buffer == null) { buffer = new float[tuples.size() * 3]; } int d = 0; for (final T tuple : tuples) { buffer[d++] = tuple.x; buffer[d++] = tuple.y; buffer[d++] = tuple.z; } return buffer; }
public static <T extends Tuple3f> float[] toFloatArray3(T[] tuples, float[] buffer) { if (buffer == null) { buffer = new float[tuples.length * 3]; } int d = 0; for (final T tuple : tuples) { buffer[d++] = tuple.x; buffer[d++] = tuple.y; buffer[d++] = tuple.z; } return buffer; }
/** * Sets the camera location to <code>newLocation</code> without affecting the rotation. */ public void setLocation( Tuple3f newLocation , boolean applyNow ) { m_pendingPosition.setLocation( newLocation ); if( applyNow ) { exportVPT( ); } }
static void checkValid( Tuple3f t ) { if( Double.isNaN( t.x ) || Double.isNaN( t.y ) || Double.isNaN( t.z ) ) { throw new IllegalArgumentException( "tuple has NaN values" ); } if( Double.isInfinite( t.x ) || Double.isInfinite( t.y ) || Double.isInfinite( t.z ) ) { throw new IllegalArgumentException( "tuple has Infinite values" ); } }
/** * Sets the camera location to <code>newLocation</code> without affecting the rotation. */ public void setLocation( Tuple3f newLocation ) { checkValid( newLocation ); tempVec.set( newLocation ); setLocation( tempVec ); }
/** * Gets the specified vertex from the buffer * * @param _nIndex The index of the vertex to retrieve * @param _tuple Tuple3f object where the vertex coordinates will be stored */ public void getVertex(int _nIndex, Tuple3f _tuple) { int nIdx = getVertexOffset(_nIndex); _tuple.x = m_vertices.get(nIdx + 0); _tuple.y = m_vertices.get(nIdx + 1); _tuple.z = m_vertices.get(nIdx + 2); }
/** * Adds the specified vertex to the buffer * * @param _tuple Tuple3f object to add to the vertex buffer */ public void addVertex(Tuple3f _tuple) { int nIdx = getVertexOffset(m_nVertexCount); m_vertices.put(nIdx + 0, _tuple.x); m_vertices.put(nIdx + 1, _tuple.y); m_vertices.put(nIdx + 2, _tuple.z); m_nVertexCount++; }
/** * Changes the specified vertex in the buffer * * @param _nIndex The index of the vertex to change * @param _tuple Tuple3f object to use to change the vertex buffer */ public void setVertex(int _nIndex, Tuple3f _tuple) { int nIdx = getVertexOffset(_nIndex); m_vertices.put(nIdx + 0, _tuple.x); m_vertices.put(nIdx + 1, _tuple.y); m_vertices.put(nIdx + 2, _tuple.z); }
/** * Gets the specified normal from the buffer * * @param _nIndex The index of the normal to retrieve * @param _tuple Tuple3f object where the normal coordinates will be stored */ public void getNormal(int _nIndex, Tuple3f _tuple) { int nIdx = _nIndex * m_nVertexInfoSize + m_nNormalOffset; _tuple.x = m_normals.get(nIdx + 0); _tuple.y = m_normals.get(nIdx + 1); _tuple.z = m_normals.get(nIdx + 2); }
/** * Adds a new normal to the buffer * * @param _tuple Tuple3f object to add to the normal buffer */ public void addNormal(Tuple3f _tuple) { int nIdx = getNormalOffset(m_nVertexCount); m_normals.put(nIdx + 0, _tuple.x); m_normals.put(nIdx + 1, _tuple.y); m_normals.put(nIdx + 2, _tuple.z); }
/** * Changes the specified normal in the buffer * * @param _nIndex The index of the normal to change * @param _tuple Tuple3f object to use to change the normal buffer */ public void setNormal(int _nIndex, Tuple3f _tuple) { int nIdx = getNormalOffset(_nIndex); m_normals.put(nIdx + 0, _tuple.x); m_normals.put(nIdx + 1, _tuple.y); m_normals.put(nIdx + 2, _tuple.z); }
public static void assertEquals(Tuple3f expected, Tuple3f actual, float error) { if (expected == null) { assertNull(actual); } else { assertNotNull(actual); String errorMessage = "Expected " + expected + ", actual" + actual; org.junit.Assert.assertEquals(errorMessage, expected.x, actual.x, error); org.junit.Assert.assertEquals(errorMessage, expected.y, actual.y, error); org.junit.Assert.assertEquals(errorMessage, expected.z, actual.z, error); } }
static float[] toFloatArray(Tuple3f tuple) { return new float[]{tuple.getX(), tuple.getY(), tuple.getZ()}; }
public static Vector3f lerp(Tuple3f from, Tuple3f to, float progress) { Vector3f res = new Vector3f(from); res.interpolate(from, to, progress); return res; }
public void setCoords(Tuple3f coords) { this.pos = new Vector3f(coords); }