Posted by
elect on
May 18, 2016; 6:22am
URL: https://forum.jogamp.org/questions-about-vertex-array-object-tp4036708p4036716.html
DearDaniel wrote
I understand now. Thanks for the small wiki. I am reading it now. So glad to see a proper tutorial on opengl 3(I can't find one from the internet, especially for java). That is very helpful. That solves most of my questions.
Glad it helps, I'll try to finish it sooner or later :p
DearDaniel wrote
So, one last minor question. Is the z coordinate default to be 0?
A vertex attribute should be padded in this way in the vertex shader (x, 0, 0, 1). But I'd suggest to not rely on the drivers policy and always specify it yourself, like I do
here.
DearDaniel wrote
If I wanted to add the z coordinate, how should I inform the opengl to set the coordinate? My guess after reading the small wiki is that just increase the offset in the gl3.glVertexAttribPointer() from offset = 2 * Float.BYTES to offset = 3 * Float.BYTES. But, is opengl smart enough to know that the new float is the z coordinate of the vertex?
Right guess. However you also need to pass 3 as the number of coordinates, so this:
int stride = (2 + 3) * Float.BYTES;
int offset = 0 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
gl3.glVertexAttribPointer(Semantic.Attr.POSITION, 2, GL_FLOAT, false, stride, offset);
offset = 2 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.COLOR);
gl3.glVertexAttribPointer(Semantic.Attr.COLOR, 3, GL_FLOAT, false, stride, offset);
becomes:
int stride = (3 + 3) * Float.BYTES;
int offset = 0 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
gl3.glVertexAttribPointer(Semantic.Attr.POSITION, 3, GL_FLOAT, false, stride, offset);
offset = 3 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.COLOR);
gl3.glVertexAttribPointer(Semantic.Attr.COLOR, 3, GL_FLOAT, false, stride, offset);
You also need to change the vertex attribute in the shader from vec2 to vec3 and the consequent padding