Skip to main content
gamedev.net
🔒 Locked

dynamic vertex buffer problem

Started by de_matt 1 reply 900+ views
Original Post
de_matt
de_matt
Hya, I'm having trouble getting a dynamic vertex buffer to work. Belows the render code and the vertex structure

bool CLANDSCAPE::RenderTriangle( LPDIRECT3DDEVICE9 pD3DDevice,
									const D3DXVECTOR3 &v1, const D3DXVECTOR3 &v2, const D3DXVECTOR3 &v3) {

	if( m_svbLandscape == NULL ) return false;

	// default nooverwrite
	DWORD dwLockFlag = D3DLOCK_NOOVERWRITE;

	// check for space left in the buffer
	if( m_suintVertexCount + 3 > m_suintMaxVertices ) {

		FlushBuffers( pD3DDevice );

		// discard the buffer
		dwLockFlag = D3DLOCK_DISCARD;
	}

	// append vertices to buffer
	VERTEX_LAND vertices[3];
	vertices[0].vPos = v1;
	vertices[0].diffuse = 1;
	vertices[1].vPos = v2;
	vertices[1].diffuse = 0;
	vertices[2].vPos = v3;
	vertices[2].diffuse = 0;

	VOID* pBuffer;
	if( FAILED( m_svbLandscape->Lock( m_suintVertexCount * sizeof(VERTEX_LAND), 3 * sizeof(VERTEX_LAND), &pBuffer, dwLockFlag ) ) ) {

		return false;
	}

	memcpy( pBuffer, vertices, 3 * sizeof(VERTEX_LAND) );
	m_svbLandscape->Unlock();

	//increment count of vertices in buffer
	m_suintVertexCount+=3;

	return true;
}


void CLANDSCAPE::FlushBuffers( LPDIRECT3DDEVICE9 pD3DDevice ) {

	if( !m_suintVertexCount ) return;

	// draw primitive
	pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_suintVertexCount / 3  );

	m_suintVertexCount = 0;
}


void CLANDSCAPE::Render(LPDIRECT3DDEVICE9 pD3DDevice) {

	if( m_svbLandscape == NULL ) return;

	pD3DDevice->SetStreamSource( 0, m_svbLandscape, 0, sizeof(VERTEX_LAND) );

	for( UINT i = 0; i < CLANDSCAPE_PATCHES_PER_SIDE; ++i ) {

		for( UINT j = 0; j < CLANDSCAPE_PATCHES_PER_SIDE; ++j ) {

			m_cPatches[j].Render( pD3DDevice );
		}
	}
}



Here's the vertex struct and fvf

struct VERTEX_LAND {

	D3DXVECTOR3 vPos;
	DWORD diffuse;
};

#define LANDSCAPE_FVF (D3DFVF_XYZ | D3DFVF_DIFFUSE )



Here's the code for initialising the vertex buffer

HRESULT CLANDSCAPE::initVertexBuffer( LPDIRECT3DDEVICE9 pD3DDevice ) {

	return pD3DDevice->CreateVertexBuffer( m_suintMaxVertices * sizeof(VERTEX_LAND), D3DUSAGE_DYNAMIC,
									LANDSCAPE_FVF, D3DPOOL_DEFAULT, &m_svbLandscape, NULL );
}

The diffuse value was added just to see if that was the problem. The problem is that the drawprimitive call doesn't work. I just get first-chance exceptions. At the drawprimitive call I've unlocked the buffer and checked the vertices and they seem fine. m_suintVertexCount and m_svbLandscape are static variables in CLANDSCAPE and RenderTriangle and FlushBuffers are static functions in CLANDSCAPE. This code's being used to render a ROAM algorithm heavily based on the tutorial on gamasutra. The CLANDSCAPE::Render function is called first and calls render functions for each ROAM patch (CPATCH). The patches recourse two triangle trees and when they reach a leaf they call the static CLANDSCAPE::RenderTriangle function. Any ideas what I'm doing wrong? Thanks Matt
de_matt
de_matt
Actually, just posting the code made me notice that I hadn't set the FVF (pD3DDevice->SetFVF()). It works now. What an idiot lol.

Soz

Matt
MasterWorks
MasterWorks
It never fails, just post your problem and your code, and ask for help, and the answer will magically come to you a few minutes later! :D

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.