JOGL 深度缓冲不起作用

发布于 2024-10-05 01:36:08 字数 2713 浏览 1 评论 0原文

我已经使用 JOGL 几天了,这现在成为一个主要障碍。我无法以正确的 z 顺序绘制形状,而是按照提供给 OpenGL 的顺序绘制它们。

我花了过去几个小时研究这个问题,一般的解决方案(和我的反应)似乎如下:

  • 确保你的视锥体是正确的

    • 我仔细检查了截锥体,看起来是正确的
    • 我已改用 gluLookAt,而不是定制的截锥体
    • 我已切换到 glOrthof 只是为了确保问题不是透视问题。
    • 我什至根本没有设置任何视图,而是在似乎默认的 -1, 1 范围内工作
  • 确保以下调用位于 init 中:

    • gl.glEnable(GL.GL_DEPTH_TEST);
    • gl.glDepthFunc(GL.GL_LEQUAL);
  • 确保在每次重绘时清除深度缓冲区

    • gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

我在下面提供了发生此问题的程序的非常基本的示例。如果您有 JOGL 并运行它,您将看到 Z 位置 -0.5f 处的红色三角形始终位于顶部,同时三角形围绕彼此旋转。如果交换两个三角形顶点调用,绿色三角形将始终位于顶部。

这对我来说是一个巨大的头痛,所以任何见解都会有帮助,无论是来自 JOGL 还是一般的 OpenGL,但我似乎看不出哪里出了问题。

另请注意,为了简洁起见,我删除了销毁窗口的正确代码。

import java.awt.Frame;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import com.jogamp.opengl.util.Animator;


public class JOGLTest implements GLEventListener
{
    static GLU glu = new GLU();
    static GLCanvas canvas = new GLCanvas();
    static Frame frame = new Frame("JOGL test");
    static Animator animator = new Animator(canvas);

    float rot = 0.0f;

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

        rot++;

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity();

        gl.glRotatef(rot, 0.0f, 1.0f, 0.0f);

        gl.glBegin(GL.GL_TRIANGLES);

        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);

        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(-1.0f, 1.0f, -0.5f);
        gl.glVertex3f(1.0f, 1.0f, -0.5f);
        gl.glVertex3f(0.0f, 0.0f, -0.5f);

        gl.glEnd();
    }

    public void dispose(GLAutoDrawable arg0)
    {

    }

    public void init(GLAutoDrawable glDrawable)
    {
        GL2 gl = glDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        gl.glClearDepth(1.0f);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);
    }

    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4)
    {

    }

    public static void main(String[] args)
    {
        canvas.addGLEventListener(new JOGLTest());
        frame.add(canvas);
        frame.setSize(640, 480);
        frame.setVisible(true);

        animator.start();
        canvas.requestFocus();
    }
}

I have been using JOGL for a few days now and this is now becoming a major road block. I can not get shapes to draw in the correct z-order, instead, they are drawn in the order they are given to OpenGL.

I have spent the last few hours researching this and the general resolutions (and my reactions) seem to be the following:

  • Make sure your frustum is correct

    • I have double checked the frustum, it seems correct
    • I have switched to gluLookAt instead of a custom built frustum
    • I have switched to glOrthof just to make sure it is not perspective that is the problem.
    • I have not even set ANY view at all, instead working in the -1, 1 range that seems to be default
  • Make sure the following calls are in the init:

    • gl.glEnable(GL.GL_DEPTH_TEST);
    • gl.glDepthFunc(GL.GL_LEQUAL);
  • Make sure you are clearing the depth buffer on each redraw

    • gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

I have provided the very basic sample of a program below where this problem is occurring. If you have JOGL and run it, you will see the red triangle at Z-position -0.5f ALWAYS on top while the triangles rotate around each other. If you swap the two triangle vertex calls, the green triangle will then always be on top.

This has been an immense headache for me so any insight will be helpful, either from JOGL or OpenGL in general, but I can't seem to see what is wrong.

Also note that for brevity I removed the proper code to destroy the window.

import java.awt.Frame;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;

import com.jogamp.opengl.util.Animator;


public class JOGLTest implements GLEventListener
{
    static GLU glu = new GLU();
    static GLCanvas canvas = new GLCanvas();
    static Frame frame = new Frame("JOGL test");
    static Animator animator = new Animator(canvas);

    float rot = 0.0f;

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

        rot++;

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity();

        gl.glRotatef(rot, 0.0f, 1.0f, 0.0f);

        gl.glBegin(GL.GL_TRIANGLES);

        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);
        gl.glVertex3f(1.0f, -1.0f, 0.0f);

        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(-1.0f, 1.0f, -0.5f);
        gl.glVertex3f(1.0f, 1.0f, -0.5f);
        gl.glVertex3f(0.0f, 0.0f, -0.5f);

        gl.glEnd();
    }

    public void dispose(GLAutoDrawable arg0)
    {

    }

    public void init(GLAutoDrawable glDrawable)
    {
        GL2 gl = glDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        gl.glClearDepth(1.0f);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);
    }

    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
            int arg4)
    {

    }

    public static void main(String[] args)
    {
        canvas.addGLEventListener(new JOGLTest());
        frame.add(canvas);
        frame.setSize(640, 480);
        frame.setVisible(true);

        animator.start();
        canvas.requestFocus();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山川志 2024-10-12 01:36:08

我经过深思熟虑解决了这个问题。

我创建了一个 GLCapability 对象,并在创建 GLCanvas 时手动设置深度缓冲区的位数。代码如下:

GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);

caps.setDepthBits(16);
canvas = new GLCanvas(caps);

I have solved the problem over much deliberation.

I created a GLCapabilities object and manually set the number of bits for a depth buffer while creating a GLCanvas. The code is as follows:

GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);

caps.setDepthBits(16);
canvas = new GLCanvas(caps);
白馒头 2024-10-12 01:36:08

刚刚偶然发现了您的问题...您是否使用英特尔显卡和“较旧”(11 月 24 日之前)版本的 JOGL?如果是这样,以下内容可能会有所帮助(忽略 win7 64 位的提及;所描述的问题更为普遍,并且在后续 JOGL 版本中已在该实例中得到解决):
http://jogamp.762907 .n3.nabble.com/Depth-buffer-not-working-on-Win7-64b-td1737435.html

Just stumbled across your question... are you using an Intel graphics card and an 'older' (pre-24th November) version of JOGL, by any chance? If so, the following may be helpful (ignore the mention of win7 64-bit; the problem described is more general and was solved in that instance in subsequent JOGL releases):
http://jogamp.762907.n3.nabble.com/Depth-buffer-not-working-on-Win7-64b-td1737435.html

心的位置 2024-10-12 01:36:08

遇到了同样的问题,但与@david不同的是,我只是在 GLData 中设置了深度大小:

GLData data = new GLData ();
data.depthSize = 24; 

#jogl #linux

Got the same problem, but unlike @david I just set the depthSize in GLData:

GLData data = new GLData ();
data.depthSize = 24; 

#jogl #linux

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文