我可以在GPU中计算法线吗?

发布于 2024-09-12 01:24:39 字数 88 浏览 0 评论 0原文

我有一个 opengl 应用程序,可以加载 dxf 并将其绘制在屏幕上, 每次我需要计算法线。有没有办法在 GPU 而不是 CPU 中计算法线?如果是这样怎么办?

I have an opengl application that loads a dxf and draws it on the screen,
each time i need to calculate normals. is there a way to calculate normals in GPU instead of CPU ? if so how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬 2024-09-19 01:24:39

您可以从几何着色器中计算“平面着色”(每个面一个法线)法线。每个三角形一个,“动态”,使用叉积。您无法以这种方式生成“平滑”法线,因为 GS 没有有关邻居的信息。

如果网格是参数化的(例如元球),则可以通过采样几个附近的函数值来计算法线。这也可以在 GPU 上完成。

如果网格基于高度图或类似的东西,您还可以在着色器中计算法线。

如果 GPU 支持 CUDA、OpenCL 或类似的东西,并且如果它可以处理任意数据数组,那么您也可以使用传统技术在 GPU 上计算法线。

另外,我认为 5 年前(大约)我看到了一篇名为“GPU 上的正常平滑”或“GPU 上的正常生成”之类的论文。它的第一页上有一个平底阴影的反光多边形心形。不幸的是,我找不到那篇论文,而且我不确定它是否存在(或者我在哪里看到它)。您也许可以在 GDC 论文、SIGGRAPH 论文、ATI SDK 或 NVidia SDK 中找到它(如果存在)。

You can calculate "flat-shaded" (one normal per face) normals from within geometry shader. One for each triangle, "on the fly", using cross-products. You cannot generate "smooth" normals that way, because GS doesn't have info about neighbors.

If mesh is parametric (say, metaballs), normal can be calculated by sampling few nearby function values. This can also be done on GPU.

You can also calculate normals in shader if mesh is based on heightmap or something similar.

IF GPU supports CUDA, OpenCL or something similar, AND if it can process arbitrary arrays of data, then you can probably also compute normals on the GPU, using traditional tehcniques.

Also, I think that 5 years ago (or so) I saw a paper titled something like "normal smoothing on GPU" or "normal generation on GPU". It had a flat-shaded reflective polygonal heart on the first page. Unfortunately, I cannot locate that paper, and I'm not exactly sure if it existed (or where I saw it). You may be able to find it(if it exists) in GDC papers, SIGGRAPH papers, ATI SDK or NVidia SDK.

打小就很酷 2024-09-19 01:24:39

您必须使用着色器。

您可以使用 GLSL 几何着色器。这是面部法线的 GLSL 版本。顶点法线需要更多工作,因为它们必须考虑相邻顶点。

  • 确保您的显卡支持几何着色器!例如使用 GPU Caps Viewer。
  • 剪切并粘贴任何教程以将几何着色器合并到您的应用程序中。
  • 着色器应该如下所示:
//glsl shader

#version 120
#extension GL_EXT_geometry_shader4 : enable
void main(void)
{
// Compute per-face normal
    vec4 d1 = gl_PositionIn[1] - gl_PositionIn[0];
    vec4 d2 = gl_PositionIn[2] - gl_PositionIn[0];
    gl_Normal = cross(d1,d2);

// Emit all input vertices
    for(i=0; i< gl_VerticesIn; i++){
        gl_Position = gl_PositionIn[i];
        EmitVertex();
    }
    EndPrimitive();
}   

You'll have to use shaders.

You can use a GLSL geometry shader. Here's the GLSL version for face normals. Vertices normals require more work since they have to take neighboring vertices into account.

  • Make sure your graphic card support geometry shaders ! Use for instance GPU Caps Viewer.
  • Cut and paste any tutorial to incorporate a geometry shader in your app.
  • The shader should look like this :
//glsl shader

#version 120
#extension GL_EXT_geometry_shader4 : enable
void main(void)
{
// Compute per-face normal
    vec4 d1 = gl_PositionIn[1] - gl_PositionIn[0];
    vec4 d2 = gl_PositionIn[2] - gl_PositionIn[0];
    gl_Normal = cross(d1,d2);

// Emit all input vertices
    for(i=0; i< gl_VerticesIn; i++){
        gl_Position = gl_PositionIn[i];
        EmitVertex();
    }
    EndPrimitive();
}   
じее 2024-09-19 01:24:39

是的,这是可能的。您将需要在 Cg 中了解着色器。

官方书籍

Yes, it's possible. You will want to learn about Shaders in Cg.

Official book.

北笙凉宸 2024-09-19 01:24:39

看看glEnable(GL_AUTO_NORMAL)

Have a look at glEnable(GL_AUTO_NORMAL).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文