corntown

Members
  • Content count

    25
  • Joined

  • Last visited

Community Reputation

122 Neutral

About corntown

  • Rank
    Member
  1. I've created a cube and activated lightning for my device but for some reason the cube seems to be colored in black and the lightning doesn't effect it, I tried ambient light and pointlight but it doesn't seem to take effect, 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 Move_Speed 10 // 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,255,0,0)}, { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 3 { -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 4 { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 5 { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, // 6 {-64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, }; //------------------------------------------------------------------------ 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; D3DXMATRIX matProj; D3DXMATRIX matView; //float CubeUp=400.0f; //float CubeLeft=-80.0f; float CubeUp=0.0f; float CubeLeft=0.0f; float CubeDepth=-1000.0f; //------------------------------------------------- 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); void createCamera(float nearClip, float farClip); void pointCamera(D3DXVECTOR3 cameraPosition, D3DXVECTOR3 cameraLook); void createDirectionalLight ( void ); void createPointLight (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; case VK_LEFT: CubeLeft-=Move_Speed; break; case VK_RIGHT: CubeLeft+=Move_Speed; break; case VK_UP: CubeUp+=Move_Speed; break; case VK_DOWN: CubeUp-=Move_Speed; break; case 189: CubeDepth-=Move_Speed; break; case 187: CubeDepth+=Move_Speed; break; //case VK_ASTERISK:{} } } 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( 0,80,60 ), 1.0f, 0 ); if ( SUCCEEDED( pd3dDevice->BeginScene( ) ) ) { pd3dDevice->SetStreamSource ( 0, buffer, 0, sizeof(customV) ); pd3dDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE); createCamera(0.0f, 0.0f); pointCamera(D3DXVECTOR3 (-64.0f, 256.0f, CubeDepth), D3DXVECTOR3 (CubeLeft, CubeUp,0.0f)); pd3dDevice->SetRenderState (D3DRS_SPECULARENABLE, TRUE); pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE); createPointLight (); pd3dDevice->SetRenderState (D3DRS_AMBIENT, D3DCOLOR_XRGB (255, 0, 255)); D3DXMatrixIdentity(&objMat); D3DXMatrixRotationZ(&matRotate,50.0f); D3DXMatrixMultiply(&finalMat, &objMat, &matRotate); D3DXMatrixTranslation(&matRotate, -100.0f,0.0f,0.0f); D3DXMatrixMultiply(&finalMat,&finalMat, &matRotate); pd3dDevice->SetTransform(D3DTS_WORLD, &finalMat); 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; } void createCamera(float nearClip, float farClip) { // Here, you specify the field of view, aspect ratio, // and near and far clipping planes D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480,nearClip, farClip); // Apply the matProj matrix to the projection stage of the pipeline pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj); } /************************************************************************* * pointCamera * points the camera at a location specified by the passed vector *************************************************************************/ void pointCamera(D3DXVECTOR3 cameraPosition, D3DXVECTOR3 cameraLook) { D3DXMatrixLookAtLH (&matView, &cameraPosition, //camera position &cameraLook, //look at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); //up direction // Apply the matrix to the view stage of the pipeline pd3dDevice->SetTransform (D3DTS_VIEW, &matView); } void createDirectionalLight ( void ) { // Create and turn on a directional light D3DLIGHT9 light; // Set the type of light light.Type = D3DLIGHT_DIRECTIONAL; // Set the direction that this light will generate light from D3DXVECTOR3 vDir( 1.0f, 0.0f, 0.0f ); // Normalize the light direction D3DXVec3Normalize ((D3DXVECTOR3*) &light.Direction, &vDir); // Set the diffuse color for this light light.Diffuse.r = 0.0f; light.Diffuse.g = 0.0f; light.Diffuse.b = 0.5f; // Set the ambient color for this light light.Ambient.r = 0.0f; light.Ambient.g = 0.0f; light.Ambient.b = 0.3f; // Set the range of this light light.Range = sqrtf (FLT_MAX); // Tell Direct3D to set the newly created light pd3dDevice->SetLight (0, &light); // Enable the new light pd3dDevice->LightEnable (0, TRUE); } void createPointLight (void) { // Create and turn on a directional light D3DLIGHT9 light; // Set the type of light light.Type = D3DLIGHT_POINT; // Set the position for this light light.Position = D3DXVECTOR3( 200.0f, 150.0f, 0.0f ); // Set the red, green, and blue diffuse components for this // light source light.Diffuse.r = 1.0f; light.Diffuse.g = 0.5f; light.Diffuse.b = 0.5f; // Set the red, green, and blue ambient components for this // light source light.Ambient.r = 0.5f; light.Ambient.g = 0.0f; light.Ambient.b = 0.0f; light.Range = sqrtf(FLT_MAX); // Tell Direct3D to create the new light pd3dDevice->SetLight(0, &light ); // Enable the light pd3dDevice->LightEnable (0, TRUE); } What am I missing ? thanks
  2. 2 questions I have

    Quote:Original post by sirob The two questions you asked are similar in nature. The difference between transformed vertices and untransformed vertices, is the same as the difference between the points of the cube as they are presented by the book (in world space) and the point of the cube as you are expecting them to be (in screen space). Vertices marked as XYZRHW provide coordinates in pixels on the screen. This means that a point at 512, 384 will be at the center of a 1024x768 screen. In this case, Z is the depth "into the screen" (in the range of [0..1]) and W should always be 1. Untransformed vertices, on the other hand, are not in screen space. They are in world space. That means that their position is relative to "the world". While "the world" isn't anything specific, in most cases, "world space" is the playing field for your 3D game. If you would image a soccer field, that could be your world. One corner might be at 0,0,0 and another at 100, 0, 20. How you use this space is up to you, and the size of each unit, is also up to you. When Untransformed vertices are rendered, they go through a transformation process that moves them from one space to another. In this case, from world space to screen space. This is how a 3D scene is turned into a 2D picture - using transformation. The actual transformation depends on several things, such as the position of the "camera" taking the picture. These are all defined using Transformation Matrices, which determine how a vertex is transfered from being XYZ to being XYZRHW. Going back to your cube, the positions are specified in some "world space". Where this space is, or how big each unit in it is hasn't been specified yet, and will be when Transformation Matrices are set. In this space, negative or positive numbers don't make much difference. I hope this clears things up a bit, if you have more questions, feel free to ask. Thanks alot for your response. I have some questions about what you wrote: - I have a bit of a problem understanding where is the "World Space" defined in my program: // 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 Move_Speed 10 // 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,255,0,0)}, { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 3 { -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 4 { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 5 { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, // 6 {-64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, }; //------------------------------------------------------------------------ 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; D3DXMATRIX matProj; D3DXMATRIX matView; float CubeUp=400.0f; float CubeLeft=-80.0f; //------------------------------------------------- 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); void createCamera(float nearClip, float farClip); void pointCamera(D3DXVECTOR3 cameraPosition, D3DXVECTOR3 cameraLook); //------------------------------------------------- // 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; case VK_LEFT: CubeLeft+=Move_Speed; break; case VK_RIGHT: CubeLeft-=Move_Speed; break; case VK_UP: CubeUp+=Move_Speed; break; case VK_DOWN: CubeUp-=Move_Speed; break; //case VK_ASTERISK:{} } } 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( 0,0,0 ), 1.0f, 0 ); if ( SUCCEEDED( pd3dDevice->BeginScene( ) ) ) { pd3dDevice->SetStreamSource ( 0, buffer, 0, sizeof(customV) ); pd3dDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE); createCamera(0.0f, 0.0f); pointCamera(D3DXVECTOR3 (-64.0f, 64.0f, -500.0f), D3DXVECTOR3 (0.0f, 0.0f,0.0f)); pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); // Set meshMat to identity D3DXMatrixIdentity(&objMat); // Set the rotation D3DXMatrixRotationY(&matRotate, timeGetTime()/1000.0f); // Multiply the scaling and rotation matrices to create the objMat matrix D3DXMatrixMultiply(&finalMat, &objMat, &matRotate); pd3dDevice->SetTransform(D3DTS_WORLD, &finalMat); 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; } void createCamera(float nearClip, float farClip) { // Here, you specify the field of view, aspect ratio, // and near and far clipping planes D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip); // Apply the matProj matrix to the projection stage of the pipeline pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj); } /************************************************************************* * pointCamera * points the camera at a location specified by the passed vector *************************************************************************/ void pointCamera(D3DXVECTOR3 cameraPosition, D3DXVECTOR3 cameraLook) { D3DXMatrixLookAtLH (&matView, &cameraPosition, //camera position &cameraLook, //look at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); //up direction // Apply the matrix to the view stage of the pipeline pd3dDevice->SetTransform (D3DTS_VIEW, &matView); } -Is there a default "world space" maybe? -And what are the limits of the "world space" (if for example my "Screen space" is 640x480) -Is the function "createCamera" creates the transformation between the xyz to xyzRHW -Can you explain to me what is the purpose of nearClip and farClip in the "createCamera" function? thanks again
  3. Hello all I have 2 questions I hope you can reply them for me: 1. I can't seem to understand the difference between two FVFs- D3DFVF_XYZ - coordinate of an untransformed vertex. D3DFVF_XYZRHW - already transformed coordinates. Don't understand what "Transformed" means in those cases 2.In the book Im reading, a cube is created by these vertex: { // 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,255,0,0)}, { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 3 { -64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { -64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 4 { -64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { -64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,0,0)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)}, // 5 { 64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,255,255,255)}, { 64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,255,255,255)}, // 6 {-64.0f, 64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, -64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, 64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, {-64.0f, -64.0f, 64.0f, D3DCOLOR_ARGB(0,80,80,80)}, }; I have a problem understanding how are the cordinates located on the screen.. I think if the Z coordinate is negative meaning it's looking further and if it's positive it's closer.. but I don't understand what's the meaning of the X , Y coordinate when they are negative. Can anyone tell me how are they x,y,z axis located on the screen.. because until now I though X,Y can only be positive...the bigger value Y gets it goes down the screen and the bigger value X gets it goes right in the screen.. but when it's negative? is it out of the screen or what? thanks
  4. Sprite's background

    Quote:Original post by speedie what art program are you using, lots of them support alpha channels letting you to leave the background blank (i.e. no color), then you'd just have to save the image as a PNG or a TARGA format. Adobe photoshop and photoimpact but I couldn't manage to make a picture without a background.. it always leaves me with a white background . **I understand I need to save my sprite somehow as .png file which supports alpha channels and then tell me device not to render it somehow.. can anyone tell me how? I searched google and I only found methods which use vertex buffers but I think there's another way. [Edited by - corntown on May 6, 2007 3:37:07 AM]
  5. Sprite's background

    Quote:Original post by Crypter You can use transparency to not render the background. As long as the background color is a single color, simply render each pixel, and skip each pixel with the color of the sprites background. Doing this, the background will not be rendered. Alot of APIs provide routined to do this. What API are you using? can you tell me how do I tell my device not to render black pixels please? and isn't this problematic if what Im trying to render consists of black pixels?
  6. Sprite's background

    here's my 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 typedef struct sprite_struct { //size RECT size; //location long x; long y; //movement float moveX; float moveY; //animations int numframes; int curframe; } sprite; 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* character_sur; 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; RECT src,des; RECT src2,des2; sprite character; //------------------------------------------------- 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); void init_background(); void init_character(); //------------------------------------------------- // 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 ) ) return false; if ( !initDirect3D( ) ) return false; surface=getSurfaceFromBitmap("test.bmp"); character_sur=getSurfaceFromBitmap("char5.bmp"); if ((surface==NULL)||(character_sur==NULL)) return false; //init_background(); init_character(); 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) { static counter=0; IDirect3DSurface9* backbuffer = NULL; if( NULL == pd3dDevice ) return; pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,50,100 ), 1.0f, 0 ); pd3dDevice->GetBackBuffer( 0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer ); pd3dDevice->StretchRect(character_sur,&src2,backbuffer,&des2,D3DTEXF_NONE); // pd3dDevice->StretchRect(surface,&src,backbuffer,&des,D3DTEXF_NONE);//background niceling // 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; } void init_background() { src.top=50; src.left=140; src.bottom=250; src.right=520; des.top=50; des.left=140; des.bottom=250; des.right=520; } void init_character() { //size src2.top=87; src2.left=9; src2.bottom=165; src2.right=39; //location character.x=0; character.y=0; //dest des2.top=character.y; des2.left=character.x; des2.bottom=character.y+(src2.bottom-src2.top)-0; des2.right=character.x+(src2.right-src2.left)-0; } The character's background is black :)
  7. Hey everyone,Im new to game programming and i've been trying to create a 2d game. In my game there's a character but when I put it on the screen it comes with a background.. is there any way for me to create a sprite with no background? btw Im programming with c++(directx 9) thx
  8. problems creating a cube

    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
  9. problems creating a cube

    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);
  10. problems creating a cube

    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; }
  11. problems creating a cube

    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?
  12. problems creating a cube

    Quote:Original post by Evil Steve Quote:Original post by corntown FLOAT x, y, z, rhw; // the transformed, 3D position for the vertexYou'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
  13. 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; }
  14. Quote:Original post by Evil Steve What happens when you run the code in the debugger? Is it an access violation? If so, is it reading or writing, and what address? Are you sure the device is not NULL? Have you tried the debug runtimes? Why are you creating the VB in the default pool? You probably want D3DPOOL_MANAGED. Why aren't you creating the VB with the D3DCREATE_WRITEONLY flag? You almost never want to have to read from a VB. oops the device was NULL .. thanks.. and sorry for the newbie mistake :(
  15. My program is compiled without errors or warnings and when I run it I get an error msg ("Send" "Don't Send") and it happens when I try to create a vertexbuffer: //globals struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // the transformed, 3D position for the vertex DWORD color; // the vertex color }; LPDIRECT3DVERTEXBUFFER9 buffer = NULL; VOID* pVertices; HRESULT SetupVB() { HRESULT hr; // Create the vertex buffer hr = pd3dDevice->CreateVertexBuffer( 3*sizeof( CUSTOMVERTEX ), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &buffer, NULL ); if FAILED ( hr ) return NULL; // Define the vertices to be used in the buffer CUSTOMVERTEX g_Vertices[]= { {320.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (0, 255, 0, 0),}, {250.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (0, 0, 255, 0),}, {50.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_ARGB (0, 0, 0, 255),}, }; 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; } The program runs fine when I don't tries to create the vertexbuffer..anyone have any clue ?