Beginner Problems with Index Buffers

Started by
6 comments, last by dpro 17 years, 11 months ago
I am honestly stumped, I fixed my earlier question, however I cannot seem to get this to work. All I am trying to do is render a relatively simple terrain concept. NOTE: testcol = testrow = 4;

...
float boundsx = 20.0;
float boundsz = 20.0;

void fillVerts()
{
	
	//use tilesize as 10.0
	float tileSize = 10.0;
	char stuff[2000];
	int tCount = 0;
	// 20 is the number of tiles we want
	int i = 0;
	D3DXVECTOR3 zerod( 0, 0, 0 );
	for( int in = 0; in < vtxSize; in++ )
	{
		vertices.pos = zerod;
	}
	srand( timeGetTime( ) );

	float stepX = (boundsx*2) / testcol;
	float stepZ = (boundsz*2) / testcol;
	float startX = boundsx;
	float startZ = -boundsz;

	DWORD color = D3DCOLOR_ARGB( 255, 255,255,255);	
	float x,y,z;
	z = startZ;
	for( int zind = 0; zind <= 4; zind++ )
	{
		
		x = startX;
	
		for( int xind = 0; xind <= 4; xind++ )
		{
			if( xind == 0 || xind == 4 || zind == 0 || zind == 4 )
			{
				y = 0.0;
			}
			else
			{
				y = (float)( ( rand() % 10 ) + 2 );
			}

			D3DXVECTOR3 posvec( x, y, z );
			vertices.pos = posvec; 
			vertices.color = color;
			
			i++;
			
			x -= stepX;
		}
		z += stepZ;
	}
	createIdxBuff();
}


</pre></div><!–ENDSCRIPT–>

then create the vertexbuffer, of size 5 x 5

testcol = 4, testrow = 4, numInd = 96
then I create an IndexBuffer:

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">void</span> createIdxBuff()
{
	<span class="cpp-keyword">void</span> * pbuffInd;
	hr = p_dev-&gt;CreateIndexBuffer( numInd * <span class="cpp-keyword">sizeof</span>( WORD ), NULL, D3DFMT_INDEX32, D3DPOOL_MANAGED, &amp;idxBuff, NULL );
			
	<span class="cpp-keyword">if</span>( !FAILED( hr ) )
	{
		WORD * pind = <span class="cpp-keyword">new</span> WORD[ numInd ];
		WORD x = <span class="cpp-number">0</span>, z = <span class="cpp-number">0</span>, i = <span class="cpp-number">0</span>;
		WORD ind = <span class="cpp-number">0</span>;
		<span class="cpp-keyword">int</span> counter = <span class="cpp-number">0</span>;
		<span class="cpp-keyword">int</span> wid = testcol + <span class="cpp-number">1</span>;

		<span class="cpp-keyword">for</span>( z = <span class="cpp-number">0</span>; z &lt; testcol; z++ )
		{
			<span class="cpp-keyword">for</span>( x = <span class="cpp-number">0</span>; x &lt; testrow; x++ )
			{
				<span class="cpp-comment">//first tri</span>
				pind[counter++]	= ind;
				pind[counter++]	= ind + <span class="cpp-number">1</span>;
				pind[counter++]	= ind + wid;
				<span class="cpp-comment">//second tri</span>
				pind[counter++]	= ind + wid;
				pind[counter++]	= ind + <span class="cpp-number">1</span>;
				pind[counter++]	= ind + wid + <span class="cpp-number">1</span>;

				ind++;
			}
			ind++;
		}
		
		hr = idxBuff-&gt;Lock( <span class="cpp-number">0</span>, numInd * <span class="cpp-keyword">sizeof</span>( WORD ), &amp;pbuffInd, <span class="cpp-number">0</span> );
		<span class="cpp-keyword">if</span>( !FAILED( hr ) )
		{
			<span class="cpp-keyword">if</span>( memcpy( pbuffInd, pind, numInd * <span class="cpp-keyword">sizeof</span>( WORD ) ) )
			{
				idxBuff-&gt;Unlock();
			}
		}
		
		<span class="cpp-keyword">delete</span>[] pind;
		pind = NULL;
	}
	<span class="cpp-keyword">else</span>
	{
		<span class="cpp-keyword">char</span> buf[<span class="cpp-number">2048</span>];
		sprintf( buf, <span class="cpp-literal">"Error: %s"</span>, DXGetErrorDescription9( hr ) );
	}

}


</pre></div><!–ENDSCRIPT–>

finally we render

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
…
hresult = p_dev-&gt;SetStreamSource( <span class="cpp-number">0</span>, vtxbuffer, <span class="cpp-number">0</span>, <span class="cpp-keyword">sizeof</span>( vtx ) );

…
p_dev-&gt;SetIndices( idxBuff );
hr = p_dev-&gt;DrawIndexedPrimitive( D3DPT_TRIANGLELIST, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, totalVerts, <span class="cpp-number">0</span>, numPolys * <span class="cpp-number">2</span> );


</pre></div><!–ENDSCRIPT–>

I should get something like a grid, but instead I get messed up triangles.  Anyone able to offer help? Or see something I am doing thats dumb?  Thanks in advance.
Advertisement
being half asleep, and not really having used d3d before, and not really reading your code.... are u sure u are meaning to use D3DPT_TRIANGLELIST ? secondly, if u do mean to, then are u sure the the second triangle that creates the other half of the quad has the vertex's submitted in the correct order (clockwise / anti-clockwise... have u rotated what u are drawing to see if the other side is showing what u mean to have on the front side?). just something to think about, but back to delerium for me.

bye from bris-vegas. N-B
Thanks for the reply, I am pretty sure I am winding in the correct order, I am following a tutorial fairly closely, and they use trianglelists as well, so I am thinking thats a good thing. As for the quads matching up, thats also something I keep seeing repeated in the tutorials so I am either not understanding or I am just totally off somewhere else. However, right now I am not seeing a lot of benefit, so if someone sees something else for fixing be more than welcome, I just cannot see it right now. Perhaps I am just missing something small.
Hopefully this isn't against the rules, but I have new info on this problem.

I do not know why but for whatever reason I moved the vertex buffer creation and locking code into the fillVerts function.

It now works much better than before, but I have no idea why.

old code =
void fillVerts(){	//use tilesize as 10.0	float tileSize = 10.0;	char stuff[2000];	int tCount = 0;	//float zmax = -10000000.00;	// 20 is the number of tiles we want	/*float xStart = (float)( 0.0 - ( tSize / 2.0 ) );	float xEnd = (float)(( tSize / 2.0 ) );	float zStart = (float)( 0.0 - ( tSize / 2.0 ) );	float zEnd = (float)(( tSize / 2.0 ) );*/	int i = 0;	int ix = 0;	D3DXVECTOR3 zerod( 0, 0, 0 );	for( int in = 0; in < vtxSize; in++ )	{		vertices.pos = zerod;	}	srand( timeGetTime( ) );	float stepX = (boundsx*2) / m_cols;	float stepZ = (boundsz*2) / m_rows;	float startX = -boundsx;	float startZ = boundsz;	DWORD color = D3DCOLOR_ARGB( 255, 255,255,255);		float x,y,z;	z = startZ;	for( int zind = 0; zind <= m_rows; zind++ )	{		//z = zf[ zind ];		x = startX;			for( int xind = 0; xind <= m_cols; xind++ )		{			if( xind == 0 || xind == m_cols-1 || zind == 0 || zind == m_rows-1 )			{				y = 0.0;			}			else			{				y = (float)( ( rand() % 10 ) + 2 );				//y = 0.0;			}			D3DXVECTOR3 posvec( x, y, z );			vertices.pos = posvec; <br>			vertices.color = color;<br>			<br>			i++;<br>			<br>			x += stepX;<br>		}<br>		z -= stepZ;<br>	}<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>new code<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-keyword">void</span> fillVerts()<br>{<br>	<br>	<span class="cpp-comment">//use tilesize as 10.0</span><br>	<span class="cpp-keyword">float</span> tileSize = <span class="cpp-number">10</span>.<span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">char</span> stuff[<span class="cpp-number">2000</span>];<br>	<span class="cpp-keyword">int</span> tCount = <span class="cpp-number">0</span>;<br>	<span class="cpp-comment">// 20 is the number of tiles we want</span><br>	<span class="cpp-comment">/*float xStart = (float)( 0.0 - ( tSize / 2.0 ) );<br>	float xEnd = (float)(( tSize / 2.0 ) );<br>	float zStart = (float)( 0.0 - ( tSize / 2.0 ) );<br>	float zEnd = (float)(( tSize / 2.0 ) );*/</span><br>	<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>;<br>	<span class="cpp-keyword">int</span> ix = <span class="cpp-number">0</span>;<br>	D3DXVECTOR3 zerod( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span> );<br>	<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> in = <span class="cpp-number">0</span>; in &lt; vtxSize; in++ )<br>	{<br>		vertices<span style="font-weight:bold;">.pos = zerod;<br>	}<br>	srand( timeGetTime( ) );<br><br>	<span class="cpp-keyword">float</span> stepX = (boundsx*<span class="cpp-number">2</span>) / m_cols;<br>	<span class="cpp-keyword">float</span> stepZ = (boundsz*<span class="cpp-number">2</span>) / m_rows;<br>	<span class="cpp-keyword">float</span> startX = -boundsx;<br>	<span class="cpp-keyword">float</span> startZ = boundsz;<br><br>	DWORD color = D3DCOLOR_ARGB( <span class="cpp-number">255</span>, <span class="cpp-number">255</span>,<span class="cpp-number">255</span>,<span class="cpp-number">255</span>);	<br>	<span class="cpp-keyword">float</span> x,y,z;<br>	z = startZ;<br>	<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> zind = <span class="cpp-number">0</span>; zind &lt;= m_rows; zind++ )<br>	{<br>		<span class="cpp-comment">//z = zf[ zind ];</span><br>		x = startX;<br>	<br>		<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> xind = <span class="cpp-number">0</span>; xind &lt;= m_cols; xind++ )<br>		{<br>			<span class="cpp-keyword">if</span>( xind == <span class="cpp-number">0</span> || xind == m_cols-<span class="cpp-number">1</span> || zind == <span class="cpp-number">0</span> || zind == m_rows-<span class="cpp-number">1</span> )<br>			{<br>				y = <span class="cpp-number">0</span>.<span class="cpp-number">0</span>;<br>			}<br>			<span class="cpp-keyword">else</span><br>			{<br>				y = (<span class="cpp-keyword">float</span>)( ( rand() % <span class="cpp-number">10</span> ) + <span class="cpp-number">2</span> );<br>				<span class="cpp-comment">//y = 0.0;</span><br>			}<br><br>			D3DXVECTOR3 posvec( x, y, z );<br>			vertices.pos = posvec; <br>			vertices.color = color;<br>			<br>			i++;<br>			x += stepX;<br>		}<br>		z -= stepZ;<br>	}<br>	<span class="cpp-comment">//MOVED BUFFER CREATION AND LOCKING HERE INSTEAD OF OUTSIDE FUNCTION</span><br>	hr = p_dev-&gt;CreateVertexBuffer( totalVerts * <span class="cpp-keyword">sizeof</span>( vtx ), D3DUSAGE_WRITEONLY, vtxfvf, D3DPOOL_MANAGED, <br>		&amp;vtxbuffer, NULL );<br><br>	<span class="cpp-keyword">if</span>( FAILED( hr ) )<br>	{<br>		<span class="cpp-keyword">char</span> buf[<span class="cpp-number">2048</span>];<br>		sprintf( buf, <span class="cpp-literal">"Found the error: %s"</span>, DXGetErrorDescription9( hr ) );<br>	}<br>	<span class="cpp-keyword">else</span><br>	{<br><br>		<span class="cpp-keyword">void</span> *data;<br>		hr = vtxbuffer-&gt;Lock( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, &amp;data, D3DLOCK_DISCARD );<br>		<span class="cpp-keyword">if</span>( !FAILED( hr ) )<br>		{<br>			<span class="cpp-keyword">if</span>( !memcpy( data, vertices, totalVerts * <span class="cpp-keyword">sizeof</span>( vtx ) ) )<br>			{<br>				<span class="cpp-keyword">int</span> pasnfias = <span class="cpp-number">34</span>;<br>			}<br>		}<br>		hr = vtxbuffer-&gt;Unlock();<br>	}<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>Suddenly this move of code seems to have largely fixed it.  Vertices was a globally defined buffer in the .h file, so I cannot see why this would be any different.  Anyone got any ideas &#111;n this?  This is very very odd to me, but perhaps I have been looking at this too long to see whats wrong.  Any ideas would be aweomse, thanks again!<br>
Hey dpro hello :)
I'd like to comment on your Index creation method. From the line below:
hr = p_dev->CreateIndexBuffer( numInd * sizeof( WORD ), NULL, D3DFMT_INDEX32, D3DPOOL_MANAGED, &idxBuff, NULL );


I see a big problem.

Unless on your machine your WORD is a 32-bits value (which I doubt ^^) you are creating an index buffer which is actually half the index-count you want. Say you want 100 indices and your WORD is a 2 byte value ( = 16 bits ). You will end up with a 200 bytes Index buffer. But since you created it with D3DFMT_INDEX32, DX will think that you'll fill it with 4 bytes values, and he'll consider your buffer is then a 200 / 4 = 50 indices buffer :) Too bad heh ?

direct quote from the SDK documentation:
Quote:
I notice that 32-bit indices are a supported type; can I use them on all devices?
No. You must check the D3DCAPS9::MaxVertexIndex field to determine the maximum index value that is supported by the device. This value must be greater than 2 to the 16th power -1 (0xffff) in order for index buffers of type D3DFMT_INDEX32 to be supported. In addition, note that some devices may support 32-bit indices but support a maximum index value less than 2 to the 32nd power -1 (0xffffffff); in this case the application must respect the limit reported by the device.


So I suggest you to use 16 bits indices wherever they would fit your needs.

Now another remark, about the way you fill up your index buffer (and maybe your vertex buffer too although I dont know what type of object your "vertices" var is). When you lock a buffer (whether it is an index or vertex buffer matters little), you retrieve a pointer to memory that directx has had ready just for you to write to it :) So why bother allocation a
new WORD[ numInd ];

Go ahead man, lock that buffer like you did:
hr = idxBuff->Lock( 0, numInd * sizeof( WORD ), &pbuffInd, 0 );

or, since you are locking the whole buffer anyway:
hr = idxBuff->Lock( 0, 0, &pbuffInd, 0 );

and use pbuffInd as if you had new'd it (of course do not delete[] it ^^ I think it's obvious heh ? :p)

Well, this is what I cans say so far about your code, try to fix that up a shoot again. Let us know about it is working or not.

And If I said some bullshit, shame on me till the end of time ;)
Good points :) Changing those things turned out to be the key!

Also thats an excellent point about using the pointer already created by d3d :)
I always saw it that way and figured that was the only way to do it. No bull from you at all :) good points all around and thanks again, frustration always makes finding a problem harder.

Well I could delete the pointer given to me by D3D, but I don't wanna make it harder on myself :)
Quote:Original post by dpro
Well I could delete the pointer given to me by D3D, but I don't wanna make it harder on myself :)

Yeah go ahead :) And you'll be back here pretty soon :p
Quote:Original post by janta
Quote:Original post by dpro
Well I could delete the pointer given to me by D3D, but I don't wanna make it harder on myself :)

Yeah go ahead :) And you'll be back here pretty soon :p


Lol I know better than that, I'm not as incompetent as I seem ;) Thanks again for the pointers, I took up D3D a while ago... then dropped it, but I never got far into it as I kept on getting busy with other projects sadly.

This topic is closed to new replies.

Advertisement