为什么此代码用于绘制光线投射矢量而无法正确显示矢量?

问题描述 投票:0回答:1

按照 Pascal 驱动的 Castle Game Engine 中给出的用于绘制网格的示例,我尝试使用这段特定的代码来直观地绘制光线投射矢量以供参考:


  procedure CreateMesh;
  begin
    Mesh := TCastleRenderUnlitMesh.Create(true);
    Mesh.SetVertexes([
      Vector4(-1, -1, -1, 1),
      Vector4( 1, -1, -1, 1),
      Vector4( 1,  1, -1, 1),
      Vector4(-1,  1, -1, 1),

      Vector4(-1, -1,  1, 1),
      Vector4( 1, -1,  1, 1),
      Vector4( 1,  1,  1, 1),
      Vector4(-1,  1,  1, 1)
    ], false);
    Mesh.SetIndexes([
      // line loop on Z = -1
      0, 1,
      1, 2,
      2, 3,
      3, 0,

      // line loop on Z = 1
      4, 5,
      5, 6,
      6, 7,
      7, 4,

      // connect Z = -1 with Z = 1
      0, 4,
      1, 5,
      2, 6,
      3, 7
    ]);

    // Later on in the code
    CreateMesh;

this特定线程上,当我要求澄清时,我被告知“SetIndexes”中的数字表示正确绘制线条的顺序,就像我猜的那样,并且 SetVertices 设置您绘制的顶点和绘制的顶点。那就好啦。

但是,当我看到上面示例中的“SetVertices”不接受参数并且我需要参数来精确获取玩家角色的位置时,我询问是否可以使用参数创建自己的函数。

我没有得到回复,因为他们希望我自己解决问题,以便真正学习,这很好,但知道足智多谋和负责任的做法是查找手册中的功能,看看是否它是用特定数量的参数预先定义的,

但幸运的是事实并非如此,所以我认为这可能是用户在这种情况下自定义定义的一段代码,而不是预先编程到引擎的语言中。

我猜想,我可以参考上面的代码来制作自己的带参数的自定义SetVertices,并使用参数的坐标作为矢量坐标的输入,就像这样:


  procedure CreateMesh(const AvatarTransform: TCastleTransform);
  begin
    Mesh := TCastleRenderUnlitMesh.Create(true);
    Mesh.SetVertexes([
      Vector4(0.0, AvatarTransform.Translation.Y, 0.0, 1.0),
      Vector4(0.0, (AvatarTransform.Translation.Y - AvatarTransform.Direction.Y), 0.0, 1.0),
    ], false);
    Mesh.SetIndexes([
      // line loop on Z = -1
      0, 1
    ]);

 
 // Line somewhere later down the code; I guessed the problem was
 // not having the correct amount of inputs to match the procedure 
 // definition above, but it still doesn't draw anything afterward

CreateMesh;
  

尽管我确保在函数声明中实际定义了参数,并遵循游戏引擎的示例来执行正确的格式和语法,但它仍然可以正常编译,但实际上从未绘制任何内容。

有这方面专业知识的人知道这是为什么吗?

可供参考下载的完整项目在这里,因为此代码只是其中的一小部分:

ZIP 存档中完整项目的链接

我按照我需要的特定示例的指示进行操作,并期望它能起到同样的作用。

然而,它最终根本不起作用,这就是为什么我很困惑。

game-development pascal lazarus
1个回答
0
投票

您的主要问题最有可能的答案是您没有调用“Mesh.Render(pmLines)”。可能您只是忘记了它,这解释了为什么您看不到任何东西。

这个答案有点猜测,因为您没有显示完整的源代码,只有一个无法编译的片段。我看到您已经在评论中指出了 https://stackoverflow.com/help/minimal-reproducible-example 。您的“ZIP 存档中完整项目的链接”不公开可读。

如果您不确定 TCastleRenderUnlitMesh 的工作原理,请:

渲染前真正需要设置的内容是:

  • 顶点,使用 SetVertexes。

  • 矩阵(组合投影*相机*模型变换)ModelViewProjection。

  • 就是这样,其余的都有合理的默认值。您只需调用 Render 即可。

此问题与您在 Lazarus 论坛上提出的问题重复:https://forum.lazarus.freepascal.org/index.php/topic,66238.0.html。我在那里提供更多细节。 Castle Game Engine 论坛主题中有更多详细信息,我们尽力为您提供帮助,请参见 https://forum.castle-engine.io/t/need-help-making-collision-with-ground-正确工作/1025/70 .

© www.soinside.com 2019 - 2024. All rights reserved.