OpenGL:模型显示不正确。 (需要固定绘制距离吗?)

发布于 2024-09-19 01:49:42 字数 2832 浏览 1 评论 0原文

我是 OpenGL 新手。我正在玩 JOGL。

我有一个 .obj 模型,我正在将其绘制为多边形。看起来还不错,只是大部分都被剪掉了。所以我认为我需要增加绘制距离。

我不太确定该怎么做。这是我的渲染代码:

private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        GLUgl2 glu = new GLUgl2();

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

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();

        // Position the camera
        glu.gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, cameraPos.x
                + cameraForward.x, cameraPos.y + cameraForward.y, cameraPos.z
                + cameraForward.z, cameraUp.x, cameraUp.y, cameraUp.z);

            // uncommenting out this line will make everything disappear.
        // glu.gluPerspective(2, 1, 10, 1000);

        // ****** Start of example code ******
        gl.glCallList(axes); // Show X, Y, Z axes

        gl.glCallList(secondLines);

        gl.glCallList(triangle);

        gazebo.render(gl);

                // ...

我的问题可能完全是其他问题。 gazebo 属于 ObjModel 类型,它是一个读取和表示 .obj 文件的类。以下是它的渲染和构建绘制列表方法:

private int drawID = 0;

public ObjModel(String fn, GL2 gl) {

            // ...

    readFile(fn);

    drawID = buildDrawList(gl);
}

public void render(GL2 gl) {
    gl.glCallList(drawID);
}

private int buildDrawList(GL2 gl) {
    int result = gl.glGenLists(1);

    gl.glNewList(result, GL2.GL_COMPILE);
    gl.glPushMatrix();
    gl.glBegin(GL2.GL_POLYGON);

    for (Point3f vert : vertices) {
        gl.glVertex3f(vert.x, vert.y, vert.z);
    }

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

    return result;
}

private void readFile(String filename) {
    try {
        BufferedReader input = new BufferedReader(new FileReader(fileName));
        try {
            String newLine = null;
            while ((newLine = input.readLine()) != null) {

                int indVn = newLine.indexOf("vn ");
                if (indVn != -1) {
                    readNormal(newLine);
                    continue;
                }

                int indV = newLine.indexOf("v ");
                if (indV != -1) {
                    readVertex(newLine);
                    continue;
                }

                int indF = newLine.indexOf("f ");
                if (indF != -1) {
                    readPolygon(newLine);
                    continue;
                }

                int indVt = newLine.indexOf("vt ");
                if (indVt != -1) {
                    readTexture(newLine);
                    continue;
                }
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

如果需要更多信息,我可以发布屏幕截图。

有什么建议吗?谢谢。

I'm new to OpenGL. I'm playing around with JOGL.

I have a .obj model that I'm drawing as polygons. It looks ok, except that most of it gets clipped. So I think that I need to increase the draw distance.

I'm not really sure how to do it. Here is my render code:

private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        GLUgl2 glu = new GLUgl2();

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

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();

        // Position the camera
        glu.gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, cameraPos.x
                + cameraForward.x, cameraPos.y + cameraForward.y, cameraPos.z
                + cameraForward.z, cameraUp.x, cameraUp.y, cameraUp.z);

            // uncommenting out this line will make everything disappear.
        // glu.gluPerspective(2, 1, 10, 1000);

        // ****** Start of example code ******
        gl.glCallList(axes); // Show X, Y, Z axes

        gl.glCallList(secondLines);

        gl.glCallList(triangle);

        gazebo.render(gl);

                // ...

It's possible that my problem is something else entirely. gazebo is of type ObjModel, a class that reads and represents .obj files. Here are its render and build draw list methods:

private int drawID = 0;

public ObjModel(String fn, GL2 gl) {

            // ...

    readFile(fn);

    drawID = buildDrawList(gl);
}

public void render(GL2 gl) {
    gl.glCallList(drawID);
}

private int buildDrawList(GL2 gl) {
    int result = gl.glGenLists(1);

    gl.glNewList(result, GL2.GL_COMPILE);
    gl.glPushMatrix();
    gl.glBegin(GL2.GL_POLYGON);

    for (Point3f vert : vertices) {
        gl.glVertex3f(vert.x, vert.y, vert.z);
    }

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

    return result;
}

private void readFile(String filename) {
    try {
        BufferedReader input = new BufferedReader(new FileReader(fileName));
        try {
            String newLine = null;
            while ((newLine = input.readLine()) != null) {

                int indVn = newLine.indexOf("vn ");
                if (indVn != -1) {
                    readNormal(newLine);
                    continue;
                }

                int indV = newLine.indexOf("v ");
                if (indV != -1) {
                    readVertex(newLine);
                    continue;
                }

                int indF = newLine.indexOf("f ");
                if (indF != -1) {
                    readPolygon(newLine);
                    continue;
                }

                int indVt = newLine.indexOf("vt ");
                if (indVt != -1) {
                    readTexture(newLine);
                    continue;
                }
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

I can post screenshots if more information is needed.

Any advice? Thanks.

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

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

发布评论

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

评论(2

送你一个梦 2024-09-26 01:49:42

听起来您的模型的顶点位于剪裁平面之外。有一些解决方案。

1) 缩小模型,使所有顶点都适合剪辑坐标(使用 glScale()

2) 使用 gluPerspective 设置具有更深剪辑平面的“相机”。尽管您已经尝试过此操作,但我怀疑您不需要的结果是因为您在使用此调用时没有修改投影矩阵。
尝试:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60, 1, 0.1, 1000.0 );

3)使用glFrustrum()修改裁剪平面。这与 gluPerspective 类似,但提供了更大的灵活性,但代价是增加了一些复杂性。

仅供参考,在模型视图矩阵模式下,您仍然可以使用 gluPerspective 或 glFrustrum,但请记住,进行这些调用和其他矩阵转换调用(例如 glScale、glTranslate、glRotate)的顺序很重要。

It sounds like your model has vertices outside the clipping plane. There are a few solutions for this.

1) scale down your model so that all vertices fit within the clip coordinates (use glScale())

2) Use gluPerspective to set up a 'camera' which has a deeper clipping plane. Though you've tried this, I suspect your unwanted results are because you're not modifying the projection matrix when using this call.
Try:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60, 1, 0.1, 1000.0 );

3) Use glFrustrum() to modify the clipping planes. This is similar to gluPerspective but provides greater flexibility at the expensive of some added complexity.

FYI you can still use gluPerspective or glFrustrum when in modelview matrix mode, but keep in mind that the order with which you make those calls and other matrix transformation calls (eg. glScale, glTranslate, glRotate) is important.

别在捏我脸啦 2024-09-26 01:49:42

试试这个: gluPerspective(60, 1, 0.1, 1000);

try this instead : gluPerspective(60, 1, 0.1, 1000);

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