Sign in to follow this  
Volvic

primities for ground problem?

Recommended Posts

Volvic    133
I'm trying to draw primatives to make my ground but am having problems, can anyone help, it is rendering but is very small and i cant scale it anymore. i callin main.cpp CGround *pGround // Pointer to a CGround object pGround = NULL; pGround= new CGround(); // Create the Ground object pGround->setSize(1.0f); // Scale the Ground to make it larger if (!pGround->create(dxMgr.getD3DDevice())) // Create the Ground return false; // Move the Ground to the origin pGround->setPosition(D3DXVECTOR3(0.0f, 0.0f, 0.0f)); pGround->render(dxMgr.getD3DDevice()); Ground.h

class CGround : CGameObject
{
public:
	// A structure for our custom vertex type
	struct CUSTOMVERTEX
	{
		FLOAT x, y, z;  // The untransformed, 3D position for the vertex
		float tu, tv;
	};
	CGround(void);
	~CGround(void);
	LPDIRECT3DVERTEXBUFFER9 g_pVB; // vertex buffer
	LPDIRECT3DTEXTURE9      g_pTexture;// texture info
	
	void setSize(float groundSize);
	// Overridden methods from the parent class
	bool create(LPDIRECT3DDEVICE9 device);
	void render(LPDIRECT3DDEVICE9 device);
	void setPosition(D3DXVECTOR3 newPosition);

	LPDIRECT3DVERTEXBUFFER9 createVertexBuffer(int size, DWORD usage);
	HRESULT SetupVB();
	void drawVB(LPDIRECT3DVERTEXBUFFER9 buffer, LPDIRECT3DDEVICE9 device);

private:
	D3DXVECTOR3 positionVector;
	D3DXVECTOR3 forwardVector;
	float size;

};



Ground.cpp
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)

CGround::CGround(void)
{

}
CGround::~CGround(void)
{

}
bool CGround::create(LPDIRECT3DDEVICE9 device)
{
	g_pVB        = NULL; // vertex buffer
	g_pTexture   = NULL;// texture info

	D3DXCreateTextureFromFile( device, "./models/test.bmp", &g_pTexture );
	device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	// setup the vertex buffer and add the triangle to it.
	SetupVB();

	positionVector.x = 0.0f;
	positionVector.y = 0.0f;
	positionVector.z = 0.0f;

	forwardVector.x = 0.0f;
	forwardVector.y = 0.0f;
	forwardVector.z = 1.0f;

	return true;
}
void CGround::setPosition(D3DXVECTOR3 newPosition)
{
	positionVector = newPosition;
}
void CGround::render(LPDIRECT3DDEVICE9 device)
{
	drawVB(g_pVB);

}
void CGround::setSize(float groundSize)
{
	size = groundSize;
}

HRESULT CGround::SetupVB()
{
	HRESULT hr;

	// Initialize three vertices for rendering a triangle
	CUSTOMVERTEX g_Vertices[] =
	{
		{-1.0f, 1.0f,-1.0f,  0.0f,0.0f },
		{ 1.0f, 1.0f,-1.0f,  1.0f,0.0f },
		{-1.0f,-1.0f,-1.0f,  0.0f,1.0f },
		{ 1.0f,-1.0f,-1.0f,  1.0f,1.0f },

		{-1.0f, 1.0f, 1.0f,  1.0f,0.0f },
		{-1.0f,-1.0f, 1.0f,  1.0f,1.0f },
		{ 1.0f, 1.0f, 1.0f,  0.0f,0.0f },
		{ 1.0f,-1.0f, 1.0f,  0.0f,1.0f },

		{-1.0f, 1.0f, 1.0f,  0.0f,0.0f },
		{ 1.0f, 1.0f, 1.0f,  1.0f,0.0f },
		{-1.0f, 1.0f,-1.0f,  0.0f,1.0f },
		{ 1.0f, 1.0f,-1.0f,  1.0f,1.0f },

		{-1.0f,-1.0f, 1.0f,  0.0f,0.0f },
		{-1.0f,-1.0f,-1.0f,  0.0f,1.0f },
		{ 1.0f,-1.0f, 1.0f,  1.0f,0.0f },
		{ 1.0f,-1.0f,-1.0f,  1.0f,1.0f },

		{ 1.0f, 1.0f,-1.0f,  0.0f,0.0f },
		{ 1.0f, 1.0f, 1.0f,  1.0f,0.0f },
		{ 1.0f,-1.0f,-1.0f,  0.0f,1.0f },
		{ 1.0f,-1.0f, 1.0f,  1.0f,1.0f },

		{-1.0f, 1.0f,-1.0f,  1.0f,0.0f },
		{-1.0f,-1.0f,-1.0f,  1.0f,1.0f },
		{-1.0f, 1.0f, 1.0f,  0.0f,0.0f },
		{-1.0f,-1.0f, 1.0f,  0.0f,1.0f }
	};


	// Create the vertex buffer
	g_pVB = createVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX), D3DFVF_CUSTOMVERTEX);

	// Fill the vertex buffer.
	VOID* pVertices;

	hr = g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 );
	if FAILED (hr)
		return E_FAIL;

	// copy the vertices into the buffer
	memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

	// unlock the vertex buffer
	g_pVB->Unlock();

	return S_OK;
}
LPDIRECT3DVERTEXBUFFER9 CGround::createVertexBuffer(int size, DWORD usage)
{
	HRESULT hr;
	LPDIRECT3DVERTEXBUFFER9 buffer;

	// Create the vertex buffer.
	hr = dxMgr.getD3DDevice()->CreateVertexBuffer( size,
		0, 
		usage,
		D3DPOOL_DEFAULT, 
		&buffer, 
		NULL );
	if FAILED ( hr)
		return NULL;

	return buffer;
}
void CGround::drawVB(LPDIRECT3DVERTEXBUFFER9 buffer, LPDIRECT3DDEVICE9 device)
{
	D3DXMATRIX rotMatrix;
	D3DXMATRIX transMatrix;	
	D3DXMATRIX scaleMatrix;
	
	D3DXMatrixRotationY(&rotMatrix, timeGetTime()/1000.0f);
	D3DXMatrixScaling(&scaleMatrix, size, size, size);
	D3DXMatrixTranslation(&transMatrix, positionVector.x, positionVector.y, positionVector.z);
	D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);
	D3DXMatrixMultiply(&transMatrix, &rotMatrix, &transMatrix);	

	device->SetTransform( D3DTS_WORLD, &transMatrix );

	// Set the current texture to use
	device->SetTexture( 0, g_pTexture );
	// Set the vertex stream
	device->SetStreamSource( 0, buffer, 0, sizeof(CUSTOMVERTEX) );
	device->SetFVF( D3DFVF_CUSTOMVERTEX );

	// Draw the trianglestrips that make up the cube
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
	device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
}


[Edited by - Volvic on December 12, 2006 11:27:19 AM]

Share this post


Link to post
Share on other sites
Arex    204
Use your size function? Set size as 2.0 for example.

Or you could do apply your size variable to your vertices :
<code>
HRESULT CGround::SetupVB()
{
HRESULT hr;

// Initialize three vertices for rendering a triangle
CUSTOMVERTEX g_Vertices[] =
{
{-size, size,-size, 0.0f,0.0f },
{ size, size,-size, 1.0f,0.0f },
{-size,-size,-size, 0.0f,1.0f },
{ size,-size,-size, 1.0f,1.0f },
// and so on....
};
//...
}

</code>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this