我试图绘制一个“透明”立方体(仅包含边缘),并将一个金字塔放置在此立方体内,问题是当我旋转它时,立方体的边缘在金字塔上绘制。

这是一个例子

c++ - 在立方体内绘制金字塔-LMLPHP

我知道为什么会发生这种情况(因为我先绘制了金字塔,然后再绘制了一个立方体),但是我希望在不需要时不要在金字塔上绘制这些边缘。

主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QOpenGLWindow>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <QTimer>

#ifdef _DEBUG
#   include <QDebug>
#endif

class MainWindow: public QOpenGLWindow
{
    Q_OBJECT

public:
    MainWindow();

private:
    QOpenGLContext *m_context;
    QTimer m_timer;
    GLfloat m_angle;

private slots:
    void rotate();

protected:
    void initializeGL() override;
    void paintGL() override;

    void paintEvent(QPaintEvent *event) override;
};

#endif // MAINWINDOW_H

主窗口
#include "mainwindow.h"

MainWindow::MainWindow():
    QOpenGLWindow(),
    m_angle(0.0f)
{
    QSurfaceFormat sf;

    sf.setProfile(QSurfaceFormat::CompatibilityProfile);
    sf.setVersion(2, 1);

    setFormat(sf);
    setSurfaceType(QWindow::OpenGLSurface);
    create();

    m_context = new QOpenGLContext();
    m_context->setFormat(sf);
    m_context->create();
    m_context->makeCurrent(this);

    connect(&m_timer, SIGNAL(timeout()), this, SLOT(rotate()));

    m_timer.start(33);
}

void MainWindow::rotate()
{
    m_angle += 0.5f;

    if (m_angle >= 360.0f) {
        m_angle = 0.0f;
    }

    update();
}

void MainWindow::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
}

void MainWindow::paintGL()
{
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glRotatef(m_angle, 1.0f, 1.0f, 1.0f);
    glScalef(0.3f, 0.3f, 0.3f);

    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);

        glVertex3f( 0.0f,  1.0f,  0.0f);
        glVertex3f( 1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);

        glColor3f(0.0f, 1.0f, 0.0f);

        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f( 1.0f, -1.0f, 1.0f);
        glVertex3f( 0.0f,  1.0f,  0.0f);

        glColor3f(0.0f, 0.0f, 1.0f);

        glVertex3f(-1.0f, -1.0f,  1.0f);
        glVertex3f( 0.0f,  1.0f,  0.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);

        glColor3f(1.0f, 1.0f, 0.0f);

        glVertex3f(1.0f, -1.0f,  1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f,  1.0f,  0.0f);

        // bottom
        glColor3f(0.0f, 1.0f, 1.0f);

        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f( 1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f,  1.0f);

        glVertex3f(-1.0f, -1.0f,  1.0f);
        glVertex3f( 1.0f, -1.0f, -1.0f);
        glVertex3f( 1.0f, -1.0f,  1.0f);
    glEnd();

    // The cube (only edges)

    glScalef(1.5f, 1.5f, 1.5f);
    glColor3f(0.0f, 0.0f, 0.0f);

    glBegin(GL_LINE_LOOP);
        glVertex3f( 1.0f,  1.0f, -1.0f);
        glVertex3f(-1.0f,  1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f( 1.0f, -1.0f, -1.0f);
    glEnd();

    glBegin(GL_LINE_LOOP);
        glVertex3f( 1.0f,  1.0f, 1.0f);
        glVertex3f(-1.0f,  1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f( 1.0f, -1.0f, 1.0f);
    glEnd();

    glBegin(GL_LINES);
        glVertex3f(-1.0f, 1.0f, -1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);

        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);

        glVertex3f(1.0f, 1.0f, -1.0f);
        glVertex3f(1.0f, 1.0f, 1.0f);

        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
    glEnd();

    glFlush();
}

void MainWindow::paintEvent(QPaintEvent*)
{
    paintGL();
}

main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    w.resize(480, 480);
    w.show();

    return a.exec();
}

最佳答案



金字塔的三角形是沿顺时针方向设置的,但是默认情况下,逆时针方向的多边形是朝前的。设置 glFrontFace(GL_CW) 或更改glVertex3f的顺序。



Depth Testing处于 Activity 状态时,将根据片段的深度显示片段。将片段中的深度值与当前帧缓冲区中匹配样本中的深度值进行比较。条件测试由函数 glDepthFunc 指定。 glDepthFunc的初始值为GL_LESS

在您的示例中,三角形碎片的深度比四边形碎片的深度少,因此三角形在四边形上方。

c&#43;&#43; - 在立方体内绘制金字塔-LMLPHP

关于c++ - 在立方体内绘制金字塔,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52379526/

10-13 05:02