简体   繁体   English

如何从着色器获取计算结果?

[英]How do I get a calculation result from a shader?

I load a 3D-model from a file and need to see it all on my screen.我从一个文件中加载了一个 3D 模型,并且需要在我的屏幕上看到它。 All vertices should be on the screen within the main window. Then I rotate and zoom the model and at some point I would like to fit the model to the window again.所有顶点都应位于主 window 内的屏幕上。然后我旋转并缩放 model,在某个时候我想再次将 model 适合 window。 So, I have written the function OptimiseView (see below) which multiplies the view matrix by each vertex position and then calculates minimum and maximum coordinates of the screen plane.因此,我编写了 function OptimiseView (见下文),它将视图矩阵乘以每个顶点 position,然后计算屏幕平面的最小和最大坐标。

The above multiplication takes a lot of time.上面的乘法需要很多时间。 My shader does the same multiplication but I can't manage to store minimum and maximum coordinates in the shader (GPU) and return these values back to the program (CPU) after processing the last vertex.我的着色器执行相同的乘法运算,但我无法在着色器 (GPU) 中存储最小和最大坐标,并在处理完最后一个顶点后将这些值返回给程序 (CPU)。

Is this possible at all?这可能吗? How do CAD-systems implement this Fit View (Optimise View) feature? CAD 系统如何实现此适合视图(优化视图)功能? How does a space mouse (eg 3Dconnexion) work in this relation?太空鼠标(例如 3Dconnexion)如何在这种关系中发挥作用?

I am currently using C++ and OpenGL.我目前使用的是 C++ 和 OpenGL。

void OptimiseView(glm::mat4& view, glm::mat4* proj, int trianglesNumber, float* positions)
{
    if ((rotAngleX != 0) || (rotAngleY != 0) || (mouseScroll != 0))
    {
        float minX, maxX, minY, maxY, minZ, maxZ;
        float centreX, centreY;
        float largestDimension;

        int triangleCount{ 0 };

        glm::vec4 vertexPosition;

        if (trianglesNumber < 1)
        {
            minX = maxX = minY = maxY = minZ = maxZ = 0;
        }
        else
        {
            vertexPosition = view * glm::vec4(
                positions[0],
                positions[1],
                positions[2],
                1.0f);

            minX = maxX = vertexPosition.x;
            minY = maxY = vertexPosition.y;
            minZ = maxZ = vertexPosition.z;
        }

        while (triangleCount < trianglesNumber)
        {
            for (int initialPosition : {0, 3, 9})
            {
                vertexPosition = view * glm::vec4(
                    positions[triangleCount * 9 * 2 + initialPosition],
                    positions[triangleCount * 9 * 2 + initialPosition + 1],
                    positions[triangleCount * 9 * 2 + initialPosition + 2],
                    1.0f);

                if (vertexPosition.x < minX) minX = vertexPosition.x;
                if (vertexPosition.x > maxX) maxX = vertexPosition.x;

                if (vertexPosition.y < minY) minY = vertexPosition.y;
                if (vertexPosition.y > maxY) maxY = vertexPosition.y;

                if (vertexPosition.z < minZ) minZ = vertexPosition.z;
                if (vertexPosition.z > maxZ) maxZ = vertexPosition.z;
            }
            triangleCount++;
        }

        centreX = minX + (maxX - minX) / 2.0f;
        centreY = minY + (maxY - minY) / 2.0f;
        largestDimension = ((maxX - minX) >= (maxY - minY)) ? (maxX - minX) : (maxY - minY);

        minX = centreX - largestDimension / 2.0f;
        maxX = centreX + largestDimension / 2.0f;

        minY = centreY - largestDimension / 2.0f;
        maxY = centreY + largestDimension / 2.0f;

        *proj = glm::ortho(minX, maxX, minY, maxY, -minZ, -maxZ);
    }
}

I have also tried to use global variables in the shader but they seem to store values during processing a single vertex only.我也曾尝试在着色器中使用全局变量,但它们似乎只在处理单个顶点期间存储值。

I know the Fit View feature already works in different CAD-systems and am just wondering what approach would be the best.我知道“适合视图”功能已经在不同的 CAD 系统中运行,我只是想知道哪种方法最好。

Thanks.谢谢。

After more deep research, I found the OpenGL thing which is called 'Transform Feedback'.经过更深入的研究,我发现了 OpenGL 的东西,它被称为“转换反馈”。 The best practical manual and the answer to my question is here .最好的实用手册和我的问题的答案就在这里

More information is here .更多信息在这里

It will also be useful to read the book of Sam Buss ' 3D Computer Graphics: A mathematical approach with OpenGL .'阅读 Sam Buss 的书“ 3D Computer Graphics:A mathematical approach with OpenGL ”也很有用。 It contains a lot of examples on using different OpenGL functions.它包含许多使用不同 OpenGL 函数的示例。

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

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