[Qt5] QML and OpenGL
-
Hi,
I'm trying to draw OpenGL content with the new Qt5 architecture. I found one example doing that, the one named Squircle. But it draw on the background. And I want to draw like before (with Qt4 like "here":http://qt-project.org/forums/viewthread/4109 ) in a QQuickItem. So I Tried to use a QuickPaintedItem like I use a QDeclarativeItem. But nothing is draw. Here a my Code :
PaintedOpenGL.h
@#ifndef PAINTEDOPENGL_H
#define PAINTEDOPENGL_H#include <QQuickPaintedItem>
class PaintedOpenGL : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit PaintedOpenGL(QQuickItem *parent = 0);void paint(QPainter *painter);
private:
int m_prog;
};#endif // PAINTEDOPENGL_H@
PaintedOpenGL.cpp
@#include "paintedopengl.h"
#include <QPainter>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLContext>extern GLuint Init ( GLbyte* vert,GLbyte* frag); //classic function which load, build an link shader
PaintedOpenGL::PaintedOpenGL(QQuickItem *parent)
: QQuickPaintedItem(parent)
, m_prog(0)
{
}void PaintedOpenGL::paint(QPainter *painter)
{
painter->beginNativePainting();
if(m_prog == 0)
{
GLbyte vShader[] =
"attribute vec4 vPosition; \n"
"uniform mat4 matrix; \n"
"void main() \n"
"{ \n"
" gl_Position = matrix * vPosition; \n"
"} \n";GLbyte fShader[] = "precision mediump float; \n" "void main() \n" "{ \n" " gl_FragColor = vec4 ( 0.0, 1.0, 0.0, 1.0 );\n" "} \n"; m_prog = Init(vShader,fShader); glBindAttribLocation ( m_prog, 0, "vPosition" ); } glUseProgram ( m_prog ); glEnableVertexAttribArray(0); GLfloat tab_matrix[] = { 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 }; glUniformMatrix4fv(glGetUniformLocation(m_prog, "matrix"), 1, GL_FALSE, tab_matrix); GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); glEnableVertexAttribArray ( 0 ); glDrawArrays ( GL_TRIANGLES, 0, 3 ); glDisableVertexAttribArray(0); glUseProgram(0); painter->endNativePainting();
}@
I use the same code in the squircle example and I've got my blue triangle drawn;
Did someone knows how to make it work ?I've got another question. In the doc of QQuickPaintedItem I've read this :"QQuickPaintedItem is meant to make it easier to port old code that is using the QPainter API to the QML Scene Graph API and it should be used only for that purpose." That's not what I want. I just want to draw OpenGL content in a QQuickItem, is there a better way, like subclasse SGNoge or something like that?
Thank for reading this.
-
Try subclassing QQuickItem instead.
-
I found what was mssing from the previous code. I've juste added @PaintedOpenGL::PaintedOpenGL(QQuickItem *parent)
: QQuickPaintedItem(parent)
, m_prog(0)
{
setRenderTarget(QQuickPaintedItem::FramebufferObject);
}@In my constructor and thing are working pretty well.
I'm note quite sur subclassing QQuickItem and using QSGMatarial will more simple since I need to draw using raw opengl.
-
OK as long as you found something that works.