Jump to content
  • Advertisement
Sign in to follow this  
deathwearer

Cube not showing with index buffer

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Now I'm trying to draw a Cube using an Index buffer and it doens't work. I'v followed every line of my books and i beleive i didn't miss anything. It's compile there is no error but show nothing. I'v got simmilar problem when i tried to draw with vertex buffer and the error was my vertices order. Now i beleive it's alright. Here is the code main.cpp
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "Game.h"


HINSTANCE hInst;
HWND wndHandle;

CGame oGame;



bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool Setup_VB();




int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}
	
	if (!oGame.initDirect3D(wndHandle))
	{
		MessageBox(NULL, "Unable to Initialize D3D", "ERROR", MB_OK);
		return false;
	}
	
	oGame.Setup_VB();
	oGame.Setup_IB();



	oGame.createCamera(1.0f, 500.0f);		// near clip plane, far clip plane
	oGame.moveCamera(D3DXVECTOR3(0.0f, 0.0f, -500.0f));
	oGame.pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
		

	// Main message loop:
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else
		{
			oGame.Render();
		}
    }
		
	return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "Pong 3D";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	// create the window
	wndHandle = CreateWindow("Pong 3D", 
							 "Pong 3D", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

Game.cpp
#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, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, 100.0f, -100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, 100.0f, -100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, -100.0f, -100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, -100.0f, 100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, -100.0f, 100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{100.0f, 100.0f, 100.0f, 1.0f, D3DCOLOR_ARGB(0,0,255,0)},
		{-100.0f, 100.0f, 100.0f,1.0f, D3DCOLOR_ARGB(0,0,255,0)}
		
	};

	HRESULT hr;

	hr = pd3dDevice->CreateVertexBuffer( sizeof(g_Vertices) * 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[ ] =
	{
		0,1,2,
		2,3,0,
		4,5,6,
		6,7,4,
		0,3,5,
		5,4,0,
		3,2,6,
		6,5,3,
		2,1,7,
		7,6,2,
		1,0,4,
		4,7,1
	};

	HRESULT hr;

	hr = pd3dDevice->CreateIndexBuffer(sizeof(IndexData)*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_TRIANGLELIST,
									  0,
									  0,
									  8,
									  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);
}

Here is D3DFVF_CUSTOMVERTEX #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

Share this post


Link to post
Share on other sites
Advertisement

d3dpp.BackBufferCount = 1;


... probably should be...


d3dpp.BackBufferCount = 0;


... 0 == 1 in this case.


hr = pd3dDevice->CreateVertexBuffer( sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),


... should be...


hr = pd3dDevice->CreateVertexBuffer( 8 * sizeof(CUSTOMVERTEX),


... sizeof(g_Vertices) would return the size of g_Vertices in bytes (not 8).


hr = pd3dDevice->CreateIndexBuffer(sizeof(IndexData)*sizeof(WORD),
D3DUSAGE_WRITEONLY,


...


hr = pd3dDevice->CreateIndexBuffer(36*sizeof(WORD),
0,


... try that, see if it's any better.


Share this post


Link to post
Share on other sites
Change your FVF, you dont want RHW: thats for tranformed vertices, so everything you're rendering you're giving in screen coordinates. It makes everything look 2D of course. Try move your camera closer too...your clipping plane is only 499units and you're 500units back: its might be getting lost due to precision of the buffers.

Share this post


Link to post
Share on other sites
Hi there Deathwearer, how are you doing?

The Problem
Drawing a cube using vertex and index buffers.

The Solution
There are a few minor that I notice right off the bat that might cause problems, I think the best way to do this would be to follow andy pike's tutorial on cubes. It's a fantastic starter.

1) As noted by David Jones, your D3DXYZRHW is telling direct3d that you are using transformed vertices, this means that transformations aren't applied to your vertices and that they are in screen space already. you will want to change that to D3DFVF_XYZ to get the results you seek I believe.

but without further a due Andy Pike's Cube tutorial

I hope this tutorial helps you buddy. Take care.

Share this post


Link to post
Share on other sites
Ah yes, thank guys this is working. I still don't understand the concept of the camera if someone would be kind enought to explain it to me.

And what's wrong with my distance, and why if i put a too big value e.g -700z instead of -500 it doens't show anything anymore?

Share this post


Link to post
Share on other sites
Okay well looking at your calls:

oGame.createCamera(1.0f, 500.0f);

which leads to the building of a perspective matrix as follows:

D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);

I think you're confusing yourself by calling this function createCamera...because this function really defines how objects will be seen in your scene, as it builds your projection matrix. For an easy illustration change either D3DX_PI/4 or 640/480 and you'll see things are different. Back to your question: you define the near and far clipping planes as 1 to 500, which means the distance viewable at any time is 499 units. You defined your cube in 100units around the origin, so if you move back to -700 you wont be able to see it cos you can only see from -700 to -201units (assuming you're looking straight forward of course). Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!