aboutsummaryrefslogtreecommitdiff
path: root/src/accelerate/myqthreads.cpp
blob: ead3aeca20fe52f2087ff38b2f04a0365a4bc1be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "raytracer/raytracer.h"
#include <QThread>

/**
 * Extra credit -> own implementation of multithreading using QThreads.
 * NOT USED for illuminate (not any faster than QT's version), but was used in intersect.
 */

//struct intersectRoutineArgs {
//    RenderShapeData shape;
//    glm::vec4 pWorld;
//    glm::vec4 dWorld;
//};
//
//struct intersectData {
//    float distance;
//    glm::vec4 intersectionWorld;
//    glm::vec4 intersectionObj;
//    RenderShapeData intersectedShape;
//};
//
//Q_DECLARE_METATYPE(intersectData);
//
//class IntersectWorker : public QThread
//{
//    Q_OBJECT
//    void run() override {
//        exec();
//        /* ... here is the expensive or blocking operation ... */
//        glm::vec4 pObject = glm::inverse(a.shape.ctm) * a.pWorld;
//        glm::vec4 dObject = glm::normalize(glm::inverse(a.shape.ctm) * a.dWorld);
//
//        glm::vec4 intersectionObj = RayTracer::findIntersection(pObject, dObject, a.shape);
//        if (intersectionObj.w == 0) // no hit
//        {
//            const intersectData response{
//                    FINF,
//                    glm::vec4(0.f),
//                    glm::vec4(0.f),
//                    a.shape
//            };
//            ps.append(response);
//            emit data(response);
//        } else {
//            auto intersectionWorld = a.shape.ctm * intersectionObj;
//            float distance = glm::distance(intersectionWorld, a.pWorld);
//
//            const intersectData response{
//                    distance,
//                    intersectionWorld,
//                    intersectionObj,
//                    a.shape
//            };
//            ps.append(response);
//            emit data(response);
//        }
//        emit finished();
//    }
//public:
//    intersectRoutineArgs a;
//    QList<intersectData> &ps;
//    IntersectWorker(intersectRoutineArgs args, QList<intersectData> &p) : ps(p)
//    {
//        a = args;
//    }
//    signals:
//            void data(const intersectData &s);
//    void finished();
//};
//
//
//class IntersectController : public QObject
//{
//    Q_OBJECT
//public:
//    std::vector<QThread*> qthreads;
//    QList<intersectData> intersectPoints;
//    IntersectController(const std::vector<RenderShapeData> &shapes, glm::vec4 pWorld, glm::vec4 dWorld) {
//        qRegisterMetaType<const intersectData&>("myType");
//        int id = 0;
//        for (const RenderShapeData &shape: shapes) {
//            const intersectRoutineArgs threadArgs{shape, pWorld, dWorld};
//            IntersectWorker *thread = new IntersectWorker(threadArgs, intersectPoints);
//
//            connect(thread, &IntersectWorker::data, this, &IntersectController::addIntersectionPoint);
//            connect(thread, &IntersectWorker::finished, thread, &QThread::quit);
//
//            connect(thread, &IntersectWorker::finished, thread, &QThread::deleteLater);
//
//            qthreads.push_back(thread);
//        }
//    }
//    ~IntersectController() {
//        for (QThread* workerThread: qthreads) {
//            workerThread->exit();
//        }
//        qthreads.clear();
//        intersectPoints.clear();
//    }
//    void getClosestIntersection(float &minDist, glm::vec4 &closestIntersectionWorld, glm::vec4 &closestIntersectionObj, RenderShapeData intersectedShape) {
//        // start then wait
//        for (QThread* thread: qthreads) {
//            thread->start();
//        }
//        for (QThread* thread: qthreads) {
//            thread->quit();
//            thread->wait();
//        }
//
//
//        // once all threads are done, find the closest
//        for (const intersectData &i : intersectPoints) {
//            if (i.distance < minDist) {
//                minDist = i.distance;
//
//                intersectedShape = i.intersectedShape;
//                closestIntersectionObj = i.intersectionObj;
//                closestIntersectionWorld = i.intersectionWorld;
//            }
//        }
//}
//public slots:
//            void addIntersectionPoint(const intersectData &s) {
//        intersectPoints.append(s);
//    }
//    signals:
//            void operate(intersectRoutineArgs a);
//};
//
//#include "myqthreads.moc"