我正在尝试通过绘制四面体来在 JOGL 中使用带有索引缓冲区对象的顶点缓冲区来启动自己。我完成了代码,没有错误。但我不知道为什么它没有显示任何三角形。缓冲区是在渲染器初始化之后通过调用方法创建的createBuffers
。类属性 VBO 和 IBO(非常顶点/索引缓冲区对象)和其他缓冲区(顶点 - 顶点坐标,col - 颜色值,ind - 形成四面体的每个三角形的索引)createBuffers
通过调用在内部初始化initDataBuffer
。
我已经尝试直接绘制三角形,而不是使用for
循环使用缓冲区。就像是 :
for(int i=0;i<ind.capacity();){
int i1 = ind.get(i++);
int i2 = ind.get(i++);
int i3 = ind.get(i++);
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(col.get(i1), col.get(i1+1), col.get(i1+2));
gl.glVertex3f(vert.get(i1), vert.get(i1+1), vert.get(i1+2));
gl.glColor3f(col.get(i2), col.get(i2+1), col.get(i2+2));
gl.glVertex3f(vert.get(i2), vert.get(i2+1), vert.get(i2+2));
gl.glColor3f(col.get(i3), col.get(i3+1), col.get(i3+2));
gl.glVertex3f(vert.get(i3), vert.get(i3+1), vert.get(i3+2));
gl.glEnd();
}
使用此代码,四面体已成功显示。但是当我使用gl*Buffer
andgl*Pointer
函数时,它不会显示任何内容。这是完整的代码:
import com.sun.opengl.util.BufferUtil;
import java.nio.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
public class GLRenderer implements GLEventListener {
IntBuffer VBO, IBO;
FloatBuffer vert,col;
ShortBuffer ind;
public void init(GLAutoDrawable drawable) {
// initialization routine generated by netbeans + a method for creating the buffers
// Use debug pipeline
// drawable.setGL(new DebugGL(drawable.getGL()));
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());
// Enable VSync
gl.setSwapInterval(1);
gl.glEnable(GL.GL_DEPTH_TEST);
// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
// create buffers
createBuffers(drawable);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();
if (height <= 0) { // avoid a divide by zero error!
height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
glu.gluLookAt(3, 3, 3, 0, 0, 0, 0, 0, 1);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void initDataBuffer(){
// Data to put into the buffers
float[] vert = new float[]{
0, 0, 1,
-1, 0, 0,
0, 1, 0,
1, 0, 0,
0, -1, 0
};
float[] col = new float[]{
0.3f, 0.35f, 0.4f,
0.45f, 0.5f, 0.55f,
0.6f, 0.65f, 0.7f,
0.75f, 0.8f, 0.85f,
0.9f, 0.95f, 1.0f
};
short[] ind = new short[]{
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 1, 4,
1, 2, 3,
1, 3, 4
};
this.vert = BufferUtil.newFloatBuffer(vert.length);
this.col = BufferUtil.newFloatBuffer(col.length);
this.ind = BufferUtil.newShortBuffer(ind.length);
this.vert.put(vert);
this.col.put(col);
this.ind.put(ind);
}
public void createBuffers(GLAutoDrawable drawable){
// creating VBO and IBO
GL gl = drawable.getGL();
VBO = IntBuffer.allocate(1);
IBO = IntBuffer.allocate(1);
initDataBuffer();
// Verification
if(!(gl.isFunctionAvailable("glGenBuffersARB") &&
gl.isFunctionAvailable("glBindBufferARB") &&
gl.isFunctionAvailable("glBufferDataARB") &&
gl.isFunctionAvailable("glDeleteBuffersARB"))){
System.err.println("OpenGL buffering not supported");
System.exit(-1);
}
// Vertex Buffer
gl.glGenBuffers(1, VBO);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO.get(0));
gl.glBufferData(
GL.GL_ARRAY_BUFFER,
Float.SIZE*vert.capacity()+Float.SIZE*col.capacity(),
null,
GL.GL_STREAM_DRAW);
gl.glBufferSubData(
GL.GL_ARRAY_BUFFER,
0,
Float.SIZE*vert.capacity(),
vert);
gl.glBufferSubData(
GL.GL_ARRAY_BUFFER,
Float.SIZE*vert.capacity(),
Float.SIZE*col.capacity(),
col);
// Index Buffer
gl.glGenBuffers(1, IBO);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, IBO.get(0));
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, Short.SIZE*ind.capacity(), ind, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
this.vert.rewind();
this.col.rewind();
this.ind.rewind();
// Activating
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO.get(0));
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
// Pointing
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
gl.glColorPointer(3, GL.GL_FLOAT, 0, Float.SIZE*vert.capacity());
// drawing
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, IBO.get(0));
gl.glDrawElements(GL.GL_TRIANGLE_STRIP, ind.capacity(), GL.GL_UNSIGNED_BYTE, 0);
// Desactivating
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
// Flush all drawing operations to the graphics card
gl.glFlush();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
}