package javax.media.opengl; import javax.media.opengl.util.*; import com.sun.opengl.impl.GLReflection; import java.nio.*; public class GLArrayDataClient implements GLArrayData { /** * @arg index The GL array index * @arg name The optional custom name for the GL array index, maybe null. * If null, the default name mapping will be used, see 'getPredefinedArrayIndexName(int)'. * This name might be used as the shader attribute name. * @arg comps The array component number * @arg dataType The array index GL data type * @arg normalized Wheather the data shall be normalized * * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int) */ public static GLArrayDataClient createFixed(int index, String name, int comps, int dataType, boolean normalized, int initialSize) throws GLException { GLProfile.isValidateArrayDataType(index, comps, dataType, false, true); GLArrayDataClient adc = new GLArrayDataClient(); adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, false); return adc; } public static GLArrayDataClient createFixed(int index, String name, int comps, int dataType, boolean normalized, int stride, Buffer buffer) throws GLException { GLProfile.isValidateArrayDataType(index, comps, dataType, false, true); GLArrayDataClient adc = new GLArrayDataClient(); adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, false); return adc; } public static GLArrayDataClient createGLSL(int index, String name, int comps, int dataType, boolean normalized, int initialSize) throws GLException { GLProfile.isValidateArrayDataType(index, comps, dataType, true, true); GLArrayDataClient adc = new GLArrayDataClient(); adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, true); return adc; } public static GLArrayDataClient createGLSL(int index, String name, int comps, int dataType, boolean normalized, int stride, Buffer buffer) throws GLException { GLProfile.isValidateArrayDataType(index, comps, dataType, true, true); GLArrayDataClient adc = new GLArrayDataClient(); adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, true); return adc; } // // Data read access // public boolean isVertexAttribute() { return isVertexAttribute; } public int getIndex() { return index; } public int getLocation() { return location; } public void setLocation(int v) { location = v; } public String getName() { return name; } public long getOffset() { return -1; } public Buffer getBuffer() { return buffer; } public boolean isVBO() { return false; } public int getComponents() { return components; } public int getDataType() { return dataType; } public boolean getNormalized() { return normalized; } public int getStride() { return stride; } public boolean sealed() { return sealed; } public Class getBufferClass() { return clazz; } public int getVerticeNumber() { return ( buffer!=null ) ? ( buffer.limit() / components ) : 0 ; } // // Data and GL state modification .. // public void setName(String newName) { location = -1; name = newName; } public void destroy(GL gl) { reset(gl); } public void reset(GL gl) { enableBuffer(gl, false); reset(); } public void seal(GL gl, boolean seal) { seal(seal); if(sealedGL==seal) return; sealedGL = seal; if(seal) { init_vbo(gl); enableBuffer(gl, true); } else { enableBuffer(gl, false); } } public void enableBuffer(GL gl, boolean enable) { if(enable) { checkSeal(true); if(!bufferEnabled && null!=buffer) { buffer.rewind(); enableBufferGLImpl(gl, true); } } else if(bufferEnabled) { enableBufferGLImpl(gl, false); } } // // Data modification .. // public void reset() { if(buffer!=null) { buffer.clear(); } this.sealed=false; this.bufferEnabled=false; this.bufferWritten=false; } public void seal(boolean seal) { if(sealed==seal) return; sealed = seal; if(seal) { bufferWritten=false; if (null!=buffer) { buffer.flip(); } } else { if (null!=buffer) { buffer.position(buffer.limit()); buffer.limit(buffer.capacity()); } } } public void rewind() { if(buffer!=null) { buffer.rewind(); } } public void padding(int done) { if ( buffer==null || sealed ) return; while(done0) { int osize = (buffer!=null)?buffer.capacity():0; if(clazz==ByteBuffer.class) { ByteBuffer newBBuffer = BufferUtil.newByteBuffer( (osize+additional) * components ); if(buffer!=null) { buffer.flip(); newBBuffer.put((ByteBuffer)buffer); } buffer = newBBuffer; } else if(clazz==ShortBuffer.class) { ShortBuffer newSBuffer = BufferUtil.newShortBuffer( (osize+additional) * components ); if(buffer!=null) { buffer.flip(); newSBuffer.put((ShortBuffer)buffer); } buffer = newSBuffer; } else if(clazz==IntBuffer.class) { IntBuffer newIBuffer = BufferUtil.newIntBuffer( (osize+additional) * components ); if(buffer!=null) { buffer.flip(); newIBuffer.put((IntBuffer)buffer); } buffer = newIBuffer; } else if(clazz==FloatBuffer.class) { FloatBuffer newFBuffer = BufferUtil.newFloatBuffer( (osize+additional) * components ); if(buffer!=null) { buffer.flip(); newFBuffer.put((FloatBuffer)buffer); } buffer = newFBuffer; } else { throw new GLException("Given Buffer Class not supported: "+clazz+":\n\t"+this); } } } protected final void checkSeal(boolean test) throws GLException { if(sealed!=test) { if(test) { throw new GLException("Not Sealed yet, seal first:\n\t"+this); } else { throw new GLException("Already Sealed, can't modify VBO:\n\t"+this); } } } protected void init(String name, int index, int comps, int dataType, boolean normalized, int stride, Buffer data, int initialSize, boolean isVertexAttribute) throws GLException { this.isVertexAttribute = isVertexAttribute; this.index = index; this.location = -1; this.name = (null==name)?GLContext.getPredefinedArrayIndexName(index):name; if(null==this.name) { throw new GLException("Not a valid GL array index: "+index); } this.dataType = dataType; this.clazz = getBufferClass(dataType); switch(dataType) { case GL.GL_BYTE: case GL.GL_UNSIGNED_BYTE: case GL.GL_SHORT: case GL.GL_UNSIGNED_SHORT: case GL2ES1.GL_FIXED: this.normalized = normalized; break; default: this.normalized = false; } int bpc = getBufferCompSize(); if(0