beginNativePainting() with QPainter's shader (for transformation and opacity purpose)
I created a object inherited from QGraphicsItem and reimplement the paint() function.
Inside the paint() function, I draw something by QPainter and call painter->beginNativePainting() to draw some elements with native openGL command.
It all works well, but when I rotate the object( object->rotate(45)), only the elements drawn by QPainter is rotated, the elements drawn by openGL command doesn't work.
It seems the QPainter use the shader to do those transformation(rotate), how can I apply the shader to my openGL elements?
Sample Code:
Code:
{
public:
{
painter->drawText(0, 0, "123");
painter->fillRect(0, 0, 128, 128, Qt::green);
painter->beginNativePainting();
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 64, 64);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
glEnd();
painter->endNativePainting();
}
QRectF boundingRect
(void) const {
return QRectF(0,
0,
100,
100);
}
};
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
I think, you need to use glRotate function and Qt rotate ( if you paint something with QPainter ).
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Quote:
Originally Posted by
UASlaX
I think, you need to use glRotate function and Qt rotate ( if you paint something with QPainter ).
Yes, I know this. But there may have some other transformations, if I call glRotate manually, it would be complexity. So, I am wonder if I could apply the shader in QPainter :)
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
What happens if you don't call beginNativePainting() at all?
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Quote:
Originally Posted by
wysota
What happens if you don't call beginNativePainting() at all?
Explain it clearly, the QPainter's drawing function all works well with transformation. But the elements drawn by gl command (follow by beginNativePainting()) won't be affected by the transformations.
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Ok but what happens if you don't use beginNativePainting() and just start making gl calls?
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Quote:
Originally Posted by
wysota
Ok but what happens if you don't use beginNativePainting() and just start making gl calls?
Oh, I got you! I have trace into the beginNativePainting() function, and it will call glUseProgram(0) to unload the shader program.
But I have two questions about don't use beginNativePainting() before gl calls:
1.Will it cause any side-effect? If not, why Qt design this function?
2.Because I use shader program, too. Could we have two program at the same time?
Anyway, really thanks for your suggestion :-)
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Quote:
Originally Posted by
diro
1.Will it cause any side-effect?
I don't know, make an example and find it out on your own. I don't even know if it works at all.
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
Quote:
Originally Posted by
wysota
I don't know, make an example and find it out on your own. I don't even know if it works at all.
After some testing..
1.Will it cause any side-effect? If not, why Qt design this function?
Not sure, but the projection seems wrong, the elements didn't draw in the position I wanted.
2.Because I use shader program, too. Could we have two program at the same time?
Yes, I still can use my own shader program.
Finally, I give up this way and solve this problem by my own transformation matrix.
Re: beginNativePainting() with QPainter's shader (for transformation and opacity purp
I think I have to apology first, after many study and experiments, I got a new conclusion now.
For Qt painting framework, after calling the beginNativePainting(), the following gl commands will be affected by the shaders constructed by Qt(all transformation of coz)
So, you don't have to do anything, and all the elements drawn by gl commands will also be rotate, flip and so on.
The code I made in previous edition, I use some custom projection in openGL, so it looks like the previous shaders is not enabled, it is a wrong concept.