Q_OBJECT
public:
GLWidget( );
protected:
void paintGL();
void initializeGL();
private:
void genCross();
QGLShaderProgram program;
int matrixUniform;
};
const QPainter::PixmapFragment crossCoord
[4] = { QPainter::PixmapFragment::create(QPointF(300-15-8,
240),
QRect(0,
0,
32,
4),
1,
1,
180,
1 ),
QPainter::PixmapFragment::create(QPointF(300+15+8,
240),
QRect(0,
0,
32,
4),
1,
1,
0,
1 ),
QPainter::PixmapFragment::create(QPointF(300,
240-15-8),
QRect(0,
0,
32,
4),
1,
1,
-90,
1 ),
};
GLWidget::GLWidget( )
{
genCross();
this->setAttribute(Qt::WA_AcceptTouchEvents);
this->setAutoBufferSwap(false);
this->setAttribute(Qt::WA_DeleteOnClose);
this->setAutoFillBackground (false);
}
void GLWidget::initializeGL ()
{
QGLShader *vshader = new QGLShader(QGLShader::Vertex);
const char *vsrc =
"attribute highp vec4 vertex;\n"
"attribute highp vec2 texCoord;\n"
"uniform mediump mat4 matrix;\n"
"varying highp vec2 texc;\n"
"void main(void)\n"
"{\n"
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
" gl_Position = matrix * vertex;\n"
" texc = texCoord;\n"
"}\n";
vshader->compileSourceCode(vsrc);
QGLShader *fshader = new QGLShader(QGLShader::Fragment);
const char *fsrc =
"varying highp vec2 texc;\n"
"uniform sampler2D tex;\n"
"void main(void)\n"
"{\n"
" highp vec3 color = texture2D(tex, texc.st).rgb;\n"
" gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n"
"}\n";
fshader->compileSourceCode(fsrc);
program.addShader(vshader);
program.addShader(fshader);
program.link();
int vertexAttr = program.attributeLocation("vertex");
int texCoordAttr = program.attributeLocation("texCoord");
matrixUniform = program.uniformLocation("matrix");
int textureUniform = program.uniformLocation("tex");
glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
}
void GLWidget::paintGL()
{
livePainter.beginNativePainting();
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 modelview;
modelview.translate(0, 0, 0);
modelview.scale(1.0,1.0);
program.bind();
program.setUniformValue(matrixUniform, modelview);
//gl stuff
// ...
program.release();
livePainter.endNativePainting();
livePainter.
drawPixmapFragments ( crossCoord,
4,
*crossPixmap,
QPainter::OpaqueHint );
livePainter.end();
swapBuffers();
}
void GLWidget::genCross()
{
pen.setBrush(Qt::black);
painter.setBrush(Qt::yellow);
painter.setPen(pen);
painter.drawPolygon(pts, 5);
painter.end();
}
class GLWidget : public QGLWidget {
Q_OBJECT
public:
GLWidget( );
protected:
void paintGL();
void initializeGL();
private:
void genCross();
QPixmap *crossPixmap;
void drawCross(QPainter *painter);
QGLShaderProgram program;
int matrixUniform;
};
const QPainter::PixmapFragment crossCoord[4] = {
QPainter::PixmapFragment::create(QPointF(300-15-8, 240), QRect(0, 0, 32, 4), 1, 1, 180, 1 ),
QPainter::PixmapFragment::create(QPointF(300+15+8, 240), QRect(0, 0, 32, 4), 1, 1, 0, 1 ),
QPainter::PixmapFragment::create(QPointF(300, 240-15-8), QRect(0, 0, 32, 4), 1, 1, -90, 1 ),
QPainter::PixmapFragment::create(QPointF(300, 240+15+8), QRect(0, 0, 32, 4), 1, 1, 90, 1 )
};
const QPointF pts[5] = {
QPointF(0, 0), QPointF(28, 0), QPointF(32, 2), QPointF(28, 4), QPointF(0, 4)};
GLWidget::GLWidget( )
{
genCross();
this->setAttribute(Qt::WA_AcceptTouchEvents);
this->setAutoBufferSwap(false);
this->setAttribute(Qt::WA_DeleteOnClose);
this->setAutoFillBackground (false);
}
void GLWidget::initializeGL ()
{
QGLShader *vshader = new QGLShader(QGLShader::Vertex);
const char *vsrc =
"attribute highp vec4 vertex;\n"
"attribute highp vec2 texCoord;\n"
"uniform mediump mat4 matrix;\n"
"varying highp vec2 texc;\n"
"void main(void)\n"
"{\n"
" vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
" gl_Position = matrix * vertex;\n"
" texc = texCoord;\n"
"}\n";
vshader->compileSourceCode(vsrc);
QGLShader *fshader = new QGLShader(QGLShader::Fragment);
const char *fsrc =
"varying highp vec2 texc;\n"
"uniform sampler2D tex;\n"
"void main(void)\n"
"{\n"
" highp vec3 color = texture2D(tex, texc.st).rgb;\n"
" gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n"
"}\n";
fshader->compileSourceCode(fsrc);
program.addShader(vshader);
program.addShader(fshader);
program.link();
int vertexAttr = program.attributeLocation("vertex");
int texCoordAttr = program.attributeLocation("texCoord");
matrixUniform = program.uniformLocation("matrix");
int textureUniform = program.uniformLocation("tex");
glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
}
void GLWidget::paintGL()
{
QPainter livePainter(this);
livePainter.beginNativePainting();
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 modelview;
modelview.translate(0, 0, 0);
modelview.scale(1.0,1.0);
program.bind();
program.setUniformValue(matrixUniform, modelview);
//gl stuff
// ...
program.release();
livePainter.endNativePainting();
livePainter.drawPixmapFragments ( crossCoord, 4, *crossPixmap, QPainter::OpaqueHint );
livePainter.end();
swapBuffers();
}
void GLWidget::genCross()
{
crossPixmap = new QPixmap(32, 4);
QPen pen;
pen.setBrush(Qt::black);
QPainter painter(crossPixmap);
painter.setBrush(Qt::yellow);
painter.setPen(pen);
painter.drawPolygon(pts, 5);
painter.end();
}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
{
Q_OBJECT
private slots:
void toLog();
void toLive();
public:
MainWindow();
private:
GLWidget *glwidget;
};
MainWindow::MainWindow()
{
this->setCentralWidget(centralWidget);
//Vertical menu
verticalMenu->setFixedWidth(200);
layout->addWidget(liveButton);
layout->addWidget(logButton);
layout->addWidget(quitButton);
verticalMenu->setLayout(layout);
mainLayout->addWidget(verticalMenu, 0, 2);
//Log view
model
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Time"));
model
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Source"));
model
->setHeaderData
(2, Qt
::Horizontal,
QObject::tr("Type"));
model
->setHeaderData
(3, Qt
::Horizontal,
QObject::tr("Object"));
tableView->setModel(model);
TableLayout->addWidget(tableView);
mainLayout->addWidget(iEvent, 0, 1);
iEvent->setLayout(TableLayout);
//glWidget
glwidget = new GLWidget();
glwidget->hide();
mainLayout->addWidget(glwidget, 0, 0);
mainTimer->start(5);
QObject::connect(mainTimer,
SIGNAL(timeout
()), glwidget,
SLOT(updateGL
()));
centralWidget->setLayout(mainLayout);
// Signals
QObject::connect(logButton,
SIGNAL(clicked
()),
this,
SLOT(toLog
()));
QObject::connect(liveButton,
SIGNAL(clicked
()),
this,
SLOT(toLive
()));
QObject::connect(quitButton,
SIGNAL(clicked
()),
this,
SLOT(close
()));
}
void MainWindow::toLog()
{
glwidget->hide();
iEvent->show();
}
void MainWindow::toLive()
{
glwidget->show();
iEvent->hide();
}
#include "mainwindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
private slots:
void toLog();
void toLive();
public:
MainWindow();
private:
GLWidget *glwidget;
QWidget *iEvent;
};
MainWindow::MainWindow()
{
QWidget *centralWidget = new QWidget();
this->setCentralWidget(centralWidget);
QGridLayout *mainLayout = new QGridLayout();
//Vertical menu
QWidget *verticalMenu = new QWidget(centralWidget);
verticalMenu->setFixedWidth(200);
QVBoxLayout *layout = new QVBoxLayout();
QPushButton *liveButton = new QPushButton("Live", verticalMenu);
layout->addWidget(liveButton);
QPushButton *logButton = new QPushButton("Log", verticalMenu);
layout->addWidget(logButton);
QPushButton *quitButton = new QPushButton("Quit", verticalMenu);
layout->addWidget(quitButton);
verticalMenu->setLayout(layout);
mainLayout->addWidget(verticalMenu, 0, 2);
//Log view
iEvent = new QWidget();
QHBoxLayout *TableLayout = new QHBoxLayout();
QTableView *tableView = new QTableView(this);
QStandardItemModel *model = new QStandardItemModel(0, 4);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Source"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("Type"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Object"));
tableView->setModel(model);
TableLayout->addWidget(tableView);
mainLayout->addWidget(iEvent, 0, 1);
iEvent->setLayout(TableLayout);
//glWidget
glwidget = new GLWidget();
glwidget->hide();
mainLayout->addWidget(glwidget, 0, 0);
QTimer *mainTimer = new QTimer();
mainTimer->start(5);
QObject::connect(mainTimer, SIGNAL(timeout()), glwidget, SLOT(updateGL()));
centralWidget->setLayout(mainLayout);
// Signals
QObject::connect(logButton, SIGNAL(clicked()), this, SLOT(toLog()));
QObject::connect(liveButton, SIGNAL(clicked()), this, SLOT(toLive()));
QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}
void MainWindow::toLog()
{
glwidget->hide();
iEvent->show();
}
void MainWindow::toLive()
{
glwidget->show();
iEvent->hide();
}
To copy to clipboard, switch view to plain text mode
Bookmarks