# [MDX] Invalid stride size for CustomVertex.PositionOnly?

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

## Recommended Posts

I'm mostly doing 2D stuff in MDX, but I need to draw a selection rectangle for my RTS, and I figured, doing it as a LineStrip would be most efficient. I created a class for this, and ended up with this:
    public class SelectionRectangle
{
private VertexBuffer vb;
private Engine2D engine;
private Point topLeft;
private Point bottomRight;

public SelectionRectangle(Engine2D engine)
{
this.engine = engine;

// Spawn our VertexBuffer
vb = new VertexBuffer(typeof(CustomVertex.PositionOnly), 3, engine.Device,
Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default);
vb.Created += new EventHandler(OnVertexBufferCreated);

SetRectangle(new Point(10, 10), new Point(20, 20));
}

public void SetRectangle(Point topLeft, Point bottomRight)
{
this.topLeft = topLeft;
this.bottomRight = bottomRight;

fillVertexBuffer(vb);
}

public void Render()
{
engine.Device.SetStreamSource(0, vb, 0);
engine.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}

private void OnVertexBufferCreated(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;

fillVertexBuffer(vb);
}

private void fillVertexBuffer(VertexBuffer vb)
{
// Now fill our vertexbuffer
CustomVertex.PositionOnly[] verts = new CustomVertex.PositionOnly[3];
verts[0].Position = new Vector3(topLeft.X, topLeft.Y, 1.0f);
verts[1].Position = new Vector3(topLeft.X, bottomRight.Y, 1.0f);
verts[2].Position = new Vector3(bottomRight.X, bottomRight.Y, 1.0f);
//verts[3].Position = new Vector3(bottomRight.X, topLeft.Y, 1.0f);
//verts[3].Color = Color.Red.ToArgb();

vb.SetData(verts, 0, LockFlags.None);
}
}


It throws an InvalidCallException on the DrawPrimitives() call, and in DebugView it shows this:
[4056] Direct3D9: (ERROR) :Vertex stride in stream 0 is less than in the declaration
[4056] Direct3D9: (ERROR) :DrawPrimitive failed.

From what I understand, is that the FVF size is invalid with what is stored in the VB. However, looking at the code, I can't see why that is, since I do use the same vertexformat all over the place. What is wrong here? Note: The above code uses a trianglelist for testing purposes. Toolmaker

##### Share on other sites

I believe it's pretty much mandatory to specify the vertex format prior to drawing any primitives. You shouldn't count on the vertex format being already set in previous code fragment or retained from a previous frame, since rendering meshes and sprites or a device reset may implicitly alter the vertex format. Try adding the following line at the beginning of your render method:

engine.Device.VertexFormat = CustomVertex.PositionOnly.Format;

##### Share on other sites
<3

That did the trick. Thanks for pointing out [grin]

Toolmaker

##### Share on other sites
1 more question tho: I'm now also rendering my map again, and it seems odd: Without my map, it shows the line perfectly:

However, when I do render my map aswell, the line is gone:

I tried changing the z position of the line to 0.9f or 1.0f, but that doesn't help much. Any ideas why this happens?

Toolmaker

##### Share on other sites
An alternative would be to use CustomVertex.Transformed for the rectangle's vertices. This would make sense, since the rectangle is in clip/view/screen space anyway and it would make sure there aren't any shady clip space transformations applied to your vertices. A little hack would be to disable the ZBuffer by calling engine.Device.ZBufferEnable = false; (don't forget to turn it back on after you're done with the rectangle!).

Nice to see some progress on your Dune2 clone again, multiple unit selection was indeed one of the few things the original missed. In your 2nd screenshot it looks like there shouldn't be any rectangle visible anyway btw, since the Cross and the Quad sprite are in the same place. Guess that's just nitpicking though [smile]

##### Share on other sites
Quote:
 Original post by remigiusAn alternative would be to use CustomVertex.Transformed for the rectangle's vertices. This would make sense, since the rectangle is in clip/view/screen space anyway and it would make sure there aren't any shady clip space transformations applied to your vertices. A little hack would be to disable the ZBuffer by calling engine.Device.ZBufferEnable = false; (don't forget to turn it back on after you're done with the rectangle!).Nice to see some progress on your Dune2 clone again, multiple unit selection was indeed one of the few things the original missed. In your 2nd screenshot it looks like there shouldn't be any rectangle visible anyway btw, since the Cross and the Quad sprite are in the same place. Guess that's just nitpicking though [smile]

Right now, the rectangle is hard-coded from 10,10 to 100,100. It doesn't respond to the mouse as of yet. I just wanted to make it work first, before adding responding to the mouse.

I tried disabling the ZBuffer before rendering the linestrip, and re-enabling it when done, but that doesn't seem to do much(ie. the rect still isn't there).

How much of a difference would the CustomVertex.Transformed make in this case? And could this all be a camera problem? Would it help if I change the world view/world transform?

Toolmaker

##### Share on other sites
It could be some setting you're setting to draw the terrain, but not unsetting afterwards. Theres lots of options here, just have a look at the terrain rendering code and see what might affect the rectangle code.

Hope this helps :).

##### Share on other sites
The terrain is rendered using the (D3DX)Sprite class, so I have no idea what settings it changes.

Everything is rendered using Sprite, except the rectangle, which is done by rendering a primitive.

Toolmaker

##### Share on other sites
All I can think of is these two possibilities:
1) You're drawing the terrain over the line. Are you sure you're rendering the line last?

2) Sprite is messing with one of your matrices. It shouldn't, really, unless you use the ObjectSpace flag anywhere, but it would be worth checking anyways. Try resetting you matrices to their old values before drawing again. Maybe that'll help :)

[EDIT] I also see you're using the 3 parameter overload of SetStreamSource. I'm not exactly sure about MDX1.1, but in 2.0 that one causes some "problems" :). If there's a 4 parameter overload, one with the stride in it, you might want to try that one instead.

Also, someone else might have more ideas :).

##### Share on other sites
Quote:
 Original post by sirob[EDIT] I also see you're using the 3 parameter overload of SetStreamSource. I'm not exactly sure about MDX1.1, but in 2.0 that one causes some "problems" :). If there's a 4 parameter overload, one with the stride in it, you might want to try that one instead.

You have to use the 4-parameter version if you have a type less vertex buffer. If your vertex buffer contains a format the 3-parameter version calculate the stride from this format. This is the same for MDX 1.1 and MDX 2.0

An additional reason I can see is that after drawing the map the pixel pipeline (texture, stage setup, shader) is not set correctly to render a white line.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 12
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632952
• Total Posts
3009440
• ### Who's Online (See full list)

There are no registered users currently online

×