Skip to content

Commit

Permalink
Added multisampling functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
florianblume committed Jan 22, 2021
1 parent 8395118 commit 78691e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
Binary file modified lib/libqt3dwidget.a
Binary file not shown.
Binary file modified lib/libqt3dwidget.so.2.0.0
Binary file not shown.
37 changes: 27 additions & 10 deletions qt3dwidget/qt3dwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ Qt3DWidgetPrivate::Qt3DWidgetPrivate()
, m_root(new Qt3DCore::QEntity)
, m_userRoot(nullptr)
, m_offscreenSurface(new QOffscreenSurface)
, m_renderStateSet(new Qt3DRender::QRenderStateSet)
, m_depthTest(new Qt3DRender::QDepthTest)
, m_multisampleAntialiasing(new Qt3DRender::QMultiSampleAntiAliasing)
, m_renderTargetSelector(new Qt3DRender::QRenderTargetSelector)
, m_renderSurfaceSelector(new Qt3DRender::QRenderSurfaceSelector)
, m_renderTarget(new Qt3DRender::QRenderTarget)
, m_colorOutput(new Qt3DRender::QRenderTargetOutput)
, m_colorTexture(new Qt3DRender::QTexture2D)
, m_colorTexture(new Qt3DRender::QTexture2DMultisample)
, m_depthOutput(new Qt3DRender::QRenderTargetOutput)
, m_depthTexture(new Qt3DRender::QTexture2D)
, m_depthTexture(new Qt3DRender::QTexture2DMultisample)
, m_initialized(false) {
}

Expand Down Expand Up @@ -71,7 +74,7 @@ void Qt3DWidgetPrivate::init() {
m_shaderProgram.reset(new QOpenGLShaderProgram);
m_shaderProgram->addShaderFromSourceCode(
QOpenGLShader::Vertex,
"#version 130\n"
"#version 150\n"
"in highp vec3 vertex;\n"
"in mediump vec2 texCoord;\n"
"out mediump vec2 texc;\n"
Expand All @@ -84,14 +87,18 @@ void Qt3DWidgetPrivate::init() {
);
m_shaderProgram->addShaderFromSourceCode(
QOpenGLShader::Fragment,
"#version 130\n"
"uniform sampler2D texture;\n"
"#version 150\n"
"uniform sampler2DMS texture;\n"
"in mediump vec2 texc;\n"
"uniform int samples;\n"
"void main(void)\n"
"{\n"
"vec2 finalCoord = texc;"
"finalCoord.y = 1 - finalCoord.y;"
" gl_FragColor = texture2D(texture, finalCoord.xy);\n"
" ivec2 tc = ivec2(floor(textureSize(texture) * texc));\n"
" vec4 color = vec4(0.0);\n"
" for(int i = 0; i < samples; i++) {\n"
" color += texelFetch(texture, tc, i);\n"
" }\n"
" gl_FragColor = color / float(samples);\n"
"}\n"
);
m_shaderProgram->bindAttributeLocation("vertex", m_vertexAttributeLoc);
Expand All @@ -100,6 +107,7 @@ void Qt3DWidgetPrivate::init() {

m_shaderProgram->bind();
m_shaderProgram->setUniformValue("texture", 0);
m_shaderProgram->setUniformValue("samples", QSurfaceFormat::defaultFormat().samples());
m_shaderProgram->release();
}

Expand All @@ -110,6 +118,8 @@ Qt3DWidget::Qt3DWidget(QWidget *parent)

setMouseTracking(true);

int samples = QSurfaceFormat::defaultFormat().samples();

d->m_offscreenSurface->setFormat(QSurfaceFormat::defaultFormat());
d->m_offscreenSurface->create();

Expand All @@ -129,6 +139,7 @@ Qt3DWidget::Qt3DWidget(QWidget *parent)

// Hook the texture up to our output, and the output up to this object.
d->m_colorOutput->setTexture(d->m_colorTexture);
d->m_colorTexture->setSamples(samples);
d->m_renderTarget->addOutput(d->m_colorOutput);

// Setup depth
Expand All @@ -144,8 +155,13 @@ Qt3DWidget::Qt3DWidget(QWidget *parent)

// Hook up the depth texture
d->m_depthOutput->setTexture(d->m_depthTexture);
d->m_depthTexture->setSamples(samples);
d->m_renderTarget->addOutput(d->m_depthOutput);

d->m_renderStateSet->addRenderState(d->m_multisampleAntialiasing);
d->m_renderStateSet->addRenderState(d->m_depthTest);
d->m_depthTest->setDepthFunction(Qt3DRender::QDepthTest::LessOrEqual);
d->m_renderTargetSelector->setParent(d->m_renderStateSet);
d->m_renderTargetSelector->setTarget(d->m_renderTarget);

d->m_renderSurfaceSelector->setSurface(d->m_offscreenSurface);
Expand All @@ -154,7 +170,7 @@ Qt3DWidget::Qt3DWidget(QWidget *parent)
d->m_forwardRenderer->setCamera(d->m_defaultCamera);
d->m_forwardRenderer->setSurface(d->m_offscreenSurface);
d->m_forwardRenderer->setParent(d->m_renderSurfaceSelector);
d->m_renderSettings->setActiveFrameGraph(d->m_renderTargetSelector);
d->m_renderSettings->setActiveFrameGraph(d->m_renderStateSet);
d->m_inputSettings->setEventSource(this);

d->m_activeFrameGraph = d->m_forwardRenderer;
Expand All @@ -175,6 +191,7 @@ void Qt3DWidget::paintGL() {

glClearColor(1.0, 1.0, 1.0, 1.0);
glDisable(GL_BLEND);
glEnable(GL_MULTISAMPLE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

d->m_shaderProgram->bind();
Expand All @@ -186,7 +203,7 @@ void Qt3DWidget::paintGL() {
QOpenGLVertexArrayObject::Binder vaoBinder(&d->m_vao);

d->m_shaderProgram->setUniformValue("matrix", m);
glBindTexture(GL_TEXTURE_2D, d->m_colorTexture->handle().toUInt());
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, d->m_colorTexture->handle().toUInt());
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
d->m_shaderProgram->release();
Expand Down
10 changes: 8 additions & 2 deletions qt3dwidget/qt3dwidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
#include <Qt3DRender/QRenderCapture>
#include <Qt3DRender/QRenderCaptureReply>
#include <Qt3DRender/QViewport>
#include <Qt3DRender/QDepthTest>
#include <Qt3DRender/QCameraSelector>
#include <Qt3DRender/QClearBuffers>
#include <Qt3DRender/QRenderStateSet>
#include <Qt3DRender/QMultiSampleAntiAliasing>
#include <QOffscreenSurface>

class Qt3DWidgetPrivate : public QObject
Expand Down Expand Up @@ -66,13 +69,16 @@ class Qt3DWidgetPrivate : public QObject

// Offscreen framegraph
QOffscreenSurface *m_offscreenSurface;
Qt3DRender::QRenderStateSet *m_renderStateSet;
Qt3DRender::QDepthTest *m_depthTest;
Qt3DRender::QMultiSampleAntiAliasing *m_multisampleAntialiasing;
Qt3DRender::QRenderTargetSelector *m_renderTargetSelector;
Qt3DRender::QRenderSurfaceSelector *m_renderSurfaceSelector;
Qt3DRender::QRenderTarget *m_renderTarget;
Qt3DRender::QRenderTargetOutput *m_colorOutput;
Qt3DRender::QTexture2D *m_colorTexture;
Qt3DRender::QTexture2DMultisample *m_colorTexture;
Qt3DRender::QRenderTargetOutput *m_depthOutput;
Qt3DRender::QTexture2D *m_depthTexture;
Qt3DRender::QTexture2DMultisample *m_depthTexture;

// OpenGL setup
ShaderProgramPtr m_shaderProgram;
Expand Down
7 changes: 6 additions & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QTimer>
#include <QPushButton>

#include <QSurfaceFormat>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QTorusMesh>
Expand All @@ -14,6 +15,10 @@
#include <Qt3DExtras/QForwardRenderer>

int main(int argc, char *argv[]) {
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSamples(4);
format.setVersion(3, 0);
QSurfaceFormat::setDefaultFormat(format);
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QApplication a(argc, argv);
MainWindow w;
Expand Down Expand Up @@ -42,7 +47,7 @@ int main(int argc, char *argv[]) {

Qt3DExtras::QForwardRenderer *forwardRenderer = new Qt3DExtras::QForwardRenderer;
forwardRenderer->setCamera(widget->camera());
forwardRenderer->setClearColor(Qt::black);
forwardRenderer->setClearColor(Qt::gray);
widget->setActiveFrameGraph(forwardRenderer);

QTimer animationTimer;
Expand Down

0 comments on commit 78691e2

Please sign in to comment.