noNchaoTic

Is it possible to get the graphics device inside the content processor, to create vertex buffers, etc.


Re: XNA Framework Device inside a content processor?

Bjoern Graf

Let's start with a "why would you want to create a VB, etc " first :)



Re: XNA Framework Device inside a content processor?

noNchaoTic

I have figured out how to do it now, the reason is I am going to write a processor that takes an arbitrary 3D model and produces a QuadTree. At the leaf nodes of the tree will be Mesh classes which will contain parts and of course a VB/IB pair.

For those interested:
       
IGraphicsDeviceService GraphicsDeviceService = (IGraphicsDeviceService)Input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
 




Re: XNA Framework Device inside a content processor?

Johnnylightbulb

Yes, but you don't need a device to do that. The ContentTypeWriter for your type should write out VertexBufferContent and IndexCollection types. Then, when reading them back in using your ContentTypeReader, let XNA create 'real' vertex & index buffers.

You won't need a device to create VertexBufferContent or IndexCollection types, which is nice because that happens off-line anyways.

Write like:

output.WriteObject<VertexBufferContent>(value.BuildVertexBuffer());

output.WriteObject<IndexCollection>(value.BuildIndexCollection());

and read like:

VertexBuffer vertexBuffer = input.ReadObject<VertexBuffer>();

IndexBuffer indexBuffer = input.ReadObject<IndexBuffer>();





Re: XNA Framework Device inside a content processor?

noNchaoTic

Awesome I didn't know this... the docs for the content stuff aren't the best. Thanks so much.




Re: XNA Framework Device inside a content processor?

noNchaoTic

If I had a class that contained a VertexBufferContent instance and a IndexCollection, and I wanted to supply a draw method for it that handled drawing those with the graphcis device, how would I do that




Re: XNA Framework Device inside a content processor?

Johnnylightbulb

I dont think you would - In my case I have a 'Level' class with a vertex buffer, etc. It has methods for drawing and all the things I need to do at runtime. On the content processor side, I'm creating and filling a different class ('LevelContent' in my case) that has VertexBufferContent and IndexCollection members. In addition, all the arrays and such are List<> types instead of [] types to facilitate lookups and adding things dynamically.

My writer class specifies the reader of the other type, so it's not like 'Level' in, 'Level' out - more like:

(During compile time)

- Import a map as Leadwerks14Content

- Process Leadwerks14Content to LevelContent

- Write LevelContent to XNB

(And then during runtime)

- Read Level from XNB

- Draw, Collide, SetEffectX, etc.

(Leadwerks14Content is the base type for the actual format my levels are stored in)





Re: XNA Framework Device inside a content processor?

noNchaoTic

I thought about this last night, reading the data in, in the ContentTypeReader and putting it in a different type (class) to return to the client. In that different class (which would contain VB/IB, rather than the content/collection versions), I could easily supply a draw method.

Now I just need to figure out the best way to serialize a tree :-)




Re: XNA Framework Device inside a content processor?

Johnnylightbulb

I don't know if it's the same situation you're against, but I'm serializing my bsp tree by writing out the front and back nodes like this:

(No compiler handy right now, psuedo code:)

class BspNodeWriter : ContentTypeWriter<BspNode>{

public Write(ContentWriter output, BspNode value)
{

output.WriteObject<BspNode>(value.FrontChild);

output.WriteObject<BspNode>(value.BackChild);
}
}

I haven't had any problems with type writers calling themselves, it all just works :)





Re: XNA Framework Device inside a content processor?

noNchaoTic

Yeah, but say I have a class that contains the content of a node with the VertexBufferContent and such in, and another class which will be used in the game, which contains the actual compiled VertexBuffers, etc. Assuming there is a tree structure of the first type, how do you build the equivalent tree in the 2nd type




Re: XNA Framework Device inside a content processor?

Johnnylightbulb

I decompiled (via Reflector) the XNA types to get an idea of how they serialize using the ContentTypeReader/Writer classes - That's when I realized that the readers & writers don't have to use the same types. When you're writing a VertexBufferContent class, what's written to disk in the XNB is essentially each vertex in order. When reading a VertexBuffer class from the XNB, it just gets the GraphicsDevice from the Services of the reader, creates a VB instance, then reads all vertexes, in order, from the XNB and into the VB.

Similarly, if you write a tree structure from your content types to an XNB, you can read them into any other object, such as your runtime types, so long as they read the same bytes.

So if you write this: byte, float, float, VertexBufferContent, boolean, string, string, byte, float, float, VertexBufferContent, boolean, string string

You should be able to read those values into any classes that read those types in that order. I might have misunderstood what you were asking.





Re: XNA Framework Device inside a content processor?

noNchaoTic

Yeah I figured what you just posted. I think it was an issue I was having with cyclic in the graph, I.e.: back edges going to the parent node, which aren't really needed. So if I remove those, it should serialize fine, if I have a type Node, which contains:

VertexBufferContent
IndexCollection
Node[4]

And I serialize those, I should be able to read back the tree fine