Commit 9a189016 authored by Jonas Karlsson's avatar Jonas Karlsson
Browse files

Boomer bounding box

To use add Qt::Quick3DHackarinoPrivate to CMakeLists.txt of the project
using a bounding box.

To add it to qml add a model like this:

        Model {
            castsShadows: false
            receivesShadows: false
            visible: true
            geometry: ExampleBoxGeometry {
                name: "mybox"
            }
            materials: [
                DefaultMaterial {
                    lighting: DefaultMaterial.NoLighting
                    cullMode: DefaultMaterial.NoCulling
                    diffuseColor: "yellow"
                }
            ]
        }

Include <QtQuick3DHackarino/private/bboxhandler_p.h> in c++ source file.

To add a debug box named mybox do something like:

std::array<QVector3D, 8> vts;
vts[0] = camVerts[0];
vts[1] = camVerts[1];
vts[2] = camVerts[2];
vts[3] = camVerts[3];
vts[4] = camVerts[4];
vts[5] = camVerts[5];
vts[6] = camVerts[6];
vts[7] = camVerts[7];

BoxHandler::addDebugBox(QStringLiteral("mybox"), vts);

Change-Id: Ie5baa3d5abda1754534b574531a85088c239469d
parent ad6258f1
......@@ -4,6 +4,7 @@
# Generated from src.pro.
add_subdirectory(3rdparty)
add_subdirectory(hackarino)
add_subdirectory(utils)
add_subdirectory(assetimport)
add_subdirectory(runtimerender)
......
#####################################################################
## Quick3DHackarino Module:
#####################################################################
qt_internal_add_module(Quick3DHackarino
SOURCES
bboxhandler.cpp bboxhandler_p.h
DEFINES
QT_BUILD_QUICK3DHACKARINO_LIB
LIBRARIES
Qt::CorePrivate
Qt::GuiPrivate
PUBLIC_LIBRARIES
Qt::Core
Qt::Gui
PRIVATE_MODULE_INTERFACE
Qt::CorePrivate
Qt::GuiPrivate
)
#include "bboxhandler_p.h"
#include <mutex>
static QHash<QString, QList<std::array<QVector3D, 8>>> boxes;
static QHash<QString, bool> g_isDirty;
std::mutex g_pages_mutex;
QT_BEGIN_NAMESPACE
QList<std::array<QVector3D, 8>> BoxHandler::getDebugBoxes(QString name)
{
std::lock_guard<std::mutex> guard(g_pages_mutex);
g_isDirty[name] = false;
if (boxes.contains(name)) {
return boxes[name];
}
const std::array<QVector3D, 8> points = {
QVector3D(-0.5f, -0.5f, -0.5f), QVector3D(-0.5f, -0.5f, +0.5f), QVector3D(-0.5f, +0.5f, -0.5f),
QVector3D(-0.5f, +0.5f, +0.5f), QVector3D(+0.5f, -0.5f, -0.5f), QVector3D(+0.5f, -0.5f, +0.5f),
QVector3D(+0.5f, +0.5f, -0.5f), QVector3D(+0.5f, +0.5f, +0.5f),
};
return { points };
}
void BoxHandler::addDebugBox(const QString &s, const std::array<QVector3D, 8> &b)
{
std::lock_guard<std::mutex> guard(g_pages_mutex);
boxes[s] = { b };
g_isDirty[s] = true;
}
void BoxHandler::addDebugBoxes(const QString &s, const QList<std::array<QVector3D, 8>> &b)
{
std::lock_guard<std::mutex> guard(g_pages_mutex);
boxes[s] = b;
g_isDirty[s] = true;
}
bool BoxHandler::isDirty(QString name)
{
std::lock_guard<std::mutex> guard(g_pages_mutex);
if (!g_isDirty.contains(name))
g_isDirty[name] = true;
return g_isDirty[name];
}
QT_END_NAMESPACE
#pragma once
#include <QHash>
#include <QString>
#include <QVector3D>
#include <array>
#include <QList>
QT_BEGIN_NAMESPACE
#ifndef QT_STATIC
#if defined(QT_BUILD_QUICK3DHACKARINO_LIB)
#define Q_QUICK3DHACKARINO_EXPORT Q_DECL_EXPORT
#else
#define Q_QUICK3DHACKARINO_EXPORT Q_DECL_IMPORT
#endif
#else
#define Q_QUICK3DHACKARINO_EXPORT
#endif
class Q_QUICK3DHACKARINO_EXPORT BoxHandler
{
public:
static QList<std::array<QVector3D, 8>> getDebugBoxes(QString name);
static void addDebugBox(const QString &s, const std::array<QVector3D, 8> &b);
static void addDebugBoxes(const QString &s, const QList<std::array<QVector3D, 8>> &b);
static bool isDirty(QString name);
};
QT_END_NAMESPACE
......@@ -15,6 +15,7 @@ qt_internal_add_qml_module(Quick3DHelpers
QtQuick3D/auto
PAST_MAJOR_VERSIONS 1
SOURCES
bboxgeometry.cpp bboxgeometry_p.h
gridgeometry.cpp gridgeometry_p.h
heightfieldgeometry.cpp heightfieldgeometry_p.h
randominstancing.cpp randominstancing_p.h
......@@ -34,6 +35,8 @@ qt_internal_add_qml_module(Quick3DHelpers
QT_BUILD_QUICK3DHELPERS_LIB
LIBRARIES
Qt::Quick3D
Qt::Quick3DRuntimeRender
Qt::Quick3DHackarinoPrivate
PUBLIC_LIBRARIES
Qt::Qml
Qt::Quick
......
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "bboxgeometry_p.h"
#include <QVector3D>
#include <QTimer>
#include <QtQuick3DHackarino/private/bboxhandler_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendererimpllayerrenderdata_p.h>
ExampleBoxGeometry::ExampleBoxGeometry()
{
updateData();
activeTimer = new QTimer(this);
activeTimer->setInterval(1000 / 60);
connect(activeTimer, SIGNAL(timeout()), this, SLOT(timeUpdate()));
activeTimer->start();
}
ExampleBoxGeometry::~ExampleBoxGeometry()
{
activeTimer->stop();
delete activeTimer;
}
void ExampleBoxGeometry::timeUpdate()
{
updateData();
update();
}
void ExampleBoxGeometry::setName(QString name)
{
if (m_name == name)
return;
m_name = name;
emit nameChanged();
updateData();
update();
}
/////////////////////////////////////////////////
// 0,1 ... 4,5
// 2,3 ... 6,7
void ExampleBoxGeometry::uploadBox(const QList<std::array<QVector3D, 8>> &boxes)
{
QVector3D min(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
QVector3D max(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
constexpr int BOX_DATA_SIZE = 12 * sizeof(float) * 3 * 2;
QByteArray vertexData;
const int size = boxes.length() * BOX_DATA_SIZE;
vertexData.resize(size);
int ctr = 0;
for (const auto &points : boxes) {
for (const auto &point : points) {
min.setX(std::min(min.x(), point.x()));
min.setY(std::min(min.y(), point.y()));
min.setZ(std::min(min.z(), point.z()));
max.setX(std::max(max.x(), point.x()));
max.setY(std::max(max.y(), point.y()));
max.setZ(std::max(max.z(), point.z()));
}
std::array<std::array<int, 2>, 12> indices = {
std::array<int, 2> { 0, 1 }, std::array<int, 2> { 1, 2 }, std::array<int, 2> { 2, 3 },
std::array<int, 2> { 3, 0 }, std::array<int, 2> { 4, 5 }, std::array<int, 2> { 5, 6 },
std::array<int, 2> { 6, 7 }, std::array<int, 2> { 7, 4 }, std::array<int, 2> { 0, 4 },
std::array<int, 2> { 1, 5 }, std::array<int, 2> { 2, 6 }, std::array<int, 2> { 3, 7 },
};
QVector3D *dataPtr = reinterpret_cast<QVector3D *>(vertexData.data() + ctr * BOX_DATA_SIZE);
++ctr;
for (size_t i = 0; i < indices.size(); i++) {
const QVector3D from = points[indices[i][0]];
const QVector3D to = points[indices[i][1]];
dataPtr[i * 2] = from;
dataPtr[i * 2 + 1] = to;
}
}
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0, QQuick3DGeometry::Attribute::ComponentType::F32Type);
setStride(3 * sizeof(float));
setVertexData(vertexData);
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Lines);
setBounds(min, max);
}
void ExampleBoxGeometry::updateData()
{
if (!BoxHandler::isDirty(m_name))
return;
clear();
const auto boxes = BoxHandler::getDebugBoxes(m_name);
uploadBox(boxes);
update();
}
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef BBOX_GEOMETRY_H
#define BBOX_GEOMETRY_H
#include <QtQuick3D/private/qquick3dgeometry_p.h>
#include <QVector3D>
#include <QString>
#include <array>
#include <QList>
class ExampleBoxGeometry : public QQuick3DGeometry
{
Q_OBJECT
QML_NAMED_ELEMENT(ExampleBoxGeometry)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
ExampleBoxGeometry();
~ExampleBoxGeometry();
QString name() const { return m_name; }
void setName(QString name);
public slots:
void timeUpdate();
signals:
void nameChanged();
private:
void uploadBox(const QList<std::array<QVector3D, 8>> &points);
void updateData();
QString m_name;
QTimer *activeTimer;
};
#endif
%modules = ( # path to module name map
"QtQuick3DHackarino" => "$basedir/src/hackarino",
"QtQuick3DUtils" => "$basedir/src/utils",
"QtQuick3DRuntimeRender" => "$basedir/src/runtimerender",
"QtQuick3DAssetImport" => "$basedir/src/assetimport",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment