Sign in to follow this  
deathwearer

Weird drawing problem. [FIXED]

Recommended Posts

Ok, I'v now changed my mine and will draw line in my pong3d game instead of a cube ( i don't want the diagonals line and i want it to be wireframe. ) So now I'v setup my vertex buffer and my index buffer i assume corectly and when i draw on the screen i get weird stuff and it doens't exactly look like it should. Here is how it look : http://img82.imageshack.us/img82/8232/weirddraw2sq.jpg And here the code of my class ( I'm not gonna post the definition of the class and the main since there is no error in that. ) I'm using a vertex + index buffer to make the drawing, also what shall i use, LINELIST or LINESTRIP, and what is the difference?
#include ".\game.h"

CGame::CGame(void)
{
	
	


}

CGame::~CGame(void)
{
}

bool CGame::initDirect3D(HWND wndHandle)
{
	pD3D = NULL;
	pd3dDevice = NULL;

	if ( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return false;

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.BackBufferWidth = 640;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.MultiSampleQuality = 0;
	d3dpp.SwapEffect         = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow      = wndHandle;
	d3dpp.Windowed			 = true;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	

	pD3D->CreateDevice( D3DADAPTER_DEFAULT,
						D3DDEVTYPE_HAL,
						wndHandle,
						D3DCREATE_SOFTWARE_VERTEXPROCESSING,
						&d3dpp,
						&pd3dDevice );

	pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
	pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

    pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
	//pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

	
	return true;
}

void CGame::Clear_Screen()
{
	if ( NULL != pd3dDevice )
	{
		pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0);
	}
}

bool CGame::Setup_VB()
{
	CUSTOMVERTEX g_Vertices[]=
	{
		{-100.0f, -100.0f, -100.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, 100.0f, -100.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, 100.0f, -100.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, -100.0f, -100.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, -100.0f, 400.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, -100.0f, 400.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, 100.0f, 400.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, 100.0f, 400.0f, D3DCOLOR_ARGB(0,0,255,0)}
		
	};

	HRESULT hr;

	hr = pd3dDevice->CreateVertexBuffer( 8 * sizeof(CUSTOMVERTEX),
										0,
										D3DFVF_CUSTOMVERTEX,
										D3DPOOL_DEFAULT,
										&vBuffer,
										NULL );

	VOID* pVertices;

	vBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices,0 );

	memcpy( pVertices, g_Vertices, sizeof(g_Vertices));

	vBuffer->Unlock();

	if (hr == S_OK)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CGame::Setup_IB()
{
	WORD IndexData[ ] =
	{
		1,2,
		1,4,
		1,5,
		2,3,
		2,8,
		3,4,
		3,7,
		4,6,
		5,8,
		5,6,
		6,7,
		7,8
	};

	HRESULT hr;

	hr = pd3dDevice->CreateIndexBuffer(36*sizeof(WORD),
									   D3DUSAGE_WRITEONLY,
									   D3DFMT_INDEX16,
									   D3DPOOL_DEFAULT,
									   &iBuffer,
									   NULL);

	VOID* IndexPtr;
	iBuffer->Lock(0,0, (void**)& IndexPtr, D3DLOCK_DISCARD);
	memcpy(IndexPtr, IndexData, sizeof(IndexData));
	iBuffer->Unlock();

	if (hr == S_OK)
	{
		return true;
	}
	else
	{
		return false;
	}

}


void CGame::Render()
{
	Clear_Screen();
	pd3dDevice->BeginScene();
	pd3dDevice->SetStreamSource( 0, vBuffer, 0, sizeof(CUSTOMVERTEX) );
	pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	pd3dDevice->SetIndices( iBuffer );
	pd3dDevice->DrawIndexedPrimitive( D3DPT_LINESTRIP,
									  0,
									  0,
									  12,
									  0,
									  12 );
	
	pd3dDevice->EndScene();
	pd3dDevice->Present( NULL, NULL, NULL, NULL);
}

void CGame::createCamera(float nearClip, float farClip)
{
	//Here we specify the field of view, aspect ration and near and far clipping planes.
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

}

/*************************************************************************
* moveCamera
* moves the camera to a position specified by the vector passed as a 
* parameter
*************************************************************************/
void CGame::moveCamera(D3DXVECTOR3 vec)
{
	cameraPosition = vec;
}

/*************************************************************************
* pointCamera
* points the camera a location specified by the passed vector
*************************************************************************/
void CGame::pointCamera(D3DXVECTOR3 vec)
{
	cameraLook = vec;

	D3DXMatrixLookAtLH(&matView, &cameraPosition,		//Camera Position
                                 &cameraLook,		//Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f));		//Up Direction

	pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
}


[Edited by - deathwearer on September 5, 2005 3:08:09 PM]

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