myGLWidget::myGLWidget
{
setAutoBufferSwap(false);
takingSnapshot = false; // member variable. When taking snapshot turns true
}
void myGLWidget::initializeGL()
{
glClearColor(1., 1., 1., 0.);
}
void myGLWidget::resizeGL(int ww, int hh)
{
glViewport(0, 0, (GLint)ww, (GLint)hh);
}
void myGLWidget::paintGL()
{
qDebug() << "paintGL";
glPushAttrib(GL_TRANSFORM_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glOrtho(-1., 1., -1., 1., -1., 1.);
/* Clear screen */
if (takingSnapshot) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0., 0., 1., 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
} else {
glClearColor(1., 1., 0., 1.);
glClear(GL_COLOR_BUFFER_BIT);
}
/* Begin Draw */
glColor4f(1., 0., 0., 1.);
glLineWidth(10);
glBegin(GL_LINES);
glVertex2f(-0.3, 0.0);
glVertex2f(0.3, 0.0);
glEnd();
glEnable(GL_BLEND);
glColor4f(0., 1., 0., 0.4);
glRotatef(90., 0., 0., 1.);
glBegin(GL_LINES);
glVertex2f(-0.3, 0.0);
glVertex2f(0.3, 0.0);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glDisable(GL_BLEND);
if (!takingSnapshot) {
swapBuffers();
} else {
qDebug() << "Finished drawing after snapshot\n";
}
}
myGLWidget::myGLWidget
{
setAutoBufferSwap(false);
takingSnapshot = false; // member variable. When taking snapshot turns true
}
void myGLWidget::initializeGL()
{
glClearColor(1., 1., 1., 0.);
}
void myGLWidget::resizeGL(int ww, int hh)
{
glViewport(0, 0, (GLint)ww, (GLint)hh);
}
void myGLWidget::paintGL()
{
qDebug() << "paintGL";
glPushAttrib(GL_TRANSFORM_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glOrtho(-1., 1., -1., 1., -1., 1.);
/* Clear screen */
if (takingSnapshot) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0., 0., 1., 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
} else {
glClearColor(1., 1., 0., 1.);
glClear(GL_COLOR_BUFFER_BIT);
}
/* Begin Draw */
glColor4f(1., 0., 0., 1.);
glLineWidth(10);
glBegin(GL_LINES);
glVertex2f(-0.3, 0.0);
glVertex2f(0.3, 0.0);
glEnd();
glEnable(GL_BLEND);
glColor4f(0., 1., 0., 0.4);
glRotatef(90., 0., 0., 1.);
glBegin(GL_LINES);
glVertex2f(-0.3, 0.0);
glVertex2f(0.3, 0.0);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glDisable(GL_BLEND);
if (!takingSnapshot) {
swapBuffers();
} else {
qDebug() << "Finished drawing after snapshot\n";
}
}
To copy to clipboard, switch view to plain text mode
MainWindow
::MainWindow(QWidget *parent
){
setCentralWidget(mainFrame);
mainFrame->setLayout(mainLay);
glw = new myGLWidget(mainFrame);
label
= new QLabel(mainFrame
);
mainLay->addWidget(glw);
mainLay->addWidget(snap);
mainLay->addWidget(label);
mainFrame->show();
glw->show();
snap->show();
label->show();
QObject::connect(snap,
SIGNAL(clicked
()),
this,
SLOT(takeImage
()));
}
static QImage createQImage
(const unsigned char *image2,
int ww,
int hh
) {
/* Do some stuff here with "image2" */
/* and return the final image */
}
void MainWindow::takeImage()
{
int ww = glw->width();
int hh = glw->height();
GLubyte *image = (GLubyte *)malloc(ww * hh * sizeof(GLubyte) * 4);
glw->takingSnapshot = true;
glPushAttrib(GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT); // readBuffer + drawBuffer
glReadBuffer(GL_BACK);
glDrawBuffer(GL_BACK);
qDebug() << "takeImage, will call updateGL";
glw->updateGL();
glReadPixels(0,0,ww,hh,GL_RGBA,GL_UNSIGNED_BYTE,image);
finalImage = createQImage(image, ww, hh);
if (!finalImage.isNull()) {
pix.convertFromImage(finalImage);
label->setPixmap(pix);
}
glPopAttrib();
glw->takingSnapshot = false;
if (image) free(image);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
mainFrame = new QFrame(this);
setCentralWidget(mainFrame);
mainLay = new QVBoxLayout(mainFrame);
mainFrame->setLayout(mainLay);
glw = new myGLWidget(mainFrame);
snap = new QPushButton("Snap", mainFrame);
label = new QLabel(mainFrame);
mainLay->addWidget(glw);
mainLay->addWidget(snap);
mainLay->addWidget(label);
mainFrame->show();
glw->show();
snap->show();
label->show();
QObject::connect(snap,SIGNAL(clicked()),this,SLOT(takeImage()));
}
static QImage createQImage(const unsigned char *image2, int ww, int hh)
{
/* Do some stuff here with "image2" */
/* and return the final image */
}
void MainWindow::takeImage()
{
int ww = glw->width();
int hh = glw->height();
QImage finalImage;
GLubyte *image = (GLubyte *)malloc(ww * hh * sizeof(GLubyte) * 4);
glw->takingSnapshot = true;
glPushAttrib(GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT); // readBuffer + drawBuffer
glReadBuffer(GL_BACK);
glDrawBuffer(GL_BACK);
qDebug() << "takeImage, will call updateGL";
glw->updateGL();
glReadPixels(0,0,ww,hh,GL_RGBA,GL_UNSIGNED_BYTE,image);
finalImage = createQImage(image, ww, hh);
if (!finalImage.isNull()) {
QPixmap pix;
pix.convertFromImage(finalImage);
label->setPixmap(pix);
}
glPopAttrib();
glw->takingSnapshot = false;
if (image) free(image);
}
To copy to clipboard, switch view to plain text mode
Bookmarks