From 4667ad39359ac0f096ad8257bc3e29740493028a Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 29 Aug 2008 08:49:29 +0000 Subject: BufferUtil: - new GL type tools - new put methods ImmModeSink: - using 1 VBO array for 1-4 attributes GLArrayData - split into GLArrayData and GLArrayDataEditable, the latter of modifying purposes only - GLArrayDataWrapper implements a container only git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/branches/JOGL_2_SANDBOX@1764 232f8b59-042b-4e1e-8c03-345bb8c30851 --- .../com/sun/opengl/impl/GLFixedArrayHandler.java | 4 +- .../com/sun/opengl/impl/glsl/GLSLArrayHandler.java | 16 +- src/classes/javax/media/opengl/GLArrayData.java | 96 --- .../javax/media/opengl/GLArrayDataClient.java | 206 +---- .../javax/media/opengl/GLArrayDataEditable.java | 108 +++ .../javax/media/opengl/GLArrayDataServer.java | 87 +-- .../javax/media/opengl/GLArrayDataWrapper.java | 203 +++++ src/classes/javax/media/opengl/GLProfile.java | 30 + .../javax/media/opengl/glsl/ShaderState.java | 1 - .../opengl/util/BufferUtil.java.javame_cdc_fp | 135 ++++ .../javax/media/opengl/util/BufferUtil.java.javase | 147 ++++ .../javax/media/opengl/util/ImmModeSink.java | 867 ++++++++++++++------- 12 files changed, 1264 insertions(+), 636 deletions(-) create mode 100644 src/classes/javax/media/opengl/GLArrayDataEditable.java create mode 100644 src/classes/javax/media/opengl/GLArrayDataWrapper.java (limited to 'src') diff --git a/src/classes/com/sun/opengl/impl/GLFixedArrayHandler.java b/src/classes/com/sun/opengl/impl/GLFixedArrayHandler.java index 9882beb69..333a75aa0 100644 --- a/src/classes/com/sun/opengl/impl/GLFixedArrayHandler.java +++ b/src/classes/com/sun/opengl/impl/GLFixedArrayHandler.java @@ -6,9 +6,9 @@ import javax.media.opengl.glsl.ShaderState; import java.nio.*; public class GLFixedArrayHandler implements GLArrayHandler { - private GLArrayData ad; + private GLArrayDataEditable ad; - public GLFixedArrayHandler(GLArrayData ad) { + public GLFixedArrayHandler(GLArrayDataEditable ad) { this.ad = ad; } diff --git a/src/classes/com/sun/opengl/impl/glsl/GLSLArrayHandler.java b/src/classes/com/sun/opengl/impl/glsl/GLSLArrayHandler.java index 2910e67bd..3d48f2014 100644 --- a/src/classes/com/sun/opengl/impl/glsl/GLSLArrayHandler.java +++ b/src/classes/com/sun/opengl/impl/glsl/GLSLArrayHandler.java @@ -8,16 +8,14 @@ import javax.media.opengl.glsl.ShaderState; import java.nio.*; public class GLSLArrayHandler implements GLArrayHandler { - private GLArrayData ad; + private GLArrayDataEditable ad; - public GLSLArrayHandler(GLArrayData ad) { + public GLSLArrayHandler(GLArrayDataEditable ad) { this.ad = ad; } protected final void passVertexAttribPointer(GL2ES2 gl, ShaderState st) { - if ( ! st.glVertexAttribPointer(gl, ad) ) { - throw new RuntimeException("Internal Error"); - } + st.glVertexAttribPointer(gl, ad); } public void enableBuffer(GL gl, boolean enable) { @@ -28,9 +26,7 @@ public class GLSLArrayHandler implements GLArrayHandler { } if(enable) { - if(!st.glEnableVertexAttribArray(glsl, ad.getName())) { - throw new RuntimeException("Internal Error"); - } + st.glEnableVertexAttribArray(glsl, ad.getName()); Buffer buffer = ad.getBuffer(); @@ -53,9 +49,7 @@ public class GLSLArrayHandler implements GLArrayHandler { if(ad.isVBO()) { gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); } - if(!st.glDisableVertexAttribArray(glsl, ad.getName())) { - throw new RuntimeException("Internal Error"); - } + st.glDisableVertexAttribArray(glsl, ad.getName()); } } diff --git a/src/classes/javax/media/opengl/GLArrayData.java b/src/classes/javax/media/opengl/GLArrayData.java index bdffa3c44..a51229cd6 100644 --- a/src/classes/javax/media/opengl/GLArrayData.java +++ b/src/classes/javax/media/opengl/GLArrayData.java @@ -53,9 +53,6 @@ public interface GLArrayData { */ public void setLocation(int v); - - public boolean sealed(); - /** * Determines wheather the data is server side (VBO), * or a client side array (false). @@ -72,26 +69,11 @@ public interface GLArrayData { */ public int getVBOName(); - /** - * The VBO buffer usage, if it's an VBO, otherwise -1 - */ - public int getBufferUsage(); - /** * The Buffer holding the data, may be null in case of VBO */ public Buffer getBuffer(); - /** - * Is the buffer written to the GPU ? - */ - public boolean isBufferWritten(); - - /** - * Marks the buffer written to the GPU - */ - public void setBufferWritten(boolean written); - /** * The number of components per element */ @@ -126,83 +108,5 @@ public interface GLArrayData { public String toString(); - // - // Data and GL state modification .. - // - - public void destroy(GL gl); - - public void reset(GL gl); - - /** - * If seal is true, it - * disable write operations to the buffer. - * Calls flip, ie limit:=position and position:=0. - * Also enables the buffer for OpenGL, and passes the data. - * - * If seal is false, it - * enable write operations continuing - * at the buffer position, where you left off at seal(true), - * ie position:=limit and limit:=capacity. - * Also disables the buffer for OpenGL. - * - * @see #seal(boolean) - */ - public void seal(GL gl, boolean seal); - - /** - * Enables/disables the buffer, which implies - * the client state, binding the VBO - * and transfering the data if not done yet. - * - * The above will only be executed, - * if the buffer is disabled, - * or 'setEnableAlways' was called with 'true'. - * - * @see #setEnableAlways(boolean) - */ - public void enableBuffer(GL gl, boolean enable); - - /** - * Affects the behavior of 'enableBuffer'. - * - * The default is 'false' - * - * This is usefull when you mix up - * GLArrayData usage with conventional GL array calls. - * - * @see #enableBuffer(GL, boolean) - */ - public void setEnableAlways(boolean always); - - // - // Data modification .. - // - - public void reset(); - - /** - * If seal is true, it - * disable write operations to the buffer. - * Calls flip, ie limit:=position and position:=0. - * - * If seal is false, it - * enable write operations continuing - * at the buffer position, where you left off at seal(true), - * ie position:=limit and limit:=capacity. - * - */ - public void seal(boolean seal); - - public void rewind(); - public void padding(int done); - public void put(Buffer v); - public void putb(byte v); - public void puts(short v); - public void puti(int v); - public void putx(int v); - public void putf(float v); - public void putd(double v); - } diff --git a/src/classes/javax/media/opengl/GLArrayDataClient.java b/src/classes/javax/media/opengl/GLArrayDataClient.java index a4b4cae19..4819e9067 100644 --- a/src/classes/javax/media/opengl/GLArrayDataClient.java +++ b/src/classes/javax/media/opengl/GLArrayDataClient.java @@ -7,7 +7,7 @@ import com.sun.opengl.impl.glsl.*; import java.nio.*; -public class GLArrayDataClient implements GLArrayData { +public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayDataEditable { /** * The OpenGL ES emulation on the PC probably has a buggy VBO implementation, @@ -36,7 +36,7 @@ public class GLArrayDataClient implements GLArrayData { GLProfile.isValidateArrayDataType(index, comps, dataType, false, true); GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLFixedArrayHandler(adc); - adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, false, glArrayHandler); + adc.init(name, index, comps, dataType, normalized, 0, null, initialSize, false, glArrayHandler, 0, 0); return adc; } @@ -47,7 +47,7 @@ public class GLArrayDataClient implements GLArrayData { GLProfile.isValidateArrayDataType(index, comps, dataType, false, true); GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLFixedArrayHandler(adc); - adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, false, glArrayHandler); + adc.init(name, index, comps, dataType, normalized, stride, buffer, comps*comps, false, glArrayHandler, 0, 0); return adc; } @@ -62,7 +62,7 @@ public class GLArrayDataClient implements GLArrayData { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLSLArrayHandler(adc); - adc.init(name, -1, comps, dataType, normalized, 0, null, initialSize, true, glArrayHandler); + adc.init(name, -1, comps, dataType, normalized, 0, null, initialSize, true, glArrayHandler, 0, 0); return adc; } @@ -77,7 +77,7 @@ public class GLArrayDataClient implements GLArrayData { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLSLArrayHandler(adc); - adc.init(name, -1, comps, dataType, normalized, stride, buffer, comps*comps, true, glArrayHandler); + adc.init(name, -1, comps, dataType, normalized, stride, buffer, comps*comps, true, glArrayHandler, 0, 0); return adc; } @@ -85,74 +85,21 @@ public class GLArrayDataClient implements GLArrayData { // Data read access // - public final boolean isVertexAttribute() { return isVertexAttribute; } - - public final int getIndex() { return index; } - - public final int getLocation() { return location; } - - public final void setLocation(int v) { location = v; } - - public final String getName() { return name; } - - public long getOffset() { return -1; } - public final boolean isBufferWritten() { return bufferWritten; } - public boolean isVBO() { return false; } - - public int getVBOName() { return -1; } - - public int getBufferUsage() { return -1; } - - public final int getComponentNumber() { return components; } - - public final int getComponentType() { return dataType; } - - public final int getComponentSize() { - if(clazz==ByteBuffer.class) { - return BufferUtil.SIZEOF_BYTE; - } - if(clazz==ShortBuffer.class) { - return BufferUtil.SIZEOF_SHORT; - } - if(clazz==IntBuffer.class) { - return BufferUtil.SIZEOF_INT; - } - if(clazz==FloatBuffer.class) { - return BufferUtil.SIZEOF_FLOAT; - } - throw new GLException("Given Buffer Class not supported: "+clazz+":\n\t"+this); - } - - public final int getElementNumber() { - if(null==buffer) return 0; - return ( sealed ) ? ( buffer.limit() / components ) : ( buffer.position() / components ) ; - } - - public final boolean getNormalized() { return normalized; } - - public final int getStride() { return stride; } - public final boolean sealed() { return sealed; } - public final Class getBufferClass() { return clazz; } + public int getBufferUsage() { return -1; } // // Data and GL state modification .. // - public final Buffer getBuffer() { return buffer; } - public final void setBufferWritten(boolean written) { bufferWritten=written; } - public void setName(String newName) { - location = -1; - name = newName; - } - public void destroy(GL gl) { reset(gl); + buffer=null; } public void reset(GL gl) { @@ -234,17 +181,7 @@ public class GLArrayDataClient implements GLArrayData { public void padding(int done) { if ( buffer==null || sealed ) return; while(done0); + } + + protected void init(String name, int index, int comps, int dataType, boolean normalized, int stride, Buffer data, + boolean isVertexAttribute, + int vboName, long bufferOffset) + 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 = getComponentSize(); + if(00; + this.bufferOffset=bufferOffset; + } + + protected GLArrayDataWrapper() { } + + protected int index; + protected int location; + protected String name; + protected int components; + protected int dataType; + protected boolean normalized; + protected int stride; // user given stride + protected int strideB; // stride in bytes + protected int strideL; // stride in logical components + protected Class clazz; + protected Buffer buffer; + protected boolean isVertexAttribute; + + protected long bufferOffset; + protected int vboName; + protected boolean vboUsage; +} + diff --git a/src/classes/javax/media/opengl/GLProfile.java b/src/classes/javax/media/opengl/GLProfile.java index ac37305b2..14a57cc8c 100644 --- a/src/classes/javax/media/opengl/GLProfile.java +++ b/src/classes/javax/media/opengl/GLProfile.java @@ -223,6 +223,36 @@ public class GLProfile { } } + public static String getGLTypeName(int type) { + switch (type) { + case GL.GL_UNSIGNED_BYTE: + return "GL_UNSIGNED_BYTE"; + case GL.GL_BYTE: + return "GL_BYTE"; + case GL.GL_UNSIGNED_SHORT: + return "GL_UNSIGNED_SHORT"; + case GL.GL_SHORT: + return "GL_SHORT"; + case GL.GL_FLOAT: + return "GL_FLOAT"; + case GL.GL_FIXED: + return "GL_FIXED"; + case javax.media.opengl.GL2ES2.GL_INT: + return "GL_INT"; + case javax.media.opengl.GL2ES2.GL_UNSIGNED_INT: + return "GL_UNSIGNED_INT"; + case javax.media.opengl.GL2.GL_DOUBLE: + return "GL_DOUBLE"; + case javax.media.opengl.GL2.GL_2_BYTES: + return "GL_2_BYTES"; + case javax.media.opengl.GL2.GL_3_BYTES: + return "GL_3_BYTES"; + case javax.media.opengl.GL2.GL_4_BYTES: + return "GL_4_BYTES"; + } + return null; + } + /** * General validation if type is a valid GL data type * for the current profile diff --git a/src/classes/javax/media/opengl/glsl/ShaderState.java b/src/classes/javax/media/opengl/glsl/ShaderState.java index c6c1f2152..6419402fd 100644 --- a/src/classes/javax/media/opengl/glsl/ShaderState.java +++ b/src/classes/javax/media/opengl/glsl/ShaderState.java @@ -459,7 +459,6 @@ public class ShaderState { if(0>loc) { // not used in shader - System.err.println("*** skip: "+name); continue; } diff --git a/src/classes/javax/media/opengl/util/BufferUtil.java.javame_cdc_fp b/src/classes/javax/media/opengl/util/BufferUtil.java.javame_cdc_fp index 425756c07..2496950be 100755 --- a/src/classes/javax/media/opengl/util/BufferUtil.java.javame_cdc_fp +++ b/src/classes/javax/media/opengl/util/BufferUtil.java.javame_cdc_fp @@ -39,6 +39,13 @@ package javax.media.opengl.util; +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLException; +import javax.media.opengl.GLProfile; +import com.sun.opengl.impl.GLReflection; + import java.nio.*; import java.util.*; @@ -54,12 +61,76 @@ public class BufferUtil { public static final int SIZEOF_LONG = -1; // not supported public static final int SIZEOF_DOUBLE = -1; // not supported + public static final int sizeOfGLType(int glType) { + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + return SIZEOF_BYTE; + case GL.GL_BYTE: + return SIZEOF_BYTE; + case GL.GL_UNSIGNED_SHORT: + return SIZEOF_SHORT; + case GL.GL_SHORT: + return SIZEOF_SHORT; + case GL.GL_FLOAT: + return SIZEOF_FLOAT; + case GL.GL_FIXED: + return SIZEOF_INT; + case GL2ES2.GL_INT: + return SIZEOF_INT; + case GL2ES2.GL_UNSIGNED_INT: + return SIZEOF_INT; + case GL2.GL_DOUBLE: + return SIZEOF_DOUBLE; + } + return -1; + } + private BufferUtil() {} //---------------------------------------------------------------------- // Allocation routines // + public static final Buffer newGLBuffer(int glType, int numElements) { + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + case GL.GL_BYTE: + return newByteBuffer(numElements); + case GL.GL_UNSIGNED_SHORT: + case GL.GL_SHORT: + return newShortBuffer(numElements); + case GL.GL_FLOAT: + return newFloatBuffer(numElements); + case GL.GL_FIXED: + case GL2ES2.GL_INT: + case GL2ES2.GL_UNSIGNED_INT: + return newIntBuffer(numElements); + } + return null; + } + + public static final Buffer sliceGLBuffer(ByteBuffer parent, int bytePos, int byteLen, int glType) { + if(parent==null || byteLen==0) return null; + parent.position(bytePos); + parent.limit(bytePos + byteLen); + + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + case GL.GL_BYTE: + return parent.slice(); + case GL.GL_UNSIGNED_SHORT: + case GL.GL_SHORT: + return parent.asShortBuffer(); + case GL.GL_FLOAT: + return parent.asFloatBuffer(); + case GL.GL_FIXED: + case GL2ES2.GL_INT: + case GL2ES2.GL_UNSIGNED_INT: + return parent.asIntBuffer(); + } + return null; + } + /** Allocates a new direct ByteBuffer with the specified number of elements. The returned buffer will have its byte order set to the host platform's native byte order. */ @@ -287,6 +358,70 @@ public class BufferUtil { return buf; } + //---------------------------------------------------------------------- + // Convenient GL put methods with generic target Buffer + // + public static void put(Buffer dest, Buffer v) { + Class dClazz = dest.getClass(); + Class vClazz = v.getClass(); + if(!GLReflection.instanceOf(vClazz, dClazz.getName())) { + throw new GLException("This array's dest class "+dClazz+" doesn't match the argument's Class: "+vClazz); + } + if(dest instanceof ByteBuffer) { + ((ByteBuffer)dest).put((ByteBuffer)v); + } else if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put((ShortBuffer)v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((IntBuffer)v); + } else if(dest instanceof FloatBuffer) { + ((FloatBuffer)dest).put((FloatBuffer)v); + } + } + + public static void putb(Buffer dest, byte v) { + if(dest instanceof ByteBuffer) { + ((ByteBuffer)dest).put(v); + } else if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put((short)v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((int)v); + } else { + throw new GLException("Byte doesn't match Buffer Class: "+dest); + } + } + + public static void puts(Buffer dest, short v) { + if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put(v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((int)v); + } else { + throw new GLException("Short doesn't match Buffer Class: "+dest); + } + } + + public static void puti(Buffer dest, int v) { + if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put(v); + } else { + throw new GLException("Integer doesn't match Buffer Class: "+dest); + } + } + + public static void putx(Buffer dest, int v) { + puti(dest, v); + } + + public static void putf(Buffer dest, float v) { + if(dest instanceof FloatBuffer) { + ((FloatBuffer)dest).put(v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put(FixedPoint.toFixed(v)); + } else { + throw new GLException("Float doesn't match Buffer Class: "+dest); + } + } + //---------------------------------------------------------------------- // Internals only below this point // diff --git a/src/classes/javax/media/opengl/util/BufferUtil.java.javase b/src/classes/javax/media/opengl/util/BufferUtil.java.javase index 6ea6fe2c7..f9f6a5d24 100755 --- a/src/classes/javax/media/opengl/util/BufferUtil.java.javase +++ b/src/classes/javax/media/opengl/util/BufferUtil.java.javase @@ -39,6 +39,13 @@ package javax.media.opengl.util; +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLException; +import javax.media.opengl.GLProfile; +import com.sun.opengl.impl.GLReflection; + import java.nio.*; import java.util.*; @@ -54,12 +61,80 @@ public class BufferUtil { public static final int SIZEOF_LONG = 8; public static final int SIZEOF_DOUBLE = 8; + public static final int sizeOfGLType(int glType) { + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + return SIZEOF_BYTE; + case GL.GL_BYTE: + return SIZEOF_BYTE; + case GL.GL_UNSIGNED_SHORT: + return SIZEOF_SHORT; + case GL.GL_SHORT: + return SIZEOF_SHORT; + case GL.GL_FLOAT: + return SIZEOF_FLOAT; + case GL.GL_FIXED: + return SIZEOF_INT; + case GL2ES2.GL_INT: + return SIZEOF_INT; + case GL2ES2.GL_UNSIGNED_INT: + return SIZEOF_INT; + case GL2.GL_DOUBLE: + return SIZEOF_DOUBLE; + } + return -1; + } + private BufferUtil() {} //---------------------------------------------------------------------- // Allocation routines // + public static final Buffer newGLBuffer(int glType, int numElements) { + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + case GL.GL_BYTE: + return newByteBuffer(numElements); + case GL.GL_UNSIGNED_SHORT: + case GL.GL_SHORT: + return newShortBuffer(numElements); + case GL.GL_FLOAT: + return newFloatBuffer(numElements); + case GL.GL_FIXED: + case GL2ES2.GL_INT: + case GL2ES2.GL_UNSIGNED_INT: + return newIntBuffer(numElements); + case GL2.GL_DOUBLE: + return newDoubleBuffer(numElements); + } + return null; + } + + public static final Buffer sliceGLBuffer(ByteBuffer parent, int bytePos, int byteLen, int glType) { + if(parent==null || byteLen==0) return null; + parent.position(bytePos); + parent.limit(bytePos + byteLen); + + switch (glType) { + case GL.GL_UNSIGNED_BYTE: + case GL.GL_BYTE: + return parent.slice(); + case GL.GL_UNSIGNED_SHORT: + case GL.GL_SHORT: + return parent.asShortBuffer(); + case GL.GL_FLOAT: + return parent.asFloatBuffer(); + case GL.GL_FIXED: + case GL2ES2.GL_INT: + case GL2ES2.GL_UNSIGNED_INT: + return parent.asIntBuffer(); + case GL2.GL_DOUBLE: + return parent.asDoubleBuffer(); + } + return null; + } + /** Allocates a new direct ByteBuffer with the specified number of elements. The returned buffer will have its byte order set to the host platform's native byte order. */ @@ -323,6 +398,78 @@ public class BufferUtil { return buf; } + //---------------------------------------------------------------------- + // Convenient GL put methods with generic target Buffer + // + public static void put(Buffer dest, Buffer v) { + Class dClazz = dest.getClass(); + Class vClazz = v.getClass(); + if(!GLReflection.instanceOf(vClazz, dClazz.getName())) { + throw new GLException("This array's dest class "+dClazz+" doesn't match the argument's Class: "+vClazz); + } + if(dest instanceof ByteBuffer) { + ((ByteBuffer)dest).put((ByteBuffer)v); + } else if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put((ShortBuffer)v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((IntBuffer)v); + } else if(dest instanceof FloatBuffer) { + ((FloatBuffer)dest).put((FloatBuffer)v); + } + } + + public static void putb(Buffer dest, byte v) { + if(dest instanceof ByteBuffer) { + ((ByteBuffer)dest).put(v); + } else if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put((short)v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((int)v); + } else { + throw new GLException("Byte doesn't match Buffer Class: "+dest); + } + } + + public static void puts(Buffer dest, short v) { + if(dest instanceof ShortBuffer) { + ((ShortBuffer)dest).put(v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put((int)v); + } else { + throw new GLException("Short doesn't match Buffer Class: "+dest); + } + } + + public static void puti(Buffer dest, int v) { + if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put(v); + } else { + throw new GLException("Integer doesn't match Buffer Class: "+dest); + } + } + + public static void putx(Buffer dest, int v) { + puti(dest, v); + } + + public static void putf(Buffer dest, float v) { + if(dest instanceof FloatBuffer) { + ((FloatBuffer)dest).put(v); + } else if(dest instanceof IntBuffer) { + ((IntBuffer)dest).put(FixedPoint.toFixed(v)); + } else { + throw new GLException("Float doesn't match Buffer Class: "+dest); + } + } + + public static void putd(Buffer dest, double v) { + if(dest instanceof FloatBuffer) { + ((FloatBuffer)dest).put((float)v); + } else { + throw new GLException("Double doesn't match Buffer Class: "+dest); + } + } + //---------------------------------------------------------------------- // Internals only below this point // diff --git a/src/classes/javax/media/opengl/util/ImmModeSink.java b/src/classes/javax/media/opengl/util/ImmModeSink.java index 4ef8cacf7..16b030e69 100644 --- a/src/classes/javax/media/opengl/util/ImmModeSink.java +++ b/src/classes/javax/media/opengl/util/ImmModeSink.java @@ -144,11 +144,12 @@ public class ImmModeSink { e.printStackTrace(); } if(immediateDraw) { - vboSet.seal(gl, false); + vboSet.seal(gl, true); vboSet.draw(gl, indices, true, -1); reset(gl); } else { vboSet.seal(gl, true); + vboSet.enableBuffer(gl, false); vboSetList.add(vboSet); vboSet = vboSet.regenerate(); } @@ -157,6 +158,16 @@ public class ImmModeSink { public void glVertexv(Buffer v) { vboSet.glVertexv(v); } + public void glNormalv(Buffer v) { + vboSet.glNormalv(v); + } + public void glColorv(Buffer v) { + vboSet.glColorv(v); + } + public void glTexCoordv(Buffer v) { + vboSet.glTexCoordv(v); + } + public final void glVertex2f(float x, float y) { vboSet.glVertex2f(x,y); } @@ -165,16 +176,10 @@ public class ImmModeSink { vboSet.glVertex3f(x,y,z); } - public void glNormalv(Buffer v) { - vboSet.glNormalv(v); - } public final void glNormal3f(float x, float y, float z) { vboSet.glNormal3f(x,y,z); } - public void glColorv(Buffer v) { - vboSet.glColorv(v); - } public final void glColor3f(float x, float y, float z) { vboSet.glColor3f(x,y,z); } @@ -183,9 +188,6 @@ public class ImmModeSink { vboSet.glColor4f(x,y,z, a); } - public void glTexCoordv(Buffer v) { - vboSet.glTexCoordv(v); - } public final void glTexCoord2f(float x, float y) { vboSet.glTexCoord2f(x,y); } @@ -292,27 +294,15 @@ public class ImmModeSink { this.tComps=tComps; this.useGLSL=useGLSL; - if(!useGLSL) { - this.vertexVBO = GLArrayDataServer.createFixed(GL.GL_VERTEX_ARRAY, null, vComps, vDataType, false, initialSize, glBufferUsage); - this.colorVBO = GLArrayDataServer.createFixed(GL.GL_COLOR_ARRAY, null, cComps, cDataType, false, initialSize, glBufferUsage); - this.normalVBO = GLArrayDataServer.createFixed(GL.GL_NORMAL_ARRAY, null, nComps, nDataType, false, initialSize, glBufferUsage); - this.texcoordVBO = GLArrayDataServer.createFixed(GL.GL_TEXTURE_COORD_ARRAY, null, tComps, tDataType, false, initialSize, glBufferUsage); - } else { - this.vertexVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Vertex, vComps, vDataType, false, initialSize, glBufferUsage); - this.colorVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Color, cComps, cDataType, false, initialSize, glBufferUsage); - this.normalVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Normal, nComps, nDataType, false, initialSize, glBufferUsage); - this.texcoordVBO = GLArrayDataServer.createGLSL(GLContext.mgl_MultiTexCoord, tComps, tDataType, false, initialSize, glBufferUsage); - } - if(!vboUsage) { - this.vertexVBO.setVBOUsage(vboUsage); - this.colorVBO.setVBOUsage(vboUsage); - this.normalVBO.setVBOUsage(vboUsage); - this.texcoordVBO.setVBOUsage(vboUsage); - } + allocateBuffer(initialSize); + rewind(); this.sealed=false; + this.sealedGL=false; this.mode = -1; this.modeOrig = -1; + this.bufferEnabled=false; + this.bufferWritten=false; } protected final VBOSet regenerate() { @@ -320,39 +310,6 @@ public class ImmModeSink { vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType, useGLSL); } - protected void destroy(GL gl) { - vertexVBO.destroy(gl); - normalVBO.destroy(gl); - colorVBO.destroy(gl); - texcoordVBO.destroy(gl); - - this.mode = -1; - this.modeOrig = -1; - this.sealed=false; - } - - protected void reset(GL gl) { - vertexVBO.reset(gl); - normalVBO.reset(gl); - colorVBO.reset(gl); - texcoordVBO.reset(gl); - - this.mode = -1; - this.modeOrig = -1; - this.sealed=false; - } - - public String toString() { - return "VBOSet[mode "+mode+ - ", modeOrig "+modeOrig+ - ", sealed "+sealed+ - ",\n\t"+vertexVBO+ - ",\n\t"+normalVBO+ - ",\n\t"+colorVBO+ - ",\n\t"+texcoordVBO+ - "]"; - } - protected void checkSeal(boolean test) throws GLException { if(mode<0) { throw new GLException("No mode set yet, call glBegin(mode) first:\n\t"+this); @@ -366,47 +323,17 @@ public class ImmModeSink { } } - protected void rewind() { - checkSeal(true); - - vertexVBO.rewind(); - normalVBO.rewind(); - colorVBO.rewind(); - texcoordVBO.rewind(); - } - - protected void seal(GL gl, boolean disableBufferAfterSeal) - { - checkSeal(false); - sealed = true; - - vertexVBO.seal(gl, true); - normalVBO.seal(gl, true); - colorVBO.seal(gl, true); - texcoordVBO.seal(gl, true); - - if(disableBufferAfterSeal) { - vertexVBO.enableBuffer(gl, false); - normalVBO.enableBuffer(gl, false); - colorVBO.enableBuffer(gl, false); - texcoordVBO.enableBuffer(gl, false); - } - } - protected void draw(GL gl, Buffer indices, boolean disableBufferAfterDraw, int i) { if(DEBUG_DRAW) { Exception e = new Exception("ImmModeSink.draw["+i+"](disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this); e.printStackTrace(); } - normalVBO.enableBuffer(gl, true); - colorVBO.enableBuffer(gl, true); - texcoordVBO.enableBuffer(gl, true); - vertexVBO.enableBuffer(gl, true); + enableBuffer(gl, true); - if (vertexVBO.getBuffer()!=null) { + if (buffer!=null) { if(null==indices) { - gl.glDrawArrays(mode, 0, vertexVBO.getElementNumber()); + gl.glDrawArrays(mode, 0, count); } else { Class clazz = indices.getClass(); int type=-1; @@ -424,233 +351,615 @@ public class ImmModeSink { } if(disableBufferAfterDraw) { - vertexVBO.enableBuffer(gl, false); - texcoordVBO.enableBuffer(gl, false); - colorVBO.enableBuffer(gl, false); - normalVBO.enableBuffer(gl, false); + enableBuffer(gl, false); } } - protected void glVertexv(Buffer v) { + public void glVertexv(Buffer v) { checkSeal(false); - vertexVBO.put(v); + BufferUtil.put(vertexArray, v); } - protected void glVertex2f(float x, float y) { + public void glNormalv(Buffer v) { checkSeal(false); - vertexVBO.putf(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.putf(y); - vertexVBO.padding(2); + BufferUtil.put(normalArray, v); } - protected void glVertex3f(float x, float y, float z) { + public void glColorv(Buffer v) { checkSeal(false); - vertexVBO.putf(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.putf(y); - if(vertexVBO.getComponentNumber()>2) - vertexVBO.putf(z); - vertexVBO.padding(3); + BufferUtil.put(colorArray, v); } - - protected void glNormalv(Buffer v) { + public void glTexCoordv(Buffer v) { checkSeal(false); - normalVBO.put(v); + BufferUtil.put(textCoordArray, v); } - protected void glNormal3f(float x, float y, float z) { + + public void glVertex2b(byte x, byte y) { checkSeal(false); - normalVBO.putf(x); - if(normalVBO.getComponentNumber()>1) - normalVBO.putf(y); - if(normalVBO.getComponentNumber()>2) - normalVBO.putf(z); - normalVBO.padding(3); + growBufferIfNecessary(VERTEX, 2); + if(vComps>0) + BufferUtil.putb(vertexArray, x); + if(vComps>1) + BufferUtil.putb(vertexArray, y); + padding(VERTEX, vComps-2); + } + public void glVertex3b(byte x, byte y, byte z) { + checkSeal(false); + growBufferIfNecessary(VERTEX, 3); + if(vComps>0) + BufferUtil.putb(vertexArray, x); + if(vComps>1) + BufferUtil.putb(vertexArray, y); + if(vComps>2) + BufferUtil.putb(vertexArray, z); + padding(VERTEX, vComps-3); + } + public void glVertex2s(short x, short y) { + checkSeal(false); + growBufferIfNecessary(VERTEX, 2); + if(vComps>0) + BufferUtil.puts(vertexArray, x); + if(vComps>1) + BufferUtil.puts(vertexArray, y); + padding(VERTEX, vComps-2); + } + public void glVertex3s(short x, short y, short z) { + checkSeal(false); + growBufferIfNecessary(VERTEX, 3); + if(vComps>0) + BufferUtil.puts(vertexArray, x); + if(vComps>1) + BufferUtil.puts(vertexArray, y); + if(vComps>2) + BufferUtil.puts(vertexArray, z); + padding(VERTEX, vComps-3); + } + public void glVertex2f(float x, float y) { + checkSeal(false); + growBufferIfNecessary(VERTEX, 2); + if(vComps>0) + BufferUtil.putf(vertexArray, x); + if(vComps>1) + BufferUtil.putf(vertexArray, y); + padding(VERTEX, vComps-2); + } + public void glVertex3f(float x, float y, float z) { + checkSeal(false); + growBufferIfNecessary(VERTEX, 3); + if(vComps>0) + BufferUtil.putf(vertexArray, x); + if(vComps>1) + BufferUtil.putf(vertexArray, y); + if(vComps>2) + BufferUtil.putf(vertexArray, z); + padding(VERTEX, vComps-3); } - protected void glColorv(Buffer v) { + public void glNormal3b(byte x, byte y, byte z) { checkSeal(false); - colorVBO.put(v); - } - protected void glColor3f(float x, float y, float z) { + growBufferIfNecessary(NORMAL, 3); + if(nComps>0) + BufferUtil.putb(normalArray, x); + if(nComps>1) + BufferUtil.putb(normalArray, y); + if(nComps>2) + BufferUtil.putb(normalArray, z); + padding(NORMAL, nComps-3); + } + public void glNormal3s(short x, short y, short z) { checkSeal(false); - colorVBO.putf(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.putf(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.putf(z); - colorVBO.padding(3); - } - protected void glColor4f(float x, float y, float z, float a) { + growBufferIfNecessary(NORMAL, 3); + if(nComps>0) + BufferUtil.puts(normalArray, x); + if(nComps>1) + BufferUtil.puts(normalArray, y); + if(nComps>2) + BufferUtil.puts(normalArray, z); + padding(NORMAL, nComps-3); + } + public void glNormal3f(float x, float y, float z) { checkSeal(false); - colorVBO.putf(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.putf(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.putf(z); - if(colorVBO.getComponentNumber()>3) - colorVBO.putf(a); - colorVBO.padding(4); + growBufferIfNecessary(NORMAL, 3); + if(nComps>0) + BufferUtil.putf(normalArray, x); + if(nComps>1) + BufferUtil.putf(normalArray, y); + if(nComps>2) + BufferUtil.putf(normalArray, z); + padding(NORMAL, nComps-3); } - protected void glTexCoordv(Buffer v) { + public void glColor3b(byte r, byte g, byte b) { checkSeal(false); - texcoordVBO.put(v); - } - protected void glTexCoord2f(float x, float y) { + growBufferIfNecessary(COLOR, 3); + if(cComps>0) + BufferUtil.putb(colorArray, r); + if(cComps>1) + BufferUtil.putb(colorArray, g); + if(cComps>2) + BufferUtil.putb(colorArray, b); + padding(COLOR, cComps-3); + } + public void glColor4b(byte r, byte g, byte b, byte a) { checkSeal(false); - texcoordVBO.putf(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.putf(y); - texcoordVBO.padding(2); - } - protected void glTexCoord3f(float x, float y, float z) { + growBufferIfNecessary(COLOR, 4); + if(cComps>0) + BufferUtil.putb(colorArray, r); + if(cComps>1) + BufferUtil.putb(colorArray, g); + if(cComps>2) + BufferUtil.putb(colorArray, b); + if(cComps>3) + BufferUtil.putb(colorArray, a); + padding(COLOR, cComps-4); + } + public void glColor3s(short r, short g, short b) { + checkSeal(false); + growBufferIfNecessary(COLOR, 3); + if(cComps>0) + BufferUtil.puts(colorArray, r); + if(cComps>1) + BufferUtil.puts(colorArray, g); + if(cComps>2) + BufferUtil.puts(colorArray, b); + padding(COLOR, cComps-3); + } + public void glColor4s(short r, short g, short b, short a) { + checkSeal(false); + growBufferIfNecessary(COLOR, 4); + if(cComps>0) + BufferUtil.puts(colorArray, r); + if(cComps>1) + BufferUtil.puts(colorArray, g); + if(cComps>2) + BufferUtil.puts(colorArray, b); + if(cComps>3) + BufferUtil.puts(colorArray, a); + padding(COLOR, cComps-4); + } + public void glColor3f(float r, float g, float b) { + checkSeal(false); + growBufferIfNecessary(COLOR, 3); + if(cComps>0) + BufferUtil.putf(colorArray, r); + if(cComps>1) + BufferUtil.putf(colorArray, g); + if(cComps>2) + BufferUtil.putf(colorArray, b); + padding(COLOR, cComps-3); + } + public void glColor4f(float r, float g, float b, float a) { + checkSeal(false); + growBufferIfNecessary(COLOR, 4); + if(cComps>0) + BufferUtil.putf(colorArray, r); + if(cComps>1) + BufferUtil.putf(colorArray, g); + if(cComps>2) + BufferUtil.putf(colorArray, b); + if(cComps>3) + BufferUtil.putf(colorArray, a); + padding(COLOR, cComps-4); + } + + public void glTexCoord2b(byte x, byte y) { checkSeal(false); - texcoordVBO.putf(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.putf(y); - if(texcoordVBO.getComponentNumber()>2) - texcoordVBO.putf(z); - texcoordVBO.padding(3); + growBufferIfNecessary(TEXTCOORD, 2); + if(tComps>0) + BufferUtil.putb(textCoordArray, x); + if(tComps>1) + BufferUtil.putb(textCoordArray, y); + padding(TEXTCOORD, tComps-2); + } + public void glTexCoord3b(byte x, byte y, byte z) { + checkSeal(false); + growBufferIfNecessary(TEXTCOORD, 3); + if(tComps>0) + BufferUtil.putb(textCoordArray, x); + if(tComps>1) + BufferUtil.putb(textCoordArray, y); + if(tComps>2) + BufferUtil.putb(textCoordArray, z); + padding(TEXTCOORD, tComps-3); + } + public void glTexCoord2s(short x, short y) { + checkSeal(false); + growBufferIfNecessary(TEXTCOORD, 2); + if(tComps>0) + BufferUtil.puts(textCoordArray, x); + if(tComps>1) + BufferUtil.puts(textCoordArray, y); + padding(TEXTCOORD, tComps-2); + } + public void glTexCoord3s(short x, short y, short z) { + checkSeal(false); + growBufferIfNecessary(TEXTCOORD, 3); + if(tComps>0) + BufferUtil.puts(textCoordArray, x); + if(tComps>1) + BufferUtil.puts(textCoordArray, y); + if(tComps>2) + BufferUtil.puts(textCoordArray, z); + padding(TEXTCOORD, tComps-3); + } + public void glTexCoord2f(float x, float y) { + checkSeal(false); + growBufferIfNecessary(TEXTCOORD, 2); + if(tComps>0) + BufferUtil.putf(textCoordArray, x); + if(tComps>1) + BufferUtil.putf(textCoordArray, y); + padding(TEXTCOORD, tComps-2); + } + public void glTexCoord3f(float x, float y, float z) { + checkSeal(false); + growBufferIfNecessary(TEXTCOORD, 3); + if(tComps>0) + BufferUtil.putf(textCoordArray, x); + if(tComps>1) + BufferUtil.putf(textCoordArray, y); + if(tComps>2) + BufferUtil.putf(textCoordArray, z); + padding(TEXTCOORD, tComps-3); + } + + public void rewind() { + if(null!=vertexArray) { + vertexArray.rewind(); + } + if(null!=colorArray) { + colorArray.rewind(); + } + if(null!=normalArray) { + normalArray.rewind(); + } + if(null!=textCoordArray) { + textCoordArray.rewind(); + } } - protected void glVertex2s(short x, short y) { - checkSeal(false); - vertexVBO.puts(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.puts(y); - vertexVBO.padding(2); + public void destroy(GL gl) { + reset(gl); + + vertexArray=null; colorArray=null; normalArray=null; textCoordArray=null; + vArrayData=null; cArrayData=null; nArrayData=null; tArrayData=null; + buffer=null; + bSize=0; count=0; } - protected void glVertex3s(short x, short y, short z) { - checkSeal(false); - vertexVBO.puts(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.puts(y); - if(vertexVBO.getComponentNumber()>2) - vertexVBO.puts(z); - vertexVBO.padding(3); + + public void reset(GL gl) { + enableBuffer(gl, false); + reset(); } - protected void glNormal3s(short x, short y, short z) { - checkSeal(false); - normalVBO.puts(x); - if(normalVBO.getComponentNumber()>1) - normalVBO.puts(y); - if(normalVBO.getComponentNumber()>2) - normalVBO.puts(z); - normalVBO.padding(3); + public void reset() { + if(buffer!=null) { + buffer.clear(); + } + rewind(); + + this.mode = -1; + this.modeOrig = -1; + this.sealed=false; + this.bufferEnabled=false; + this.bufferWritten=false; } - protected void glColor3s(short x, short y, short z) { - checkSeal(false); - colorVBO.puts(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.puts(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.puts(z); - colorVBO.padding(3); - } - protected void glColor4s(short x, short y, short z, short a) { - checkSeal(false); - colorVBO.puts(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.puts(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.puts(z); - if(colorVBO.getComponentNumber()>3) - colorVBO.puts(a); - colorVBO.padding(4); + public void seal(GL gl, boolean seal) + { + seal(seal); + if(sealedGL==seal) return; + sealedGL = seal; + if(seal) { + if(vboUsage && vboName==0) { + int[] tmp = new int[1]; + gl.glGenBuffers(1, tmp, 0); + vboName = tmp[0]; + } + if(null!=vArrayData) + vArrayData.setVBOName(vboName); + if(null!=cArrayData) + cArrayData.setVBOName(vboName); + if(null!=nArrayData) + nArrayData.setVBOName(vboName); + if(null!=tArrayData) + tArrayData.setVBOName(vboName); + enableBuffer(gl, true); + } else { + enableBuffer(gl, false); + } } - protected void glTexCoord2s(short x, short y) { - checkSeal(false); - texcoordVBO.puts(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.puts(y); - texcoordVBO.padding(2); + public void seal(boolean seal) + { + if(sealed==seal) return; + sealed = seal; + if(seal) { + bufferWritten=false; + } } - protected void glTexCoord3s(short x, short y, short z) { - checkSeal(false); - texcoordVBO.puts(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.puts(y); - if(texcoordVBO.getComponentNumber()>2) - texcoordVBO.puts(z); - texcoordVBO.padding(3); + + public void enableBuffer(GL gl, boolean enable) { + /* if(enableBufferAlways && enable) { + bufferEnabled = false; + } */ + if( bufferEnabled != enable && count>0 ) { + if(enable) { + checkSeal(true); + } + if(useGLSL) { + enableBufferGLSL(gl, enable); + } else { + enableBufferFixed(gl, enable); + } + bufferEnabled = enable; } + } - protected void glVertex2b(byte x, byte y) { - checkSeal(false); - vertexVBO.putb(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.putb(y); - vertexVBO.padding(2); + public void enableBufferFixed(GL gl, boolean enable) { + if(enable) { + gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); + + if(!bufferWritten) { + gl.glBufferData(GL.GL_ARRAY_BUFFER, buffer.limit(), buffer, GL.GL_STATIC_DRAW); + bufferWritten=true; + } + + if(vComps>0) { + gl.glEnableClientState(gl.GL_VERTEX_ARRAY); + gl.glVertexPointer(vArrayData); + } + if(cComps>0) { + gl.glEnableClientState(gl.GL_COLOR_ARRAY); + gl.glColorPointer(cArrayData); + } + if(nComps>0) { + gl.glEnableClientState(gl.GL_NORMAL_ARRAY); + gl.glNormalPointer(nArrayData); + } + if(tComps>0) { + gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY); + gl.glTexCoordPointer(tArrayData); + } + + gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); + } else { + if(vComps>0) { + gl.glDisableClientState(gl.GL_VERTEX_ARRAY); + } + if(cComps>0) { + gl.glDisableClientState(gl.GL_COLOR_ARRAY); + } + if(nComps>0) { + gl.glDisableClientState(gl.GL_NORMAL_ARRAY); + } + if(tComps>0) { + gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY); + } } - protected void glVertex3b(byte x, byte y, byte z) { - checkSeal(false); - vertexVBO.putb(x); - if(vertexVBO.getComponentNumber()>1) - vertexVBO.putb(y); - if(vertexVBO.getComponentNumber()>2) - vertexVBO.putb(z); - vertexVBO.padding(3); + } + + public void enableBufferGLSL(GL gl, boolean enable) { + GL2ES2 glsl = gl.getGL2ES2(); + javax.media.opengl.glsl.ShaderState st = javax.media.opengl.glsl.ShaderState.getCurrent(); + if(null==st) { + throw new GLException("No ShaderState current"); } + + if(enable) { + gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); - protected void glNormal3b(byte x, byte y, byte z) { - checkSeal(false); - normalVBO.putb(x); - if(normalVBO.getComponentNumber()>1) - normalVBO.putb(y); - if(normalVBO.getComponentNumber()>2) - normalVBO.putb(z); - normalVBO.padding(3); + if(!bufferWritten) { + gl.glBufferData(GL.GL_ARRAY_BUFFER, buffer.limit(), buffer, GL.GL_STATIC_DRAW); + bufferWritten=true; + } + + if(vComps>0) { + st.glEnableVertexAttribArray(glsl, vArrayData.getName()); + st.glVertexAttribPointer(glsl, vArrayData); + } + if(cComps>0) { + st.glEnableVertexAttribArray(glsl, cArrayData.getName()); + st.glVertexAttribPointer(glsl, cArrayData); + } + if(nComps>0) { + st.glEnableVertexAttribArray(glsl, nArrayData.getName()); + st.glVertexAttribPointer(glsl, nArrayData); + } + if(tComps>0) { + st.glEnableVertexAttribArray(glsl, tArrayData.getName()); + st.glVertexAttribPointer(glsl, tArrayData); + } + + gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); + } else { + if(vComps>0) { + st.glDisableVertexAttribArray(glsl, vArrayData.getName()); + } + if(cComps>0) { + st.glDisableVertexAttribArray(glsl, cArrayData.getName()); + } + if(nComps>0) { + st.glDisableVertexAttribArray(glsl, nArrayData.getName()); + } + if(tComps>0) { + st.glDisableVertexAttribArray(glsl, tArrayData.getName()); + } } + } - protected void glColor3b(byte x, byte y, byte z) { - checkSeal(false); - colorVBO.putb(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.putb(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.putb(z); - colorVBO.padding(3); - } - protected void glColor4b(byte x, byte y, byte z, byte a) { - checkSeal(false); - colorVBO.putb(x); - if(colorVBO.getComponentNumber()>1) - colorVBO.putb(y); - if(colorVBO.getComponentNumber()>2) - colorVBO.putb(z); - if(colorVBO.getComponentNumber()>3) - colorVBO.putb(a); - colorVBO.padding(4); - } - protected void glTexCoord2b(byte x, byte y) { - checkSeal(false); - texcoordVBO.putb(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.putb(y); - texcoordVBO.padding(2); + public String toString() { + return "VBOSet[mode "+mode+ + ", modeOrig "+modeOrig+ + ", sealed "+sealed+ + ", bufferEnabled "+bufferEnabled+ + ", bufferWritten "+bufferWritten+ + ",\n\t"+vArrayData+ + ",\n\t"+cArrayData+ + ",\n\t"+nArrayData+ + ",\n\t"+tArrayData+ + "]"; } - protected void glTexCoord3b(byte x, byte y, byte z) { - checkSeal(false); - texcoordVBO.putb(x); - if(texcoordVBO.getComponentNumber()>1) - texcoordVBO.putb(y); - if(texcoordVBO.getComponentNumber()>2) - texcoordVBO.putb(z); - texcoordVBO.padding(3); - } - - GLArrayDataServer vertexVBO; - GLArrayDataServer normalVBO; - GLArrayDataServer colorVBO; - GLArrayDataServer texcoordVBO; - int mode, modeOrig; - int glBufferUsage, initialSize; - int vComps, cComps, nComps, tComps; - int vDataType, cDataType, nDataType, tDataType; - boolean sealed, useGLSL; + // non public matters + + protected void allocateBuffer(int elements) { + int vWidth = vComps * BufferUtil.sizeOfGLType(vDataType); + int cWidth = cComps * BufferUtil.sizeOfGLType(cDataType); + int nWidth = nComps * BufferUtil.sizeOfGLType(nDataType); + int tWidth = tComps * BufferUtil.sizeOfGLType(tDataType); + + count = elements; + bSize = count * ( vWidth + cWidth + nWidth + tWidth ) ; + + buffer = BufferUtil.newByteBuffer(bSize); + + int pos = 0; + int size= count * vWidth ; + if(size>0) { + vertexArray = BufferUtil.sliceGLBuffer(buffer, pos, size, vDataType); + } else { + vertexArray = null; + } + vOffset = pos; + pos+=size; + + size= count * cWidth ; + if(size>0) { + colorArray = BufferUtil.sliceGLBuffer(buffer, pos, size, cDataType); + } else { + colorArray = null; + } + cOffset = pos; + pos+=size; + + size= count * nWidth ; + if(size>0) { + normalArray = BufferUtil.sliceGLBuffer(buffer, pos, size, nDataType); + } else { + normalArray = null; + } + nOffset = pos; + pos+=size; + + size= count * tWidth ; + if(size>0) { + textCoordArray = BufferUtil.sliceGLBuffer(buffer, pos, size, tDataType); + } else { + textCoordArray = null; + } + tOffset = pos; + pos+=size; + + buffer.position(pos); + buffer.flip(); + + if(vComps>0) { + vArrayData = GLArrayDataWrapper.createFixed(GL.GL_VERTEX_ARRAY, vComps, vDataType, false, + 0, vertexArray, 0, vOffset); + } else { + vArrayData = null; + } + if(cComps>0) { + cArrayData = GLArrayDataWrapper.createFixed(GL.GL_COLOR_ARRAY, cComps, cDataType, false, + 0, colorArray, 0, cOffset); + } else { + cArrayData = null; + } + if(nComps>0) { + nArrayData = GLArrayDataWrapper.createFixed(GL.GL_NORMAL_ARRAY, nComps, nDataType, false, + 0, normalArray, 0, nOffset); + } else { + nArrayData = null; + } + if(tComps>0) { + tArrayData = GLArrayDataWrapper.createFixed(GL.GL_TEXTURE_COORD_ARRAY, tComps, tDataType, false, + 0, textCoordArray, 0, tOffset); + } else { + tArrayData = null; + } + + } + + protected final boolean growBufferIfNecessary(int type, int spare) { + if(buffer==null || count < spare) { + growBuffer(type, initialSize); + return true; + } + return false; + } + + protected final void growBuffer(int type, int additional) { + if(sealed || 0==additional) return; + + // save olde values .. + Buffer _vertexArray=vertexArray, _colorArray=colorArray, _normalArray=normalArray, _textCoordArray=textCoordArray; + ByteBuffer _buffer = buffer; + + allocateBuffer(count+additional); + + if(null!=_vertexArray) { + _vertexArray.flip(); + BufferUtil.put(vertexArray, _vertexArray); + } + if(null!=_colorArray) { + _colorArray.flip(); + BufferUtil.put(colorArray, _colorArray); + } + if(null!=_normalArray) { + _normalArray.flip(); + BufferUtil.put(normalArray, _normalArray); + } + if(null!=_textCoordArray) { + _textCoordArray.flip(); + BufferUtil.put(textCoordArray, _textCoordArray); + } + } + + protected void padding(int type, int fill) { + if ( sealed ) return; + + Buffer dest = null; + + switch (type) { + case VERTEX: + dest = vertexArray; + break; + case COLOR: + dest = colorArray; + break; + case NORMAL: + dest = normalArray; + break; + case TEXTCOORD: + dest = textCoordArray; + break; + } + + if ( null==dest ) return; + + while((fill--)>0) { + BufferUtil.putb(dest, (byte)0); + } + } + + protected int mode, modeOrig; + protected int glBufferUsage, initialSize; + + protected ByteBuffer buffer; + protected int bSize, count, vboName; + + public static final int VERTEX = 0; + public static final int COLOR = 1; + public static final int NORMAL = 2; + public static final int TEXTCOORD = 3; + + protected int vOffset, cOffset, nOffset, tOffset; + protected int vComps, cComps, nComps, tComps; + protected int vDataType, cDataType, nDataType, tDataType; + protected Buffer vertexArray, colorArray, normalArray, textCoordArray; + protected GLArrayDataWrapper vArrayData, cArrayData, nArrayData, tArrayData; + + protected boolean sealed, sealedGL, useGLSL; + protected boolean bufferEnabled, bufferWritten; } } -- cgit v1.2.3