Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions jme3-core/src/main/java/com/jme3/scene/mesh/IndexBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
*/
package com.jme3.scene.mesh;

import com.jme3.util.BufferUtils;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import com.jme3.scene.VertexBuffer.Format;
import com.jme3.util.BufferUtils;

/**
* <code>IndexBuffer</code> is an abstraction for integer index buffers,
* it is used to retrieve indices without knowing in which format they
Expand All @@ -59,21 +61,22 @@ public static IndexBuffer wrapIndexBuffer(Buffer buf) {
}

/**
* Creates an index buffer that can contain the given amount
* of vertices.
* Returns {@link IndexShortBuffer}
* Creates an index buffer that can contain the given amount of vertices.
* <br/>
* Returns either {@link IndexByteBuffer}, {@link IndexShortBuffer} or
* {@link IndexIntBuffer}
*
* @param vertexCount The amount of vertices to contain
* @param indexCount The amount of indices
* to contain.
* @return A new index buffer
* @param indexCount The amount of indices to contain
* @return A new, apropriately sized index buffer
*/
public static IndexBuffer createIndexBuffer(int vertexCount, int indexCount){
if (vertexCount > 65535){
return new IndexIntBuffer(BufferUtils.createIntBuffer(indexCount));
}else{
if (vertexCount < 128)
return new IndexByteBuffer(BufferUtils.createByteBuffer (indexCount));
else if (vertexCount < 65536)
return new IndexShortBuffer(BufferUtils.createShortBuffer(indexCount));
}
else
return new IndexIntBuffer(BufferUtils.createIntBuffer(indexCount));
}

/**
Expand Down Expand Up @@ -107,12 +110,31 @@ public int remaining() {
public abstract int get(int i);

/**
* Puts the vertex index at the index buffer's index.
* Absolute put method.
*
* <p>Puts the vertex index at the index buffer's index.
* Implementations may throw an {@link UnsupportedOperationException}
* if modifying the IndexBuffer is not supported (e.g. virtual index
* buffers).
* buffers).</p>
*
* @param i The buffer index
* @param value The vertex index
* @return This buffer
*/
public abstract void put(int i, int value);
public abstract IndexBuffer put(int i, int value);

/**
* Relative put method.
*
* <p>Puts the vertex index at the current position, then increments the
* position. Implementations may throw an
* {@link UnsupportedOperationException} if modifying the IndexBuffer is not
* supported (e.g. virtual index buffers).</p>
*
* @param value The vertex index
* @return This buffer
*/
public abstract IndexBuffer put(int value);

/**
* Returns the size of the index buffer.
Expand All @@ -129,4 +151,17 @@ public int remaining() {
* @return the underlying {@link Buffer}.
*/
public abstract Buffer getBuffer();

/**
* Returns the format of the data stored in this buffer.
*
* <p>This method can be used to set an {@link IndexBuffer} to a
* {@link com.jme3.scene.Mesh Mesh}:</p>
* <pre>
* mesh.setBuffer(Type.Index, 3,
* indexBuffer.getFormat(), indexBuffer);
* </pre>
* @return
*/
public abstract Format getFormat();
}
16 changes: 15 additions & 1 deletion jme3-core/src/main/java/com/jme3/scene/mesh/IndexByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.nio.Buffer;
import java.nio.ByteBuffer;

import com.jme3.scene.VertexBuffer.Format;

/**
* IndexBuffer implementation for {@link ByteBuffer}s.
*
Expand All @@ -59,8 +61,15 @@ public int get(int i) {
}

@Override
public void put(int i, int value) {
public IndexByteBuffer put(int i, int value) {
buf.put(i, (byte) value);
return this;
}

@Override
public IndexByteBuffer put(int value) {
buf.put((byte) value);
return this;
}

@Override
Expand All @@ -72,5 +81,10 @@ public int size() {
public Buffer getBuffer() {
return buf;
}

@Override
public Format getFormat () {
return Format.UnsignedByte;
}

}
16 changes: 15 additions & 1 deletion jme3-core/src/main/java/com/jme3/scene/mesh/IndexIntBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.nio.Buffer;
import java.nio.IntBuffer;

import com.jme3.scene.VertexBuffer.Format;

/**
* IndexBuffer implementation for {@link IntBuffer}s.
*
Expand All @@ -58,8 +60,15 @@ public int get(int i) {
}

@Override
public void put(int i, int value) {
public IndexIntBuffer put(int i, int value) {
buf.put(i, value);
return this;
}

@Override
public IndexIntBuffer put(int value) {
buf.put(value);
return this;
}

@Override
Expand All @@ -71,4 +80,9 @@ public int size() {
public Buffer getBuffer() {
return buf;
}

@Override
public Format getFormat () {
return Format.UnsignedInt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.nio.Buffer;
import java.nio.ShortBuffer;

import com.jme3.scene.VertexBuffer.Format;

/**
* IndexBuffer implementation for {@link ShortBuffer}s.
*
Expand All @@ -58,8 +60,15 @@ public int get(int i) {
}

@Override
public void put(int i, int value) {
public IndexShortBuffer put(int i, int value) {
buf.put(i, (short) value);
return this;
}

@Override
public IndexShortBuffer put(int value) {
buf.put((short) value);
return this;
}

@Override
Expand All @@ -71,4 +80,9 @@ public int size() {
public Buffer getBuffer() {
return buf;
}

@Override
public Format getFormat () {
return Format.UnsignedShort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
package com.jme3.scene.mesh;

import com.jme3.scene.Mesh.Mode;
import com.jme3.scene.VertexBuffer.Format;

import java.nio.Buffer;

/**
Expand Down Expand Up @@ -138,7 +140,7 @@ public int get(int i) {
}

@Override
public void put(int i, int value) {
public IndexBuffer put(int i, int value) {
throw new UnsupportedOperationException("Does not represent index buffer");
}

Expand All @@ -152,4 +154,15 @@ public Buffer getBuffer() {
return null;
}

@Override
public IndexBuffer put (int value) {
throw new UnsupportedOperationException("Does not represent index buffer");
}

@Override
public Format getFormat () {
// return largest size
return Format.UnsignedInt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ public static void convertToList(Mesh mesh){
default:
break;
}
if (outBuf instanceof IndexIntBuffer){
mesh.setBuffer(Type.Index, 3, (IntBuffer)outBuf.getBuffer());
}else{
mesh.setBuffer(Type.Index, 3, (ShortBuffer)outBuf.getBuffer());
}
mesh.setBuffer(Type.Index, 3, outBuf.getFormat(), outBuf.getBuffer());
}

}