Hello guys

I've got a problem with my current VertexArray implementation. Not like most implementations in java it uses a ByteBuffer instead of a FloatBuffer. Probably this does not work, but it should, shouldn't it?
The code for the VertexArray class is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| package org.matheusdev.arcengine.ogl;
import java.nio.ByteBuffer; import java.util.List;
import javax.media.opengl.GL2;
import org.matheusdev.arcengine.utils.BufferUtils;
public class VertexArray {
protected final List<ShaderAttrib> attributes; protected final int totalComponents; protected final int totalByteLength; protected final ByteBuffer buffer;
public VertexArray(ShaderProg glslShader, int vertices) { this(glslShader.getAttributes(), vertices); }
public VertexArray(List<ShaderAttrib> attributes, int vertices) { this.attributes = attributes;
int components = 0; int stride = 0; for (ShaderAttrib attribute : attributes) { components += attribute.components; stride += attribute.getTypeByteSize(); } this.totalComponents = components; this.totalByteLength = stride; this.buffer = BufferUtils.newByteBuffer(totalByteLength * vertices); }
public void begin(GL2 gl) { gl.glEnableClientState(GL2.GL_ARRAY_BUFFER);
int offset = 0; for (ShaderAttrib attribute : attributes) { buffer.position(offset);
gl.glEnableVertexAttribArray(attribute.location); gl.glVertexAttribPointer( attribute.location, attribute.components, attribute.getDataType(), false, totalByteLength, buffer);
System.out.printf("glEnableVertexAttribArray(%d)%n", attribute.location); System.out.printf("glVertexAttribPointer(%n\tindx: %d,%n\tsize: %d,%n\ttype: %d,%n\tnormalized: false,%n\tstride: %d,%n\tbuffer: %s(offset = %d));%n", attribute.location, attribute.components, attribute.getDataType(), totalByteLength, buffer, offset);
offset += attribute.byteLength; } }
public void render(GL2 gl, int mode, int offset, int count) { gl.glDrawArrays(mode, offset, count); }
public void end(GL2 gl) { for (ShaderAttrib attribute : attributes) { gl.glDisableVertexAttribArray(attribute.location); } gl.glDisableClientState(GL2.GL_ARRAY_BUFFER); }
@Override public String toString() { StringBuffer str = new StringBuffer(); str.append(String.format("VertexArray[components: %d, stride: %d, attributes:\n", totalComponents, totalByteLength)); for (ShaderAttrib attribute : attributes) { str.append("\t").append(attribute).append("\n"); } str.append("];"); return str.toString(); }
public ByteBuffer getBuffer() { return buffer; }
public int getComponentSum() { return totalComponents; }
public int getComponentByteSum() { return totalByteLength; }
} |
I've tested the
class, it works perfectly with
. The array's
prints the following:
VertexArray[components: 2, stride: 8, attributes:
ShaderAttrib[layout (location = 0) vec2 aVertex; components: 2; size: 1; byte-size: 8]
];
So the attributes of the shader are loaded properly. The stride is in bytes and seems to be okay (components = 2 = vec2, all floats, which means stride = 2 components * 4 bytes = 8 bytes).
The ByteBuffer is properly filled by me, with this code:
1 2 3 4 5 6 7 8 9
| onlyVerts = new VertexArray(Arrays.asList( onlyVertShader.getAttribute("aVertex")), 4); ByteBuffer buf0 = onlyVerts.getBuffer(); buf0 .putFloat(256).putFloat(0) .putFloat(384).putFloat(0) .putFloat(384).putFloat(128) .putFloat(256).putFloat(128) .flip(); |
It's content is then tested with this and reset:
1 2 3 4 5
| while (buf0.hasRemaining()) { System.out.printf("Vertex: aVertex(%G, %G)%n", buf0.getFloat(), buf0.getFloat()); } buf0.position(0); |
This is getting printed:
Vertex: aVertex(256.000, 0.00000)
Vertex: aVertex(384.000, 0.00000)
Vertex: aVertex(384.000, 128.000)
Vertex: aVertex(256.000, 128.000)
Finally this is getting print from the VertexArray#begin(gl) method:
glEnableVertexAttribArray(0)
glVertexAttribPointer(
indx: 0,
size: 2,
type: 5126,
normalized: false,
stride: 8,
buffer: java.nio.DirectByteBuffer[pos=0 lim=32 cap=32](offset = 0));
The array is rendered like this:
1 2 3 4
| onlyVertShader.use(gl); onlyVerts.begin(gl); onlyVerts.render(gl, GL2.GL_QUADS, 0, 1); onlyVerts.end(gl); |
The vertex shader is the following:
1 2 3 4 5 6 7
| uniform mat4 uViewProjMatrix;
attribute vec2 aVertex;
void main(void) { gl_Position = uViewProjMatrix * vec4(aVertex, 0.0, 1.0); } |
The fragment shader:
1 2 3 4 5
| uniform vec3 color;
void main(void) { gl_FragColor = vec4(color, 1.0); } |
The uniform "vec3 color" is set to (0, 1, 1); Setting uniforms works with my ShaderProg class, it is tested with glBegin(); / glEnd();.
The problem with this is, that NOTHING is drawn... just black. Other stuff drawn in the rendering loop with non-interleaved FloatBuffers and vertex arrays (not using the VertexArray class) and shaders work, and glBegin(); / glEnd(); work too.
Just the stuff which should be drawn with the VertexArray class is not drawn. Just black, transparent, nothing.
Probably this is due to the arguments I give to
glVertexAttribPointer(...); |
, but I can't find whats wrong with it

I really need help! I've googled for years, as it feels now, and I have this issue longer than a week, and I just can't find the cause...