Implement QQuickItem with antialiasing for custom triangle object
I am writing a QQuickItem to draw custom triangle object. I have tried to run setAntialiasing(true) but it don't work on custom QQuickItem. Many suggestions relay on the multi-sampled context but it need to set globally. I prefer a method that affect my QQuickItem item only.
Since I am not familiar with Qt Scene Graph , should I implement my custom QSGGeometryNode or QSGMaterial?
I found that there has a private class called QSGSmoothColorMaterial. I wonder is that already the solution of my questions.
Thanks for any advice.
What kind of hardware are you running on? Depending on what your target is, turning on multisampling might not be a bad choice, and that gives you antialiasing for free.
On many embedded chips, you have cheap 4x multisampling. On most desktop chips, you have more expensive 8-16x multisampling, but on a chip with a proper graphics card, it isn't a problem. Intel chips, especially the older ones, will struggle, if it is supported at all.
If you want to implement per-vertex antialiasing, you would need to create a QSGGeometryNode. If you add just the triangle, it will be aliased. To get antialiasing you need to add more vertices along the exterior of the triangle which fade the interior color to transparent. You could do that with a QSGGeometryNode using 6 vertices and indices making up 7 triangles (1 for the interior and 2 for each antialiasing edge).
A trivial implementation could use geometry of QSGGeometry::ColoredPoint2D combined with QSGVertexColorMaterial, where interior vertices get alpha=1 and exterior vertices get alpha=0.
If you want to antialiasing edge adapt based on scale factor you would need to implement your own material and take the scale factor of the matrix into account in the vertex shader and change the width of the exterior based on that.
Good Luck :)
Thanks for your reply! Your explanation is very clear!
My target platform is Android and iOS. I think it don't has any problem in iOS but not sure about Android.
Is it safe to use multisampling on Android platform?
I think the hard part of per-vertex antialising is to determine the no. of vertex should be added. Making triangle is just my first step. I need to make a lots of arrow and zoom-in is supported.