2package com.jogamp.opengl.util;
5import java.nio.ByteBuffer;
6import java.nio.IntBuffer;
7import java.nio.ShortBuffer;
8import java.util.ArrayList;
9import java.util.Iterator;
11import com.jogamp.opengl.GL;
12import com.jogamp.opengl.GL2ES1;
13import com.jogamp.opengl.GL2ES2;
14import com.jogamp.opengl.GLException;
15import com.jogamp.opengl.fixedfunc.GLPointerFunc;
17import jogamp.opengl.Debug;
19import com.jogamp.common.ExceptionUtils;
20import com.jogamp.common.nio.Buffers;
21import com.jogamp.common.os.Platform;
22import com.jogamp.common.util.PropertyAccess;
23import com.jogamp.opengl.util.glsl.ShaderState;
58 Debug.initSingleton();
59 DEBUG_BEGIN_END = PropertyAccess.isPropertyDefined(
"jogl.debug.ImmModeSink.BeginEnd",
true);
60 DEBUG_DRAW = PropertyAccess.isPropertyDefined(
"jogl.debug.ImmModeSink.Draw",
true);
61 DEBUG_BUFFER = PropertyAccess.isPropertyDefined(
"jogl.debug.ImmModeSink.Buffer",
true);
87 final int vComps,
final int vDataType,
88 final int cComps,
final int cDataType,
89 final int nComps,
final int nDataType,
90 final int tComps,
final int tDataType,
91 final int glBufferUsage) {
93 vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType,
94 false, glBufferUsage,
null, 0);
120 final int vComps,
final int vDataType,
121 final int cComps,
final int cDataType,
122 final int nComps,
final int nDataType,
123 final int tComps,
final int tDataType,
126 vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType,
127 true, glBufferUsage, st, 0);
153 final int vComps,
final int vDataType,
154 final int cComps,
final int cDataType,
155 final int nComps,
final int nDataType,
156 final int tComps,
final int tDataType,
157 final int glBufferUsage,
final int shaderProgram) {
159 vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType,
160 true, glBufferUsage,
null, shaderProgram);
180 final StringBuilder sb =
new StringBuilder(
"ImmModeSink[");
181 sb.append(
",\n\tVBO list: "+vboSetList.size()+
" [");
182 for(
final Iterator<VBOSet> i=vboSetList.iterator(); i.hasNext() ; ) {
184 sb.append( i.next() );
186 if(vboSetList.size()>0) {
187 sb.append(
"\n\t],\nVBO current: NOP]");
189 sb.append(
"\n\t],\nVBO current: \n");
193 return sb.toString();
196 public void draw(
final GL gl,
final boolean disableBufferAfterDraw) {
198 System.err.println(
"ImmModeSink.draw(disableBufferAfterDraw: "+disableBufferAfterDraw+
"):\n\t"+
this);
201 for(
int i=0; i<vboSetList.size(); i++, n++) {
202 vboSetList.get(i).draw(gl,
null, disableBufferAfterDraw, n);
206 public void draw(
final GL gl,
final Buffer indices,
final boolean disableBufferAfterDraw) {
208 System.err.println(
"ImmModeSink.draw(disableBufferAfterDraw: "+disableBufferAfterDraw+
"):\n\t"+
this);
211 for(
int i=0; i<vboSetList.size(); i++, n++) {
212 vboSetList.get(i).draw(gl, indices, disableBufferAfterDraw, n);
217 vboSet.modeOrig = mode;
228 System.err.println(
"ImmModeSink.glBegin("+vboSet.modeOrig+
" -> "+vboSet.mode+
")");
234 glEnd(gl,
null,
true);
237 public void glEnd(
final GL gl,
final boolean immediateDraw) {
238 glEnd(gl,
null, immediateDraw);
241 public final void glEnd(
final GL gl,
final Buffer indices) {
242 glEnd(gl, indices,
true);
245 private void glEnd(
final GL gl,
final Buffer indices,
final boolean immediateDraw) {
247 System.err.println(
"ImmModeSink START glEnd(immediate: "+immediateDraw+
")");
250 vboSet.
seal(gl,
true);
251 vboSet.
draw(gl, indices,
true, -1);
254 vboSet.
seal(gl,
true);
256 vboSetList.add(vboSet);
260 System.err.println(
"ImmModeSink END glEnd(immediate: "+immediateDraw+
")");
281 public final void glVertex3f(
final float x,
final float y,
final float z) {
285 public final void glNormal3f(
final float x,
final float y,
final float z) {
289 public final void glColor3f(
final float x,
final float y,
final float z) {
293 public final void glColor4f(
final float x,
final float y,
final float z,
final float a) {
301 public final void glTexCoord3f(
final float x,
final float y,
final float z) {
309 public final void glVertex3s(
final short x,
final short y,
final short z) {
313 public final void glNormal3s(
final short x,
final short y,
final short z) {
317 public final void glColor3s(
final short x,
final short y,
final short z) {
321 public final void glColor4s(
final short x,
final short y,
final short z,
final short a) {
329 public final void glTexCoord3s(
final short x,
final short y,
final short z) {
337 public final void glVertex3b(
final byte x,
final byte y,
final byte z) {
341 public final void glNormal3b(
final byte x,
final byte y,
final byte z) {
345 public final void glColor3b(
final byte x,
final byte y,
final byte z) {
349 public final void glColor3ub(
final byte x,
final byte y,
final byte z) {
353 public final void glColor4b(
final byte x,
final byte y,
final byte z,
final byte a) {
357 public final void glColor4ub(
final byte x,
final byte y,
final byte z,
final byte a) {
365 public final void glTexCoord3b(
final byte x,
final byte y,
final byte z) {
370 final int vComps,
final int vDataType,
371 final int cComps,
final int cDataType,
372 final int nComps,
final int nDataType,
373 final int tComps,
final int tDataType,
374 final boolean useGLSL,
final int glBufferUsage,
final ShaderState st,
final int shaderProgram) {
375 vboSet =
new VBOSet(initialElementCount,
376 vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType,
377 useGLSL, glBufferUsage, st, shaderProgram);
378 this.vboSetList =
new ArrayList<VBOSet>();
397 private void destroyList(
final GL gl) {
398 for(
int i=0; i<vboSetList.size(); i++) {
399 vboSetList.get(i).destroy(gl);
404 private VBOSet vboSet;
405 private final ArrayList<VBOSet> vboSetList;
408 protected VBOSet (
final int initialElementCount,
409 final int vComps,
final int vDataType,
410 final int cComps,
final int cDataType,
411 final int nComps,
final int nDataType,
412 final int tComps,
final int tDataType,
413 final boolean useGLSL,
final int glBufferUsage,
final ShaderState st,
final int shaderProgram) {
415 this.glBufferUsage=glBufferUsage;
416 this.initialElementCount=initialElementCount;
417 this.useVBO = 0 != glBufferUsage;
418 this.useGLSL=useGLSL;
419 this.shaderState = st;
420 this.shaderProgram = shaderProgram;
422 if(useGLSL &&
null == shaderState && 0 == shaderProgram) {
423 throw new IllegalArgumentException(
"Using GLSL but neither a valid shader-program nor ShaderState has been passed!");
426 this.resizeElementCount=initialElementCount;
427 this.vDataType=vDataType;
431 this.cDataType=cDataType;
435 this.nDataType=nDataType;
439 this.tDataType=tDataType;
454 this.pageSize = Platform.getMachineDataInfo().pageSizeInBytes();
463 this.bufferEnabled=
false;
464 this.bufferWritten=
false;
465 this.bufferWrittenOnce=
false;
466 this.glslLocationSet =
false;
475 return new VBOSet(initialElementCount, vComps,
476 vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType,
477 useGLSL, glBufferUsage, shaderState, shaderProgram);
482 throw new GLException(
"No mode set yet, call glBegin(mode) first:\n\t"+
this);
486 throw new GLException(
"Not Sealed yet, call glEnd() first:\n\t"+
this);
488 throw new GLException(
"Already Sealed, can't modify VBO after glEnd():\n\t"+
this);
493 private boolean usingShaderProgram =
false;
496 if( force || !usingShaderProgram ) {
497 if(
null != shaderState) {
502 usingShaderProgram =
true;
506 protected void draw(
final GL gl,
final Buffer indices,
final boolean disableBufferAfterDraw,
final int i)
510 if(
null != shaderState || 0 != shaderProgram) {
515 System.err.println(
"ImmModeSink.draw["+i+
"].0 (disableBufferAfterDraw: "+disableBufferAfterDraw+
"):\n\t"+
this);
521 for (
int j = 0; j < vElems - 3; j += 4) {
533 if(indices instanceof ByteBuffer) {
535 }
else if(indices instanceof ShortBuffer) {
537 }
else if(indices instanceof IntBuffer) {
540 throw new GLException(
"Given Buffer Class not supported: "+indices.getClass()+
", should be ubyte, ushort or uint:\n\t"+
this);
542 final int idxLen = indices.remaining();
543 final int idx0 = indices.position();
547 final ByteBuffer b = (ByteBuffer) indices;
548 for (
int j = 0; j < idxLen; j++) {
552 final ShortBuffer b = (ShortBuffer) indices;
553 for (
int j = 0; j < idxLen; j++) {
557 final IntBuffer b = (IntBuffer) indices;
558 for (
int j = 0; j < idxLen; j++) {
563 ((
GL2ES1)gl).glDrawElements(mode, idxLen, type, indices);
569 if(disableBufferAfterDraw) {
574 System.err.println(
"ImmModeSink.draw["+i+
"].X (disableBufferAfterDraw: "+disableBufferAfterDraw+
")");
580 Buffers.put(vertexArray, v);
584 Buffers.put(normalArray, v);
588 Buffers.put(colorArray, v);
592 Buffers.put(textCoordArray, v);
599 Buffers.putNb(vertexArray, vDataTypeSigned, x,
true);
601 Buffers.putNb(vertexArray, vDataTypeSigned, y,
true);
602 countAndPadding(VERTEX, vComps-2);
604 public void glVertex3b(
final byte x,
final byte y,
final byte z) {
608 Buffers.putNb(vertexArray, vDataTypeSigned, x,
true);
610 Buffers.putNb(vertexArray, vDataTypeSigned, y,
true);
612 Buffers.putNb(vertexArray, vDataTypeSigned, z,
true);
613 countAndPadding(VERTEX, vComps-3);
619 Buffers.putNs(vertexArray, vDataTypeSigned, x,
true);
621 Buffers.putNs(vertexArray, vDataTypeSigned, y,
true);
622 countAndPadding(VERTEX, vComps-2);
624 public void glVertex3s(
final short x,
final short y,
final short z) {
628 Buffers.putNs(vertexArray, vDataTypeSigned, x,
true);
630 Buffers.putNs(vertexArray, vDataTypeSigned, y,
true);
632 Buffers.putNs(vertexArray, vDataTypeSigned, z,
true);
633 countAndPadding(VERTEX, vComps-3);
639 Buffers.putNf(vertexArray, vDataTypeSigned, x);
641 Buffers.putNf(vertexArray, vDataTypeSigned, y);
642 countAndPadding(VERTEX, vComps-2);
644 public void glVertex3f(
final float x,
final float y,
final float z) {
648 Buffers.putNf(vertexArray, vDataTypeSigned, x);
650 Buffers.putNf(vertexArray, vDataTypeSigned, y);
652 Buffers.putNf(vertexArray, vDataTypeSigned, z);
653 countAndPadding(VERTEX, vComps-3);
656 public void glNormal3b(
final byte x,
final byte y,
final byte z) {
660 Buffers.putNb(normalArray, nDataTypeSigned, x,
true);
662 Buffers.putNb(normalArray, nDataTypeSigned, y,
true);
664 Buffers.putNb(normalArray, nDataTypeSigned, z,
true);
665 countAndPadding(NORMAL, nComps-3);
667 public void glNormal3s(
final short x,
final short y,
final short z) {
671 Buffers.putNs(normalArray, nDataTypeSigned, x,
true);
673 Buffers.putNs(normalArray, nDataTypeSigned, y,
true);
675 Buffers.putNs(normalArray, nDataTypeSigned, z,
true);
676 countAndPadding(NORMAL, nComps-3);
678 public void glNormal3f(
final float x,
final float y,
final float z) {
682 Buffers.putNf(normalArray, nDataTypeSigned, x);
684 Buffers.putNf(normalArray, nDataTypeSigned, y);
686 Buffers.putNf(normalArray, nDataTypeSigned, z);
687 countAndPadding(NORMAL, nComps-3);
690 public void glColor3b(
final byte r,
final byte g,
final byte b) {
694 Buffers.putNb(colorArray, cDataTypeSigned, r,
true);
696 Buffers.putNb(colorArray, cDataTypeSigned, g,
true);
698 Buffers.putNb(colorArray, cDataTypeSigned, b,
true);
699 countAndPadding(COLOR, cComps-3);
701 public void glColor3ub(
final byte r,
final byte g,
final byte b) {
705 Buffers.putNb(colorArray, cDataTypeSigned, r,
false);
707 Buffers.putNb(colorArray, cDataTypeSigned, g,
false);
709 Buffers.putNb(colorArray, cDataTypeSigned, b,
false);
710 countAndPadding(COLOR, cComps-3);
712 public void glColor4b(
final byte r,
final byte g,
final byte b,
final byte a) {
716 Buffers.putNb(colorArray, cDataTypeSigned, r,
true);
718 Buffers.putNb(colorArray, cDataTypeSigned, g,
true);
720 Buffers.putNb(colorArray, cDataTypeSigned, b,
true);
722 Buffers.putNb(colorArray, cDataTypeSigned, a,
true);
723 countAndPadding(COLOR, cComps-4);
725 public void glColor4ub(
final byte r,
final byte g,
final byte b,
final byte a) {
729 Buffers.putNb(colorArray, cDataTypeSigned, r,
false);
731 Buffers.putNb(colorArray, cDataTypeSigned, g,
false);
733 Buffers.putNb(colorArray, cDataTypeSigned, b,
false);
735 Buffers.putNb(colorArray, cDataTypeSigned, a,
false);
736 countAndPadding(COLOR, cComps-4);
738 public void glColor3s(
final short r,
final short g,
final short b) {
742 Buffers.putNs(colorArray, cDataTypeSigned, r,
true);
744 Buffers.putNs(colorArray, cDataTypeSigned, g,
true);
746 Buffers.putNs(colorArray, cDataTypeSigned, b,
true);
747 countAndPadding(COLOR, cComps-3);
749 public void glColor4s(
final short r,
final short g,
final short b,
final short a) {
753 Buffers.putNs(colorArray, cDataTypeSigned, r,
true);
755 Buffers.putNs(colorArray, cDataTypeSigned, g,
true);
757 Buffers.putNs(colorArray, cDataTypeSigned, b,
true);
759 Buffers.putNs(colorArray, cDataTypeSigned, a,
true);
760 countAndPadding(COLOR, cComps-4);
762 public void glColor3f(
final float r,
final float g,
final float b) {
766 Buffers.putNf(colorArray, cDataTypeSigned, r);
768 Buffers.putNf(colorArray, cDataTypeSigned, g);
770 Buffers.putNf(colorArray, cDataTypeSigned, b);
771 countAndPadding(COLOR, cComps-3);
773 public void glColor4f(
final float r,
final float g,
final float b,
final float a) {
777 Buffers.putNf(colorArray, cDataTypeSigned, r);
779 Buffers.putNf(colorArray, cDataTypeSigned, g);
781 Buffers.putNf(colorArray, cDataTypeSigned, b);
783 Buffers.putNf(colorArray, cDataTypeSigned, a);
784 countAndPadding(COLOR, cComps-4);
791 Buffers.putNb(textCoordArray, tDataTypeSigned, x,
true);
793 Buffers.putNb(textCoordArray, tDataTypeSigned, y,
true);
794 countAndPadding(TEXTCOORD, tComps-2);
800 Buffers.putNb(textCoordArray, tDataTypeSigned, x,
true);
802 Buffers.putNb(textCoordArray, tDataTypeSigned, y,
true);
804 Buffers.putNb(textCoordArray, tDataTypeSigned, z,
true);
805 countAndPadding(TEXTCOORD, tComps-3);
811 Buffers.putNs(textCoordArray, tDataTypeSigned, x,
true);
813 Buffers.putNs(textCoordArray, tDataTypeSigned, y,
true);
814 countAndPadding(TEXTCOORD, tComps-2);
816 public void glTexCoord3s(
final short x,
final short y,
final short z) {
820 Buffers.putNs(textCoordArray, tDataTypeSigned, x,
true);
822 Buffers.putNs(textCoordArray, tDataTypeSigned, y,
true);
824 Buffers.putNs(textCoordArray, tDataTypeSigned, z,
true);
825 countAndPadding(TEXTCOORD, tComps-3);
831 Buffers.putNf(textCoordArray, tDataTypeSigned, x);
833 Buffers.putNf(textCoordArray, tDataTypeSigned, y);
834 countAndPadding(TEXTCOORD, tComps-2);
836 public void glTexCoord3f(
final float x,
final float y,
final float z) {
840 Buffers.putNf(textCoordArray, tDataTypeSigned, x);
842 Buffers.putNf(textCoordArray, tDataTypeSigned, y);
844 Buffers.putNf(textCoordArray, tDataTypeSigned, z);
845 countAndPadding(TEXTCOORD, tComps-3);
849 if(
null!=vertexArray) {
850 vertexArray.rewind();
852 if(
null!=colorArray) {
855 if(
null!=normalArray) {
856 normalArray.rewind();
858 if(
null!=textCoordArray) {
859 textCoordArray.rewind();
864 if(
null == shaderState && 0 == program) {
865 throw new IllegalArgumentException(
"Not allowed to zero shader program if no ShaderState is set");
867 shaderProgram = program;
868 glslLocationSet =
false;
876 private boolean resetGLSLArrayLocation(
final GL2ES2 gl) {
880 if(
null != vArrayData) {
882 if( vArrayData.
setLocation(gl, shaderProgram) >= 0 ) {
886 if(
null != cArrayData) {
888 if( cArrayData.
setLocation(gl, shaderProgram) >= 0 ) {
892 if(
null != nArrayData) {
894 if( nArrayData.
setLocation(gl, shaderProgram) >= 0 ) {
898 if(
null != tArrayData) {
900 if( tArrayData.
setLocation(gl, shaderProgram) >= 0 ) {
904 glslLocationSet = iA == iL;
905 return glslLocationSet;
911 vCount=0; cCount=0; nCount=0; tCount=0;
912 vertexArray=
null; colorArray=
null; normalArray=
null; textCoordArray=
null;
913 vArrayData=
null; cArrayData=
null; nArrayData=
null; tArrayData=
null;
932 this.bufferEnabled=
false;
933 this.bufferWritten=
false;
943 if(sealedGL==
seal)
return;
949 final int[] tmp =
new int[1];
953 if(
null!=vArrayData) {
956 if(
null!=cArrayData) {
959 if(
null!=nArrayData) {
962 if(
null!=tArrayData) {
974 if(sealed==
seal)
return;
983 if( bufferEnabled != enable && vElems>0 ) {
987 bufferEnabled = enable;
990 if(
null != shaderState) {
991 enableBufferGLSLShaderState(gl, enable);
993 enableBufferGLSLSimple(gl, enable);
996 enableBufferFixed(gl, enable);
1001 private final void writeBuffer(
final GL gl) {
1002 final int vBytes = vElems * vCompsBytes;
1003 final int cBytes = cElems * cCompsBytes;
1004 final int nBytes = nElems * nCompsBytes;
1005 final int tBytes = tElems * tCompsBytes;
1006 final int delta = buffer.limit() - (vBytes+cBytes+nBytes+tBytes);
1007 if( bufferWrittenOnce && delta > pageSize ) {
1018 gl.
glBufferSubData(GL.GL_ARRAY_BUFFER, tOffset, tBytes, textCoordArray);
1021 gl.
glBufferData(GL.GL_ARRAY_BUFFER, buffer.limit(), buffer, glBufferUsage);
1022 bufferWrittenOnce =
true;
1026 private void enableBufferFixed(
final GL gl,
final boolean enable) {
1027 final GL2ES1 glf = gl.getGL2ES1();
1029 final boolean useV = vComps>0 && vElems>0 ;
1030 final boolean useC = cComps>0 && cElems>0 ;
1031 final boolean useN = nComps>0 && nElems>0 ;
1032 final boolean useT = tComps>0 && tElems>0 ;
1035 System.err.println(
"ImmModeSink.enableFixed.0 "+enable+
": use [ v "+useV+
", c "+useC+
", n "+useN+
", t "+useT+
"], "+
getElemUseCountStr()+
", "+buffer);
1041 throw new InternalError(
"Using VBO but no vboName");
1043 glf.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);
1045 if(!bufferWritten) {
1054 glf.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
1055 glf.glVertexPointer(vArrayData);
1057 glf.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
1062 glf.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);
1063 glf.glColorPointer(cArrayData);
1065 glf.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
1070 glf.glEnableClientState(GLPointerFunc.GL_NORMAL_ARRAY);
1071 glf.glNormalPointer(nArrayData);
1073 glf.glDisableClientState(GLPointerFunc.GL_NORMAL_ARRAY);
1078 glf.glEnableClientState(GLPointerFunc.GL_TEXTURE_COORD_ARRAY);
1079 glf.glTexCoordPointer(tArrayData);
1081 glf.glDisableClientState(GLPointerFunc.GL_TEXTURE_COORD_ARRAY);
1085 if(enable && useVBO) {
1086 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
1090 System.err.println(
"ImmModeSink.enableFixed.X ");
1094 private void enableBufferGLSLShaderState(
final GL gl,
final boolean enable) {
1095 final GL2ES2 glsl = gl.getGL2ES2();
1097 final boolean useV = vComps>0 && vElems>0 ;
1098 final boolean useC = cComps>0 && cElems>0 ;
1099 final boolean useN = nComps>0 && nElems>0 ;
1100 final boolean useT = tComps>0 && tElems>0 ;
1103 System.err.println(
"ImmModeSink.enableGLSL.A.0 "+enable+
": use [ v "+useV+
", c "+useC+
", n "+useN+
", t "+useT+
"], "+
getElemUseCountStr()+
", "+buffer);
1109 throw new InternalError(
"Using VBO but no vboName");
1111 glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);
1112 if(!bufferWritten) {
1121 shaderState.enableVertexAttribArray(glsl, vArrayData);
1124 shaderState.disableVertexAttribArray(glsl, vArrayData);
1129 shaderState.enableVertexAttribArray(glsl, cArrayData);
1132 shaderState.disableVertexAttribArray(glsl, cArrayData);
1137 shaderState.enableVertexAttribArray(glsl, nArrayData);
1140 shaderState.disableVertexAttribArray(glsl, nArrayData);
1145 shaderState.enableVertexAttribArray(glsl, tArrayData);
1148 shaderState.disableVertexAttribArray(glsl, tArrayData);
1151 glslLocationSet =
true;
1153 if(enable && useVBO) {
1154 glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
1158 System.err.println(
"ImmModeSink.enableGLSL.A.X ");
1162 private void enableBufferGLSLSimple(
final GL gl,
final boolean enable) {
1163 final GL2ES2 glsl = gl.getGL2ES2();
1165 final boolean useV = vComps>0 && vElems>0 ;
1166 final boolean useC = cComps>0 && cElems>0 ;
1167 final boolean useN = nComps>0 && nElems>0 ;
1168 final boolean useT = tComps>0 && tElems>0 ;
1171 System.err.println(
"ImmModeSink.enableGLSL.B.0 "+enable+
": use [ v "+useV+
", c "+useC+
", n "+useN+
", t "+useT+
"], "+
getElemUseCountStr()+
", "+buffer);
1174 if(!glslLocationSet) {
1175 if( !resetGLSLArrayLocation(glsl) ) {
1177 final int vLoc =
null != vArrayData ? vArrayData.
getLocation() : -1;
1178 final int cLoc =
null != cArrayData ? cArrayData.
getLocation() : -1;
1179 final int nLoc =
null != nArrayData ? nArrayData.
getLocation() : -1;
1180 final int tLoc =
null != tArrayData ? tArrayData.
getLocation() : -1;
1181 System.err.println(
"ImmModeSink.enableGLSL.B.X attribute locations in shader program "+shaderProgram+
", incomplete ["+vLoc+
", "+cLoc+
", "+nLoc+
", "+tLoc+
"] - glslLocationSet "+glslLocationSet);
1190 throw new InternalError(
"Using VBO but no vboName");
1192 glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName);
1193 if(!bufferWritten) {
1202 glsl.glEnableVertexAttribArray(vArrayData.
getLocation());
1203 glsl.glVertexAttribPointer(vArrayData);
1205 glsl.glDisableVertexAttribArray(vArrayData.
getLocation());
1210 glsl.glEnableVertexAttribArray(cArrayData.
getLocation());
1211 glsl.glVertexAttribPointer(cArrayData);
1213 glsl.glDisableVertexAttribArray(cArrayData.
getLocation());
1218 glsl.glEnableVertexAttribArray(nArrayData.
getLocation());
1219 glsl.glVertexAttribPointer(nArrayData);
1221 glsl.glDisableVertexAttribArray(nArrayData.
getLocation());
1226 glsl.glEnableVertexAttribArray(tArrayData.
getLocation());
1227 glsl.glVertexAttribPointer(tArrayData);
1229 glsl.glDisableVertexAttribArray(tArrayData.
getLocation());
1233 if(enable && useVBO) {
1234 glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
1238 System.err.println(
"ImmModeSink.enableGLSL.B.X ");
1244 final String glslS = useGLSL ?
1245 ", useShaderState "+(
null!=shaderState)+
1246 ", shaderProgram "+shaderProgram+
1247 ", glslLocationSet "+glslLocationSet :
"";
1249 return "VBOSet[mode "+mode+
1250 ", modeOrig "+modeOrig+
1253 ", sealedGL "+sealedGL+
1254 ", bufferEnabled "+bufferEnabled+
1255 ", bufferWritten "+bufferWritten+
" (once "+bufferWrittenOnce+
")"+
1256 ", useVBO "+useVBO+
", vboName "+vboName+
1257 ", useGLSL "+useGLSL+
1269 return "[v "+vElems+
"/"+vCount+
", c "+cElems+
"/"+cCount+
", n "+nElems+
"/"+nCount+
", t "+tElems+
"/"+tCount+
"]";
1273 final int addElems = 1;
1276 return ( vCount - vElems ) >= addElems ;
1278 return ( cCount - cElems ) >= addElems ;
1280 return ( nCount - nElems ) >= addElems ;
1282 return ( tCount - tElems ) >= addElems ;
1284 throw new InternalError(
"XXX");
1289 final int vAdd = addElems - ( vCount - vElems );
1290 final int cAdd = addElems - ( cCount - cElems );
1291 final int nAdd = addElems - ( nCount - nElems );
1292 final int tAdd = addElems - ( tCount - tElems );
1294 if( 0>=vAdd && 0>=cAdd && 0>=nAdd && 0>=tAdd) {
1296 System.err.println(
"ImmModeSink.realloc: "+
getElemUseCountStr()+
" + "+addElems+
" -> NOP");
1309 final int vBytes = vCount * vCompsBytes;
1310 final int cBytes = cCount * cCompsBytes;
1311 final int nBytes = nCount * nCompsBytes;
1312 final int tBytes = tCount * tCompsBytes;
1314 buffer = Buffers.newDirectByteBuffer( vBytes + cBytes + nBytes + tBytes );
1322 cOffset=vOffset+vBytes;
1329 nOffset=cOffset+cBytes;
1336 tOffset=nOffset+nBytes;
1341 textCoordArray =
null;
1344 buffer.position(tOffset+tBytes);
1376 bufferWrittenOnce =
false;
1379 System.err.println(
"ImmModeSink.realloc.X: "+this.
toString());
1380 ExceptionUtils.dumpStack(System.err);
1387 if(
null !=buffer && !sealed ) {
1390 final Buffer _vertexArray=vertexArray, _colorArray=colorArray, _normalArray=normalArray, _textCoordArray=textCoordArray;
1393 if(
null!=_vertexArray) {
1394 _vertexArray.flip();
1395 Buffers.put(vertexArray, _vertexArray);
1397 if(
null!=_colorArray) {
1399 Buffers.put(colorArray, _colorArray);
1401 if(
null!=_normalArray) {
1402 _normalArray.flip();
1403 Buffers.put(normalArray, _normalArray);
1405 if(
null!=_textCoordArray) {
1406 _textCoordArray.flip();
1407 Buffers.put(textCoordArray, _textCoordArray);
1427 private void countAndPadding(
final int type,
int fill) {
1428 if ( sealed )
return;
1431 final boolean dSigned;
1437 dSigned = vDataTypeSigned;
1438 e = 4 == vComps ? 1 : 0;
1443 dSigned = cDataTypeSigned;
1444 e = 4 == cComps ? 1 : 0;
1449 dSigned = nDataTypeSigned;
1454 dest = textCoordArray;
1455 dSigned = tDataTypeSigned;
1459 default:
throw new InternalError(
"Invalid type "+type);
1462 if (
null==dest )
return;
1466 Buffers.putNf(dest, dSigned, 0f);
1469 Buffers.putNf(dest, dSigned, 1f);
1473 final private int glBufferUsage, initialElementCount;
1474 final private boolean useVBO, useGLSL;
1475 final private ShaderState shaderState;
1476 private int shaderProgram;
1477 private int mode, modeOrig, resizeElementCount;
1479 private ByteBuffer buffer;
1480 private int vboName;
1482 private static final int VERTEX = 0;
1483 private static final int COLOR = 1;
1484 private static final int NORMAL = 2;
1485 private static final int TEXTCOORD = 3;
1487 private int vCount, cCount, nCount, tCount;
1488 private int vOffset, cOffset, nOffset, tOffset;
1489 private int vElems, cElems, nElems, tElems;
1490 private final int vComps, cComps, nComps, tComps;
1491 private final int vCompsBytes, cCompsBytes, nCompsBytes, tCompsBytes;
1492 private final int vDataType, cDataType, nDataType, tDataType;
1493 private final boolean vDataTypeSigned, cDataTypeSigned, nDataTypeSigned, tDataTypeSigned;
1494 private final int pageSize;
1495 private Buffer vertexArray, colorArray, normalArray, textCoordArray;
1496 private GLArrayDataWrapper vArrayData, cArrayData, nArrayData, tArrayData;
1498 private boolean sealed, sealedGL;
1499 private boolean bufferEnabled, bufferWritten, bufferWrittenOnce;
1500 private boolean glslLocationSet;
final boolean isCPUDataSourcingAvail()
Indicates whether this GLContext allows CPU data sourcing (indices, vertices ..) as opposed to using ...
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
static GLArrayDataWrapper createFixed(final int index, final int comps, final int dataType, final boolean normalized, final int stride, final Buffer buffer, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
Create a VBO, using a predefined fixed function array index, wrapping the given data.
void setVBOName(final int vboName)
Set the VBO buffer name, if valid (!= 0) enable use of VBO, otherwise (==0) disable VBO usage.
final int setLocation(final int v)
Sets the given location of the shader attribute.
final int getLocation()
Returns the shader attribute location for this name, -1 if not yet determined.
Utility routines for dealing with direct buffers.
static final Buffer sliceGLBuffer(final ByteBuffer parent, final int bytePos, final int byteLen, final int glType)
static final boolean isSignedGLType(final int glType)
static final boolean isGLTypeFixedPoint(final int glType)
static final int sizeOfGLType(final int glType)
void glColor4f(final float r, final float g, final float b, final float a)
void glColor4ub(final byte r, final byte g, final byte b, final byte a)
void draw(final GL gl, final Buffer indices, final boolean disableBufferAfterDraw, final int i)
VBOSet(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final boolean useGLSL, final int glBufferUsage, final ShaderState st, final int shaderProgram)
void glTexCoord3b(final byte x, final byte y, final byte z)
void glTexCoord2f(final float x, final float y)
void glTexCoord3s(final short x, final short y, final short z)
final VBOSet regenerate(final GL gl)
void destroy(final GL gl)
void seal(final boolean seal)
void glColorv(final Buffer v)
void glVertex2f(final float x, final float y)
final boolean growBuffer(final int type)
grow buffer by initialElementCount if there is no space for one more element in the designated buffer
void glTexCoord2b(final byte x, final byte y)
void glVertex3b(final byte x, final byte y, final byte z)
void glColor4b(final byte r, final byte g, final byte b, final byte a)
void glColor3b(final byte r, final byte g, final byte b)
void glTexCoord3f(final float x, final float y, final float z)
void glColor4s(final short r, final short g, final short b, final short a)
void glColor3s(final short r, final short g, final short b)
void seal(final GL glObj, final boolean seal)
boolean reallocateBuffer(final int addElems)
void glNormalv(final Buffer v)
boolean fitElementInBuffer(final int type)
void glColor3ub(final byte r, final byte g, final byte b)
void glVertex2s(final short x, final short y)
void enableBuffer(final GL gl, final boolean enable)
String getElemUseCountStr()
void setResizeElementCount(final int v)
void useShaderProgram(final GL2ES2 gl, final boolean force)
void glVertex3f(final float x, final float y, final float z)
void glNormal3b(final byte x, final byte y, final byte z)
void glVertexv(final Buffer v)
void checkSeal(final boolean test)
void glNormal3s(final short x, final short y, final short z)
int getResizeElementCount()
void glTexCoord2s(final short x, final short y)
void setShaderProgram(final int program)
void glColor3f(final float r, final float g, final float b)
void glVertex2b(final byte x, final byte y)
void glVertex3s(final short x, final short y, final short z)
void glNormal3f(final float x, final float y, final float z)
void glTexCoordv(final Buffer v)
final void glVertex2f(final float x, final float y)
final void glVertex3f(final float x, final float y, final float z)
final void glTexCoord3b(final byte x, final byte y, final byte z)
final void glVertex2b(final byte x, final byte y)
final void glColor4s(final short x, final short y, final short z, final short a)
final void glColor4b(final byte x, final byte y, final byte z, final byte a)
static final int GL_QUAD_STRIP
final void glColor3s(final short x, final short y, final short z)
ImmModeSink(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final boolean useGLSL, final int glBufferUsage, final ShaderState st, final int shaderProgram)
void glColorv(final Buffer v)
void glTexCoordv(final Buffer v)
final void glNormal3f(final float x, final float y, final float z)
void destroy(final GL gl)
final void glColor3ub(final byte x, final byte y, final byte z)
final void glVertex3s(final short x, final short y, final short z)
final void glEnd(final GL gl, final Buffer indices)
final void glColor4ub(final byte x, final byte y, final byte z, final byte a)
void glEnd(final GL gl, final boolean immediateDraw)
static ImmModeSink createFixed(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final int glBufferUsage)
Uses a GL2ES1, or ES2 fixed function emulation immediate mode sink.
static ImmModeSink createGLSL(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final int glBufferUsage, final ShaderState st)
Uses a GL2ES2 GLSL shader immediate mode sink, utilizing the given ShaderState.
void glNormalv(final Buffer v)
final void glColor3b(final byte x, final byte y, final byte z)
final void glTexCoord2f(final float x, final float y)
void setResizeElementCount(final int v)
Sets the additional element count if buffer resize is required, defaults to initialElementCount of fa...
int getResizeElementCount()
Returns the additional element count if buffer resize is required.
void draw(final GL gl, final boolean disableBufferAfterDraw)
final void glTexCoord3s(final short x, final short y, final short z)
static final boolean DEBUG_BEGIN_END
final void glTexCoord2s(final short x, final short y)
void draw(final GL gl, final Buffer indices, final boolean disableBufferAfterDraw)
void glVertexv(final Buffer v)
static final boolean DEBUG_DRAW
static ImmModeSink createGLSL(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final int glBufferUsage, final int shaderProgram)
Uses a GL2ES2 GLSL shader immediate mode sink, utilizing the given shader-program.
final void glNormal3s(final short x, final short y, final short z)
final void glTexCoord2b(final byte x, final byte y)
final void glColor4f(final float x, final float y, final float z, final float a)
final void glColor3f(final float x, final float y, final float z)
static final int GL_POLYGON
static final boolean DEBUG_BUFFER
final void glVertex2s(final short x, final short y)
final void glNormal3b(final byte x, final byte y, final byte z)
final void glEnd(final GL gl)
final void glTexCoord3f(final float x, final float y, final float z)
static final int GL_QUADS
final void glVertex3b(final byte x, final byte y, final byte z)
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
boolean vertexAttribPointer(final GL2ES2 gl, final GLArrayData data)
Set the GLArrayData vertex attribute data, if it's location is valid, i.e.
synchronized void useProgram(final GL2ES2 gl, final boolean on)
Turns the shader program on or off.
void glUseProgram(int program)
Entry point to C language function: void {@native glUseProgram}(GLuint program) Part of GL_ES_VERS...
GL getGL()
Casts this object to the GL interface.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
boolean isGL2()
Indicates whether this GL object conforms to the OpenGL ≤ 3.0 profile.
GLContext getContext()
Returns the GLContext associated which this GL object.
void glGenBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glGenBuffers}(GLsizei n, GLuint * buffers) Part ...
void glDrawArrays(int mode, int first, int count)
Entry point to C language function: void {@native glDrawArrays}(GLenum mode, GLint first,...
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
static final int GL_UNSIGNED_INT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_OES_element_index_uint Define "GL_UNSIGNED_INT"...
void glBufferSubData(int target, long offset, long size, Buffer data)
Entry point to C language function: void {@native glBufferSubData}(GLenum target,...
static final int GL_UNSIGNED_SHORT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_SHORT" with ...
static final int GL_TRIANGLE_FAN
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_FAN" with ex...
static final int GL_TRIANGLE_STRIP
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_STRIP" with ...
static final int GL_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
void glBufferData(int target, long size, Buffer data, int usage)
Entry point to C language function: void {@native glBufferData}(GLenum target, GLsizeiptr size,...
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
static final int GL_COLOR_ARRAY
static final int GL_TEXTURE_COORD_ARRAY
static final int GL_VERTEX_ARRAY
static final int GL_NORMAL_ARRAY