QOpenGLTexture being loaded or linked to a shader as monochromatic image



  • I am doing an OpenGL application in Qt and I am having problems with the textures. My objects are painted with a monochromatic texture but my images are colorful images in a .png format. I had the application donde in Visual Studio with C++ programming and there it works well.

    I tried to disable illuminance but it doesn't provide any change. I also tried to force the shader to paint all the objects as red and it works, so the surface allows me to paint with colors. Also, I have a mode where I don't use textures, i just paint with materials and it works fine. I saw an example that is provided by Qt and it doesn't have so much differences with mine. The way the texture is created is like mine and, in the example, a cube is painted with the colorful texture.

    This is the texture class:

    texture::texture()
    {
        initializeOpenGLFunctions();
    }
    
    texture::texture(QVector<QString> rutas, QVector<TexturesTypes> tipo)
    {
        initializeOpenGLFunctions();
        for (int i = 0; i < rutas.size(); i++)
        {
    
    
            QString filename = rutas[i];
            TexturesTypes tipoTextura = tipo[i];
            QImage imagen = QImage(filename);
    
            if(imagen.isNull())
            {
                std::cout<<"IS NULL"<<std::endl;
            }
    
            QOpenGLTexture *texture = new QOpenGLTexture(QImage(filename).mirrored());
    
            if (texture == nullptr)
            {
                std::cout << filename.toStdString() << " cannot be loaded" << std::endl;
                return;
            }
            std::cout<<"Textura cargada correctamente"<<std::endl;
    
            //Construimos la textura con las imágenes que queremos y la función de cada imagen
            switch (tipoTextura)
            {
                case COLOR:
                {
                    color = texture;
                    color->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
                    color->setMagnificationFilter(QOpenGLTexture::Linear);
                    color->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
                    color->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
                    color->generateMipMaps();
    
                    break;
                }
                case BRILLO:
                {
                    brillo = texture;
                    brillo->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
                    brillo->setMagnificationFilter(QOpenGLTexture::Linear);
                    brillo->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
                    brillo->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
                    brillo->generateMipMaps();
                    break;
                }
                case BUMPMAPPING:
                {
                    bumpMapping = texture;
                    bumpMapping->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
                    bumpMapping->setMagnificationFilter(QOpenGLTexture::Linear);
                    bumpMapping->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
                    bumpMapping->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
                    bumpMapping->generateMipMaps();
                    break;
                }
    
                case DISPLACEMENTMAPPING:
                {
                    displacementMapping = texture;
                    displacementMapping->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
                    displacementMapping->setMagnificationFilter(QOpenGLTexture::Linear);
                    displacementMapping->setWrapMode(QOpenGLTexture::DirectionS,QOpenGLTexture::ClampToEdge );
                    displacementMapping->setWrapMode(QOpenGLTexture::DirectionT,QOpenGLTexture::ClampToEdge );
                    displacementMapping->generateMipMaps();
                    break;
                }
    
            }
    
        }
    
    }
    
    
    /**
     * @brief Obtener una de las imágenes que forman la textura
     * @param tipo Tipode imagen que se quiere obtener
     * @return imagen Puntero a la imagen que se ha obtenido
     */
    
    QOpenGLTexture* texture::getTextura(TexturesTypes tipo)
    {
        switch (tipo)
        {
            case COLOR:
            {
                return this->color;
            }
            case BRILLO:
            {
                return this->brillo;
            }
    
            case BUMPMAPPING:
            {
                return this->bumpMapping;
            }
            case DISPLACEMENTMAPPING:
            {
                return this->displacementMapping;
            }
        }
    }
    
    /**
     * @brief Aplicar la textura al shader
     * @param shader Shader al que se le va a aplicar la textura
     */
    
    void texture::aplicar(shaderProgram* shader)
    {
        shader->use();
        if (color != nullptr)
        {
            shader->setUniform("TexSamplerColor", 0);
            color->bind(GL_TEXTURE0);
        }
        if (brillo != nullptr)
        {
            shader->setUniform("TexSamplerBrillo", 1);
            brillo->bind(GL_TEXTURE1);
        }
    }
    
    
    texture::~texture()
    {
        color->destroy();
        brillo->destroy();
        bumpMapping->destroy();
        displacementMapping->destroy();
        delete color;
        delete brillo;
        delete bumpMapping;
        delete displacementMapping;
    }
    

    This is the fragment shader:

    #version 400
    
    in vec3 position;
    in vec3 normal;
    in vec2 texCoord;
    
    uniform vec3 lightPosition;
    uniform vec3 Ia;
    uniform vec3 Id;
    uniform vec3 Is;
    
    uniform sampler2D TexSamplerColor;
    uniform sampler2D TexSamplerBrillo;
    
    layout (location = 0) out vec4 FragColor;
    
    vec3 ads(vec4 texColor, vec4 texBrillo)
    {
        vec3 Kad = texColor.rgb;
        vec3 Ks = texBrillo.rgb;
        float Shininess = texBrillo.a;
        Shininess = Shininess*200;
    
        vec3 n;
        if (gl_FrontFacing) {
            n = normalize(normal);
        } else {
            n = normalize(-normal);
        }
        vec3 l = normalize( lightPosition-position );
        vec3 v = normalize( -position );
        vec3 r = reflect( -l, n );
        return (Ia * Kad) + (Id * Kad * max( dot(l, n), 0.0)) + (Is * Ks * pow( max( dot(r,v), 0.0), Shininess ));
    }
    
    void main() {
        vec4 texColor = texture(TexSamplerColor, texCoord);
        vec4 texBrillo = texture(TexSamplerBrillo, texCoord);
            FragColor = vec4(ads(texColor,texBrillo), 1.0);
    }
    

    I expected to have the textures well applied to my objects but what I have is all the objects with a monochromatic texture.

    0_1557237592258_textura.png


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.