Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to use textures in Qt OpenGL?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to use textures in Qt OpenGL?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    Hello

    Why I don't see a texture on my triangle? https://github.com/8Observer8/TexturedTriangle

    1 Reply Last reply
    0
    • I Offline
      I Offline
      infinicat
      wrote on last edited by
      #2

      Here's what I found:

      1. In the fragment shader, to get the color from the texture, you need to use the texture2d() glsl function. Like so:
        @color = texture2d( textureUniform, texture );@

      2. In Triangle::initTexture(), you're initializing that textureCoords array incorrectly. Change it to this:
        @textureCoords[0] = 0.5f;
        textureCoords[1] = 1.0f;
        textureCoords[2] = 0.0f;
        textureCoords[3] = 0.0f;
        textureCoords[4] = 1.0f;
        textureCoords[5] = 0.0f;@

      That should do it.

      1 Reply Last reply
      0
      • 8Observer88 Offline
        8Observer88 Offline
        8Observer8
        wrote on last edited by
        #3

        Thank you! I solved this problem

        Source: https://github.com/8Observer8/TexturedTriangle
        Exe for Win 7: https://yadi.sk/d/dD84FsxPdPbBf

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          I would recommend to not use QGLWidget in new code. This class is soon to be deprecated in favour of QOpenGLWindow, QOpenGLWidget or just a plain old QWindow + QOpenGLContext.

          To work with textures, QOpenGLTexture will make you life much easier.

          I would also recommend you to use the OpenGL core profile rather than the compatibility profile. In which case your shaders will need updating to replace "attribute" and "varying" with "in" and "out" in the vertex shader and to replace "varying" with "in" in the frag shader. Plus declaring an "out vec4 fragColor" or similar output variable in the fragment shader.

          Then for sampling from a texture in your shaders you can always use the overloaded texture() function rather than the texture1d(), texture2d(), texture3d() etc. functions.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • 8Observer88 Offline
            8Observer88 Offline
            8Observer8
            wrote on last edited by
            #5

            It is very interesting. I will install Qt 5.4. Thank you very much for your reply

            1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #6

              ZapB, thank you again. I changed my project how did you say: https://github.com/8Observer8/TexturedTriangle

              But I want use “attribute” and “varying” because it is OpenGL ES. I saw it here: C:\Qt\Qt5.4.0\Examples\Qt-5.4\opengl\qopenglwidget

              How do you see I draw in the class Scene:

              @void Scene::drawTriangle()
              {
              m_texture->bind();

              m_program.setAttributeArray( m_vertexAttr, m_triangle.vertices.data(), 3 );
              m_program.setAttributeArray( m_texCoordAttr, m_triangle.texCoords.data(), 2 );
              m_program.setUniformValue( m_texUniform, 0 );
              
              m_program.enableAttributeArray( m_vertexAttr );
              m_program.enableAttributeArray( m_texCoordAttr );
              
              glDrawArrays( GL_TRIANGLES, 0, 3 );
              
              m_program.disableAttributeArray( m_vertexAttr );
              m_program.disableAttributeArray( m_texCoordAttr );
              

              }@

              But I want to draw in the class Triangle, like this:

              @#ifndef TRIANGLE_H
              #define TRIANGLE_H

              #include <vector>

              class Triangle
              {
              public:
              Triangle();
              void initVertices();
              void initTexCoords();

              void draw();
              
              std::vector<float> vertices;
              std::vector<float> texCoords;
              

              };

              #endif // TRIANGLE_H@

              @void Triangle::draw()
              {
              m_texture->bind();

              m_program.setAttributeArray( m_vertexAttr, m_triangle.vertices.data(), 3 );
              m_program.setAttributeArray( m_texCoordAttr, m_triangle.texCoords.data(), 2 );
              m_program.setUniformValue( m_texUniform, 0 );
              
              m_program.enableAttributeArray( m_vertexAttr );
              m_program.enableAttributeArray( m_texCoordAttr );
              
              glDrawArrays( GL_TRIANGLES, 0, 3 );
              
              m_program.disableAttributeArray( m_vertexAttr );
              m_program.disableAttributeArray( m_texCoordAttr );
              

              }@

              I hope you understand my problem. The glDrawArrays() function is a member of the QOpenGLWidget class and so on

              1 Reply Last reply
              0
              • 8Observer88 Offline
                8Observer88 Offline
                8Observer8
                wrote on last edited by
                #7

                bq. How do you see I draw in the class Scene

                I solved this:

                @#ifndef TRIANGLE_H
                #define TRIANGLE_H

                #include <QOpenGLShaderProgram>
                #include <QOpenGLTexture>
                #include <vector>

                class Triangle
                {
                public:
                Triangle( QOpenGLShaderProgram *program, QOpenGLTexture *texture,
                int vertexAttr, int texCoordAttr, int texUniform );
                void initVertices();
                void initTexCoords();

                void draw();
                
                std::vector<float> vertices;
                std::vector<float> texCoords;
                

                private:
                QOpenGLShaderProgram *m_program;
                QOpenGLTexture *m_texture;
                int m_vertexAttr;
                int m_texCoordAttr;
                int m_texUniform;
                };

                #endif // TRIANGLE_H@

                @
                #include "Triangle.h"
                #include <GL/gl.h>

                Triangle::Triangle( QOpenGLShaderProgram *program, QOpenGLTexture *texture,
                int vertexAttr, int texCoordAttr, int texUniform ) :
                m_program( program ),
                m_texture( texture ),
                m_vertexAttr( vertexAttr ),
                m_texCoordAttr( texCoordAttr ),
                m_texUniform( texUniform )
                {
                initVertices();
                initTexCoords();
                }

                void Triangle::initVertices()
                {
                vertices.clear();
                vertices.resize( 9 );
                // 0
                vertices[0] = 0.0f;
                vertices[1] = 0.5f;
                vertices[2] = 0.0f;
                // 1
                vertices[3] = -0.5f;
                vertices[4] = -0.5f;
                vertices[5] = 0.0f;
                // 2
                vertices[6] = 0.5f;
                vertices[7] = -0.5f;
                vertices[8] = 0.0f;
                }

                void Triangle::initTexCoords()
                {
                texCoords.clear();
                texCoords.resize( 6 );
                // 0
                texCoords[0] = 0.5f;
                texCoords[1] = 1.0f;
                // 1
                texCoords[2] = 0.0f;
                texCoords[3] = 0.0f;
                // 2
                texCoords[4] = 1.0f;
                texCoords[5] = 0.0f;
                }

                void Triangle::draw()
                {
                m_texture->bind();

                m_program->setAttributeArray( m_vertexAttr, vertices.data(), 3 );
                m_program->setAttributeArray( m_texCoordAttr, texCoords.data(), 2 );
                m_program->setUniformValue( m_texUniform, 0 );
                
                m_program->enableAttributeArray( m_vertexAttr );
                m_program->enableAttributeArray( m_texCoordAttr );
                
                glDrawArrays( GL_TRIANGLES, 0, 3 );
                
                m_program->disableAttributeArray( m_vertexAttr );
                m_program->disableAttributeArray( m_texCoordAttr );
                

                }
                @

                1 Reply Last reply
                0
                • 8Observer88 Offline
                  8Observer88 Offline
                  8Observer8
                  wrote on last edited by
                  #8

                  https://github.com/8Observer8/DrawingInsideOfTriangleClass

                  http://i9.pixs.ru/storage/3/5/8/TexturedTr_9091779_15268358.png

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved