private static List<VertexData> splitVertices(Mesh mesh, List<VertexData> vertexData, boolean splitMirorred) { int nbVertices = mesh.getBuffer(Type.Position).getNumElements(); List<VertexData> newVertices = new ArrayList<VertexData>(); Map<Integer, Integer> indiceMap = new HashMap<Integer, Integer>(); if(vb==null || vb.getNumComponents() == 0) continue; Buffer buffer = vb.getData(); boolean isShortBuffer = vb.getFormat() == VertexBuffer.Format.UnsignedShort; for (VertexData vertex : newVertices) { for (TriangleData tri : vertex.triangles) { vb.setUpdateNeeded(); }else{ Buffer newVerts = VertexBuffer.createBuffer(vb.getFormat(), vb.getNumComponents(), nbVertices); if (buffer != null) { buffer.rewind(); bulkPut(vb.getFormat(), newVerts,buffer); newVerts.position(vertexData.size() * vb.getNumComponents()); for (int j = 0; j < newVertices.size(); j++) { int oldInd = indiceMap.get(index) ; for (int i = 0; i < vb.getNumComponents(); i++) { putValue(vb.getFormat(), newVerts, buffer, oldInd* vb.getNumComponents() + i); vb.updateData(newVerts);
BufferUtils.destroyDirectBuffer(transformInstanceData.getData()); transformInstanceData.updateData(BufferUtils.createFloatBuffer(geometries.length * INSTANCE_SIZE)); } else if (transformInstanceData == null) { transformInstanceData = new VertexBuffer(Type.InstanceData); transformInstanceData.setInstanced(true); transformInstanceData.setupData(Usage.Stream, INSTANCE_SIZE, Format.Float,
if (!indices.getData().hasArray()) { if (indices.getFormat() == Format.UnsignedByte) { ByteBuffer originalIndex = (ByteBuffer) indices.getData(); ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity()); originalIndex.clear(); arrayIndex.put(originalIndex); indices.updateData(arrayIndex); } else { ShortBuffer originalIndex = (ShortBuffer) indices.getData(); ShortBuffer arrayIndex = ShortBuffer.allocate(originalIndex.capacity()); originalIndex.clear(); arrayIndex.put(originalIndex); indices.updateData(arrayIndex); indices.setUsage(Usage.CpuOnly); if (!weights.getData().hasArray()) { FloatBuffer originalWeight = (FloatBuffer) weights.getData(); FloatBuffer arrayWeight = FloatBuffer.allocate(originalWeight.capacity()); originalWeight.clear(); arrayWeight.put(originalWeight); weights.updateData(arrayWeight); weights.setUsage(Usage.CpuOnly); positions.setUsage(Usage.Stream); if (normals != null) { normals.setUsage(Usage.Stream);
/** * @return normals buffer */ public VertexBuffer getNormalsBuffer() { VertexBuffer positionBuffer = new VertexBuffer(Type.Normal); Vector3f[] data = normals.toArray(new Vector3f[normals.size()]); positionBuffer.setupData(Usage.Static, 3, Format.Float, BufferUtils.createFloatBuffer(data)); return positionBuffer; }
private void applyFrame(Mesh target, int frameIndex, float weight){ PoseFrame frame = frames[frameIndex]; VertexBuffer pb = target.getBuffer(Type.Position); for (int i = 0; i < frame.poses.length; i++){ Pose pose = frame.poses[i]; float poseWeight = frame.weights[i] * weight; pose.apply(poseWeight, (FloatBuffer) pb.getData()); } // force to re-upload data to gpu pb.updateData(pb.getData()); }
private void setInBuffer(Mesh mesh, int index, Vector3f normal, Vector3f tangent, Vector3f binormal) { VertexBuffer NB = mesh.getBuffer(Type.Normal); VertexBuffer TB = mesh.getBuffer(Type.Tangent); VertexBuffer BB = mesh.getBuffer(Type.Binormal); BufferUtils.setInBuffer(normal, (FloatBuffer)NB.getData(), index); BufferUtils.setInBuffer(tangent, (FloatBuffer)TB.getData(), index); BufferUtils.setInBuffer(binormal, (FloatBuffer)BB.getData(), index); NB.setUpdateNeeded(); TB.setUpdateNeeded(); BB.setUpdateNeeded(); }
int currentCompsForBuf = compsForBuf[vb.getBufferType().ordinal()]; if (vb.getBufferType() != Type.Index && currentCompsForBuf != 0 && currentCompsForBuf != vb.getNumComponents()) { throw new UnsupportedOperationException("The geometry " + geom + " buffer " + vb.getBufferType() + " has different number of components than the rest of the meshes " + "(this: " + vb.getNumComponents() + ", expected: " + currentCompsForBuf + ")"); compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents(); formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat(); normForBuf[vb.getBufferType().ordinal()] = vb.isNormalized(); data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris); } else { data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts); VertexBuffer vb = new VertexBuffer(Type.values()[i]); vb.setupData(Usage.Static, compsForBuf[i], formatForBuf[i], data); vb.setNormalized(normForBuf[i]); outMesh.setBuffer(vb); FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Normal.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Tangent.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer outPos = (FloatBuffer) outBuf.getData();
VertexBuffer newIdxBuf = new VertexBuffer(Type.Index); newIdxBuf.setupData(oldIdxBuf.getUsage(), oldIdxBuf.getNumComponents(), newIndexBuf instanceof IndexIntBuffer ? Format.UnsignedInt : Format.UnsignedShort, newIndexBuf.getBuffer()); if (oldVb.getBufferType() == VertexBuffer.Type.Index) { VertexBuffer newVb = new VertexBuffer(oldVb.getBufferType()); newVb.setNormalized(oldVb.isNormalized()); if(oldVb.getData()!=null){ Buffer buffer = VertexBuffer.createBuffer(oldVb.getFormat(), oldVb.getNumComponents(), newNumVerts); newVb.setupData(oldVb.getUsage(), oldVb.getNumComponents(), oldVb.getFormat(), buffer); oldVb.copyElement(oldIndex, newVb, i); clearBuffer(newVb.getBufferType()); setBuffer(newVb);
for (Geometry geom : geoms) { for (VertexBuffer buffer : geom.getMesh().getBufferList()) { if (types.get(buffer.getBufferType()) == null) { types.put(buffer.getBufferType(), buffer); logger.log(Level.FINE, buffer.getBufferType().toString()); Integer count = typesCount.get(buffer.getBufferType()); if (count == null) { count = 0; typesCount.put(buffer.getBufferType(), count); Integer count = typesCount.get(buffer.getBufferType()); if (count != null && count < geoms.size()) { geom.getMesh().clearBuffer(buffer.getBufferType()); logger.log(Level.FINE, "removing {0} from {1}", new Object[]{buffer.getBufferType(), geom.getName()}); for (VertexBuffer.Type type : types.keySet()) { if (geom.getMesh().getBuffer(type) == null) { VertexBuffer vb = new VertexBuffer(type); Buffer b; switch (type) { case BoneIndex: case HWBoneIndex: b = BufferUtils.createIntBuffer(geom.getMesh().getVertexCount() * types.get(type).getNumComponents()); break; case InterleavedData: b = BufferUtils.createByteBuffer(geom.getMesh().getVertexCount() * types.get(type).getNumComponents());
compsForBuf[entry.getKey()] = entry.getValue().getNumComponents(); formatForBuf[entry.getKey()] = entry.getValue().getFormat(); data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris); } else { data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts); VertexBuffer vb = new VertexBuffer(Type.values()[i]); vb.setupData(Usage.Static, compsForBuf[i], formatForBuf[i], data); outMesh.setBuffer(vb); FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix); } else if (Type.Normal.ordinal() == bufType || Type.Tangent.ordinal() == bufType) { FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); } else { for (int vert = 0; vert < geomVertCount; vert++) { int curGlobalVertIndex = globalVertIndex + vert; inBuf.copyElement(vert, outBuf, curGlobalVertIndex);
VertexBuffer bindPos = new VertexBuffer(Type.BindPosePosition); bindPos.setupData(Usage.CpuOnly, pos.getNumComponents(), pos.getFormat(), BufferUtils.clone(pos.getData())); setBuffer(bindPos); pos.setUsage(Usage.Stream); VertexBuffer bindNorm = new VertexBuffer(Type.BindPoseNormal); bindNorm.setupData(Usage.CpuOnly, norm.getNumComponents(), norm.getFormat(), BufferUtils.clone(norm.getData())); setBuffer(bindNorm); norm.setUsage(Usage.Stream); VertexBuffer bindTangents = new VertexBuffer(Type.BindPoseTangent); bindTangents.setupData(Usage.CpuOnly, tangents.getNumComponents(), tangents.getFormat(), BufferUtils.clone(tangents.getData())); setBuffer(bindTangents); tangents.setUsage(Usage.Stream);
vb.getData().clear(); // reset position & limit (used later) VertexBuffer allData = new VertexBuffer(Type.InterleavedData); ByteBuffer dataBuf = BufferUtils.createByteBuffer(stride * getVertexCount()); allData.setupData(Usage.Static, 1, Format.UnsignedByte, dataBuf); for (int i = 0; i < vbs.size(); i++){ VertexBuffer vb = vbs.get(i); switch (vb.getFormat()){ case Float: FloatBuffer fb = (FloatBuffer) vb.getData(); for (int comp = 0; comp < vb.components; comp++){ dataBuf.putFloat(fb.get()); case Byte: case UnsignedByte: ByteBuffer bb = (ByteBuffer) vb.getData(); for (int comp = 0; comp < vb.components; comp++){ dataBuf.put(bb.get()); case Short: case UnsignedShort: ShortBuffer sb = (ShortBuffer) vb.getData(); for (int comp = 0; comp < vb.components; comp++){ dataBuf.putShort(sb.get()); case Int: case UnsignedInt: IntBuffer ib = (IntBuffer) vb.getData();
private Mesh createMesh() { Cylinder c = new Cylinder(30, 16, 0.1f, 1, true); ShortBuffer jointIndex = (ShortBuffer) VertexBuffer.createBuffer(VertexBuffer.Format.UnsignedShort, 4, c.getVertexCount()); jointIndex.rewind(); c.setMaxNumWeights(1); FloatBuffer jointWeight = (FloatBuffer) VertexBuffer.createBuffer(VertexBuffer.Format.Float, 4, c.getVertexCount()); jointWeight.rewind(); VertexBuffer vb = c.getBuffer(VertexBuffer.Type.Position); FloatBuffer fvb = (FloatBuffer) vb.getData(); fvb.rewind(); for (int i = 0; i < c.getVertexCount(); i++) { c.updateBound(); VertexBuffer weightsHW = new VertexBuffer(VertexBuffer.Type.HWBoneWeight); VertexBuffer indicesHW = new VertexBuffer(VertexBuffer.Type.HWBoneIndex); indicesHW.setUsage(VertexBuffer.Usage.CpuOnly); weightsHW.setUsage(VertexBuffer.Usage.CpuOnly); c.setBuffer(weightsHW); c.setBuffer(indicesHW);
public static VertexBuffer convertToUByte(VertexBuffer vb){ FloatBuffer fb = (FloatBuffer) vb.getData(); ByteBuffer bb = BufferUtils.createByteBuffer(fb.capacity()); convertToUByte(fb, bb); VertexBuffer newVb = new VertexBuffer(vb.getBufferType()); newVb.setupData(vb.getUsage(), vb.getNumComponents(), Format.UnsignedByte, bb); newVb.setNormalized(true); return newVb; }
FloatBuffer fb = (FloatBuffer) positions.getData(); if (posFmt != Format.Float){ Buffer newBuf = VertexBuffer.createBuffer(posFmt, positions.getNumComponents(), mesh.getVertexCount()); Transform t = convertPositions(fb, bbox, newBuf); VertexBuffer newPosVb = new VertexBuffer(Type.Position); newPosVb.setupData(positions.getUsage(), positions.getNumComponents(), posFmt, newBuf); fb = (FloatBuffer) normals.getData(); normals = new VertexBuffer(Type.Normal); normals.setupData(Usage.Static, 3, Format.Byte, bb); normals.setNormalized(true); mesh.clearBuffer(Type.Normal); mesh.setBuffer(normals); fb = (FloatBuffer) texcoords.getData(); if (tcFmt != Format.Float){ Buffer newBuf = VertexBuffer.createBuffer(tcFmt, texcoords.getNumComponents(), mesh.getVertexCount()); convertTexCoords2D(fb, newBuf); VertexBuffer newTcVb = new VertexBuffer(Type.TexCoord); newTcVb.setupData(texcoords.getUsage(),
private FloatBuffer getFloatBuffer(VertexBuffer vb) { if (vb == null) { return null; } return (FloatBuffer) vb.getData(); }
public static void generateBindPoseTangentsIfNecessary(Mesh mesh){ if (mesh.getBuffer(VertexBuffer.Type.BindPosePosition) != null) { VertexBuffer tangents = mesh.getBuffer(VertexBuffer.Type.Tangent); if (tangents != null) { VertexBuffer bindTangents = new VertexBuffer(VertexBuffer.Type.BindPoseTangent); bindTangents.setupData(VertexBuffer.Usage.CpuOnly, 4, VertexBuffer.Format.Float, BufferUtils.clone(tangents.getData())); if (mesh.getBuffer(VertexBuffer.Type.BindPoseTangent) != null) { mesh.clearBuffer(VertexBuffer.Type.BindPoseTangent); } mesh.setBuffer(bindTangents); tangents.setUsage(VertexBuffer.Usage.Stream); } } } }
lodSize[i] += g.getMesh().getLodLevel(i).getData().limit(); numOfVertices = g.getVertexCount(); for (int i = 0; i < lodLevels; i++) { boolean isShortBuffer = g.getMesh().getLodLevel(i).getFormat() == VertexBuffer.Format.UnsignedShort; if(isShortBuffer){ ShortBuffer buffer = (ShortBuffer) g.getMesh().getLodLevel(i).getDataReadOnly(); for (int j = 0; j < buffer.limit(); j++) { lodData[i][bufferPos[i]] = (buffer.get()& 0xffff) + indexPos[i]; IntBuffer buffer = (IntBuffer) g.getMesh().getLodLevel(i).getDataReadOnly(); for (int j = 0; j < buffer.limit(); j++) { lodData[i][bufferPos[i]] = buffer.get() + indexPos[i]; lods[i] = new VertexBuffer(Type.Index); lods[i].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(lodData[i]));
/** * Creates a {@link VertexBuffer} for the mesh or modifies * the existing one per the parameters given. * * @param type The type of the buffer * @param components Number of components * @param format Data format * @param buf The buffer data * * @throws UnsupportedOperationException If the buffer already set is * incompatible with the parameters given. */ public void setBuffer(Type type, int components, Format format, Buffer buf){ VertexBuffer vb = buffers.get(type.ordinal()); if (vb == null){ vb = new VertexBuffer(type); vb.setupData(Usage.Dynamic, components, format, buf); setBuffer(vb); }else{ if (vb.getNumComponents() != components || vb.getFormat() != format){ throw new UnsupportedOperationException("The buffer already set " + "is incompatible with the given parameters"); } vb.updateData(buf); updateCounts(); } }
public void updateBufferData(VertexBuffer vb) { int bufId = vb.getId(); boolean created = false; if (bufId == -1) { vb.setId(bufId); objManager.registerObject(vb); if (vb.getBufferType() == VertexBuffer.Type.Index) { target = GL.GL_ELEMENT_ARRAY_BUFFER; if (context.boundElementArrayVBO != bufId) { int usage = convertUsage(vb.getUsage()); vb.getData().rewind(); switch (vb.getFormat()) { case Byte: case UnsignedByte: gl.glBufferData(target, (ByteBuffer) vb.getData(), usage); break; case Short: case UnsignedShort: gl.glBufferData(target, (ShortBuffer) vb.getData(), usage); break; case Int: case UnsignedInt: glext.glBufferData(target, (IntBuffer) vb.getData(), usage); break; case Float: gl.glBufferData(target, (FloatBuffer) vb.getData(), usage);