我正在尝试绘制一个简单的三角形,但在组合glEnable(GL_DEPTH_TEST)
and时遇到了问题glOrtho
。三角形的显示没有深度测试,不取决于nearVal
参数,但如果启用了深度测试并且nearVal
不为 0,则不显示任何内容。这是代码。
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glClearColor(0, 0, 0, 1);
gl.glClearDepth(100.0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// the nearVal = 1 and the triangle is not displayed
// if we set the nearVal = 1 then the triangle is displayed
gl.glOrtho(-1, 1, -1, 1, 1, 10);
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1, 0, 0);
gl.glVertex3f(0.0f, 0.0f, 0);
gl.glColor3f(0, 1, 0);
gl.glVertex3f(1, 0.0f, 0);
gl.glColor3f(0, 0, 1);
gl.glVertex3f(0f, 1f, 0);
gl.glEnd();
gl.glFlush();
}