// 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;
}
problems creating a cube
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:
Quote:Original post by corntownYou'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.
FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex
Have a look on Google for some D3D tutorials, or your PDF might cover it a bit later on.
Quote:Original post by Evil SteveQuote:Original post by corntownYou'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.
FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex
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 vertexDWORD color; // the color of the vertex};
pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,//<--- and this gives me an undefined errorD3DPOOL_DEFAULT,&vertexBuffer,NULL );
thanks for the help
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
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
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?
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 typetypedef struct CUSTOMVERTEX{FLOAT x, y, z; // the transformed, 3D position for the vertexDWORD 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 instanceHWND wndHandle; // global variable to hold the window handle// forward declarationsLPDIRECT3D9 pD3D; // the Direct3D objectLPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D deviceIDirect3DSurface9* surface; //personal surface pointerIDirect3DSurface9* surface2;LARGE_INTEGER timeStart; // holds the starting countLARGE_INTEGER timeEnd; // holds the ending countLARGE_INTEGER timerFreq; // holds the frequency of the counterfloat 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 applicationsint WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow ){ // Initialize the windowif ( !initWindow( hInstance ) ) //creates window (given instance)return false;if ( !initDirect3D( ) ) //init window for 3d usagereturn 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 objectif( 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;}
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):
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);
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.
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);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement