为什么这个三角形条只绘制三分之一的屏幕?

时间:2018-06-15 18:39:28

标签: c++ qt opengl

我正在尝试制作一个玩具来在整个屏幕上“绘制”碎片着色器。为此,我试图用一个三角形条带“填充”屏幕。我正在使用QT,因为除了openGL渲染之外我还有一个想要使用的UI。

我基于全屏三角条技术: https://github.com/Gargaj/Bonzomatic/blob/master/src/platform_glfw/Renderer.cpp

static const char *fragmentShaderSource =
        "varying mediump vec4 color;\n"
        "void main(void)\n"
        "{\n"
        "    gl_FragColor = vec4(1.0,0.0,0.0,0.0);\n"
        "}\n";


static float pFullscreenQuadVertices[] =
    {
      -1.0, -1.0,  0.5, 0.0, 0.0,
      -1.0,  1.0,  0.5, 0.0, 1.0,
       1.0, -1.0,  0.5, 1.0, 0.0,
       1.0,  1.0,  0.5, 1.0, 1.0,
    };

static const char *szVertexShader =
      "#version 410 core\n"
      "in vec3 in_pos;\n"
      "in vec2 in_texcoord;\n"
      "out vec2 out_texcoord;\n"
      "void main()\n"
      "{\n"
      "  gl_Position = vec4( in_pos.x, in_pos.y, in_pos.z, 1.0 );\n"
      "  out_texcoord = in_texcoord;\n"
      "}";


void DemoRender::initializeGL(){
         m_background = qRgb(255,255,255);
         initializeOpenGLFunctions();
         m_program = new QOpenGLShaderProgram(this);
         m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, szVertexShader);
         m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
         m_program->link();
         m_program->log();
         m_posAttr = m_program->attributeLocation("in_pos");

         m_vao1.create();
         m_vao1.bind();



}


void DemoRender::paintGL(){
        QPainter painter;
        painter.begin(this);

        painter.beginNativePainting();
        glClearColor(m_background.redF(), m_background.greenF(), m_background.blueF(),0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //glFrontFace(GL_CW);
        //glCullFace(GL_FRONT);
        //glEnable(GL_CULL_FACE);
        //glEnable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT);

        m_program->bind();
        drawFullScreen();
        m_program->release();
           //glDisable(GL_DEPTH_TEST);
           //glDisable(GL_CULL_FACE);

           painter.endNativePainting();

           painter.end();

           update();


}

void DemoRender::drawFullScreen(){

    m_vao1.bind();

    m_vbo1.create();
    m_vbo1.bind();
    m_vbo1.allocate(pFullscreenQuadVertices,sizeof(pFullscreenQuadVertices)*sizeof(float));
    m_vbo1.write(0,pFullscreenQuadVertices,sizeof(pFullscreenQuadVertices));


    m_program->enableAttributeArray(m_posAttr);

    //m_vbo1.release();
    //I suspect you are the culprit, but I don't know how to fix it. 
    m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 3);

    m_vbo1.release();

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


    m_program->disableAttributeArray(m_posAttr);

}

作为调试技术我告诉片段着色器使每个像素都变红。现在,我的困惑是:为什么只有大约三分之一的屏幕变红?

顶点着色器似乎没有覆盖整个屏幕空间。我该怎么做才能解决这个问题?

我目前的理论是,由于顶点数组如何设置的一些细节,三角形条带不能正确渲染,但因为qt不会让我这样做C风格我不能遵循原作者的代码一为了一个。

Something is definitely wrong here

1 个答案:

答案 0 :(得分:4)

顶点属性指针,特别是步幅,是错误的。数据包含一个3分量位置和两个额外的浮点数,但默认步幅假设紧密打包。正确的代码应该是:

m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 3, 5*sizeof(float))

其中5 * sizeof(float)是顶点起点和下一个顶点起点之间的偏移量。