Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with glBindTexture



  • I've loaded a image using:

    image = new QImage(QString("d:\\temp.jpg"));
    

    and also I generated a OpenGL texture with:

    GLFunctions::gl30()->glGenTextures(1, &texHandle);
    GLFunctions::gl30()->glBindTexture(GL_TEXTURE_2D, texHandle);
    GLFunctions::gl30()->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->width(), image->height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image->mirrored().bits());
    

    Where GLFunctions::gl30() is a function that returns QOpenGLFunctions_3_0 of the current context.
    binding it to a shader doesn't work or it is not sent to the shader

    GLFunctions::gl30()->glBindTexture(GL_TEXTURE_2D, texHandle);
    

    In the other hand using QOpenGLTexture, it works ok

    tex = new QOpenGLTexture(image->mirrored());
    tex->bind();
    

    So the shader workflow is correct.
    Also I know that the texHandlle is ok since I can make:

    //TRY TO READ THE TEXTURE TO CHECK IF IT IS VALID
    float *landArray=(float*) malloc (image->width()*image->height()*3*sizeof(float));
    GLFunctions::gl30()->glGetTexImage(GL_TEXTURE_2D,0,GL_RGB,GL_FLOAT,landArray );
    int off=(floor(0.5*image->height())*image->width())*3;
    off+=floor(0.5*image->width())*3;
    float res[3];
    res[0]=landArray[off];
    res[1]=landArray[off+1];
    res[2]=landArray[off+2];
    qDebug()<<"GL buffer contains color "<<res[0]<<res[1]<<res[2];
    

    And it dumps correct values.

    Any tips on this? Thank you in advance



  • That's becoming even more weird:

    GLFunctions::gl30()->glGenTextures(1, &texHandle);
    

    returns 2 in texHandle.

    but for using it in the shader it works for a value of 1 in the bindTexture

    GLFunctions::gl30()->glBindTexture(GL_TEXTURE_2D, texHandle-1); 
    

    This is driving me nuts.