composition.cpp Example File
demos/composition/composition.cpp
}
CompositionRenderer::CompositionRenderer(QWidget *parent)
: ArthurFrame(parent)
{
m_animation_enabled = true;
m_image = QImage(":res/flower_2.png");
m_circle_alpha = 127;
m_circle_hue = 255;
m_current_object = NoObject;
m_composition_mode = QPainter::CompositionMode_SourceOut;
m_circle_pos = QPoint(200, 100);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
#ifdef QT_OPENGL_SUPPORT
m_pbuffer = 0;
m_pbuffer_size = 1024;
#endif
}
QRectF rectangle_around(const QPointF &p, const QSizeF &size = QSize(250, 200))
{
QRectF rect(p, size);
rect.translate(-size.width()/2, -size.height()/2);
return rect;
}
void CompositionRenderer::updateCirclePos()
{
if (m_current_object != NoObject)
return;
QDateTime dt = QDateTime::currentDateTime();
qreal t = (dt.toTime_t() * 1000 + dt.time().msec()) / 1000.0;
qreal x = width() / 2.0 + (cos(t) + sin(-t*2)) * width() / 2.0;
qreal y = height() / 2.0 + (sin(t) + cos(t * 3)) * height() / 2.0;
m_circle_pos = QLineF(m_circle_pos, QPointF(x, y)).pointAt(0.01);
}
void CompositionRenderer::drawBase(QPainter &p)
{
p.setPen(Qt::NoPen);
QLinearGradient rect_gradient(0, 0, 0, height());
rect_gradient.setColorAt(0, Qt::red);
rect_gradient.setColorAt(.17, Qt::yellow);
rect_gradient.setColorAt(.33, Qt::green);
rect_gradient.setColorAt(.50, Qt::cyan);
rect_gradient.setColorAt(.66, Qt::blue);
rect_gradient.setColorAt(.81, Qt::magenta);
rect_gradient.setColorAt(1, Qt::red);
p.setBrush(rect_gradient);
p.drawRect(width() / 2, 0, width() / 2, height());
QLinearGradient alpha_gradient(0, 0, width(), 0);
alpha_gradient.setColorAt(0, Qt::white);
alpha_gradient.setColorAt(0.2, Qt::white);
alpha_gradient.setColorAt(0.5, Qt::transparent);
alpha_gradient.setColorAt(0.8, Qt::white);
alpha_gradient.setColorAt(1, Qt::white);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.setBrush(alpha_gradient);
p.drawRect(0, 0, width(), height());
p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawImage(rect(), m_image);
}
void CompositionRenderer::drawSource(QPainter &p)
{
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
p.setCompositionMode(m_composition_mode);
QRectF circle_rect = rectangle_around(m_circle_pos);
QColor color = QColor::fromHsvF(m_circle_hue / 360.0, 1, 1, m_circle_alpha / 255.0);
QLinearGradient circle_gradient(circle_rect.topLeft(), circle_rect.bottomRight());
circle_gradient.setColorAt(0, color.light());
circle_gradient.setColorAt(0.5, color);
circle_gradient.setColorAt(1, color.dark());
p.setBrush(circle_gradient);
p.drawEllipse(circle_rect);
}
void CompositionRenderer::paint(QPainter *painter)
{
if (m_animation_enabled)
updateCirclePos();
#ifdef QT_OPENGL_SUPPORT
if (usesOpenGL()) {
int new_pbuf_size = m_pbuffer_size;
if (size().width() > m_pbuffer_size ||
size().height() > m_pbuffer_size)
new_pbuf_size *= 2;
if (size().width() < m_pbuffer_size/2 &&
size().height() < m_pbuffer_size/2)
new_pbuf_size /= 2;
if (!m_pbuffer || new_pbuf_size != m_pbuffer_size) {
if (m_pbuffer) {
m_pbuffer->deleteTexture(m_base_tex);
m_pbuffer->deleteTexture(m_compositing_tex);
delete m_pbuffer;
}
m_pbuffer = new QGLPixelBuffer(QSize(new_pbuf_size, new_pbuf_size), QGLFormat::defaultFormat(), glWidget());
m_pbuffer->makeCurrent();
m_base_tex = m_pbuffer->generateDynamicTexture();
m_compositing_tex = m_pbuffer->generateDynamicTexture();
m_pbuffer_size = new_pbuf_size;
}
if (size() != m_previous_size) {
m_previous_size = size();
QPainter p(m_pbuffer);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent);
drawBase(p);
m_pbuffer->updateDynamicTexture(m_base_tex);
}
qreal x_fraction = width()/float(m_pbuffer->width());
qreal y_fraction = height()/float(m_pbuffer->height());
{
QPainter p(m_pbuffer);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent);
p.save();
glBindTexture(GL_TEXTURE_2D, m_base_tex);
glEnable(GL_TEXTURE_2D);
glColor4f(1.,1.,1.,1.);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1.0);
glVertex2f(0, 0);
glTexCoord2f(x_fraction, 1.0);
glVertex2f(width(), 0);
glTexCoord2f(x_fraction, 1.0-y_fraction);
glVertex2f(width(), height());
glTexCoord2f(0, 1.0-y_fraction);
glVertex2f(0, height());
}
glEnd();
glDisable(GL_TEXTURE_2D);
p.restore();
drawSource(p);
m_pbuffer->updateDynamicTexture(m_compositing_tex);
}
glWidget()->makeCurrent();
glBindTexture(GL_TEXTURE_2D, m_compositing_tex);
glEnable(GL_TEXTURE_2D);
glColor4f(1.,1.,1.,1.);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1.0);
glVertex2f(0, 0);
glTexCoord2f(x_fraction, 1.0);
glVertex2f(width(), 0);
glTexCoord2f(x_fraction, 1.0-y_fraction);
glVertex2f(width(), height());
glTexCoord2f(0, 1.0-y_fraction);
glVertex2f(0, height());
}
glEnd();
glDisable(GL_TEXTURE_2D);
} else
#endif
{
if (m_buffer.size() != size()) {
m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_base_buffer.fill(0);
QPainter p(&m_base_buffer);
drawBase(p);
}
memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.numBytes());
{
QPainter p(&m_buffer);
drawSource(p);
}
painter->drawImage(0, 0, m_buffer);
}
if (m_animation_enabled)
update();
}
void CompositionRenderer::mousePressEvent(QMouseEvent *e)
{
setDescriptionEnabled(false);
QRectF circle = rectangle_around(m_circle_pos);
if (circle.contains(e->pos())) {
m_current_object = Circle;
m_offset = circle.center() - e->pos();
} else {
m_current_object = NoObject;
}
}
void CompositionRenderer::mouseMoveEvent(QMouseEvent *e)
{
if (m_current_object == Circle) setCirclePos(e->pos() + m_offset);
}
void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
{
m_current_object = NoObject;
}