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. Why the center of the fir is not in the (0, 0) (not in the left bottom corner)

Why the center of the fir is not in the (0, 0) (not in the left bottom corner)

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 114 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 8Observer8
    #1

    Hi,

    Why the center of the fir is not in the (0, 0) (not in the left bottom corner)?

    zip: https://discord.com/channels/457523061650882570/1247941120390991884/1247955899369132163

    c07c0e87-f554-429c-b9e0-030282c97bcb-image.png

    #include "opengl_widget.h"
    
    OpenGLWidget::OpenGLWidget(QWidget *parent)
        : QOpenGLWidget(parent)
        , m_texture(QOpenGLTexture::Target::Target2D)
    {
    }
    
    void OpenGLWidget::initializeGL()
    {
        initializeOpenGLFunctions();
    
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        m_program.create();
        m_program.addShaderFromSourceFile(QOpenGLShader::ShaderTypeBit::Vertex,
            ":/assets/shaders/texture.vert");
        m_program.addShaderFromSourceFile(QOpenGLShader::ShaderTypeBit::Fragment,
            ":/assets/shaders/texture.frag");
        m_program.link();
        m_program.bind();
    
        float vertPositions[] = {
            -0.5f, -0.5f,
            0.5f, -0.5f,
            -0.5f, 0.5f,
            0.5f, 0.5f
        };
        m_vertPosBuffer.create();
        m_vertPosBuffer.bind();
        m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
    
        float texCoords[] = {
            0.f, 0.f,
            1.f, 0.f,
            0.f, 1.f,
            1.f, 1.f
        };
        m_texCoordBuffer.create();
        m_texCoordBuffer.bind();
        m_texCoordBuffer.allocate(texCoords, sizeof(texCoords));
    
        m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");
        m_uSamplerLocation = m_program.uniformLocation("uSampler");
    
        m_program.setUniformValue(m_uSamplerLocation, 0);
        m_viewMatrix.lookAt(QVector3D(0, 0, 1), QVector3D(0, 0, 0), QVector3D(0, 1, 0));
    
        QString texturePath(":/assets/textures/fir.png");
        m_texture.create();
        m_texture.setData(QImage(texturePath).mirrored());
        m_texture.setMinMagFilters(QOpenGLTexture::Filter::Linear,
            QOpenGLTexture::Filter::Linear);
        m_texture.setWrapMode(QOpenGLTexture::WrapMode::ClampToEdge);
    }
    
    void OpenGLWidget::resizeGL(int w, int h)
    {
        int deviceW = w * devicePixelRatio();
        int deviceH = h * devicePixelRatio();
        float deviceAspect = deviceH / (float) deviceW;
    
        if (deviceAspect > m_worldAspect)
        {
            m_viewportWidth = deviceW;
            m_viewportHeight = (int) deviceW * m_worldAspect;
            m_viewportX = 0;
            m_viewportY = (int) (deviceH - m_viewportHeight) / 2.f;
        }
        else
        {
            m_viewportWidth = (int) deviceH / m_worldAspect;
            m_viewportHeight = deviceH;
            m_viewportX = (int) (deviceW - m_viewportWidth) / 2.f;
            m_viewportY = 0;
        }
        m_projMatrix.setToIdentity();
        m_projMatrix.ortho(0.f, m_worldWidth, 0.f, m_worldHeight, 1.f, -1.f);
        m_projViewMatrix = m_projMatrix * m_viewMatrix;
    }
    
    void OpenGLWidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.2, 0.2, 0.2, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        glViewport(m_viewportX, m_viewportY, m_viewportWidth, m_viewportHeight);
        glScissor(m_viewportX, m_viewportY, m_viewportWidth, m_viewportHeight);
        glClearColor(153.f/255.f, 220.f/255.f, 236.f/255.f, 1.f);
        glEnable(GL_SCISSOR_TEST);
        glClear(GL_COLOR_BUFFER_BIT);
        glDisable(GL_SCISSOR_TEST);
    
        m_program.bind();
        m_texture.bind();
    
        m_program.setAttributeBuffer("aPosition", GL_FLOAT, 0, 2);
        m_program.enableAttributeArray("aPosition");
    
        m_program.setAttributeBuffer("aTexCoord", GL_FLOAT, 0, 2);
        m_program.enableAttributeArray("aTexCoord");
    
        m_modelMatrix.setToIdentity();
        m_modelMatrix.translate(QVector3D(0.f, 0.f, 0.f));
        m_modelMatrix.scale(QVector3D(78.6f, 100.f, 1.f));
        m_mvpMatrix = m_projViewMatrix * m_modelMatrix;
        m_program.setUniformValue(m_uMvpMatrixLocation, m_mvpMatrix);
    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    

    The pivot point of the fir is in the center:

        float vertPositions[] = {
            -0.5f, -0.5f,
            0.5f, -0.5f,
            -0.5f, 0.5f,
            0.5f, 0.5f
        };
    
    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