1

这更像是将一些 c# 文档和语法翻译成视觉基本内容的问题。我正在用 Visual basic 编写,并使用 4.0 XNA 刷新平台。

我在Riemers.net上关注 ac# 教程,但在重现他处理顶点、位置、颜色和法线的结构时遇到了一些困难。我已经能够使用预建的 VertexPositionColor & .VertexDeclaration 很好地运行程序。

这个问题的 C# 代码是这样的:

 public struct VertexPositionColorNormal
 {            
     public Vector3 Position;
     public Color Color;
     public Vector3 Normal;

     public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
     (
         new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
         new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
         new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
     );
 }

这是我的转换尝试

Public Structure VertexPositionColorNormal

Public Position As Vector3
Public Color As Color
Public Normal As Vector3

Public Shared SizeInBytes As Integer = 7 * 4

Public Shared ReadOnly VertexDeclaration As VertexElement() = New VertexElement() _
{New VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), _
New VertexElement(4 * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0), _
New VertexElement(4 * 4, VertexElementFormat.Vector3, VertexElementUsage.Normal,                 0)}

End Structure

虽然我的尝试在语法上似乎是正确的,但它在这一行产生了一个错误:

GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration)

并给我以下错误描述:

错误 18
重载解析失败,因为无法使用这些参数调用可访问的“DrawUserIndexedPrimitives”:

'Public Sub DrawUserIndexedPrimitives(Of VertexPositionColorNormal)(primitiveType As Microsoft.Xna.Framework.Graphics.PrimitiveType, vertexData() As VertexPositionColorNormal, vertexOffset As Integer, numVertices As Integer, indexData() As Short, indexOffset As Integer, primitiveCount As Integer, vertexDeclaration As Microsoft.Xna.Framework.Graphics.VertexDeclaration)':“整数的一维数组”类型的值无法转换为“Short 的一维数组”,因为“Integer”不是从“Short”派生的。

'Public Sub DrawUserIndexedPrimitives(Of VertexPositionColorNormal)(primitiveType As Microsoft.Xna.Framework.Graphics.PrimitiveType, vertexData() As VertexPositionColorNormal, vertexOffset As Integer, numVertices As Integer, indexData() As Short, indexOffset As Integer, primitiveCount As Integer, vertexDeclaration As Microsoft.Xna.Framework.Graphics.VertexDeclaration)':“Microsoft.Xna.Framework.Graphics.VertexElement 的一维数组”类型的值无法转换为“Microsoft.Xna.Framework.Graphics.VertexDeclaration”。

'Public Sub DrawUserIndexedPrimitives(Of VertexPositionColorNormal)(primitiveType As Microsoft.Xna.Framework.Graphics.PrimitiveType, vertexData() As VertexPositionColorNormal, vertexOffset As Integer, numVertices As Integer, indexData() As Integer, indexOffset As Integer, primitiveCount As Integer, vertexDeclaration As Microsoft.Xna.Framework.Graphics.VertexDeclaration)':“Microsoft.Xna.Framework.Graphics.VertexElement 的一维数组”类型的值无法转换为“Microsoft.Xna.Framework.Graphics.VertexDeclaration”。

C:\Users\Xheis-Overlord\Documents\Visual Studio 2010\Projects\Test_Terrains2\Test_Terrains2\Test_Terrains2\Game1.vb 416 13 Test_Terrains2

4

2 回答 2

1

我不精通VB,但根据错误消息,我认为这条线有部分错误:

Public Shared ReadOnly VertexDeclaration As VertexElement() = New VertexElement() _

在您包含的 C# 中,VertexDeclaration对象的类型实际上是VertexDeclaration. 但是,在您的 VB 中,您将其视为类型VertexElement数组。

还有,什么是indices

这是关于DrawUserIndexPrimitives的 MSDN 文章的链接以供参考。此方法有四个重载,它们都非常相似。仔细检查您的参数类型。

于 2012-10-05T13:50:48.413 回答
0

原件不是数组,但在您的转换中它是一个数组。尝试这个:

 Public Structure VertexPositionColorNormal
     Public Position As Vector3
     Public Color As Color
     Public Normal As Vector3

     Public ReadOnly Shared VertexDeclaration As New VertexDeclaration( _
        New VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), _
        New VertexElement(Len(New Single) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0), _
        New VertexElement(Len(New Single) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0))
 End Structure
于 2012-10-05T14:56:42.443 回答