Problem rendering with vertex buffer

Hi, I'm trying to draw a textured square to the screen using ManagedDX 1.1. Unfortunately, the DrawPrimitives call is giving me an exception, but all the error message says is "Error in the application" which is not very helpful. I really have no idea what is wrong, because this was working before and it's stopped working when I was reorganizing the code in my classes. I've pasted the two functions which I think are relevant, if anyone has any suggestions I would greatly appreciate it.

(also, the code below seems like a terrible way to update the data in the vertex buffer, if anyone could point me to a "more correct" way of doing things that would be good too :)

publicoverridevoid Draw(Point dest_pt)

{

UpdateVertexBuffer(dest_pt.X, dest_pt.Y);

mDevice.RenderState.AlphaBlendEnable =true;

mDevice.RenderState.SourceBlend =Blend.SourceAlpha;

mDevice.RenderState.DestinationBlend =Blend.InvSourceAlpha;

mDevice.SetTexture(0, mTexture);

mDevice.TextureState[0].AlphaArgument1 =TextureArgument.TextureColor;

mDevice.TextureState[0].AlphaArgument2 =TextureArgument.Diffuse;

mDevice.TextureState[0].AlphaOperation =TextureOperation.Modulate;

mDevice.SetStreamSource(0, mVertices,0);

mDevice.DrawPrimitives(PrimitiveType.TriangleStrip,0,2);

}

protectedvoid UpdateVertexBuffer(int dest_x,int dest_y)

{

if (mVertices ==null)

{

mVertices = CreateVertexBuffer(0,0);

}

CustomVertex.TransformedColoredTextured[] verts =

(CustomVertex.TransformedColoredTextured[]) mVertices.Lock(0,0);

// find center

Point centerpt =Origin.Calc(mOwner.DisplayAlignment, mOwner.DisplaySize);

Point rotation =Origin.Calc(mOwner.RotationCenter, mOwner.DisplaySize);

rotation.X += dest_x;

rotation.Y += dest_y;

Point[] corners =newPoint[4]

{

newPoint(dest_x, dest_y),

newPoint(dest_x + mOwner.DisplayWidth, dest_y),

newPoint(dest_x, dest_y + mOwner.DisplayHeight),

newPoint(dest_x + mOwner.DisplayWidth, dest_y + mOwner.DisplayHeight)

};

PointF[] uv =newPointF[4]

{

newPointF(0,0),

newPointF(1,0),

newPointF(0,1),

newPointF(1,1),

};

if (mOwner.DisplayWidth <0)

{

// swap points

for (int i =0; i <4; i +=2)

{

Point t = cornersIdea;

cornersIdea = corners[i +1];

corners[i +1] = t;

PointF uvt = uvIdea;

uvIdea = uv[i +1];

uv[i +1] = uvt;

}

for (int i =0; i <4; i++)

cornersIdea.X -= mOwner.DisplayWidth;

}

if (mOwner.DisplayHeight <0)

{

// swap points

for (int i =0; i <2; i +=1)

{

Point t = cornersIdea;

cornersIdea = corners[i +2];

corners[i +2] = t;

PointF uvt = uvIdea;

uvIdea = uv[i +2];

uv[i +2] = uvt;

}

for (int i =0; i <4; i++)

cornersIdea.Y -= mOwner.DisplayHeight;

}

// do rotation and translation

if (mOwner.RotationAngle !=0)

{

for (int i =0; i <4; i++)

{

RotatePointInPlace(ref cornersIdea, rotation);

TranslatePointInPlace(ref cornersIdea, centerpt);

}

}

else

{

for (int i =0; i <4; i++)

{

TranslatePointInPlace(ref cornersIdea, centerpt);

}

}

for (int i =0; i <4; i++)

{

vertsIdea =new Direct3D.CustomVertex.TransformedColoredTextured(

(float)cornersIdea.X, (float)cornersIdea.Y,0.5F,1, mOwner.Color.ToArgb(), uvIdea.X, uvIdea.Y);

}

mVertices.Unlock();

}

[12173 byte] By [kanato] at [2007-12-23]
# 1

I've been playing around with it some more, and I thought I would provide some more information.

The first pass through the Device.BeginScene()..Device.DrawPrimitives()..Device.EndScene() routines apparently works.. or at least it doesn't crash. DrawPrimitives is called twice here. It draws the triangles in the correct location, but does not apply the texture, or the color information in the vertices. After that, it crashes on the next DrawPrimitives call. If I don't draw primitives, I can use a Direct3D.Line object just fine. In fact, I can draw lines before drawing trianglestrips, and then if I try to draw lines after that, it crashes there too.

This apparently has something to do with my vertex buffer, although I don't understand what. I can comment out the lines all the lines which render the texture, including the ones that set the vertex stream, and it still crashes. It's only when I comment the line calling the function which creates/updates the vertex buffer that it doesn't crash anymore. Of course, I don't get any textured objects drawn to the screen, but I can draw lines.

Edit: Well, if I forgo the vertex buffer entirely, the program works without crashing, but I don't get any textures to show up? What am I missing? (well, drawing lines still crashes.)

kanato at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...