MDX C# IndexBuffers

Started by
6 comments, last by Xrystal 17 years, 8 months ago
I am converting my 1.1 application over to 2.0. I finally got it to compile but it is now failing on my DrawIndexedPrimitives call. The error message is unhelpful "error in application". Here is where it is failing upon execution: private void DrawIHex(float x, float y, float z, Texture t) { device.Transform.World = Matrix.Translation(x, y, z); //fill hex with texture device.VertexFormat = PositionTextured.Format; device.SetTexture(0, t); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 7, 0, 6); } //index buffer for hex private static readonly short[] hexindices = {0,1,2, 0,2,3, 0,3,4, 0,4,5, 0,5,6, 0,6,1 }; This is my new vertexbuffer creation function: private void OnVertexBufferCreate(object sender, EventArgs e) { VertexBuffer buffer = (VertexBuffer)sender; //buffer.SetData(GetHexVertsIB(),0,LockFlags.None); GraphicsBuffer<PositionTextured> stm = buffer.Lock<PositionTextured>(0, 0, LockFlags.None); stm.Write(GetHexVertsIB()); buffer.Unlock(); } This is my "new" indexbuffer creation function: private void OnIndexBufferCreate(object sender, EventArgs e) { IndexBuffer buffer = (IndexBuffer)sender; //buffer.SetData(hexindices, 0, LockFlags.None); GraphicsBuffer stm = buffer.Lock<short>(0, 0, LockFlags.None); stm.GetBuffer<short>().Write(hexindices); buffer.Unlock(); } Here is the verts function used above: public PositionTextured[] GetHexVertsIB() { //fewer vertexes for use in an index buffer //create a vertex buffer for Hex //make a flat hex. 6 triangles PositionTextured[] verts = new PositionTextured[7]; verts[0] = new PositionTextured(0.0f,0.0f,1.0f,0.5f, 0.5f); verts[1] = new PositionTextured(0.0f,2.0f,1.0f,0.0f, 0.5f); verts[2] = new PositionTextured(-1.732051f,1.0f,1.0f,0.25f, 0.0f); verts[3] = new PositionTextured(-1.732051f,-1.0f,1.0f,0.75f, 0.0f); verts[4] = new PositionTextured(0.0f,-2.0f,1.0f,1.0f, 0.5f); verts[5] = new PositionTextured(1.732051f,-1.0f,1.0f,0.75f, 1.0f); verts[6] = new PositionTextured(1.732051f,1.0f,1.0f,0.25f, 1.0f); return verts; }//end gethexverts
Advertisement
Can't say for sure. Nothing jumps right out at me, but maybe the name of the exception thrown would give a clue as to what the problem might be.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
I don't see a call to SetStreamSource() or an assignment to device.Indices.
Thanks for the replies.

I call my SetStreamSource and Indices in my OnPaint override. i.e.

device.SetStreamSource(0, vb, 0);
device.Indices = ibHex;

As for error messages I don't get much. I just installed VS 2005 as well so I am still getting used to the debugging environment. The error window comes up with a "InvalidCallException was unhandled" at my device.BeginScene . I am using the general try/catch block at the DrawIndexedPrimitives line but am only getting a "Error in application".

Thanks for the help so far.

Rob
Still not working.

I am getting a message in the debug output window:

A first chance exception of type 'Microsoft.DirectX.Direct3D.InvalidCallException' occurred in Microsoft.DirectX.dll

This doesn't mean much to me though.

I also tried removing the index buffers and just use plain vertex buffers and got the same error. Just to reiterate, this all worked fine under Framework 1.1.

I am doing this in my init graphics function. Any of this look wrong?

//vertex buffer
vb = new VertexBuffer(device, 7, Usage.Dynamic | Usage.WriteOnly, PositionTextured.Format, Pool.Default, new EventHandler (this.OnVertexBufferCreate));
OnVertexBufferCreate(vb, null);

//index buffer
ibHex = new IndexBuffer(device, hexindices.Length, Usage.WriteOnly, Pool.Default, false, new EventHandler(OnIndexBufferCreate));
OnIndexBufferCreate(ibHex, null);
Try using the 4 argument overload of SetStreamSource. The 3 overload version only works with the VertexBuffer generic class.

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Thanks for the reply. That didn't fix the problem. I am going to start from scratch and try building a basic triangle. Maybe I can narrow down the problem that way.
Sounds like we are getting similar problems. Except in Managed C++. Is it possible the Managed DirectX code is buggy?

Have you tested this in windowed and fullscreen mode ? I seem to only get that error when I am using indices in conjunction with fullscreen.

Test 1 : Windowed Mode, With Indices, With Buffer - All fine
Test 2 : Windowed Mode, With Indices, Without Buffer - To Test
Test 3 : Fullscreen Mode, With Indices, With Buffer - Crash or error message or PC lock
Test 4 : Fullscreen Mode, With Indices, Without Buffer - To Test
Test 5 : Fullscreen Mode, Without Indices, With Buffer - Fine
Test 6 : Fullscreen Mode, Without Indices, Without Buffer - To Test

Will test the others to be sure and then will report back.

FYI: C++ 2003, Managed DirectX April 2006


Index Buffer Creation Code - m_indices is an Int32 array created when the DXPrimitive class is constructed. I pass the number of indices to the constructor. m_ib is the classes IndexBuffer. I am currently coding with just 3 individual triangles so don't need to use indices but obviously making sure that something more complex will work fine when using the routine.
bool DXPrimitive::CreateIndexBuffer(void){	if (m_indexCount > 0)	{		int idxsize = m_indices->Length * sizeof(Int32);		try		{			m_ib = __gc new IndexBuffer(__box(m_indices[0])->GetType(),idxsize,m_device,Usage::WriteOnly,Pool::Managed);		}		catch (InvalidCallException* ice)		{			m_error = ice->Message;			return false;		}		catch (InvalidDataException* ide)		{			m_error = ide->Message;			return false;		}		catch (OutOfVideoMemoryException* oovme)		{			m_error = oovme->Message;			return false;		}		catch (OutOfMemoryException* oome)		{			m_error = oome->Message;			return false;		}		m_ib->SetData(m_indices, 0, LockFlags::None);		m_device->Indices = m_ib;	}}


This code isn't ideal but really only used for testing purposes so the Index setup code here shouldn't be a problem. The position, normal and color settings are set into the CustomVertex array I initialise along with the index array. In my code thats currently 3 vertices and 3 indices.
void DXPrimitive::SetTriangle(int arrayItem, float size, int c){	SetTriangle(arrayItem,size);	if (Enum::IsDefined(__box(m_vformat)->GetType(),__box(VertexFormats::Diffuse)))	{		SetColor(arrayItem,c);		SetColor(arrayItem+1,c);		SetColor(arrayItem+2,c);	}}void DXPrimitive::SetTriangle(int arrayItem,float size){	if (Enum::IsDefined(__box(m_vformat)->GetType(),__box(VertexFormats::Position)))	{		SetPosition(arrayItem,Vector3(0.0f,size,size));		SetPosition(arrayItem+1,Vector3(size,-size,size));		SetPosition(arrayItem+2,Vector3(-size,-size,size));	}	if (Enum::IsDefined(__box(m_vformat)->GetType(),__box(VertexFormats::Normal)))	{		SetNormal(arrayItem,Vector3(0.0f,1.0f,0.0f));		SetNormal(arrayItem+1,Vector3(0.0f,1.0f,0.0f));		SetNormal(arrayItem+2,Vector3(0.0f,1.0f,0.0f));	}	if (m_indexCount > 0)	{		m_indices[0] = arrayItem;		m_indices[1] = arrayItem+1;		m_indices[2] = arrayItem+2;	}}


This code is where the problem lies. But in fullscreen only while using indices. m_vformat is initialised alongside the arrays in the constructor code and is what decides which vertex array is used. The values in the Indexed routine are valid from what I see with startVertex = 0, countVertex = 3, primitiveType = TriangleList. As you can see the primitiveCount is arrived at from the countVertex.
bool DXPrimitive::Draw(PrimitiveType primitiveType, int startVertex, int countVertex){	if (countVertex == -1) countVertex = m_vertexCount;	int primitiveCount;	if (primitiveType == PrimitiveType::TriangleList) primitiveCount = countVertex / 3;	if (primitiveType == PrimitiveType::LineList) primitiveCount = countVertex / 2;	m_device->VertexFormat = m_vformat;	if (this->UsingBuffers)	{		m_device->SetStreamSource(0,m_vb,0);		if (m_indexCount > 0)		{			Console::WriteLine("PrimitiveType : {0}",__box(primitiveType));			Console::WriteLine("CountVertex : {0}",__box(countVertex));			Console::WriteLine("primitiveCount : {0}",__box(primitiveCount));			try			{				m_device->DrawIndexedPrimitives(primitiveType,0,0,countVertex,0,primitiveCount);			}			catch(InvalidCallException* ice)			{				m_error = ice->Message;				Console::WriteLine(m_error);				return false;			}			return false;		}		else		{			m_device->DrawPrimitives(primitiveType,startVertex,primitiveCount);		}		return true;	}	else	{		switch (m_vformat)		{			case CustomVertex::PositionNormalColored::Format:			{				if (m_indexCount > 0)				{					m_device->DrawIndexedUserPrimitives(primitiveType,0,countVertex,primitiveCount,(Object*)m_indices, false,(Object*)m_pncverts);				}				else				{					m_device->DrawUserPrimitives(primitiveType,primitiveCount,(Object*)m_pncverts);				}				break;			}			case CustomVertex::PositionNormalTextured::Format:			{				if (m_indexCount > 0)				{					m_device->DrawIndexedUserPrimitives(primitiveType,0,countVertex,primitiveCount,(Object*)m_indices, false,(Object*)m_pntverts);				}				else				{					m_device->DrawUserPrimitives(primitiveType,primitiveCount,(Object*)m_pntverts);				}				break;			}		};		return true;	}	return false;}


This topic is closed to new replies.

Advertisement