Sign in to follow this  
corntown

problems creating a cube

Recommended Posts

I used a book in a pdf to try and create a cube and then move it on the screen.. but Im getting something wrong.. tried reading again and again but no luck..Im just getting a blue square at the top left of the screen if someone can please help:

// Include the Windows header file that’s needed for all Windows applications
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <iostream>
using namespace std;







// a structure for your custom vertex type
typedef struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex
DWORD color; // the vertex color
}customV;

customV  g_Vertices[] =
{
// 1
{ -64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 2

{ -64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 3
{ -64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 4
{ -64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 5
{ 64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 6
{-64.0f, 64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, -64.0f, -64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, 64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, -64.0f, 64.0f,1.0f, D3DCOLOR_ARGB(0,0,0,255)},
};




//------------------------------------------------------------------------




HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
// forward declarations

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

IDirect3DSurface9* surface; //personal surface pointer
IDirect3DSurface9* surface2;

LARGE_INTEGER timeStart; // holds the starting count
LARGE_INTEGER timeEnd; // holds the ending count
LARGE_INTEGER timerFreq; // holds the frequency of the counter

float anim_rate;


LPDIRECT3DVERTEXBUFFER9 buffer = NULL;
VOID* pVertices;



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

bool initDirect3D(void);
void render(void);
void cleanUp (void);

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);

HRESULT SetupVB(void);


//-------------------------------------------------




// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{


	






// Initialize the window
if ( !initWindow( hInstance ) ) //creates window (given instance)
return false;

if ( !initDirect3D( ) ) //init window for 3d usage
return false;




if (FAILED(SetupVB()))
	return false;


    QueryPerformanceFrequency(&timerFreq);



// 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
	{
		
		
		QueryPerformanceCounter(&timeStart);
		
		render( );

		QueryPerformanceCounter(&timeEnd);
		anim_rate =((float)timeEnd.QuadPart - (float)timeStart.QuadPart ) /timerFreq.QuadPart;

		
	}
}
 cleanUp ();
return (int) msg.wParam;
	
}



/******************************************************************************
* bool initWindow( HINSTANCE hInstance )
* initWindow registers the window class for the application, creates the window
******************************************************************************/
bool initWindow( HINSTANCE hInstance )
{
	WNDCLASSEX wcex;
	// Fill in the WNDCLASSEX structure. This describes how the window
	// will look to the system
	wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
	wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
	wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
	wcex.cbClsExtra = 0; // extra bytes to allocate for this class
	wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
	wcex.hInstance = hInstance; // handle to the application instance
	wcex.hIcon = 0; // icon to associate with the application
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
	wcex.lpszMenuName  = NULL; // the resource name for the menu
	wcex.lpszClassName = "DirectXExample"; // the class name being created
	wcex.hIconSm = 0; // the handle to the small icon
	RegisterClassEx(&wcex);


	// Create the window
	wndHandle = CreateWindow(
	"DirectXExample",
	"DirectXExample",
	WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
	// the window class to use
	// the title bar text
	// the window style
	CW_USEDEFAULT, // the starting x coordinate
    CW_USEDEFAULT, // the starting y coordinate
	640, // the pixel width of the window
	480, // the pixel height of the window
	NULL, // the parent window; NULL for desktop
	NULL, // the menu for the application; NULL for
	// none
	hInstance, // the handle to the application instance
	NULL); // no values passed to the window
	// Make sure that the window handle that is created is valid
	
	if (!wndHandle)
	return false;

	// Display the window on the screen
	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);
	return true;
}


/******************************************************************************
* LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
* LPARAM lParam)
* The window procedure
******************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	// Check any available messages from the queue
	switch (message)
	{
		case WM_DESTROY:
		{
	    MessageBox(NULL,"Window has ended","End",MB_OK);
		PostQuitMessage(0);
		}
		break;
	
	case WM_KEYDOWN:
		{
			switch( wParam )
            {
                case VK_ESCAPE:
                PostQuitMessage(0);
                break;
            }
	     	
		}
		break;
	
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/*********************************************************************
* initDirect3D
*********************************************************************/
bool initDirect3D(void)
{
	pD3D = NULL;
	pd3dDevice = NULL;


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




	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferWidth = 640;
	d3dpp.hDeviceWindow = wndHandle;
	




	if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,wndHandle,
		                            D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ) ) )
	{
	return false;
	}
	return true;
}

void render(void)
{
	
	HRESULT hr;
	D3DXMATRIX objMat, matRotate, finalMat,matTranslate,matScale;
	
D3DXMATRIX matFinal;

    IDirect3DSurface9* backbuffer = NULL;
	if( NULL == pd3dDevice )
	return;

  


    pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
	D3DCOLOR_XRGB( 100,90,20 ), 1.0f, 0 );

  

if ( SUCCEEDED( pd3dDevice->BeginScene( ) ) )
{
	pd3dDevice->SetStreamSource ( 0, buffer, 0, sizeof(customV) );

	pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);

	D3DXMatrixIdentity(&matFinal);
	D3DXMatrixTranslation(&matTranslate, 64.0f, 80.0f,80.0f);
	D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
	pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);

      	


	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 2 );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 8, 2 );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
	pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
	

   
	hr=pd3dDevice->EndScene();

	if ( FAILED ( hr ) )
	return ;


}

	
	

    
  
   	
	// Present the back buffer contents to the display
	pd3dDevice->Present( NULL, NULL, NULL, NULL );
	

		
	
}


void cleanUp (void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
}



/**********************************************************
* getSurfaceFromBitmap
**********************************************************/
IDirect3DSurface9* getSurfaceFromBitmap(std::string filename)
{
	HRESULT hResult;
	IDirect3DSurface9* surface = NULL;
	D3DXIMAGE_INFO imageInfo; // holds details concerning this bitmap

	// Get the width and height info from this bitmap
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	// Make sure that the call to D3DXGetImageInfoFromFile succeeded
	if FAILED (hResult)
	return NULL;

	// Create the offscreen surface that will hold the bitmap
	hResult = pd3dDevice->CreateOffscreenPlainSurface( 640,
	480,
	D3DFMT_X8R8G8B8,
	D3DPOOL_DEFAULT,
	&surface,
	NULL );

	// Make sure that this function call did not fail; if it did,
	// exit this function
	if ( FAILED( hResult ) )
	return NULL;

	// Load the bitmap into the surface that was created earlier
	hResult = D3DXLoadSurfaceFromFile( surface,
	NULL,
	NULL,
	filename.c_str( ),
	NULL,
	D3DX_DEFAULT,
	0,
	NULL );
	if ( FAILED( hResult ) )
	return NULL;
	return surface;
}






HRESULT SetupVB()
{
	HRESULT hr;
    // Define the vertices to be used in the buffer
	

	// Create the vertex buffer
	hr = pd3dDevice->CreateVertexBuffer(
	sizeof(g_Vertices)*sizeof(customV),
	0,
	D3DFVF_XYZRHW | D3DFVF_DIFFUSE,
	D3DPOOL_DEFAULT,
	&buffer,
	NULL );

	if FAILED ( hr )
	return E_FAIL;

	
	

	hr = buffer->Lock( 0,sizeof(g_Vertices), ( void** ) &pVertices, 0 );

	if FAILED (hr)
	return E_FAIL;

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

	buffer->Unlock();

    return S_OK;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by corntown
FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex
You're using transformed vertices - I.e. you're giving positions in 2D screen space. If you want to draw a cube, you'll want full 3D vertices with a complete set of transform matrices.
Have a look on Google for some D3D tutorials, or your PDF might cover it a bit later on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by corntown
FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex
You're using transformed vertices - I.e. you're giving positions in 2D screen space. If you want to draw a cube, you'll want full 3D vertices with a complete set of transform matrices.
Have a look on Google for some D3D tutorials, or your PDF might cover it a bit later on.


Im using D3DFVF_XYZRHW | D3DFVF_XYZRHW format for the verices at the moment which other ones should I use in order for it to be really 3d out of these:

D3DFVF_XYZ
D3DFVF_XYZRHW
D3DFVF_XYZW
D3DFVF_NORMAL
D3DFVF_PSIZE
D3DFVF_XYZRHW
D3DFVF_SPECULAR
D3DFVF_TEX0
D3DFVF_TEX1
D3DFVF_TEX2
D3DFVF_TEX3
D3DFVF_TEX4
D3DFVF_TEX5
D3DFVF_TEX6
D3DFVF_TEX7
D3DFVF_TEX8

the pdf-book is using this:

struct CUSTOMVERTEX
{
FLOAT x, y, z; // the untransformed, 3D position for the vertex
DWORD color; // the color of the vertex
};







pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,//<--- and this gives me an undefined error
D3DPOOL_DEFAULT,
&vertexBuffer,
NULL );






thanks for the help

Share this post


Link to post
Share on other sites
Use D3DFVF_XYZ if you want geometry to be passed through the transform & lighting parts of the pipeline.

D3DFVF_XYZRHW is your way of saying to D3D "I've already processed this data, leave it alone and just send it straight to the screen".

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Use D3DFVF_XYZ if you want geometry to be passed through the transform & lighting parts of the pipeline.

D3DFVF_XYZRHW is your way of saying to D3D "I've already processed this data, leave it alone and just send it straight to the screen".

hth
Jack


tried it like this:



pd3dDevice->CreateVertexBuffer(
sizeof(g_Vertices)*sizeof(customV),
0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&buffer,
NULL );





and it's giving me a red square at the top left corner of the screen this time
what am I doing wrong?

Share this post


Link to post
Share on other sites
Quote:
Original post by superpig
Have you updated the SetFVF call in your render function to match?


Ye I just did and now it gives me an empty screen..
here's the code


// Include the Windows header file that’s needed for all Windows applications
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <iostream>
using namespace std;

#define letterW 65
#define letterH 61





// a structure for your custom vertex type
typedef struct CUSTOMVERTEX
{
FLOAT x, y, z; // the transformed, 3D position for the vertex
DWORD color; // the vertex color
}customV;

customV g_Vertices[] =
{
// 1
{ -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 2

{ -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 3
{ -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 4
{ -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 5
{ 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{ 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
// 6
{-64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
{-64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,0,0,255)},
};




//------------------------------------------------------------------------




HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
// forward declarations

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

IDirect3DSurface9* surface; //personal surface pointer
IDirect3DSurface9* surface2;

LARGE_INTEGER timeStart; // holds the starting count
LARGE_INTEGER timeEnd; // holds the ending count
LARGE_INTEGER timerFreq; // holds the frequency of the counter

float anim_rate;


LPDIRECT3DVERTEXBUFFER9 buffer = NULL;
VOID* pVertices;



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

bool initDirect3D(void);
void render(void);
void cleanUp (void);

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);

HRESULT SetupVB(void);


//-------------------------------------------------




// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{









// Initialize the window
if ( !initWindow( hInstance ) ) //creates window (given instance)
return false;

if ( !initDirect3D( ) ) //init window for 3d usage
return false;




if (FAILED(SetupVB()))
return false;


QueryPerformanceFrequency(&timerFreq);



// 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
{


QueryPerformanceCounter(&timeStart);

render( );

QueryPerformanceCounter(&timeEnd);
anim_rate =((float)timeEnd.QuadPart - (float)timeStart.QuadPart ) /timerFreq.QuadPart;


}
}
cleanUp ();
return (int) msg.wParam;

}



/******************************************************************************
* bool initWindow( HINSTANCE hInstance )
* initWindow registers the window class for the application, creates the window
******************************************************************************/

bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = "DirectXExample"; // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(&wcex);


// Create the window
wndHandle = CreateWindow(
"DirectXExample",
"DirectXExample",
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
// the window class to use
// the title bar text
// the window style
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for
// none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid

if (!wndHandle)
return false;

// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}


/******************************************************************************
* LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
* LPARAM lParam)
* The window procedure
******************************************************************************/

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
case WM_DESTROY:
{
MessageBox(NULL,"Window has ended","End",MB_OK);
PostQuitMessage(0);
}
break;

case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}

}
break;

}

return DefWindowProc(hWnd, message, wParam, lParam);
}

/*********************************************************************
* initDirect3D
*********************************************************************/

bool initDirect3D(void)
{
pD3D = NULL;
pd3dDevice = NULL;


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




D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferWidth = 640;
d3dpp.hDeviceWindow = wndHandle;





if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ) ) )
{
return false;
}
return true;
}

void render(void)
{

HRESULT hr;
D3DXMATRIX objMat, matRotate, finalMat,matTranslate,matScale;

D3DXMATRIX matFinal;

IDirect3DSurface9* backbuffer = NULL;
if( NULL == pd3dDevice )
return;




pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 100,90,20 ), 1.0f, 0 );



if ( SUCCEEDED( pd3dDevice->BeginScene( ) ) )
{
pd3dDevice->SetStreamSource ( 0, buffer, 0, sizeof(customV) );

pd3dDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);

// D3DXMatrixIdentity(&matFinal);
// D3DXMatrixTranslation(&matTranslate, 64.0f, 80.0f,80.0f);
// D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
// pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);




pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 2 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 8, 2 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );



hr=pd3dDevice->EndScene();

if ( FAILED ( hr ) )
return ;


}




//pd3dDevice->GetBackBuffer( 0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer );


// Present the back buffer contents to the display
pd3dDevice->Present( NULL, NULL, NULL, NULL );




}


void cleanUp (void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
}



/**********************************************************
* getSurfaceFromBitmap
**********************************************************/

IDirect3DSurface9* getSurfaceFromBitmap(std::string filename)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo; // holds details concerning this bitmap

// Get the width and height info from this bitmap
hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
// Make sure that the call to D3DXGetImageInfoFromFile succeeded
if FAILED (hResult)
return NULL;

// Create the offscreen surface that will hold the bitmap
hResult = pd3dDevice->CreateOffscreenPlainSurface( 640,
480,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL );

// Make sure that this function call did not fail; if it did,
// exit this function
if ( FAILED( hResult ) )
return NULL;

// Load the bitmap into the surface that was created earlier
hResult = D3DXLoadSurfaceFromFile( surface,
NULL,
NULL,
filename.c_str( ),
NULL,
D3DX_DEFAULT,
0,
NULL );
if ( FAILED( hResult ) )
return NULL;
return surface;
}






HRESULT SetupVB()
{
HRESULT hr;
// Define the vertices to be used in the buffer


// Create the vertex buffer
hr = pd3dDevice->CreateVertexBuffer(
sizeof(g_Vertices)*sizeof(customV),
0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&buffer,
NULL );

if FAILED ( hr )
return E_FAIL;




hr = buffer->Lock( 0,sizeof(g_Vertices), ( void** ) &pVertices, 0 );

if FAILED (hr)
return E_FAIL;

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

buffer->Unlock();

return S_OK;
}




Share this post


Link to post
Share on other sites
You're not setting a view or projection matrix.

Try adding something like this (Off the top of my head) before your first DrawPrimitive() call (There are better places to put it but this'll do for simplicity's sake):

D3DXMATRIX mat;
D3DXMatrixPerspectiveFovLH(&mat, 1.04719755f, // 60 degrees in radians
640.0f/480.0f, 0.1f, 1000.0f);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat);
D3DXVECTOR3 vEye(0.0f, 0.0f, -300.0f);
D3DXVECTOR3 vAt(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&mat, &vEye, &vAt, &vUp);
pd3dDevice->SetTransform(D3DTS_VIEW, &mat);


Share this post


Link to post
Share on other sites
I imagine that as you're not setting up any transforms, it's interpreting your vertex coordinates as clip-space coordinates, which would make them waaay outside of the screen. I'd recommend setting up projection and view transforms.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
You're not setting a view or projection matrix.

Try adding something like this (Off the top of my head) before your first DrawPrimitive() call (There are better places to put it but this'll do for simplicity's sake):
*** Source Snippet Removed ***


Added it and now Im getting a big black square in the middle of the screen(why is it black ?)
but at least my translation works:


D3DXMatrixIdentity(&matFinal);
D3DXMatrixTranslation(&matTranslate, 64.0f, 80.0f,80.0f);
D3DXMatrixMultiply(&matFinal, &matFinal, &matTranslate);
pd3dDevice->SetTransform(D3DTS_WORLD, &matFinal);



Share this post


Link to post
Share on other sites
Quote:
Original post by corntown
now Im getting a big black square in the middle of the screen(why is it black ?)
It's been a while since I used FF, but try SetRenderState( D3DRS_LIGHTING, FALSE ) in your initialization code. Might be passing your geometry through the lighting engine, not finding any normals (or lights!) and thus outputting 0/black.

You could verify this with SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB( 255, 0, 0 ) ) - if your cube turns red as a result then its the lighting engine thats getting in your way.

btw, you are aware you're creating a reference rasterizer device?! I'm pretty sure ALL hardware from the last 5 years will be capable of executing your code in hardware [smile]

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Quote:
Original post by corntown
now Im getting a big black square in the middle of the screen(why is it black ?)
It's been a while since I used FF, but try SetRenderState( D3DRS_LIGHTING, FALSE ) in your initialization code. Might be passing your geometry through the lighting engine, not finding any normals (or lights!) and thus outputting 0/black.

You could verify this with SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB( 255, 0, 0 ) ) - if your cube turns red as a result then its the lighting engine thats getting in your way.

btw, you are aware you're creating a reference rasterizer device?! I'm pretty sure ALL hardware from the last 5 years will be capable of executing your code in hardware [smile]

hth
Jack


it doesn't recgonize SetRenderState

Share this post


Link to post
Share on other sites
Quote:
Original post by corntown
it doesn't recgonize SetRenderState
Had you looked up SetRenderState() in the DX documentation, you'd see that it's a member of IDirect3DDevice9.

Jack is probably correct about disabling lighting - you don't have a material set, and the default material is all black, which could explain what you're seeing. You're also not clearing your backbuffer at all (Look into IDirect3DDevice9::Clear()). You'll want to clear your backbuffer to a non-black colour for debugging, and you'll definitely want to clear your Z-buffer (Which is also going to cause this problem) each frame.

Share this post


Link to post
Share on other sites
Render states are set through the D3D Device pointer. Looking at your code, its something like:


pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );




Where D3DRS = D3D Render State.. Lighting referring to the built in lighting engine... which doesn't do you any good unless you have fixed function lights set. So thats why you set it to false.

It sounds like whatever PDF book you are using is not explaining things very well (or you're skipping ahead). I'd recommend some of these step by step tutorials you find all over the web for FFP (Fixed Function Pipeline).

Another good resource for Dx functions and how to use them, is MSDN... so for instance, if you couldn't figure out SetRenderState, you might go to: MSDN Dx9.0c C++ Docs

You might have seen: IDirect3DDevice9::SetRenderState Method - and thought, "Oh, maybe its set through a member function of the device object."

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