在jogl中使用glDrawElements的顶点数组无法渲染

时间:2015-04-02 17:41:09

标签: java graphics jogl vertex-array

我试过跟随thisthis来使jogl中的顶点数组工作,虽然它不再抛出任何错误,但它也不会渲染除了空白屏幕之外的任何内容。这是代码:

public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2(); // get the OpenGL graphics context

    vertexArray = new float[]
            {-100f,-100f,-50f,
            100f,-100f,-50f,
            100f,100f,-50f,
            -100f,100f,-50f};

    vertexBuffer = BufferUtil.newFloatBuffer(vertexArray.length);
    for(int i=0;i<vertexArray.length;i++){
        vertexBuffer.put(vertexArray[i]);
    }
    //vertexBuffer.put(vertexArray); //neither of these versions work
    vertexBuffer.rewind();

    indexArray = new int[]{0,1,2,3,0,1,2,3,0,1,2,3};
    indexBuffer = BufferUtil.newIntBuffer(indexArray.length);
    for(int i=0;i<indexArray.length;i++){
        indexBuffer.put(indexArray[i]);
    }
    //indexBuffer.put(indexArray);
    indexBuffer.rewind();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
    gl.glClearDepth(1.0f); // set clear depth value to farthest
    gl.glEnable(GL_DEPTH_TEST); // enables depth testing
    gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
    gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
    gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting

public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
    gl.glLoadIdentity(); // reset the model-view matrix

    gl.glColor3f(1f, 1f, 1f);

    gl.glEnableClientState(GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);

    gl.glDrawElements(GL_QUADS, indexArray.length, GL_UNSIGNED_INT, indexBuffer);

    gl.glDisableClientState(GL_VERTEX_ARRAY);

    gl.glTranslatef(0f, 0f, -60f);
    GLUT glut = new GLUT();
    //glut.glutSolidTeapot(1);
}

当我取消注释茶壶代码时,我可以看到,所以我非常确定它特定于glDrawElements。我完全没有信心,我使用了正确的gl.GL_TYPE常量,但我尝试过的那些都没有用过。

0 个答案:

没有答案