Fixed OpenGL
This commit is contained in:
parent
826af318d3
commit
9316eff244
6
CSV.pro
6
CSV.pro
@ -24,8 +24,7 @@ SOURCES += main.cpp \
|
||||
svobject.cpp \
|
||||
svpointcloud.cpp \
|
||||
svkernel.cpp \
|
||||
svpointcloudviewer.cpp \
|
||||
svpointcloudrenderer.cpp
|
||||
svpointcloudviewer.cpp
|
||||
|
||||
HEADERS += \
|
||||
svimage.h \
|
||||
@ -40,8 +39,7 @@ HEADERS += \
|
||||
svpointcloud.h \
|
||||
svdefs.h \
|
||||
svkernel.h \
|
||||
svpointcloudviewer.h \
|
||||
svpointcloudrenderer.h
|
||||
svpointcloudviewer.h
|
||||
|
||||
RESOURCES += \
|
||||
resource.qrc
|
||||
|
1
Main.qml
1
Main.qml
@ -152,6 +152,7 @@ ApplicationWindow {
|
||||
Item {
|
||||
anchors.fill: parent
|
||||
SvPointCloudViewer {
|
||||
//pointCloud: pointCloud
|
||||
anchors.fill: parent
|
||||
}
|
||||
}
|
||||
|
4
main.cpp
4
main.cpp
@ -18,7 +18,7 @@ int main(int argc, char *argv[])
|
||||
QQmlApplicationEngine engine;
|
||||
SvImageProvider imageProvider;
|
||||
|
||||
QImage imgLeft("../CSV/img/right9.png");
|
||||
QImage imgLeft("../ComputerVision/img/left9.png");
|
||||
QImage imgRight("../CSV/img/right1.png");
|
||||
|
||||
SvImage left(imgLeft);
|
||||
@ -35,10 +35,10 @@ int main(int argc, char *argv[])
|
||||
|
||||
engine.addImageProvider("images", &imageProvider);
|
||||
engine.rootContext()->setContextProperty("processor", &proc);
|
||||
engine.rootContext()->setContextProperty("pointCloud", &pointCloud);
|
||||
engine.load(QUrl(QStringLiteral("qrc:///Main.qml")));
|
||||
|
||||
proc.start();
|
||||
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
11
svcurve.cpp
11
svcurve.cpp
@ -9,6 +9,17 @@ void SvCurve::addPoint(SvPoint *point)
|
||||
m_points.push_back(point);
|
||||
}
|
||||
|
||||
bool SvCurve::hasPoint(SvPoint *point)
|
||||
{
|
||||
foreach (SvPoint *item, m_points) {
|
||||
if (point == item) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
SvCurve *SvCurve::cut(SvPoint *point)
|
||||
{
|
||||
|
||||
|
@ -23,10 +23,10 @@ private:
|
||||
public:
|
||||
SvPoint* head() {return m_points.first(); }
|
||||
SvPoint* tail() {return m_points.last(); }
|
||||
SvPoint* tail() {return m_points.last(); }
|
||||
void setInner(SvFigure *figure) {m_inner = figure; }
|
||||
void setOuter(SvFigure *figure) {m_outer = figure; }
|
||||
void addPoint(SvPoint *point);
|
||||
bool hasPoint(SvPoint *point);
|
||||
SvCurve *cut(SvPoint *point);
|
||||
};
|
||||
|
||||
|
@ -5,7 +5,11 @@ SvFigure::SvFigure(SvCurve *curve)
|
||||
addCurve(curve);
|
||||
}
|
||||
|
||||
SvCurve *SvFigure::findCurveByPoint()
|
||||
SvFigure::SvFigure()
|
||||
{
|
||||
}
|
||||
|
||||
SvCurve *SvFigure::findCurveByPoint(SvPoint *point)
|
||||
{
|
||||
foreach(SvCurve* curve, m_curves) {
|
||||
if (curve->hasPoint(point)) {
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef SVFIGURE_H
|
||||
#define SVFIGURE_H
|
||||
|
||||
#include <QColor>
|
||||
|
||||
#include "svpoint.h"
|
||||
#include "svcurve.h"
|
||||
|
||||
@ -11,10 +13,11 @@ protected:
|
||||
QList<SvCurve*> m_curves;
|
||||
|
||||
public:
|
||||
SvFigure();
|
||||
SvFigure(SvCurve *curve);
|
||||
|
||||
void addCurve(SvCurve *curve){m_curves.pop_back(curve); }
|
||||
SvCurve *findCurveByPoint();
|
||||
void addCurve(SvCurve *curve){m_curves.push_back(curve); }
|
||||
SvCurve *findCurveByPoint(SvPoint *point);
|
||||
SvFigure split(SvPoint *point);
|
||||
};
|
||||
|
||||
|
@ -91,8 +91,6 @@ void SvKernel::exec(SvPointCloud *pc, SvImage *image, int line)
|
||||
if (value > 0) {
|
||||
p.setX(x - 1);
|
||||
p.setY(line - 1);
|
||||
p.setHorizontalValue(value);
|
||||
p.setVerticalValue(value);
|
||||
|
||||
pc->addPoint(p);
|
||||
}
|
||||
|
@ -1,61 +0,0 @@
|
||||
#include "svpointcloudrenderer.h"
|
||||
|
||||
SvPointCloudRenderer::SvPointCloudRenderer(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void SvPointCloudRenderer::paint()
|
||||
{
|
||||
if (!m_program) {
|
||||
m_program = new QOpenGLShaderProgram();
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
|
||||
"attribute highp vec4 vertices;"
|
||||
"varying highp vec2 coords;"
|
||||
"void main() {"
|
||||
" gl_Position = vertices;"
|
||||
" coords = vertices.xy;"
|
||||
"}");
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
|
||||
"uniform lowp float t;"
|
||||
"varying highp vec2 coords;"
|
||||
"void main() {"
|
||||
" lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
|
||||
" i = smoothstep(t - 0.8, t + 0.8, i);"
|
||||
" i = floor(i * 20.) / 20.;"
|
||||
" gl_FragColor = vec4(coords * .5 + .5, i, i);"
|
||||
"}");
|
||||
|
||||
m_program->bindAttributeLocation("vertices", 0);
|
||||
m_program->link();
|
||||
|
||||
}
|
||||
|
||||
m_program->bind();
|
||||
|
||||
m_program->enableAttributeArray(0);
|
||||
|
||||
float values[] = {
|
||||
-1, -1,
|
||||
1, -1,
|
||||
-1, 1,
|
||||
1, 1
|
||||
};
|
||||
m_program->setAttributeArray(0, GL_FLOAT, values, 2);
|
||||
m_program->setUniformValue("t", (float) m_t);
|
||||
|
||||
glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
m_program->disableAttributeArray(0);
|
||||
m_program->release();
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#ifndef SVPOINTCLOUDRENDERER_H
|
||||
#define SVPOINTCLOUDRENDERER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QtGui/QOpenGLShaderProgram>
|
||||
#include <QtGui/QOpenGLContext>
|
||||
|
||||
class SvPointCloudRenderer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SvPointCloudRenderer(QObject *parent = 0);
|
||||
void setViewportSize(const QSize &size) { m_viewportSize = size; }
|
||||
|
||||
public slots:
|
||||
void paint();
|
||||
|
||||
private:
|
||||
QSize m_viewportSize;
|
||||
qreal m_t;
|
||||
QOpenGLShaderProgram *m_program;
|
||||
};
|
||||
|
||||
#endif // SVPOINTCLOUDRENDERER_H
|
@ -1,37 +1,70 @@
|
||||
#include "svpointcloudviewer.h"
|
||||
|
||||
#include <QtGui/QOpenGLShaderProgram>
|
||||
#include <QtGui/QOpenGLContext>
|
||||
|
||||
SvPointCloudViewer::SvPointCloudViewer(QQuickItem *parent) :
|
||||
QQuickItem(parent)
|
||||
QQuickPaintedItem(parent)
|
||||
{
|
||||
|
||||
setRenderTarget(QQuickPaintedItem::FramebufferObject);
|
||||
}
|
||||
|
||||
void SvPointCloudViewer::handleWindowChanged(QQuickWindow *win)
|
||||
void SvPointCloudViewer::paint(QPainter *painter)
|
||||
{
|
||||
if (win) {
|
||||
connect(win, &QQuickWindow::beforeSynchronizing,
|
||||
this, &SvPointCloudViewer::sync, Qt::DirectConnection);
|
||||
painter->beginNativePainting();
|
||||
|
||||
connect(win, &QQuickWindow::sceneGraphInvalidated,
|
||||
this, &SvPointCloudViewer::cleanup, Qt::DirectConnection);
|
||||
}
|
||||
}
|
||||
if (!m_program) {
|
||||
m_program = new QOpenGLShaderProgram();
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
|
||||
"attribute highp vec4 vertices;"
|
||||
"varying highp vec2 coords;"
|
||||
"void main() {"
|
||||
" gl_Position = vertices;"
|
||||
" coords = vertices.xy;"
|
||||
"}");
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
|
||||
"uniform lowp float t;"
|
||||
"varying highp vec2 coords;"
|
||||
"void main() {"
|
||||
" lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
|
||||
" i = smoothstep(t - 0.8, t + 0.8, i);"
|
||||
" i = floor(i * 20.) / 20.;"
|
||||
" gl_FragColor = vec4(coords * .5 + .5, i, i);"
|
||||
"}");
|
||||
|
||||
m_program->bindAttributeLocation("vertices", 0);
|
||||
m_program->link();
|
||||
|
||||
void SvPointCloudViewer::sync()
|
||||
{
|
||||
if (!m_pointCloudRenderer) {
|
||||
m_pointCloudRenderer = new SvPointCloudRenderer();
|
||||
connect(window(), &QQuickWindow::beforeRendering,
|
||||
m_pointCloudRenderer, &SvPointCloudRenderer::paint, Qt::DirectConnection);
|
||||
}
|
||||
|
||||
m_pointCloudRenderer->setViewportSize(window()->size() * window()->devicePixelRatio());
|
||||
m_program->bind();
|
||||
|
||||
m_program->enableAttributeArray(0);
|
||||
|
||||
float values[] = {
|
||||
-1, -1,
|
||||
1, -1,
|
||||
-1, 1,
|
||||
1, 1
|
||||
};
|
||||
m_program->setAttributeArray(0, GL_FLOAT, values, 2);
|
||||
m_program->setUniformValue("t", (float) 1);
|
||||
|
||||
glViewport(0, 0, width(), height());
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
glClearColor(0, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
m_program->disableAttributeArray(0);
|
||||
m_program->release();
|
||||
|
||||
painter->endNativePainting();
|
||||
}
|
||||
|
||||
void SvPointCloudViewer::cleanup()
|
||||
{
|
||||
if (m_pointCloudRenderer) {
|
||||
delete m_pointCloudRenderer;
|
||||
m_pointCloudRenderer = 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,41 +1,45 @@
|
||||
#ifndef SVPOINTCLOUDVIEWER_H
|
||||
#define SVPOINTCLOUDVIEWER_H
|
||||
|
||||
#include <QQuickItem>
|
||||
#include <QQuickPaintedItem>
|
||||
#include <QQuickWindow>
|
||||
#include <QPainter>
|
||||
#include <QtGui/QOpenGLShaderProgram>
|
||||
#include <QtGui/QOpenGLContext>
|
||||
|
||||
#include "svpointcloud.h"
|
||||
#include "svpointcloudrenderer.h"
|
||||
|
||||
class SvPointCloudViewer : public QQuickItem
|
||||
class SvPointCloudViewer : public QQuickPaintedItem
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
//Q_PROPERTY(QObject *pointCloud READ pointCloud WRITE setPointCloud NOTIFY pointCloudChanged)
|
||||
|
||||
// Q_PROPERTY(qreal cameraX READ cameraX WRITE setCameraX NOTIFY cameraXChanged)
|
||||
// Q_PROPERTY(qreal cameraY READ cameraY WRITE setCameraY NOTIFY cameraYChanged)
|
||||
// Q_PROPERTY(qreal cameraZ READ cameraZ WRITE setCameraZ NOTIFY cameraZChanged)
|
||||
|
||||
protected:
|
||||
SvPointCloud *pointCloud;
|
||||
SvPointCloudRenderer *m_pointCloudRenderer;
|
||||
SvPointCloud *m_pointCloud;
|
||||
qreal m_cameraX;
|
||||
qreal m_cameraY;
|
||||
qreal m_cameraZ;
|
||||
|
||||
QOpenGLShaderProgram *m_program;
|
||||
|
||||
public:
|
||||
explicit SvPointCloudViewer(QQuickItem *parent = 0);
|
||||
void paint(QPainter *painter);
|
||||
void setPointCloud(SvPointCloud *pointCloud) { m_pointCloud = pointCloud; }
|
||||
SvPointCloud *pointCloud() { return m_pointCloud; }
|
||||
|
||||
signals:
|
||||
void pointCloudChanged();
|
||||
void cameraXChanged();
|
||||
void cameraYChanged();
|
||||
void cameraZChanged();
|
||||
|
||||
private slots:
|
||||
void handleWindowChanged(QQuickWindow *win);
|
||||
|
||||
public slots:
|
||||
void sync();
|
||||
void cleanup();
|
||||
//void resetCamera();
|
||||
//void moveCamera(qreal X, qreal Y, qreal Z);
|
||||
// void rotateCamera(qreal X, qreal Y, qreal Z);
|
||||
|
@ -19,6 +19,7 @@ SvProcessor::SvProcessor(int numberOfWorkers)
|
||||
for (i = 0; i < m_numberOfWorkers; i++) {
|
||||
m_workers[i].setId(i);
|
||||
m_workers[i].setKernel(m_kernel);
|
||||
m_workers[i].setProcessor(this);
|
||||
m_workers[i].moveToThread(&m_threads[i]);
|
||||
|
||||
connect(&m_threads[i], &QThread::started, &m_workers[i], &SvWorker::start);
|
||||
@ -49,7 +50,7 @@ SvProcessorTask SvProcessor::nextTask()
|
||||
{
|
||||
SvProcessorTask task, *taskPtr;
|
||||
if (!m_taskQueue.size()) {
|
||||
throw new SvNoMoreTasks();
|
||||
throw SvNoMoreTasks();
|
||||
}
|
||||
|
||||
m_nextTaskMutex.lock();
|
||||
|
@ -6,12 +6,6 @@ SvWorker::SvWorker(QObject *parent) :
|
||||
|
||||
}
|
||||
|
||||
SvWorker::SvWorker(SvProcessor *processor, SvKernel *kernel)
|
||||
{
|
||||
m_processor = processor;
|
||||
m_kernel = kernel;
|
||||
}
|
||||
|
||||
void SvWorker::stop()
|
||||
{
|
||||
|
||||
|
@ -21,11 +21,10 @@ protected:
|
||||
|
||||
public:
|
||||
explicit SvWorker(QObject *parent = 0);
|
||||
SvWorker(SvProcessor *processor, SvKernel *kernel);
|
||||
|
||||
void setId(int id) {m_id = id;}
|
||||
void setKernel(SvKernel* kernel) {m_kernel = kernel;}
|
||||
|
||||
void setProcessor(SvProcessor* processor) {m_processor = processor;}
|
||||
signals:
|
||||
void finished(int id);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user