顶点缓冲区的JOGL问题

时间:2015-11-25 21:01:14

标签: java opengl jogl

我在JOGL中使用Vertex Buffers。我有几十万个三角形。每个三角形包含:

  • 9个顶点浮点数 - 每个边3个
  • 表面法线3浮动
  • 3种花车颜色。

我似乎无法显示三角形或颜色。我知道法线正确计算了。

这不起作用。

    gl.glDrawArrays(GL2.GL_TRIANGLES, 0, vertxcnt);

但是,下面的代码片段可行 - 但是我看不到颜色。所以,我知道构成三角形的点是正确的。

        gl.glDrawArrays(GL2.GL_POINTS, 0, vertxcnt);

所以,如果正确计算了点和法线,我认为我在渲染(gl)函数中出错了。代码如下。我究竟做错了什么?由于复杂性,我现在无法发布SSCCE,但想知道是否有任何明显的错误。

private void render(GL2 gl) {
    // VBO
    // Enable Pointers
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VBOVertices[0]); // Set Pointers To Our Data

    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); // Enable Vertex Arrays
    gl.glVertexPointer(3, GL.GL_FLOAT, BufferUtil.SIZEOF_FLOAT * 15, 0); //15 = 9 vertices of triangles + 3 normal + 3 colors

    gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glNormalPointer(GL.GL_FLOAT, BufferUtil.SIZEOF_FLOAT * 15, BufferUtil.SIZEOF_FLOAT * 9);

    gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
    gl.glColorPointer(3, GL.GL_FLOAT, BufferUtil.SIZEOF_FLOAT * 15, BufferUtil.SIZEOF_FLOAT * 12);

    // Render
    // Draw All Of The Triangles At Once
    gl.glPointSize(4);
    gl.glDrawArrays(GL2.GL_POINTS, 0, vertxcnt);

    // Disable Pointers
    // Disable Vertex, Normals and Color Arrays
    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
}

这是init和display函数。

    @Override
public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(0.0f, 0.0f, 6.0f, 0.5f);
    gl.glClearDepth(1.0f); // Depth Buffer Setup
    gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing (Less Or
                                    // Equal)
    gl.glEnable(GL.GL_DEPTH_TEST); // Enable Depth Testing
    gl.glDepthFunc(GL2.GL_LESS);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_AUTO_NORMAL);
    gl.glEnable(GL2.GL_NORMALIZE);
    gl.glEnable(GL2.GL_CULL_FACE);
    gl.glFrontFace(GL2.GL_CCW);
    gl.glCullFace(GL2.GL_BACK);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    gl.glShadeModel(GL2.GL_SMOOTH);

    buildVBOs(gl);
}



    @Override
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(.0f, .0f, .2f, 0.9f);
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    glu.gluLookAt(45, 0, 0, 0, 0, 0, 0.0, 1.0, 0.0);

    float ma_x = (float) getMax(fx0);
    float mi_x = (float) getMin(fx0);
    float tr_x = (ma_x + mi_x) / 2;

    float ma_y = (float) getMax(fy0);
    float mi_y = (float) getMin(fy0);
    float tr_y = (ma_y + mi_y) / 2;

    float ma_z = (float) getMax(fz0);
    float mi_z = (float) getMin(fz0);
    float tr_z = (ma_z + mi_z) / 2;

    gl.glScalef(scaleFac, scaleFac, scaleFac);
    gl.glRotatef(rotFac, 0, 1, 0);
    gl.glTranslatef(-tr_x, -tr_y, -tr_z);

    for (int i = 0; i < 30; i++) {
        render(gl);
        gl.glRotatef(12, 0, 0, 1);
    }
}


     */
private void createVects(double ang) {
    int cnt = fx0.size();
    for (int i = 0; i < cnt - 1; i++) {
        // Triangle 1 and 2 [Top]
        float x0 = (float) (fx0.get(i) * Math.cos(ang) - fy0.get(i) * Math.sin(ang));
        float y0 = (float) (fx0.get(i) * Math.sin(ang) + fy0.get(i) * Math.cos(ang));
        float z0 = fz0.get(i).floatValue();
        Vect3D v0 = new Vect3D(x0, y0, z0);
        fvert.add(v0); // 0

        float x1 = (float) (fx0.get(i + 1) * Math.cos(ang) - fy0.get(i + 1) * Math.sin(ang));
        float y1 = (float) (fx0.get(i + 1) * Math.sin(ang) + fy0.get(i + 1) * Math.cos(ang));
        float z1 = fz0.get(i + 1).floatValue();
        Vect3D v1 = new Vect3D(x1, y1, z1);
        fvert.add(v1);// 1

        float x2 = (float) (fx1.get(i + 1) * Math.cos(ang) - fy1.get(i + 1) * Math.sin(ang));
        float y2 = (float) (fx1.get(i + 1) * Math.sin(ang) + fy1.get(i + 1) * Math.cos(ang));
        float z2 = fz1.get(i + 1).floatValue();
        Vect3D v2 = new Vect3D(x2, y2, z2);
        fvert.add(v2);// 2

        Vect3D n0 = calcNormal(v0, v1, v2);
        fnorm.add(n0);

        // VBO
        vertices.put(x0); //vertices of the triangle
        vertices.put(y0);
        vertices.put(z0);

        vertices.put(x1);
        vertices.put(y1);
        vertices.put(z1);

        vertices.put(x2);
        vertices.put(y2);
        vertices.put(z2);

        vertices.put(n0.x); // normals
        vertices.put(n0.y);
        vertices.put(n0.z);

        vertices.put(0.5f); // colors // for now
        vertices.put(0.0f);
        vertices.put(0.0f);
 }
}

0 个答案:

没有答案