1

我正在尝试通过绘制四面体来在 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*Bufferandgl*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) {
}

}

4

1 回答 1

1

终于让它工作了!从最初的代码,我做了这些改变:

  • 我使用 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) {
}

}

于 2012-12-06T01:40:46.610 回答