I’ve spent whole day trying to make this render my triangle. Didn’t happen. What am I doing wrong ? Syntax is OK. Screen gets filled with background color of Z-Buffer and no triangle appears. I’ve been used to old glBegin(), glEnd() style and I have almost no idea what am I doing now.
What I understand:
- How buffers work,
- How to reference buffers with IntBuffer
- How to feed them with data using glBufferData.
- Binding buffers.
What I do not understand:
- What gl.glEnableVertexAttribArray(0); does ?
- How do I change color of those vertices ? Are they drawn the same color as background, so they’re invisible ?
package opengltest;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import jogamp.graph.font.typecast.ot.table.Program;
public class SceneRenderer implements GLEventListener {
private GLU glu = new GLU();
private float triangle[] = { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f };
private FloatBuffer verticesBuffer = FloatBuffer.wrap(triangle);
private IntBuffer vbos = IntBuffer.allocate(1);
@Override
public void init(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
gl.glEnable(GL3.GL_DEPTH_TEST);
gl.glDepthFunc(GL3.GL_LEQUAL);
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glGenBuffers(1, vbos);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbos.get(0));
gl.glBufferData(GL3.GL_ARRAY_BUFFER, 9 * 4, verticesBuffer,
GL3.GL_STATIC_DRAW);
gl.glFinish();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void display(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
gl.glClear(GL3.GL_DEPTH_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT);
gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbos.get(0));
gl.glVertexAttribPointer(0, 3, GL3.GL_FLOAT, true, 0, 0);
gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
gl.glDisableVertexAttribArray(0);
gl.glFinish();
gl.glFlush();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
float aspect = (float) width / (float) height;
glu.gluPerspective(85.0f, aspect, 0.1f, 100.0f);
drawable.getGL().glViewport(0, 0, width, height);
}
}