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. QtOpenglWidget black quad texture
Forum Update on Monday, May 27th 2025

QtOpenglWidget black quad texture

Scheduled Pinned Locked Moved General and Desktop
qt 5.4.2opengltexture
1 Posts 1 Posters 960 Views
  • 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.
  • G Offline
    G Offline
    Gmadges
    wrote on last edited by
    #1

    Hi there,
    I am trying to render a basic textured quad in openGL. I've tried this with both glew and QopenglFunctions and get the same outcome, a black quad.

    Here is a my code below.
    The shaders compile fine.
    I receive the texCoords correctly within the shader.

    Any thoughts would be helpful.

    GLfloat tmpdata[] = {
    //pos				//colour			//texCoord
    -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,  // Bottom-left
    0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
    -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
    0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f // Top-right
    
    };
    
    GLRenderer::GLRenderer(QWidget *parent)
    :
    QOpenGLWidget(parent),
    m_ShaderProgram(0)
    {
    m_vertShader = "#version 150 core\n"
    	"in vec2 position;"
    	"in vec3 color;"
    	"in vec2 texcoord;"
    	"out vec3 Color;"
    	"out vec2 Texcoord;"
    	"void main() {"
    	"   Color = color;"
    	"   Texcoord = texcoord;"
    	"   gl_Position = vec4(position, 0.0, 1.0);"
    	"}";
    
     m_fragShader = "#version 150 core\n"
    	"in vec3 Color;"
    	"in vec2 Texcoord;"
    	"out vec4 outColor;"
    	"uniform sampler2D tex;"
    	"void main() {"
    	//"   outColor = texture(tex, Texcoord) * vec4(Color, 1.0);"
    	//"   outColor = vec4(Color, 1.0f);"
    	//"   outColor = vec4(Texcoord, 0.0f, 1.0f);"
    	"   outColor = texture(tex, Texcoord);"
    	"}";
    }
     GLRenderer::~GLRenderer()
     {
         makeCurrent();
    
       delete m_ShaderProgram;
    
        m_vbo.destroy();
    
        doneCurrent();
    }
    
    void GLRenderer::initializeGL()
    {
    initializeOpenGLFunctions();
    
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    
    if (!m_vbo.create())
    	return;
    
    m_vbo.setUsagePattern(m_vbo.StaticDraw);
    m_vbo.bind();
    m_vbo.allocate(&tmpdata, sizeof(tmpdata));
    
    
    m_ShaderProgram = new QOpenGLShaderProgram(this);
    
    if (!m_ShaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertShader))
    	close();
    if (!m_ShaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragShader))
    	close();
    
    if (!m_ShaderProgram->link())
    	close();
    
    if (!m_ShaderProgram->bind())
    	close();
    
       m_ShaderProgram->setUniformValue("tex", 0);
    
       //Specify the layout of the vertex data
       GLint posAttrib = glGetAttribLocation(m_ShaderProgram->programId(), "position");
       glEnableVertexAttribArray(posAttrib);
       glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
    
       GLint colAttrib = glGetAttribLocation(m_ShaderProgram->programId(), "color");
        glEnableVertexAttribArray(colAttrib);
        glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
    
        GLint texAttrib = glGetAttribLocation(m_ShaderProgram->programId(), "texcoord");
        glEnableVertexAttribArray(texAttrib);
        glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
    
    
        QImage white;
        white.fill(Qt::blue);
    
        QOpenGLTexture* testTexture = new QOpenGLTexture(white.mirrored());
        testTexture->setMinificationFilter(QOpenGLTexture::Linear);
        testTexture->setMagnificationFilter(QOpenGLTexture::Linear);
    
        testTexture->bind();
    }
    
    void GLRenderer::resizeGL(int w, int h)
    {
    
    }
    void GLRenderer::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    
    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