• 9
• 10
• 10
• 9
• 10

# Create flexible vertex formats

This topic is 3448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know how to create custom FVF's in vb.net or c#? I've been researching this for quite a while but only turn up info for c++. All I really want is a vertex format that has position and two texture UV's. Is that so hard to ask?

##### Share on other sites
Typically it's done by "OR"ing two FVF codes together. Since the bits don't overlap, addition works just as well.

I found a c# example fairly quickly by searching for "fvf c#". Look here for how they define their structure. From there it's pretty easy to modify. Add a second set of UV coords the structure right after the first set, and change "VertexFormats.Texture1" to "VertexFormats.Texture2".

Note that the example is broken as is. The FVF doesn't have normals, but the structure does. You'll want to strip the normals out of the structure to make it work. I'm "commenting" on the page, which hopefully means someone will fix it.

##### Share on other sites
Quote:
 Original post by NamethatnobodyelsetookTypically it's done by "OR"ing two FVF codes together. Since the bits don't overlap, addition works just as well.I found a c# example fairly quickly by searching for "fvf c#". Look here for how they define their structure. From there it's pretty easy to modify. Add a second set of UV coords the structure right after the first set, and change "VertexFormats.Texture1" to "VertexFormats.Texture2".Note that the example is broken as is. The FVF doesn't have normals, but the structure does. You'll want to strip the normals out of the structure to make it work. I'm "commenting" on the page, which hopefully means someone will fix it.

This helps quite a bit, but I still have a problem. Maybe it's with the actual rendering, I don't know. I can get the zero texture stage to correctly work, but the one texture stage does nothing (it has the exact same effect as using "nothing")

Here's the FVF structure:
Public Structure MyFVF    '<-->'Positions    Public x As Single    Public y As Single    Public z As Single    Public tu0 As Single    Public tv0 As Single    Public tu1 As Single    Public tv1 As Single    Shared FVF As VertexFormats = VertexFormats.Position Or VertexFormats.Texture2    Public Sub New(ByVal ix As Single, ByVal iy As Single, ByVal iz As Single, ByVal itu0 As Single, _                ByVal itv0 As Single, ByVal itu1 As Single, ByVal itv1 As Single)        Me.x = ix        Me.y = iy        Me.z = iz        Me.tu0 = itu0        Me.tv0 = itv0        Me.tu1 = itu1        Me.tv1 = itv1    End SubEnd Structure

The render method:
Private Sub FrameRender()        '<-->'0. Variables        '<-->'1.  Clear Target        D3DDev.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Drawing.Color.FromArgb(0, 55, 55), 1.0F, 0)        '<-->'2.  Begin Scene        D3DDev.BeginScene()        '<-->'3.  Render Objects        '<->'Render terrain        For Each oTile As SEFT_Tile In _terrain.Tiles            D3DDev.SetStreamSource(0, oTile.vbTile, 0)            D3DDev.VertexFormat = MyFVF.FVF            D3DDev.SetTexture(0, _terrain.GetTexById(0))            D3DDev.DrawPrimitives(PrimitiveType.TriangleList, 0, oTile.Primitives)        Next

My RenderState and SamplerState Stuff:
        D3DDev.RenderState.ZBufferEnable = True        D3DDev.RenderState.Lighting = False        D3DDev.RenderState.SourceBlend = Blend.SourceAlpha        D3DDev.RenderState.DestinationBlend = Blend.InvSourceAlpha        D3DDev.RenderState.AlphaBlendEnable = True        D3DDev.SamplerState(0).MinFilter = TextureFilter.Linear        D3DDev.SamplerState(0).MagFilter = TextureFilter.Linear        D3DDev.SamplerState(0).AddressU = TextureAddress.Clamp        D3DDev.SamplerState(0).AddressV = TextureAddress.Clamp        D3DDev.SamplerState(0).AddressW = TextureAddress.Clamp        D3DDev.SamplerState(1).MinFilter = TextureFilter.Linear        D3DDev.SamplerState(1).MagFilter = TextureFilter.Linear        D3DDev.SamplerState(1).AddressU = TextureAddress.Border        D3DDev.SamplerState(1).AddressV = TextureAddress.Border        D3DDev.RenderState.CullMode = Cull.None

The vertexformat appears to be working how it should in every other sense. I just can't figure out why it isn't working for the second texture.

Any idea what I'm doing wrong?

##### Share on other sites
Quote:
 Original post by NewtonsBitPrivate Sub FrameRender() D3DDev.SetTexture(0, _terrain.GetTexById(0))

Looks like you just forgot to set the second texture.

Oh, and use "source" blocks, rather than "code" blocks when posting large chunks of code here. You'll get one of those nice scrollable windows. I think you can even specify the language and have it highlight correctly.

##### Share on other sites
Quote:
Original post by Namethatnobodyelsetook
Quote:
 Original post by NewtonsBitPrivate Sub FrameRender() D3DDev.SetTexture(0, _terrain.GetTexById(0))

Looks like you just forgot to set the second texture.

Oh, and use "source" blocks, rather than "code" blocks when posting large chunks of code here. You'll get one of those nice scrollable windows. I think you can even specify the language and have it highlight correctly.

That was just my working code. I can set the second texture and nothing happens at all. The polygons are just white (which is not what the texture is)

##### Share on other sites
FVFs are actually super duper outdated\deprecated. You instead want to be using vertex declarations, as they are much more programmable pipeline friendly, unless of course you HAVE to use FVFs because of some legacy code. Actually, the current implementation of FVFs convert into vdecls behind the scene.

Here's an MSDN example of creating a vertex declaration in C#
http://msdn.microsoft.com/en-us/library/bb153284(VS.85).aspx

Hope that helps,
~Graham