Subscribe to The Crispy Blog        RSS Feed
-----

DS Homebrew Development - Part 3

Icon Leave Comment
In my previous post I posted my proposed class design for wrapping up the horrible display list code used in libnds. It has taken a lot longer than I expected to get this class working correctly due to other projects taking precedence for a while.That said I have finished finally and it works great for the most part.

One headache I had was due to the way the display lists are set out in libnds

FIFO_COMMAND_PACK(FIFO_VERTEX16, FIFO_COLOR, FIFO_VERTEX16, FIFO_COLOR)



A pack like this means that the next four values are going to be a vertex, colour, vertex and another colour. There are many different values that can be passed into the COMMAND_PACK such as normals and texture coordinates. The plan was to make it so that a COMMAND_PACK would be automatically added rather than a user having to explicitly call the COMMAND_PACK.

The solution was to create a vector to push temporary values into and also another vector which contained the commands to put into the COMMAND_PACK. The COMMAND_PACK values are really just #defines for int's.

std::vector<u32> m_tempDataTypes;
std::vector<u32> m_tempValues;



Now whenever a user adds a vertex or any other value then its type gets pushed into the m_tempDataTypes vector and its values get pushed into the m_tempValues vector, also a counter is incremented. Whenever the counter reaches 4 then the pack is created from the values in m_tempDataTypes and pushed into the main list. Then the next 4 values in the main display list are added from the m_tempValues vector.
Here are the AddVertex and AddPack functions for an example:

void DisplayList::AddVertex(float x, float y, float z)
{
	m_tempValues.push_back(VERTEX_PACK(floattov16(x), floattov16(y)));
	m_tempValues.push_back(VERTEX_PACK(floattov16(z), floattov16(z)));
	m_tempDataTypes.push_back(FIFO_VERTEX16);
	m_tempListCount++;

	if(m_tempListCount == 4)
	{
		AddPack();
	}
}



void DisplayList::AddPack()
{
        // this loop makes sure that the pack gets padded out if there are not 4 values in it at the time
        // according to the documentation for libnds FIFO_END has no use so I used it to pad out pack without it expecting any values   
	for (int i = m_tempDataTypes.size(); i < 4; i++)
	{
		m_tempDataTypes.push_back(FIFO_END);
	}

	m_list.push_back(FIFO_COMMAND_PACK(m_tempDataTypes[0], m_tempDataTypes[1], m_tempDataTypes[2], m_tempDataTypes[3]));
	m_tempDataTypes.clear();

	m_listCount++;

	for (int i = 0; i < m_tempValues.size(); i++)
	{
		m_list.push_back(m_tempValues[i]);
		m_listCount++;
	}
	m_tempValues.clear();

    m_tempListCount = 0;
}



So my mesh drawing code now uses this display list class which is a lot faster and more efficient. It shows just how slow using GL_BEGIN and GL_END is when it is quicker to create a display list each frame than use it :s

DisplayList meshList;
int numTris = tris.size();
meshList->AddBeginTriangles();
for (int i = 0; i < numTris; i++)
{
    const Tri& tri = tris[i];
    for (int j = 0; j < 3; j++)
    {
       const Vert& vert = tri.m_verts[j];
       meshList->AddTexCoords(vert.m_u, vert.m_v);
       meshList->AddNormals(vert.m_nx,vert.m_ny,vert.m_nz);
       meshList->AddVertex(vert.m_x, vert.m_y, vert.m_z);
     }
}
meshList->End();
glCallList(&meshList.m_list[0]);



Thanks for reading, I should have some screen shots soon :)

0 Comments On This Entry

 

July 2014

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728 29 3031  

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)