I draw a texture on OpenGL through a FrameBufferObject in the QtQuick window.

Initialization:

FbItemRenderer::FbItemRenderer(const FbItem *item) { m_item = item; texture = new QOpenGLTexture(QImage(":/coolimage.png").mirrored()); texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); texture->setMagnificationFilter(QOpenGLTexture::Linear); glViewport(0, 0, 800, 600); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 800, 600, 0, 1, 0); glMatrixMode( GL_MODELVIEW ); } 

Render:

 void FbItemRenderer::render() { glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable ( GL_BLEND ); glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glEnable ( GL_TEXTURE_2D ); GLfloat vertices[] = { i,i, texture->width()+i,i, texture->width()+i,texture->height()+i, i,texture->height()+i }; GLfloat texVertices[] = { 0,0, 1,0, 1,1, 0,1 }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT); texture->bind(); glVertexPointer(2, GL_FLOAT, 0, vertices); glTexCoordPointer(2, GL_FLOAT, 0, texVertices); glDrawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_TEXTURE_COORD_ARRAY_EXT); glDisableClientState(GL_VERTEX_ARRAY); glDisable ( GL_TEXTURE_2D ); glDisable ( GL_BLEND ); //i++; m_item->window()->resetOpenGLState(); //update(); } 

This is how the texture is drawn as it should be. But when I add the update () function to the end of the render () function, the texture disappears after the update () call. Those. it is drawn only in the first iteration. Already tried everything, maybe something else needs to be added?

upd Found that the texture is drawn on the first two runs of the render () function

upd Immediately there is a strange behavior: with cyclic increment of variable i, the texture moves only on the third (!) cycle, despite the fact that the values ​​of the vertices have changed. More precisely, in the third cycle, it is not the texture that moves, but the square in which the texture should be.

    1 answer 1

    The problem is solved with the help of an example from Qt http://doc.qt.io/qt-5/qtquick-scenegraph-textureinsgnode-example.html on the basis of which I initially tried to draw.

    Put the texture rendering code in this example and found the missing puzzle details. It turned out the need to add shaders. Just enough empty shaders.

    We declare in class:

     QOpenGLShaderProgram program1; 

    Add to the initialization function:

     QOpenGLShader *vshader1 = new QOpenGLShader(QOpenGLShader::Vertex, &program1); const char *vsrc1 = "void main(void)\n" "{\n" "}\n"; vshader1->compileSourceCode(vsrc1); QOpenGLShader *fshader1 = new QOpenGLShader(QOpenGLShader::Fragment, &program1); const char *fsrc1 = "void main(void)\n" "{\n" "}\n"; fshader1->compileSourceCode(fsrc1); program1.addShader(vshader1); program1.addShader(fshader1); //program1.link(); 

    And in the render () function, before adding the picture we add:

     //glEnable(GL_DEPTH_TEST); //program1.bind(); //program1.setUniformValue(matrixUniform1, modelview); program1.release(); glDisable(GL_DEPTH_TEST); 

    The trick is that it is not necessary to uncomment the commented lines, if you are not planning to use shaders.