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. Qt opengl use shader to draw triangle
Forum Updated to NodeBB v4.3 + New Features

Qt opengl use shader to draw triangle

Scheduled Pinned Locked Moved General and Desktop
openglshadervbo
1 Posts 1 Posters 1.1k 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.
  • Z Offline
    Z Offline
    zhangzhisheng
    wrote on 7 May 2015, 06:04 last edited by zhangzhisheng 5 Jul 2015, 06:08
    #1

    I just want to draw a triangle with Qt. When I run the program, there is no triangle. What is wrong with my code?

    #include "glwidget.h"

    GLWidget::GLWidget(QWidget *parent) :QOpenGLWidget(parent)
    {
    this->setFocusPolicy(Qt::StrongFocus);
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setMajorVersion(4);
    format.setMinorVersion(2);
    format.setSamples(16);
    format.setProfile(QSurfaceFormat::CoreProfile);
    setFormat(format);
    }

    void GLWidget::initializeGL()
    {
    initializeOpenGLFunctions();
    glClearColor(0.13F, 0.13F, 0.14F, 0.0F);
    m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,"../PCRP/vertexshadercode.vert");
    m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,"../PCRP/fragmentshadercode.frag");
    m_shaderProgram.link();
    m_MVPMatrixLoc=m_shaderProgram.uniformLocation("u_MVPMatrix");
    GLfloat vertex[]={1.0,0.0,0.0,1.0,
    0.0,1.0,0.0,1.0,
    1.0,1.0,0.0,1.0};
    m_Vbo.create();
    m_Vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_Vbo.bind();
    m_Vbo.allocate(vertex,sizeof(vertex));
    m_ViewMatrix.lookAt(QVector3D(0.0f, 0.0f, 1.5f), QVector3D(0.0f, 0.0f, -5.0f), QVector3D(0.0f, 1.0f, 0.0f));
    }

    void GLWidget::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_MULTISAMPLE);
    m_shaderProgram.bind();
    m_shaderProgram.enableAttributeArray("a_Position");
    m_shaderProgram.setAttributeBuffer("a_Position",GL_FLOAT,0,4);

    m_ModelMatrix.setToIdentity();
    m_MVPMatrix=m_ProjectionMatrix*m_ViewMatrix*m_ModelMatrix;
    
    m_shaderProgram.setUniformValue(m_MVPMatrixLoc,m_MVPMatrix);
    glDrawArrays(GL_TRIANGLES,0,3);
    

    }

    void GLWidget::resizeGL(int w, int h)
    {
    glViewport(0, 0, (GLint)width(), (GLint)height());
    m_ProjectionMatrix.setToIdentity();
    m_ProjectionMatrix.perspective(45,(float) (width()/height()),0.1f,1000.0f);
    }

    this is vertexshadercode.vert

    attribute vec4 a_Position;

    uniform mat4 u_MVPMatrix;

    void main()
    {

    gl_Position = u_MVPMatrix*a_Position;
    } ;

    this is fragmentshadercode.frag

    void main(void)
    {
    gl_FragColor=vec4(1.0,0.0,0.0,1.0);
    }

    1 Reply Last reply
    0

    1/1

    7 May 2015, 06:04

    • Login

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