Qt3D C++程序中的玻璃材质?

问题描述 投票:0回答:1

我正在用 C++ 编写一个模拟程序,该程序在 C++ 的 QML Scene3D 对象中渲染 3D 场景。它效果很好,但我想在场景中添加几乎透明的圆锥体网格。

我尝试使用

Qt3DExtras::QDiffuseSpecularMaterial
和较低的 alpha 值,但结果不够透明。事实上,使用 alpha 值为零会产生纯白色物体。

    Qt3DExtras::QConeMesh *m_cone = new Qt3DExtras::QConeMesh(e);
    
    Qt3DExtras::QDiffuseSpecularMaterial *conemat = new Qt3DExtras::QDiffuseSpecularMaterial(e);
    QColor coneColor = QColor::fromRgbF(0,0.5,0,0);
    conemat->setDiffuse(coneColor);
    conemat->setAlphaBlendingEnabled(true);
 
    m_cone->setBottomRadius(1798444);
    m_cone->setTopRadius(0);
    m_cone->setLength(sat->orbitRadius()-EARTH_RADIUS);

我可以使用哪些 C++ 材质类和材质参数来绘制非常微弱的透明物体?

在使用着色器片段的 QML 中这似乎很容易。如何在 C++ 中做到这一点?

        CustomMaterial {
            shadingMode: CustomMaterial.Shaded
            fragmentShader: "material_transparent.frag"
        }

透明.frag

void MAIN()
{
    vec2 size = vec2(textureSize(SCREEN_TEXTURE, 0));
    vec2 uv = FRAGCOORD.xy / size;

    vec3 view = normalize(VIEW_VECTOR);
    vec3 projection = view - view * normalize(NORMAL);
    vec3 refraction = projection * projection;
    uv += refraction.xy * 0.5;

    vec3 col = texture(SCREEN_TEXTURE, uv).rgb;
    col = col * 0.8 + vec3(0.2);

    BASE_COLOR = vec4(col, 1.0);
}

编辑:karlphilip 建议我使用 QEffect。我尝试了一些基于 Qt 文档的东西(我找不到任何例子)。这是我尝试过的,但是 glShader 的状态最后总是“NotReady”。我一定是错过了什么。有什么想法吗?

    Qt3DRender::QEffect *effect = new Qt3DRender::QEffect();

    // Create technique, render pass and shader
    Qt3DRender::QTechnique *gl3Technique = new Qt3DRender::QTechnique();
    Qt3DRender::QRenderPass *gl3Pass = new Qt3DRender::QRenderPass();
    Qt3DRender::QShaderProgram *glShader = new Qt3DRender::QShaderProgram();

    // Set the shader on the render pass
    gl3Pass->setShaderProgram(glShader);

    // Add the pass to the technique
    gl3Technique->addRenderPass(gl3Pass);

    // Set the targeted GL version for the technique
    gl3Technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
    gl3Technique->graphicsApiFilter()->setMajorVersion(3);
    gl3Technique->graphicsApiFilter()->setMinorVersion(1);
    gl3Technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);

    // Add the technique to the effect
    effect->addTechnique(gl3Technique);

    QByteArray prog = glShader->loadSource(QUrl("qrc:/material_transparent.frag"));
    glShader->setFragmentShaderCode(prog);
c++ transparent qt3d materials
1个回答
0
投票

您可以仅使用

QPhongAlphaMaterial
作为圆柱体并调用
setAlpha()
设置所需的透明度级别。这是一个小屏幕截图,比较了两个 alpha 值的渲染结果:

enter image description here

还有

main.cpp
的Qt 5源代码,它渲染了上面的3D场景:

#include <QApplication>

#include <QtWidgets/QWidget>
#include <QtWidgets/QApplication>

#include <QtGui/QScreen>

#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>

#include <Qt3DRender/qcamera.h>
#include <Qt3DRender/qcameralens.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DRender/qrenderaspect.h>

#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>

#include <Qt3DExtras/QConeMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QPhongAlphaMaterial>


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

    //
    // Create window with 3D context using a 'blue' background color
    //

    Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x0000FF)));

    //
    // Instantiate main 3D entity
    //

    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);

    //
    // Setup camera
    //

    Qt3DRender::QCamera *cameraEntity = view->camera();
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 0, 20.0f));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));

    // Setup camera controls
    Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
    camController->setCamera(cameraEntity);

    //
    // Setup light source
    //

    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);

    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(cameraEntity->position());

    lightEntity->addComponent(light);
    lightEntity->addComponent(lightTransform);

    //
    // Add cylinder to the 3D scene
    //

    // Setup cone shape
    Qt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh();
    cone->setTopRadius(0.5);
    cone->setBottomRadius(1);
    cone->setLength(3);
    cone->setRings(50);
    cone->setSlices(20);

    // Setup ConeMesh transform
    Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform();
    coneTransform->setScale(3.f);
    coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
    coneTransform->setTranslation(QVector3D(0.f, 0.f, 0.f));

    // Setup ConeMesh material
    Qt3DExtras::QPhongAlphaMaterial *coneMaterial = new Qt3DExtras::QPhongAlphaMaterial();
    coneMaterial->setDiffuse(QColor(QRgb(0x00FF00)));
    coneMaterial->setAlpha(1.0f);

    // Add new cone entity to the 3D scene
    Qt3DCore::QEntity *coneEntity = new Qt3DCore::QEntity(rootEntity);
    coneEntity->addComponent(cone);
    coneEntity->addComponent(coneMaterial);
    coneEntity->addComponent(coneTransform);

    // Display 3D window
    view->show();
    view->resize(800, 600);

    return a.exec();
}

不要忘记将以下 Qt3D 模块添加到

.pro
文件上的 QT 变量中:

3dcore 3drender 3dinput 3dextras
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.