3我有以下 SceneRenderer 类,实现 GLEventListener。我想我了解创建缓冲区、存储指向这些缓冲区的指针以及用数据填充这些缓冲区的过程(参见 init 方法)。
我挣扎的地方是display()方法。我已经尝试了几乎所有我在互联网上找到的所有东西的组合,但我仍然无法画出任何东西。有人可以解释我现在该怎么做,所有缓冲区都充满了等待渲染的数据吗?
package cz.pscheidl.gui;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class SceneRenderer implements GLEventListener {
private IntBuffer buffers = IntBuffer.allocate(2);
private float[] square = {
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
};
private float[] colorData = {
255, 0, 0,
255, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
255, 0, 0
};
FloatBuffer vertexFB = FloatBuffer.wrap(square);
FloatBuffer colorFB = FloatBuffer.wrap(colorData);
GLU glu = new GLU();
@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL3 gl = glAutoDrawable.getGL().getGL3();
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glClearDepthf(10.0f);
gl.glClearColor(0.8f, 0.6f, 0.8f, 1.0f);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glGenBuffers(2, buffers);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(0));
gl.glBufferData(GL2.GL_ARRAY_BUFFER, 4 * 6 * 2, vertexFB, GL3.GL_STATIC_DRAW);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(1));
gl.glBufferData(GL2.GL_ARRAY_BUFFER, 4 * 6 * 3, colorFB, GL2.GL_STREAM_DRAW);
}
@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
GL3 gl = glAutoDrawable.getGL().getGL3();
gl.glClear(GL3.GL_DEPTH_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, buffers.get(0));
//After binding the buffer, now WHAT ?
}
@Override
public void reshape(GLAutoDrawable glAutoDrawable, int i, int i2, int i3, int i4) {
GL3 gl = glAutoDrawable.getGL().getGL3();
glu.gluPerspective(80.0f, 1920.0f / 1080.0f, 0.1f, 100f);
}
}