OpenGL的三角形包含孔和dissappear有时 [英] Opengl Triangles contain holes and dissappear sometimes

查看:258
本文介绍了OpenGL的三角形包含孔和dissappear有时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在渲染三角形带的显示列表中,它显示了古灵精怪的小漏洞。

I'm currently rendering triangle strips within a display list and it shows weird little holes.

除此之外,一些看似三角形旋转的环境时,完全消失。
这里是code:

Beside that some triangles seem to disappear completely when rotating the environment.
Here is the Code:

public class Landscape {

    int displayList;

    public Landscape() throws IOException {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLU.gluPerspective(60f, 640f/480f, 0.0001f, 1000f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
        glPolygonMode(GL_FRONT_AND_BACK, GL_FLAT);
        glEnable(GL_CULL_FACE);

        BufferedImage heightMap = ImageIO.read(new File("heightmap.png"));
        BufferedImage heightMapColor = ImageIO.read(new File("hoehenprofil.png"));
        displayList = glGenLists(1);
        glNewList(displayList, GL_COMPILE);
        for (int z = 0; z < heightMap.getHeight(); z++) {
            glBegin(GL_TRIANGLE_STRIP);
            for (int x = 0; x < heightMap.getWidth(); x++) {
                int y = 0xFF - heightMap.getRGB(x, z) & 0xFF;
                int color = heightMapColor.getRGB(y-1, 0);
                glColor3ub((byte)((color >> 16) & 0xFF), (byte)((color >> 8) & 0xFF), (byte)(color & 0xFF));
                glVertex3i(x, y*5, z);
                if (z < heightMap.getHeight() - 1) {
                    y = 0xFF - heightMap.getRGB(x, z+1) & 0xFF;
                    color = heightMapColor.getRGB(y-1, 0);
                    glColor3ub((byte)((color >> 16) & 0xFF), (byte)((color >> 8) & 0xFF), (byte)(color & 0xFF));
                    glVertex3i(x, y*5, z+1);
                }
            }
            glEnd();
        }
        glEndList();

        float camX = 500, camY = 500, camZ = -500;
        float rotX = 45, rotY = 0, rotZ = 0;
        long lastTick = System.currentTimeMillis();
        int fps = 0;
        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();
            if (Keyboard.isKeyDown(Keyboard.KEY_W)) camZ++;
            if (Keyboard.isKeyDown(Keyboard.KEY_S)) camZ--;
            if (Keyboard.isKeyDown(Keyboard.KEY_A)) camX--;
            if (Keyboard.isKeyDown(Keyboard.KEY_D)) camX++;
            if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) camY++;
            if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) camY--;
            if (Keyboard.isKeyDown(Keyboard.KEY_Q)) rotY -= 0.5f;
            if (Keyboard.isKeyDown(Keyboard.KEY_E)) rotY += 0.5f;
            glRotatef(rotX, 1, 0, 0);
            glRotatef(rotY, 0, 1, 0);
            glRotatef(rotZ, 0, 0, 1);
            glTranslatef(-camX, -camY, camZ);
            glCallList(displayList);
            Display.update();

            fps++;
            if (lastTick + 1000 <= System.currentTimeMillis()) {
                lastTick += 1000;
                System.out.println("FPS: " + fps);
                fps = 0;
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new Landscape();
    }
}

这似乎也出现与深度测试禁用。甚至与剔除面启用此不会出现工作

It seem to also appear with depth test disabled. And even with cull face enabled this doesn't appear to work.

编辑:
除此之外,我让翻译一定量绕X或Y轴只是当一些黑色闪烁。这里是一个屏幕截图。

这导致了大量的滞后,我找不到任何理由。


Other than that I get some black flickering just when translating a certain amount around the x or y axis. Here is a screenshot of that.

It causes a huge amount of lag and I can't find any reason for that.

EDIT2:
我缩放x和z轴并除去y轴刻度为截图,只是按要求。这似乎只是绕Y轴远拉伸时引起的错误。


I scaled the x and z axis and removed the y axis scale for a screenshot, just as requested. It only seem to cause the bug when stretching far around the y axis.

推荐答案

远近平面之间你差(​​最后两个参数 gluPerspective )是非常大的。在这两个,你会得到更少的深度缓冲precision之间的相对规模(远远近分)放大。你的情况,远/近为100万,这是远远超过通常要。我常想,让他们在10左右的比例,即使像100很可能仍然是好的。

Your difference between near and far plane (last two arguments to gluPerspective) is extremely large. The larger the relative size (far divided by near) between these two, the less depth buffer precision you get. In your case, the far/near is 1,000,000, which is much more than you normally want. I normally like to keep them at a ratio of around 10, even though something like 100 would probably still be fine.

同时确保没有任何您想看到的几何形状被裁剪走扭捏的值。尝试像1.0和10.0。如果剪辑走在远端的几何形状,增加值远的东西像100.0,也许看看你是否可以增加接近价值。

Tweak the values while making sure that none of the geometry you want to see gets clipped away. Try something like 1.0 and 10.0. If that clips away geometry at the far end, increase the far value to something like 100.0, and maybe see if you can increase the near value as well.

这篇关于OpenGL的三角形包含孔和dissappear有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆