下面列出了java.nio.Buffer#rewind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test whether the indexed bone has at least one vertex in the specified
* meshes with a weight greater than the specified threshold.
*
* @param boneIndex the index of the bone (≥0)
* @param targets the meshes to search (not null, no null elements)
* @param weightThreshold the threshold (≥0, ≤1)
* @return true if at least 1 vertex found, otherwise false
*/
public static boolean hasVertices(int boneIndex, Mesh[] targets,
float weightThreshold) {
for (Mesh mesh : targets) {
VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
Buffer boneIndices = biBuf.getDataReadOnly();
FloatBuffer boneWeight
= (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();
boneIndices.rewind();
boneWeight.rewind();
int vertexComponents = mesh.getVertexCount() * 3;
for (int i = 0; i < vertexComponents; i += 3) {
int start = i / 3 * 4;
for (int k = start; k < start + 4; k++) {
if (readIndex(boneIndices, k) == boneIndex
&& boneWeight.get(k) >= weightThreshold) {
return true;
}
}
}
}
return false;
}
/**
* Load an ndarray from a blob
*
* @param blob the blob to load from
* @return the loaded ndarray
*/
@Override
public INDArray load(Blob blob) throws SQLException {
if (blob == null)
return null;
try(InputStream is = blob.getBinaryStream()) {
ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
ReadableByteChannel readableByteChannel = Channels.newChannel(is);
readableByteChannel.read(direct);
Buffer byteBuffer = (Buffer) direct;
byteBuffer.rewind();
return BinarySerde.toArray(direct);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Load an ndarray from a blob
*
* @param blob the blob to load from
* @return the loaded ndarray
*/
@Override
public INDArray load(Blob blob) throws SQLException {
if (blob == null)
return null;
try(InputStream is = blob.getBinaryStream()) {
ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
ReadableByteChannel readableByteChannel = Channels.newChannel(is);
readableByteChannel.read(direct);
Buffer byteBuffer = (Buffer) direct;
byteBuffer.rewind();
return BinarySerde.toArray(direct);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void gatherIndexData(Mesh mesh, List<Vertex> vertexLookup) {
VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
indexCount = indexBuffer.getNumElements() * 3;
Buffer b = indexBuffer.getDataReadOnly();
b.rewind();
while (b.remaining() != 0) {
Triangle tri = new Triangle();
tri.isRemoved = false;
triangleList.add(tri);
for (int i = 0; i < 3; i++) {
if (b instanceof IntBuffer) {
tri.vertexId[i] = ((IntBuffer) b).get();
} else {
//bit shift to avoid negative values due to conversion form short to int.
//we need an unsigned int here.
tri.vertexId[i] = ((ShortBuffer) b).get()& 0xffff;
}
// assert (tri.vertexId[i] < vertexLookup.size());
tri.vertex[i] = vertexLookup.get(tri.vertexId[i]);
//debug only;
tri.vertex[i].index = tri.vertexId[i];
}
if (tri.isMalformed()) {
if (!tri.isRemoved) {
logger.log(Level.FINE, "malformed triangle found with ID:{0}\n{1} It will be excluded from Lod level calculations.", new Object[]{triangleList.indexOf(tri), tri.toString()});
tri.isRemoved = true;
indexCount -= 3;
}
} else {
tri.computeNormal();
addTriangleToEdges(tri);
}
}
b.rewind();
}
/**
* Convert an ndarray to a blob
*
* @param toConvert the ndarray to convert
* @return the converted ndarray
*/
@Override
public Blob convert(INDArray toConvert) throws SQLException {
ByteBuffer byteBuffer = BinarySerde.toByteBuffer(toConvert);
Buffer buffer = (Buffer) byteBuffer;
buffer.rewind();
byte[] arr = new byte[byteBuffer.capacity()];
byteBuffer.get(arr);
Connection c = dataSource.getConnection();
Blob b = c.createBlob();
b.setBytes(1, arr);
return b;
}
/**
* Convert an ndarray to a blob
*
* @param toConvert the ndarray to convert
* @return the converted ndarray
*/
@Override
public Blob convert(INDArray toConvert) throws SQLException {
ByteBuffer byteBuffer = BinarySerde.toByteBuffer(toConvert);
Buffer buffer = (Buffer) byteBuffer;
buffer.rewind();
byte[] arr = new byte[byteBuffer.capacity()];
byteBuffer.get(arr);
Connection c = dataSource.getConnection();
Blob b = c.createBlob();
b.setBytes(1, arr);
return b;
}
public void updateBufferData(VertexBuffer vb) {
int bufId = vb.getId();
if (bufId == -1) {
// create buffer
gl.glGenBuffers(1, ib1);
bufId = ib1.get(0);
vb.setId(bufId);
objManager.registerForCleanup(vb);
}
int target;
if (vb.getBufferType() == VertexBuffer.Type.Index) {
target = gl.GL_ELEMENT_ARRAY_BUFFER;
if (context.boundElementArrayVBO != bufId) {
gl.glBindBuffer(target, bufId);
context.boundElementArrayVBO = bufId;
}
} else {
target = gl.GL_ARRAY_BUFFER;
if (context.boundArrayVBO != bufId) {
gl.glBindBuffer(target, bufId);
context.boundArrayVBO = bufId;
}
}
int usage = convertUsage(vb.getUsage());
Buffer data = vb.getData();
data.rewind();
gl.glBufferData(target,
data.capacity() * vb.getFormat().getComponentSize(),
data,
usage);
vb.clearUpdateNeeded();
}
public void updateBufferData(VertexBuffer vb) {
GL gl = GLContext.getCurrentGL();
int bufId = vb.getId();
if (bufId == -1) {
// create buffer
gl.glGenBuffers(1, intBuf1);
bufId = intBuf1.get(0);
vb.setId(bufId);
objManager.registerForCleanup(vb);
}
int target;
if (vb.getBufferType() == VertexBuffer.Type.Index) {
target = GL.GL_ELEMENT_ARRAY_BUFFER;
if (context.boundElementArrayVBO != bufId) {
gl.glBindBuffer(target, bufId);
context.boundElementArrayVBO = bufId;
}
}
else {
target = GL.GL_ARRAY_BUFFER;
if (context.boundArrayVBO != bufId) {
gl.glBindBuffer(target, bufId);
context.boundArrayVBO = bufId;
}
}
int usage = convertUsage(vb.getUsage());
Buffer data = vb.getData();
data.rewind();
gl.glBufferData(target, data.capacity() * vb.getFormat().getComponentSize(), data, usage);
vb.clearUpdateNeeded();
}
/**
* Enumerate vertices that meet the weight threshold for the indexed bone.
*
* @param mesh the mesh to analyze (not null)
* @param boneIndex the index of the bone (≥0)
* @param initialScale a scale applied to vertex positions (not null,
* unaffected)
* @param offset an offset subtracted from vertex positions (not null,
* unaffected)
* @param weightThreshold the minimum bone weight for inclusion in the
* result (≥0, ≤1)
* @return a new list of vertex coordinates (not null, length a multiple of
* 3)
*/
private static List<Float> getPoints(Mesh mesh, int boneIndex, Vector3f initialScale, Vector3f offset, float weightThreshold) {
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
Buffer boneIndices = biBuf.getDataReadOnly();
FloatBuffer boneWeight = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();
vertices.rewind();
boneIndices.rewind();
boneWeight.rewind();
ArrayList<Float> results = new ArrayList<Float>();
int vertexComponents = mesh.getVertexCount() * 3;
for (int i = 0; i < vertexComponents; i += 3) {
int k;
boolean add = false;
int start = i / 3 * 4;
for (k = start; k < start + 4; k++) {
if (readIndex(boneIndices, k) == boneIndex
&& boneWeight.get(k) >= weightThreshold) {
add = true;
break;
}
}
if (add) {
Vector3f pos = new Vector3f();
pos.x = vertices.get(i);
pos.y = vertices.get(i + 1);
pos.z = vertices.get(i + 2);
pos.subtractLocal(offset).multLocal(initialScale);
results.add(pos.x);
results.add(pos.y);
results.add(pos.z);
}
}
return results;
}
private VertexBuffer makeLod(Mesh mesh) {
VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
boolean isShortBuffer = indexBuffer.getFormat() == VertexBuffer.Format.UnsignedShort;
// Create buffers.
VertexBuffer lodBuffer = new VertexBuffer(VertexBuffer.Type.Index);
int bufsize = indexCount == 0 ? 3 : indexCount;
if (isShortBuffer) {
lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedShort, BufferUtils.createShortBuffer(bufsize));
} else {
lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedInt, BufferUtils.createIntBuffer(bufsize));
}
lodBuffer.getData().rewind();
//Check if we should fill it with a "dummy" triangle.
if (indexCount == 0) {
if (isShortBuffer) {
for (int m = 0; m < 3; m++) {
((ShortBuffer) lodBuffer.getData()).put((short) 0);
}
} else {
for (int m = 0; m < 3; m++) {
((IntBuffer) lodBuffer.getData()).put(0);
}
}
}
// Fill buffers.
Buffer buf = lodBuffer.getData();
buf.rewind();
for (Triangle triangle : triangleList) {
if (!triangle.isRemoved) {
// assert (indexCount != 0);
if (isShortBuffer) {
for (int m = 0; m < 3; m++) {
((ShortBuffer) buf).put((short) triangle.vertexId[m]);
}
} else {
for (int m = 0; m < 3; m++) {
((IntBuffer) buf).put(triangle.vertexId[m]);
}
}
}
}
buf.clear();
lodBuffer.updateData(buf);
return lodBuffer;
}
/**
* returns a list of points for the given bone
* @param mesh
* @param boneIndex
* @param offset
* @param link
* @return
*/
private static List<Float> getPoints(Mesh mesh, int boneIndex, Vector3f initialScale, Vector3f offset, float weightThreshold) {
if (mesh == null) {
throw new RuntimeException("mesh is null ");
}
if (initialScale == null) {
throw new RuntimeException("initialScale is null ");
}
if (offset == null) {
throw new RuntimeException("offset is null ");
}
if (mesh.getFloatBuffer(Type.Position) == null) {
throw new RuntimeException("verticies is null ");
}
if (mesh.getBuffer(Type.BoneIndex) == null) {
throw new RuntimeException("boneIndices is null ");
}
if (mesh.getBuffer(Type.BoneWeight) == null) {
throw new RuntimeException("boneWeight is null ");
}
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
Buffer boneIndices = (Buffer) mesh.getBuffer(Type.BoneIndex).getData();
FloatBuffer boneWeight = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();
vertices.rewind();
boneIndices.rewind();
boneWeight.rewind();
ArrayList<Float> results = new ArrayList<Float>();
int vertexComponents = mesh.getVertexCount() * 3;
for (int i = 0; i < vertexComponents; i += 3) {
int k;
boolean add = false;
int start = i / 3 * 4;
for (k = start; k < start + 4; k++) {
if (getBoneIndex(boneIndices, k) == boneIndex && boneWeight.get(k) >= weightThreshold) {
add = true;
break;
}
}
if (add) {
Vector3f pos = new Vector3f();
pos.x = vertices.get(i);
pos.y = vertices.get(i + 1);
pos.z = vertices.get(i + 2);
pos.subtractLocal(offset).multLocal(initialScale);
results.add(pos.x);
results.add(pos.y);
results.add(pos.z);
}
}
return results;
}