繁体   English   中英

OpenGL 为来自一个 QVector3D 的给定缓冲区的所有顶点着色?

[英]OpenGL color all vertices for a given buffer from one QVector3D?

我有一个程序可以呈现 FACET 几何图形,然后在相同的 OpenGL 上下文中在几何图形的顶部绘制数据。 当从定义小平面三角形及其关联的 colors 的 VBO 按顶点着色时,一切都按预期工作,但是用户已请求能够将所有几何顶点设置为单一颜色(GUI 切换)。

有没有办法在不进入并将 VBO 的颜色部分设置为该颜色的情况下执行此操作? 看起来效率很低,但我无法从数小时的谷歌搜索中得到任何我尝试过的东西。

使用C++,QT6+

片段着色器

#version 150 core
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif

in vec3 Color;
out vec4 outColor;

void main()
{
    outColor = vec4(Color,1.0f);
}

顶点着色器

#version 150 core
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif

uniform mat4 mvp_matrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
attribute vec3 color;
varying vec3 Color;

void main()
{
    Color = color;

    // Calculate vertex position in screen space
    gl_Position = mvp_matrix * a_position;

    // Pass texture coordinate to fragment shader
    // Value will be automatically interpolated to fragments inside polygon faces
    v_texcoord = a_texcoord;
}

相关代码片段:

// initializing the buffers from vectors of structures
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Geometry
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Get a copy of the geometry to reference here
    std::vector<storedGeometry::facetData> tGeom = gUseGeom.getGeom();
    // Convert vector to array
    storedGeometry::facetData* aGeom = tGeom.data();

    // Transfer vertex data to VBO 0
    geomBuf.bind();
    geomBuf.allocate(aGeom, int(tGeom.size() * sizeof(storedGeometry::facetData)));

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Currents
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Get a copy of the currents data to reference here
    std::vector<storedGeometry::facetData> tCurrents = gUseCurrents.getGeom();
    // Convert vector to array
    storedGeometry::facetData* aCurrent = tCurrents.data();

    // Transfer vertex data to VBO 1
    currentsBuf.bind();
    currentsBuf.allocate(aCurrent, int(tCurrents.size() * sizeof(storedGeometry::facetData)));




// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// drawing 
// Get shader variable locations
    int vertexLocation = program->attributeLocation("a_position");
    int colorLocation = program->attributeLocation("color");
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Plot Geometry
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (bGeomVisible) {
        geomBuf.bind();
        program->enableAttributeArray(vertexLocation);
        program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(QVector3D));

        program->enableAttributeArray(colorLocation);
        if (bCobbleSolid) { // Solid
// This is what I can't get to work ************************************
            //program->setAttributeValue(colorLocation, 1.0, 1.0, 1.0);
            program->setAttributeValue(colorLocation, QVector3D(1.0,1.0,1.0));
        } else { // Cobble
            program->setAttributeBuffer(colorLocation, GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
        }

        // Set polygon/wiremesh or solid/filled mode based on fill/wire toggle
        if (bFillWire) {
            // Polygon = TRUE
            glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
        } else {
            // Filled = FALSE
            glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        }
        // Draw geometry
        glDrawArrays(GL_TRIANGLES, 0, gUseGeom.gSize() * 6);
    }
    // Debugging
    //out << "gUseGeom.gSize(): " << gUseGeom.gSize() << Qt::endl;
    // Draw cube geometry
    //glDrawElements(GL_TRIANGLES, gUseGeom.gSize() * 3, GL_UNSIGNED_SHORT, 0);
    //glDrawArrays(GL_TRIANGLES, 0, gUseGeom.gSize() * 6);

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Plot Currents
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (bCurrentsVisible) {
        currentsBuf.bind();
        program->enableAttributeArray(vertexLocation);
        program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0,                   3, sizeof(QVector3D));
        program->enableAttributeArray(colorLocation);
        program->setAttributeBuffer(colorLocation,  GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        glDrawArrays(GL_TRIANGLES, 0, gUseCurrents.gSize() * 6);
    }

当特定顶点属性数组未启用(使用glEnableVertexAttribArray )时,所有渲染顶点的该属性的值取自当前顶点属性值,如glVertexAttrib函数族所定义。

您忘记禁用顶点属性数组,因此不使用当前顶点属性值。 您可以按如下方式修复它:

    if (bCobbleSolid) { // Solid
        program->disableAttributeArray(colorLocation);
        program->setAttributeValue(colorLocation, QVector3D(1.0,1.0,1.0));
    } else { // Cobble
        program->enableAttributeArray(colorLocation);
        program->setAttributeBuffer(colorLocation, GL_FLOAT, 3*sizeof(QVector3D), 3, sizeof(QVector3D));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM