JOGL平滑度

时间:2015-10-16 17:07:25

标签: java opengl jogl

我正在使用JOGL - OpenGL。我有带有彩色边缘的色带,我使用三角形基元创建,如线框图像所示。边缘由三角形本身组成。如何获得光滑的边缘?我查找了抗锯齿,我不确定这是否是我需要采取的方法。

我愿意进行设计更改,我需要边框线和重叠图案格式。我尝试了下面的尝试和混合,问题是三角形太接近,没有任何区别。

 gl.glDisable(GL2.GL_LINE_SMOOTH);
 gl.glDisable(GL2.GL_POLYGON_SMOOTH);
 gl.glEnable(GL2.GL_MULTISAMPLE);

enter image description here

No multi-sampling

With multi-sampling

有人能指出我正确的方向吗?这是代码。

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

    int ind = 0;
    int nind = 0; // normal index

    gl.glColorMaterial(GL2.GL_FRONT, GL2.GL_DIFFUSE);
    gl.glEnable(GL2.GL_COLOR_MATERIAL);
    gl.glRotatef(0, 0, 1, 0);
    gl.glTranslatef(-tr_x, -tr_y, -tr_z);

    while (ind < fvert.size()) {
        gl.glPushMatrix();
            gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glBegin(GL2.GL_TRIANGLES);
        gl.glColor4f(.9f, .0f, 0.0f, 0.5f); // upper border
        gl.glNormal3f(fnorm.get(nind + 4).x, fnorm.get(nind + 4).y, fnorm.get(nind + 4).z);
        gl.glVertex3f(fvert.get(ind + 8).x, fvert.get(ind + 8).y, fvert.get(ind + 8).z);
        gl.glVertex3f(fvert.get(ind + 9).x, fvert.get(ind + 9).y, fvert.get(ind + 9).z);
        gl.glVertex3f(fvert.get(ind + 10).x, fvert.get(ind + 10).y, fvert.get(ind + 10).z);

        gl.glNormal3f(fnorm.get(nind + 5).x, fnorm.get(nind + 5).y, fnorm.get(nind + 5).z);
        gl.glVertex3f(fvert.get(ind + 10).x, fvert.get(ind + 10).y, fvert.get(ind + 10).z);
        gl.glVertex3f(fvert.get(ind + 11).x, fvert.get(ind + 11).y, fvert.get(ind + 11).z);
        gl.glVertex3f(fvert.get(ind + 8).x, fvert.get(ind + 8).y, fvert.get(ind + 8).z);

        gl.glColor4f(.9f, .9f, 0.2f, 0.5f);// Triangle pair upper
        gl.glNormal3f(fnorm.get(nind).x, fnorm.get(nind).y, fnorm.get(nind).z);
        gl.glVertex3f(fvert.get(ind + 2).x, fvert.get(ind + 2).y, fvert.get(ind + 2).z);
        gl.glVertex3f(fvert.get(ind).x, fvert.get(ind).y, fvert.get(ind).z);
        gl.glVertex3f(fvert.get(ind + 1).x, fvert.get(ind + 1).y, fvert.get(ind + 1).z);

        gl.glNormal3f(fnorm.get(nind + 1).x, fnorm.get(nind + 1).y, fnorm.get(nind + 1).z);
        gl.glVertex3f(fvert.get(ind + 3).x, fvert.get(ind + 3).y, fvert.get(ind + 3).z);
        gl.glVertex3f(fvert.get(ind).x, fvert.get(ind).y, fvert.get(ind).z);
        gl.glVertex3f(fvert.get(ind + 2).x, fvert.get(ind + 2).y, fvert.get(ind + 2).z);

        gl.glColor4f(.9f, .0f, 0.0f, 0.5f); // lower border
        gl.glNormal3f(fnorm.get(nind + 6).x, fnorm.get(nind + 6).y, fnorm.get(nind + 6).z);
        gl.glVertex3f(fvert.get(ind + 14).x, fvert.get(ind + 14).y, fvert.get(ind + 14).z);
        gl.glVertex3f(fvert.get(ind + 12).x, fvert.get(ind + 12).y, fvert.get(ind + 12).z);
        gl.glVertex3f(fvert.get(ind + 13).x, fvert.get(ind + 13).y, fvert.get(ind + 13).z);

        gl.glNormal3f(fnorm.get(nind + 7).x, fnorm.get(nind + 7).y, fnorm.get(nind + 7).z);
        gl.glVertex3f(fvert.get(ind + 15).x, fvert.get(ind + 15).y, fvert.get(ind + 15).z);
        gl.glVertex3f(fvert.get(ind + 12).x, fvert.get(ind + 12).y, fvert.get(ind + 12).z);
        gl.glVertex3f(fvert.get(ind + 14).x, fvert.get(ind + 14).y, fvert.get(ind + 14).z);

        gl.glColor4f(.9f, .9f, 0.2f, 0.5f);// triangle pair lower
        gl.glNormal3f(fnorm.get(nind + 2).x, fnorm.get(nind + 2).y, fnorm.get(nind + 2).z);
        gl.glVertex3f(fvert.get(ind + 6).x, fvert.get(ind + 6).y, fvert.get(ind + 6).z);
        gl.glVertex3f(fvert.get(ind + 4).x, fvert.get(ind + 4).y, fvert.get(ind + 4).z);
        gl.glVertex3f(fvert.get(ind + 5).x, fvert.get(ind + 5).y, fvert.get(ind + 5).z);

        gl.glNormal3f(fnorm.get(nind + 3).x, fnorm.get(nind + 3).y, fnorm.get(nind + 3).z);
        gl.glVertex3f(fvert.get(ind + 7).x, fvert.get(ind + 7).y, fvert.get(ind + 7).z);
        gl.glVertex3f(fvert.get(ind + 4).x, fvert.get(ind + 4).y, fvert.get(ind + 4).z);
        gl.glVertex3f(fvert.get(ind + 6).x, fvert.get(ind + 6).y, fvert.get(ind + 6).z);

        gl.glEnd();
        gl.glPopMatrix();

        ind = ind + 16;
        nind = nind + 8;
    }

    gl.glFlush();
}

@Override
public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(.0f, .0f, .0f, 0.0f);

    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LESS);
    gl.glEnable(GL2.GL_TEXTURE_GEN_S);
    gl.glEnable(GL2.GL_TEXTURE_1D);
    gl.glEnable(GL2.GL_CULL_FACE);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_AUTO_NORMAL);
    gl.glEnable(GL2.GL_NORMALIZE);
    gl.glFrontFace(GL2.GL_CCW);
    gl.glCullFace(GL2.GL_BACK);

    gl.glPolygonMode( GL2.GL_FRONT_AND_BACK,GL2.GL_LINE );

}

1 个答案:

答案 0 :(得分:0)

也许您应该在GLCapabilities中启用多重采样 示例:

GLCapabilities STANDARD_CAPS;
STANDARD_CAPS = new GLCapabilities(GLProfile.get(GLProfile.GL2)); //I assume taht you are using GL2
STANDARD_CAPS.setBackgroundOpaque(true); //opaque background
STANDARD_CAPS.setHardwareAccelerated(true); //hardware acceleration
STANDARD_CAPS.setSampleBuffers(true); //Enable sampling
STANDARD_CAPS.setNumSamples(8); //8 Samples