Slight correction: vertex types do not appear to be derived from CustomVertex. In fact, I'm not even sure that they share a base class...

Cheers,
Jon.

By on 2/13/2007 10:59 AM ()

I believe the best technique is to build a strongly-typed wrapper that uses (type 'primType) to looks at the System.Type representation of the instantiation of the primitive type and checks that it is one of the set of valid vertex types.

Here's the approximate code for the strongly typed wrapper with a strongly typed SetData method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 

type VertexBuffer<'primType> =
    class

        val vertexBuffer : VertexBuffer
        new (device:Device,usage,pool,size) = 
            { vertexBuffer = 
                new VertexBuffer((type 'primType) , size, device, usage, 
                                  begin match (type 'primType) with 
                                  | ty when ty = (type CustomVertex.PositionColored) -> 
                                        CustomVertex.PositionColored.Format
                                  | ty when ty = (type CustomVertex.PositionColoredTextured) -> 
                                        CustomVertex.PositionColoredTextured.Format
                                  | ty when ty = (type CustomVertex.PositionOnly) -> 
                                        CustomVertex.PositionOnly.Format
                                  | ty when ty = (type CustomVertex.PositionNormalColored) -> 
                                        CustomVertex.PositionNormalColored.Format
                                  | ty when ty = (type CustomVertex.PositionNormalTextured) -> 
                                        CustomVertex.PositionNormalTextured.Format
                                  // etc.
                                  | _ -> failwith "unknown format type"
                                  end,
                                  pool) }
        member x.VertexBuffer = x.vertexBuffer

        member x.SetData(pts :'primType[]) = x.vertexBuffer.SetData(pts,0,LockFlags.None)
    end

              
let vertexBuffer = new VertexBuffer<CustomVertex.PositionNormalColored>(device, Usage.None,Pool.Managed,100) 
do vertexBuffer.SetData([| |])

By on 2/13/2007 11:57 AM ()

Great stuff! Thanks, Don.

By on 2/27/2007 3:08 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper