How can I load a QPaintedTextureImage into a QTextureMaterial? - qt

I am using Qt3D to create a 360 deg panorama viewer where the image in equirectangular format is loaded over the mesh of a sphere with negative radius. The problem is that I need to load the texture from memory, instead of a file.
In order to achieve that, I developed a custom QPaintedTextureImage with paint() overloaded to draw from a QImage. It works, but only when plugged into a QDiffuseMapMaterial. Since I don't want any light effect (just the original color of the pixels) it seems that QTextureMaterial would be the right choice, but I don't know how to do that.
Any idea?

Got it!
class MyQPaintedTextureImage : public Qt3DRender::QPaintedTextureImage
{
private:
QImage image;
public:
void setImage(QImage &i){
image = i;
setSize(i.size());
}
virtual void paint(QPainter *painter) override{
painter->drawImage(0, 0, image);
}
};
And then:
auto *image = new MyQPaintedTextureImage;
image->setImage(i);
auto *planeMaterial = new Qt3DExtras::QTextureMaterial;
planeMaterial->texture()->addTextureImage(image);
m_sphereEntity->addComponent(planeMaterial);

Related

How to set far clipping plane in QOpenGLWidget?

I am pretty new to Qt so sorry if this is a straight forward question.
I am using Qt 5.5 and trying to visualize a point cloud in QOpenGLWidget.
This is my header:
class PointCloudWindow : public QOpenGLWidget
{
public:
void setDepthMap(DepthMapGrabber* grabber);
protected:
void initializeGL();
void paintGL();
private:
QMatrix4x4 m_projection;
DepthMapGrabber* m_grabber;
};
and here is the corresponding cpp:
void PointCloudWindow::setDepthMap(DepthMapGrabber* grabber) {
m_grabber = grabber;
QTimer* updatePointCloud = new QTimer(this);
connect(updatePointCloud, SIGNAL(timeout()), SLOT(update()));
updatePointCloud->start();
}
void PointCloudWindow::initializeGL() {
glewInit(); // TODO: check for return value if error occured
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void PointCloudWindow::paintGL() {
m_grabber->getNextDepthFrame(); // TODO: check for return value if error occured
m_projection.setToIdentity();
m_projection.perspective(45.0f, width() / (float)height(), 0.01f, 100.0f);
if (m_grabber->getDepthMap()->cloud) {
glBegin(GL_POINTS);
glColor3f(0.8f, 0.8f, 0.8f);
for (UINT i = 0; i < m_grabber->getDepthMap()->size; ++i)
{
glVertex3f(m_grabber->getDepthMap()->cloud[i].X, m_grabber->getDepthMap()->cloud[i].Y, m_grabber->getDepthMap()->cloud[i].Z);
}
glEnd();
}
}
This is how my point cloud looks like after visualization:
My problem is that as you can see (monitor is cut in half for example) if a point has a z value, which is bigger, then 1.0 then it gets clipped of. I tried to set the near and far plane, but no effect. I searched through Google and tried several things, but was unable to figure out how this works in Qt. I manged to visualize this point cloud with OpenGL and GLUT before. Any help or explanation how to do this in Qt would be much appreciated!
m_projection is just a member variable in your class. It's not going to automatically "jump" into the OpenGL context. You've to explicitly load it into OpenGL. Normally you'd load a matrix like that into a uniform for use in a shader. But since you're not using shaders (booo! ;-) ) and use old, ugly and slow immediate mode (don't do that) you'll have to load it into the fixed function projection matrix.
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(m_projection.constData());

QOpenGLWidget and transparency

I have small problem with QOpenGLWidget and its background color.
When I want to create semi-transparent rect on my custom QOpenGLWidget using QPainter there are 2 different results:
If MyCustomWidget have parent - on every update rect's color multiplies (and after few repaints it is opaque, like previous painting result not cleaned)
If MyCustomWidget doesn't have parent - color doesn't repaints each time
Here is code example for QPainter:
class Widget : public QOpenGLWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0)
: QOpenGLWidget(parent)
{
resize(800, 600);
Test *test = new Test(this);
}
~Widget(){}
protected:
void paintEvent(QPaintEvent *) {}
protected:
void initializeGL() {
if(paintEngine()->type() != QPaintEngine::OpenGL &&
paintEngine()->type() != QPaintEngine::OpenGL2)
qDebug() << "ERROR. Type is: " << paintEngine()->type();
}
void resizeGL(int, int) {}
void paintGL() {
QPainter p;
p.begin(this);
{
p.fillRect(rect(), Qt::white);
}
p.end();
}
private:
class Test : public QOpenGLWidget
{
public:
Test(QWidget *parent = 0) : QOpenGLWidget(parent) {
resize(100, 100);
}
protected:
void paintEvent(QPaintEvent *) {
QPainter p(this);
p.fillRect(rect(), QColor(125, 125, 125, 255/10));
}
};
};
Also by default it has black background (I don't know how to fix it. setAttribute(Qt::WA_TranslucentBackground) doesn't helps).
Also, when I'm trying to clear color using glClear it ignores alpha (both on QOpenGLWidget with parent and not). Here is Test class from previous code, but now it is using opengl to clear color:
class Test : public QOpenGLWidget
{
public:
Test(QWidget *parent = 0) : QOpenGLWidget(parent) {
resize(100, 100);
}
void initializeGL() {
QOpenGLFunctions *f = context()->functions();
f->glClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
void paintGL() {
QOpenGLFunctions *f = context()->functions();
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
};
How can I fix this problems?
I'm using Qt 5.5.0, Windows 10, MinGW 4.9.2
Xeed is correct when saying the QOpenGLWidget is painted first.
I'm not an expert but I think I found the solution. You need to set a widget attribute to always make the widget stacked on top (think of the widgets as layers on the window). Here is a link to where I got the following information:
P.S. As mentioned in the QQuickWidget post, there is a limitation regarding semi-transparency when using QQuickWidget or QOpenGLWidget as child widgets. For applications that absolutely need this, Qt 5.4 offers a workaround: the newly introduced Qt::WA_AlwaysStackOnTop widget attribute. This, at the expense of breaking the stacking order for other types of layouts, makes it possible to have a semi-transparent QQuickWidget or QOpenGLWidget with other widgets visible underneath. Of course, if the intention is only to make other applications on the desktop visible underneath, then the Qt::WA_TranslucentBackground attribute is sufficient
Solution in Python:
set attribute of OpenGL widget
setAttribute(Qt.WA_AlwaysStackOnTop)
Now the OpenGL widget is considered 'on top' in the window. Use 'glClearColor' function and specify the alpha channel to be zero (0.0).
glClearColor(0.0, 0.0, 0.0, 0.0)
I'm not sure how to write that in other languages but this worked for me. The OpenGL widget no longer has the default black background. It is transparent! Hope this helps.
As far as I know the QOpenGLWidget is always drawn first. Therefore you cannot show any widgets layered below. I'm currently looking into the same issue. I'll report back, when I find any solution.
I've had similar issue with QOpenGLWidget not repainting correctly in transparent areas and decided to switch to QOpenGLWindow wrapped inside QWidget::createWindowContainer()

Is it possible to control multiple VideoWidgets with one MediaPlayer?

I wonder if it is possible to play a video using the Qt5 QtMultimedia library in 3 widgets simultaneously.
I have a video that I would like to show in 3 widgets: one showing the full video, another focusing on some object moving in the video, and a last one again focused in some other object.
I've been struggling for a bit now and I'm starting to doubt if my effort will be worthy...
Has anyone tried something similar and/or could give me some tips on how to tackle this?
One way to show a vĂ­deo in multiple widgets is using a custom Video Surface class, and use them to generate a serie of QImage for you, and process/show those images the way you like.
Example of a custom Video Surface:
/* Here is our custom video surface, */
class VideoSurface : public QAbstractVideoSurface
{
Q_OBJECT
public:
VideoSurface(QObject *parent = 0) : QAbstractVideoSurface(parent)
{
}
QList<QVideoFrame::PixelFormat>
supportedPixelFormats(QAbstractVideoBuffer::HandleType) const
{
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB32;
}
/* this will get the QVideoFrame and convert to QImage. */
bool present(const QVideoFrame& frame)
{
if (frame.isValid())
{
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
const QImage img = QImage(cloneFrame.bits(),
cloneFrame.width(),
cloneFrame.height(),
QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
cloneFrame.unmap();
emit readyRead(img);
return true;
}
return false;
}
signals:
void readyRead(QImage);
};
Here you have a sample Project:
Double View project
Screenshot:
Hope that helps!

Efficient way of displaying a continuous stream of QImages

I am currently using a QLabel to do this, but this seems to be rather slow:
void Widget::sl_updateLiveStreamLabel(spImageHolder_t _imageHolderShPtr) //slot
{
QImage * imgPtr = _imageHolderShPtr->getImagePtr();
m_liveStreamLabel.setPixmap( QPixmap::fromImage(*imgPtr).scaled(this->size(), Qt::KeepAspectRatio, Qt::FastTransformation) );
m_liveStreamLabel.adjustSize();
}
Here I am generating a new QPixmap object for each new image that arrives. Since QPixmap operations are restricted to the GUI Thread, this also makes the GUI feel poorly responsive.
I've seen there are already some discussions on this, most of them advising to use QGraphicsView or QGLWidget, but I have not been able to find a quick example how to properly use those, which would be what I am looking for.
I'd appreciate any help.
QPixmap::fromImage is not the only problem. Using QPixmap::scaled or QImage::scaled also should be avoided. However you can't display QImage directly in QLabel or QGraphicsView. Here is my class that display QImage directly and scales it to the size of the widget:
Header:
class ImageDisplay : public QWidget {
Q_OBJECT
public:
ImageDisplay(QWidget* parent = 0);
void setImage(QImage* image);
private:
QImage* m_image;
protected:
void paintEvent(QPaintEvent* event);
};
Source:
ImageDisplay::ImageDisplay(QWidget *parent) : QWidget(parent) {
m_image = 0;
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}
void ImageDisplay::setImage(QImage *image) {
m_image = image;
repaint();
}
void ImageDisplay::paintEvent(QPaintEvent*) {
if (!m_image) { return; }
QPainter painter(this);
painter.drawImage(rect(), *m_image, m_image->rect());
}
I tested it on 3000x3000 image scaled down to 600x600 size. It gives 40 FPS, while QLabel and QGraphicsView (even with fast image transformation enabled) gives 15 FPS.
Setting up a QGraphicsView and QGraphicsScene is quite straight-forward: -
int main( int argc, char **argv )
{
QApplication app(argc, argv);
// Create the scene and set its dimensions
QGraphicsScene scene;
scene.setSceneRect( 0.0, 0.0, 400.0, 400.0 );
// create an item that will hold an image
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(0);
// load an image and set it to the pixmapItem
QPixmap pixmap("pathToImage.png") // example filename pathToImage.png
item->setPixmap(pixmap);
// add the item to the scene
scene.addItem(item);
item->setPos(200,200); // set the item's position in the scene
// create a view to look into the scene
QGraphicsView view( &scene );
view.setRenderHints( QPainter::Antialiasing );
view.show();
return app.exec();
}
I recommend not use QLabel but write own class. Every call of setPixmap causes layout system to recalculate sizes of items and this can propagate to topmost parent (QMainWindow) and this is quite big overhead.
Conversion and scaling also is a bit costly.
Finally best approach is to use profiler to detect where is the biggest problem.

setpixel of QGraphicsScene in Qt

It's simple to draw line or ellipse just by using scene.addellipse(), etc.
QGraphicsScene scene(0,0,800,600);
QGraphicsView view(&scene);
scene.addText("Hello, world!");
QPen pen(Qt::green);
scene.addLine(0,0,200,200,pen);
scene.addEllipse(400,300,100,100,pen);
view.show();
now what should i do to set some pixel color? may i use a widget like qimage? by the way performance is an issue for me.thanks
I think that performing pixel manipulation on a QImage would slow down your application quite a lot. A good alternative is to subclasse QGraphicsItem in a new class, something like QGraphicsPixelItem, and implement the paint function like this:
// code untested
void QGraphicsPixelItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0)
{
painter->save();
foreach(const QPoint& p, pointList) {
// set your pen color etc.
painter->drawPoint(p);
}
painter->restore();
}
where pointList is some kind of container that you use to store the position of the pixels you want to draw.

Resources