开发者

Putting an Object into a ByteBuffer without serializing it

I am trying to put an object into a ByteBuffer to no avail.

I am trying to make a VertexBuffer class for Android that mimics a DirectX/XNA vertex buffer. What I mean is that I want to allow any type of vertex structure (that implements a certain interface/extends a certain class.) In order to do this, i would need to convert that object into byte[].

I have looked into Serializable, ByteArrayOutputStream, and ObjectOutputStream, byte it gives me too many bytes. Ex. a Vertex class that has 3 floats for x, y, and z should be about 3 * 4 or 12 bytes, but the resulting array has a length of 51! I'm pretty sure that the extra bytes are the result of serializing, and that I probably could just take the bytes of the end of the array, but that's slower, and if there is an easier way, I would prefer that.

I have also looked into using the getClass method and recursively loop through the fields until i get to primitive data. With the primitive data, i can put it directly into the ByteBuffer using the respective put method. This is by far the most robust method (in my opinion), but that seems more difficult than just using serializing.

EDIT: The vertex buffer class is supposed to store vertex data as bytes in a ByteBuffer (or some other extension.) The vertex data class should inherit a "getVertexDeclaration" method that tells the vertex buffer how the bytes should be used. For example:

public class ColoredTexturedVertex implements Vertex
{
    public Vector3 Position;
    public Vector2 TexCoord;
    public int Color;

    public VertexDeclaration getVertexDeclaration()
    {
         return VertexDeclaration.createFromArray(new VertexElement[] {
             new VertexElement(0, 4 * 3, Usage.Position),
             new VertexElement(12, 4 * 2, Usage.TexCoord),
             new VertexElement(20, 4 * 1, Usage.Color)
             });
    }
}

when passed into the vertex buffer, this class will tell the buffer that it is a vertex with position, texture coordinate, and color data. The VertexDeclaration reflects the fields of the class. The first 12 bytes (the Vector3) will be used as pos开发者_StackOverflowition data. The next 8 bytes (the Vector2) will be used as texture coordinates. The last 4 bytes will be used as color data.

Getting back to my question, i simply want to isolate these bytes for storage within a vertex buffer. The reason why i need them in a ByteBuffer is because glDrawElements requires a Buffer to be passed in with its other parameters.

I don't want to restore the vertex to original state, so i don't need all the extra stuff that serializing churns out when it creates the bytes.

I can get along with just using float arrays and a FloatBuffer, but if i can figure this out that would simplify the process of using different types of vertexes.


Java Serialization is using a special format which is made to be read by Java, and which allows restoring objects of same structure (and content) as the written ones. This is not what you want here, if you have some specific receiver in mind. (Your question is not really clear here.)

I don't really know what your "vertex buffer" should do, but you have to design a protocol on how you want to store an "arbitrary vertex structure" object. If this is given by some interface, use the methods of this interface to store the data.

Sorry, this is a bit vague, but your question is vague, too.


The Serialization doesn't put just the floats to the output stream: it also puts some information about the Vertex class so the JVM knows which class should be instantiated when the deserialization occurs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜