我试图通过绘制四面体来使用JOGL中的索引缓冲区对象使用顶点缓冲区。我完成了代码,没有错误。但我不明白为什么它没有显示任何三角形。
通过调用方法createBuffers
,在渲染器初始化之后立即创建缓冲区。类属性VBO和IBO(极顶点/索引缓冲区对象)和其他缓冲区(顶点 - 顶点坐标,col - 颜色值,形成四面体的每个三角形的ind - 索引)在createBuffers
内通过调用{{}来初始化1}}。
我已经尝试直接绘制三角形,而不是使用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
函数时,它不会显示任何内容。
这里有完整的代码:
gl*Pointer
}
答案 0 :(得分:1)
终于搞定了!从最初的代码开始,我进行了这些更改:
allocate(nbytes)
,而是最好从BufferUtils创建一个新的缓冲区BufferUtil.SIZEOF_FLOAT
而不是Float.SIZE
因此我得到了一个显示旋转四面体的工作代码:
public class GLRenderer implements GLEventListener {
int VBO, IBO;
FloatBuffer values;
ShortBuffer ind;
int nVertex;
double angleX = 0, angleZ = 0;
public void init(GLAutoDrawable drawable) {
// 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.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
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(){
float[] vertexArray = {
0, 0, 1,
-1, 0, 0,
0, 1, 0,
1, 0, 0,
0, -1, 0
};
int n1 = vertexArray.length;
float[] colorArray ={
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f
};
int n2 = colorArray.length;
if(n1!=n2){
System.err.println("Color and vertex do not match in size");
System.exit(-1);
}
nVertex = (int)(n1/3);
short[] indexArray = {
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 1, 4,
1, 2, 3,
1, 3, 4
};
ind = BufferUtil.newShortBuffer(indexArray.length);
ind.put(indexArray);
ind.flip();
values = BufferUtil.newFloatBuffer(vertexArray.length+colorArray.length);
values.put(vertexArray);
values.put(colorArray);
values.flip();
}
public void createBuffers(GLAutoDrawable drawable){
initDataBuffer();
GL gl = drawable.getGL();
int[] temp = new int[2];
gl.glGenBuffers(2, temp, 0);
VBO = temp[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO);
gl.glBufferData(GL.GL_ARRAY_BUFFER, values.capacity() * BufferUtil.SIZEOF_FLOAT,
values, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
IBO = temp[1];
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, IBO);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, ind.capacity() * BufferUtil.SIZEOF_SHORT,
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();
gl.glRotated(angleZ,0,0,1);
gl.glRotated(angleX,1,0,0);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
gl.glColorPointer(3, GL.GL_FLOAT, 0, nVertex*3*BufferUtil.SIZEOF_FLOAT);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, IBO);
gl.glDrawElements(GL.GL_TRIANGLES, ind.capacity(), GL.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
angleX += 1;
angleZ += 1;
// Flush all drawing operations to the graphics card
gl.glFlush();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
}