Opengl简单四边形渲染

Opengl简单四边形渲染,opengl,gpu,2d,jogl,Opengl,Gpu,2d,Jogl,你知道为什么这样不行吗 旧的即时模式可以工作,但我想使用VAO和VBOs (注:我知道VOA的创建应该只创建一次,但我在测试中使用这种方法创建了它。测试后我会移动这些行) private void allocateIndexBuffer(GL2图形,int[]索引){ int[]id=新的int[1]; glGenBuffers(1,id,0); int vboId=id[0]; graphics.glBindBuffer(GL2.GL_元素_数组_缓冲区,vboId); IntBuffer=I

你知道为什么这样不行吗

旧的即时模式可以工作,但我想使用VAO和VBOs

(注:我知道VOA的创建应该只创建一次,但我在测试中使用这种方法创建了它。测试后我会移动这些行)

private void allocateIndexBuffer(GL2图形,int[]索引){
int[]id=新的int[1];
glGenBuffers(1,id,0);
int vboId=id[0];
graphics.glBindBuffer(GL2.GL_元素_数组_缓冲区,vboId);
IntBuffer=IntBuffer.allocate(index.length);
buffer.put(0,索引);
flip();
graphics.glBufferData(GL2.GL\u元素\u数组\u缓冲区、index.length、缓冲区、GL2.GL\u动态\u绘图);
//graphics.glDeleteBuffers(vboId,buffer);TODO:关闭时清理
}
私有void allocateAttributeBuffer(GL2图形,int属性,float[]数据){
int[]id=新的int[1];
glGenBuffers(1,id,0);
int vboId=id[0];
graphics.glBindBuffer(GL2.GL_ARRAY_BUFFER,vboId);//只需要remplir vboId还是remplaceráchaque fois?
FloatBuffer=FloatBuffer.allocate(data.length);
buffer.put(0,数据);
flip();
graphics.glBufferData(GL2.GL_数组_BUFFER,data.length,BUFFER,GL2.GL_动态_DRAW);
graphics.glvertexattributepointer(0,2,GL2.GL_FLOAT,false,0,0);//一旦绑定了缓冲区
graphics.GlenableVertexAttributeArray(0);
graphics.glBindBuffer(GL2.GL_数组_BUFFER,0);
//graphics.glDeleteBuffers(vboId,buffer);TODO:关闭时清理
//graphics.gldeleteVertexArray(vboId,null);TODO:清理vaos
}
@凌驾
受保护的空心图形(GL2图形){
字符串模式=“新建”;
if(模式等于(“新”)){
浮点[]顶点={
bounds.getX(),bounds.getY(),
bounds.getX(),bounds.getY()+bounds.getHeight(),
bounds.getX()+bounds.getWidth(),bounds.getY()+bounds.getHeight(),
bounds.getX()+bounds.getWidth(),bounds.getY(),
};
int[]索引={0,1,2,2,1,3};
//创造vao
int[]id=新的int[1];
图形.glgenvertexarray(1,id,0);
int-vaoId=id[0];
图形。glBindVertexArray(vaoId);
allocateIndexBuffer(图形、索引);
allocateAttributeBuffer(图形,0,顶点);
graphics.glBindVertexArray(0);
//渲染
图形。glBindVertexArray(vaoId);
graphics.GlenableVertexAttributeArray(0);
图形元素(GL2.GL_三角形,index.length,GL2.GL_无符号_INT,0);
graphics.glDisableVertexAttributeArray(0);
graphics.glBindVertexArray(0);
graphics.glFlush();
}else if(mode.equals(“old”)){
glColor3f(255,0,0);
graphics.glBegin(GL2.GL_QUADS);
graphics.glVertex2f(bounds.getX(),bounds.getY());
graphics.glVertex2f(bounds.getX()+bounds.getWidth(),bounds.getY());
graphics.glVertex2f(bounds.getX()+bounds.getWidth(),bounds.getY()+bounds.getHeight());
graphics.glVertex2f(bounds.getX(),bounds.getY()+bounds.getHeight());
graphics.glEnd();
}
}

必须以字节为单位指定缓冲区的大小(请参阅)

graphics.glBufferData(GL2.GL\u元素\u数组\u缓冲区,index.length,缓冲区,GL2.GL\u动态\u绘图)

graphics.glBufferData(GL2.GL\u元素\u数组\u缓冲区,index.capacity()*4,
缓冲区,GL2.GL\u动态绘制);
graphics.glBufferData(GL2.GL\u数组\u BUFFER,data.length,BUFFER,GL2.GL\u动态\u DRAW)

graphics.glBufferData(GL2.GL_数组_BUFFER,data.capacity()*4,
缓冲区,GL2.GL\u动态绘制);

这很有效!4字节,因为我需要整数和浮点,好吧,我明白了。我喜欢渲染三角形,但这是一个很好的改进,我被困在thx!我像这样改变了索引:int[]索引={0,1,3,3,1,2};它工作得很好,我建议您看看这个工作示例:与其使用普通数组和间接NIO缓冲区,不如使用直接NIO缓冲区。Atm我在javax.media.opengl库中找不到opengl.NIO.buffers的正确导入,但thxBuffers在com.jogamp.common.NIO中:jogamp不允许使用javax名称空间,我们不再使用它。请使用最新版本的JOGL(目前为JOGL 2.4.0 RC)。我也是这么想的,但我找不到最新的罐子最新的胖罐子在那里: