开发者

C# way to set a float buffer in memory

开发者 https://www.devze.com 2023-01-21 04:08 出处:网络
I am an usual Java developer but now I\'m trying to create some OpenGL stuff in C#. However, in Java I do it like this:

I am an usual Java developer but now I'm trying to create some OpenGL stuff in C#.

However, in Java I do it like this:

private FloatBuffer verticesBuffer;
private float[] vertices = new float[]{... some vert开发者_如何学编程ices ...};

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
verticesBuffer = vbb.asFloatBuffer();
verticesBuffer.put(vertices);
verticesBuffer.position(0);

How should I become same thing in C#?


It's not really clear what you need to do in C#, as presumably the types you need aren't the same. However, if you're trying to block copy floats into bytes, then Buffer.BlockCopy may be what you want. Sample code:

using System;

public class Test 
{
    public static void Main()
    {
        byte[] bytes = new byte[12];
        float[] floats = { 1.5f, 2.5f, 0.000001f };

        Buffer.BlockCopy(floats, 0, bytes, 0, 12);

        for (int i = 0; i < 12; i++)
        {
            Console.WriteLine("{0}: {1}", i, bytes[i]);
        }
    }
}

Of course this doesn't alias the two arrays, which may be what you're after instead.

If you have specific .NET types (or .NET-translated types) in mind, it would be useful if you'd identify them.

0

精彩评论

暂无评论...
验证码 换一张
取 消