Java 无法显示缓冲区中的对象
我试图通过绘制一个四面体,在JOGL中使用顶点缓冲区和索引缓冲区对象。我完成了代码,没有错误。但我不明白为什么它没有显示任何三角形。 缓冲区是在渲染器初始化之后通过调用方法Java 无法显示缓冲区中的对象,java,object,opengl,rendering,buffer,Java,Object,Opengl,Rendering,Buffer,我试图通过绘制一个四面体,在JOGL中使用顶点缓冲区和索引缓冲区对象。我完成了代码,没有错误。但我不明白为什么它没有显示任何三角形。 缓冲区是在渲染器初始化之后通过调用方法createBuffers创建的。类属性VBO和IBO(顶点/索引缓冲区对象)以及其他缓冲区(形成四面体的每个三角形的顶点坐标、颜色值、索引)通过调用initDataBuffer在createBuffers内部初始化 我已经尝试直接绘制三角形,而不是使用for循环使用缓冲区。比如: for(int i=0;i<ind.c
createBuffers
创建的。类属性VBO和IBO(顶点/索引缓冲区对象)以及其他缓冲区(形成四面体的每个三角形的顶点坐标、颜色值、索引)通过调用initDataBuffer
在createBuffers
内部初始化
我已经尝试直接绘制三角形,而不是使用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();
}
}终于开始工作了!在最初的代码中,我做了以下更改:
- 我使用short(0-255)作为颜色的类型,我将其更改为float(0.0-1.0)
- 与其使用glBufferSubData将数据单独上传到缓冲区,我宁愿使用glBufferData,因此需要将颜色和位置合并到一个数组中
- 在创建缓冲区对象后调用flip(),使其可供使用
- 与其从FloatBUffer类调用
,不如从BufferUtils创建一个新的缓冲区allocate(nbytes)
- 使用
而不是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) {
}
public类实现了GLEventListener{
国际VBO、IBO;
浮动缓冲区值;
短缓冲区ind;
int nVertex;
双角度X=0,角度Z=0;
公共void init(GLAutoDrawable){
//使用调试管道
//setGL(新的DebugGL(drawable.getGL());
GL=drawable.getGL();
System.err.println(“INIT GL IS:+GL.getClass().getName());
//启用VSync
总帐setSwapInterval(1);
gl.glEnable(gl.gl\U深度测试);
//设置绘图区域和着色模式
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);//尝试将其设置为gl_FLAT,看看会发生什么。
创建缓冲区(可绘制);
}
公共空间重塑(玻璃自动拉伸可拉伸、整数x、整数y、整数宽度、整数高度){
GL=drawable.getGL();
GLU-GLU=新的GLU();
如果(height最终成功了!从最初的代码开始,我做了以下更改:
- 我使用short(0-255)作为颜色的类型,我将其更改为float(0.0-1.0)
- 与其使用glBufferSubData将数据单独上传到缓冲区,我宁愿使用glBufferData,因此需要将颜色和位置合并到一个数组中
- 在创建缓冲区对象后调用flip(),使其可供使用
- 与其从FloatBUffer类调用
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) {
}
public类实现了GLEventListener{
国际VBO、IBO;
浮动缓冲区值;
短缓冲区ind;
int nVertex;
双角度X=0,角度Z=0;
公共void init(GLAutoDrawable){
//使用调试管道
//setGL(新的DebugGL(drawable.getGL());
GL=drawable.getGL();
System.err.println(“INIT GL IS:+GL.getClass().getName());
//启用VSync
总帐setSwapInterval(1);
gl.glEnable(gl.gl\U深度测试);
//设置绘图区域和着色模式
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);//尝试将其设置为gl_FLAT,看看会发生什么。
创建缓冲区(可绘制);
}
公共空间重塑(玻璃自动拉伸可拉伸、整数x、整数y、整数宽度、整数高度){
GL=drawable.getGL();
GLU-GLU=新的GLU();
如果(高度)为什么你的ind
数组中有五个顶点?你只有五个顶点,而不是六个。我已经编辑了它,但仍然不工作。现在正在更新代码。为什么你的ind
数组中有五个顶点?你只有五个顶点,而不是六个。我已经编辑了它,但仍然不工作。现在正在更新代码。