   DirectXGraphics 9 matrix problem    
  Hi there!I am making a few DirectXGraphics 9 programs based off of the SDK Samples to gain an understanding of DirectXGraphics. I am on the third tutorial, and I am having trouble with the world, view, and projection matrices. I am doing everything that the SDK wants me to, but it does not work. Do you know what I am doing wrong here?The matrices seem to have no effect on the output of my program. I rotate the world matrix and apply it to DirectXGraphics' world matrix, but the triangle does not appear on the scene. All I get is the background. I've compared my code to the SDK Sample code, and there seems to be no fundemental difference. /////////////////////////////////////////////////////////////////////////// Function to render the scene in Direct3D./////////////////////////////////////////////////////////////////////////int Main(){    /////////////////////////////////////////////////////////////////////////    // Clear the backbuffer.    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0 ); /////////////////////////////////////////////////////////////////////////    // Begin rendering the scene. All rendering is done between BeginScene()    // and EndScene().    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->BeginScene();    // *NEW* ////////////////////////////////////////////////////////////////   // Call the D3DXMatrixRotationY() function to rotate the world matrix.    // The first parameter specifies the matrix the function will modify, the   // second parameter specifies the angle of rotation in radians.   /////////////////////////////////////////////////////////////////////////   D3DXMatrixRotationY( &g_matWorld, timeGetTime()/150.0f );// *NEW* ////////////////////////////////////////////////////////////////   // Call the D3DXMatrixLookAtLH() function to set the view matrix. The   // view matrix is the camera. The first parameter is a pointer to the   // view matrix we are modifying. The second parameter is the position of   // the camera, the third parameter specifies the point the camera is   // looking at. The fourth parameter specifies the 'up' direction, which   // should be a vector pointing up.   /////////////////////////////////////////////////////////////////////////   D3DXMatrixLookAtLH( &g_matView, &D3DXVECTOR3( 0.0f, 3.0f, -5.0f ),&D3DXVECTOR3( 0.0f, 0.0f,  0.0f ),&D3DXVECTOR3( 0.0f, 0.0f,  1.0f ) );// *NEW* ////////////////////////////////////////////////////////////////   // Set up the projection matrix with a call to D3DXPerspectiveFovLH().    // The first parameter is a pointer to the projection matrix to modify.   // The second parameter defines a field of view, which tells how objects   // in the distance get smaller. The third parameter defines the aspect   // ratio. The fourth and fifth parameters define the near and far   // clipping plane.   /////////////////////////////////////////////////////////////////////////   D3DXMatrixPerspectiveFovLH( &g_matView, D3DX_PI/4, 1.0f, 1.0f, 100.0f );    // *NEW* ////////////////////////////////////////////////////////////////   // Call the SetTransform() function to apply the matrix we are modifying   // to the APIs matrix. We are just using g_matWorld as an interface to   // Direct3D9's world matrix. We are updating Direct3D's world matrix   // with our data here. The same thing is happening with the view matrix   // and the projection matrix.   /////////////////////////////////////////////////////////////////////////   g_pD3DDevice->SetTransform( D3DTS_WORLD, &g_matWorld );   g_pD3DDevice->SetTransform( D3DTS_VIEW,  &g_matView );   g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &g_matProj );/////////////////////////////////////////////////////////////////////////   // Set the stream source. This is basically just choosing which vertex   // buffer you want to render from, and where in the vertex buffer.   /////////////////////////////////////////////////////////////////////////   g_pD3DDevice->SetStreamSource( 0, g_pVB, 0, sizeof(vertex_t) );    /////////////////////////////////////////////////////////////////////////   // Set the flexible vertex format that is used in the vertex buffer we   // just selected. This is done so that the renderer knows what kind of   // data we are sending it, whether it be the color or the position or the   // texture coordinate.   /////////////////////////////////////////////////////////////////////////   g_pD3DDevice->SetFVF( D3DFVF_XYZ | D3DFVF_DIFFUSE ); /////////////////////////////////////////////////////////////////////////   // Now we render the vertices. The first parameter specifies what kind   // of vertices we want to render (triangle strips), the second parameter   // is the index of the first vertex to render (0), and the third tells    // how many primitives we want to render (1).   /////////////////////////////////////////////////////////////////////////   g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); /////////////////////////////////////////////////////////////////////////    // End rendering the scene. All rendering is done between BeginScene()// and end scene.    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->EndScene();/////////////////////////////////////////////////////////////////////////    // Present the scene on the screen.    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->Present( NULL, NULL, NULL, NULL );return 0;}   That is the code for my rendering loop. The following is the code for my vertices and the setup of DirectXGraphics.    /////////////////////////////////////////////////////////////////////////// Define the custom vertex type./////////////////////////////////////////////////////////////////////////struct vertex_t{    float m_fX;   // X position    float m_fY;   // Y position    float m_fZ;   // Z position    DWORD m_dwColor;   // Color};   /////////////////////////////////////////////////////////////////////////// Define three instances of the vertex type. These three vertices will// be connected to form a triangle./////////////////////////////////////////////////////////////////////////vertex_t vertices[3] ={   { -1.0f,-1.0f, 0.0f, 0xffff0000, },// First vertex (bottom-left)   {  1.0f,-1.0f, 0.0f, 0xff0000ff, },// Second vertex (bottom-right)   {  0.0f, 1.0f, 0.0f, 0xffffffff, },// Third vertex (top-center)};   /////////////////////////////////////////////////////////////////////////// Function to initialize Direct3D./////////////////////////////////////////////////////////////////////////int Init(){    /////////////////////////////////////////////////////////////////////////    // Create the Direct3D object. The parameter D3D_SDK_VERSION informs    // Direct3D that the correct headers are being used.    /////////////////////////////////////////////////////////////////////////    if( ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) == NULL )    {   MessageBox(g_hWnd, "Could not create the Direct3D 9 device.", "Error", MB_OK);   return 1;    }/////////////////////////////////////////////////////////////////////////    // Clear the memory in the present parameters structure and fill it in.    /////////////////////////////////////////////////////////////////////////    ZeroMemory( &d3dpp, sizeof( d3dpp ) ); // Clear out the present parameters structure    d3dpp.Windowed   = true;// Our application will be windowed    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;   // Discard the contents of the backbuffer every rendering    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Unknown backbuffer format/////////////////////////////////////////////////////////////////////////    // Create the Direct3D 9 device. The first parameter tells Direct3D to    // use the default graphics adapter. The second tells Direct3D that we    // want to use HAL, which gives us direct access to the rendering    // hardware. The third tells Direct3D what the window handle is. The    // fourth tells Direct3D to process vertices in software. The fifth is a// pointer the the present parameters structure. The sixth is a pointer// to the device we are creating in this function.    /////////////////////////////////////////////////////////////////////////    if( FAILED ( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,    D3DDEVTYPE_HAL,    g_hWnd,    D3DCREATE_SOFTWARE_VERTEXPROCESSING,    &d3dpp,    &g_pD3DDevice ) ) )    {   MessageBox(g_hWnd, "Could not create the Direct3D 9 device.", "Error", MB_OK);   return 1;    }/////////////////////////////////////////////////////////////////////////    // Create a vertex buffer. The first parameter specifies the vertex    // buffer's size. The second specifies its usage (none). The third// defines the type of vertex format we are using (X, Y, Z, RHW, and// color). The fourth specifies the pool to use (default). The fifth is    // a pointer to the vertex buffer that is being created. The sixth is    // reserved. Pay no attention to it.    /////////////////////////////////////////////////////////////////////////    if( FAILED ( g_pD3DDevice->CreateVertexBuffer( 3*sizeof(vertex_t),0,  D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {   MessageBox(g_hWnd, "Could not create the vertex buffer.", "Error", MB_OK);   return 1;    }/////////////////////////////////////////////////////////////////////////    // Use the Lock() and Unlock() functions to write to the vertex buffer.    // Like many buffers, it has to be locked and unlocked. In between these    // calls, you can modify the vertex buffer's data through a pointer.    /////////////////////////////////////////////////////////////////////////   void *pVertices = NULL;  // Pointer to the data in the vertex buffer *NEW*   /////////////////////////////////////////////////////////////////////////    // Lock the vertex buffer. The first parameter is the offset of the data    // we will be locking in bytes (0). The second specifies the size to    // lock in the vertex buffer (sizeof(vertices)). In the third parameter    // we have to specify a pointer that will get access to the vertex    // buffer. The fourth parameter specifies how we want to lock it.    /////////////////////////////////////////////////////////////////////////    if( FAILED ( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )    {   MessageBox(g_hWnd, "Could not lock the vertex buffer.", "Error", MB_OK);   return 1;    }  /////////////////////////////////////////////////////////////////////////    // Copy the vertices we want into the vertex buffer by copying it to the    // pointer that points to the data in the vertex buffer.    /////////////////////////////////////////////////////////////////////////    memcpy( pVertices, vertices, sizeof(vertices) );  /////////////////////////////////////////////////////////////////////////    // Unlock the vertex buffer because we are done writing to it. There is    // nothing to it, no parameters, but you need it so that you can tell    // Direct3D that you are done modifying it.    /////////////////////////////////////////////////////////////////////////    if( FAILED( g_pVB->Unlock() ) )    {   MessageBox(g_hWnd, "Could not unlock the vertex buffer.", "Error", MB_OK);   return 1;    } return 0;}  You can download the Microsoft Visual C++ 6 source here, but I'm not sure if Brinkster will let you get at it.Thanks for your consideration.    [edit(fixed formatting)][edit(anyone?)]    [  c o d e  m a t r i x  ][edited by - elendil67 on January 1, 2003 2:09:58 PM]
  Hmm... I took another pass through my code and compared it to the samples and the only thing I noticed is that the ''up'' vector for the view matrix was actually pointing in the Z axis. I don''t think it''s my camera position or vertices or anything because if I use RHW the triangles appear on the screen, but I can''t rotate them.   [  c o d e  m a t r i x  ];  Note that I added this in the end of my initialization function:  /////////////////////////////////////////////////////////////////////////    // Turn off backface culling by setting the render state for the cull    // mode parameter to no culling.    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    /////////////////////////////////////////////////////////////////////////    // Turn off lighting by setting the render state for the lighting mode    // parameter to turn off lighting.    /////////////////////////////////////////////////////////////////////////    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, false );  [  c o d e  m a t r i x  ];  well i think vertices having a RHW format aren't transformed by the D3d world matrix, because if you specify RHW vertices, DX assumes you have transformed them allready.when you look at the source code of tutorial 3 they have the following vertex format:#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)and here the whole tutorial3 source://-----------------------------------------------------------------------------// File: Matrices.cpp//// Desc: Now that we know how to create a device and render some 2D vertices,//  this tutorial goes the next step and renders 3D geometry. To deal with//  3D geometry we need to introduce the use of 4x4 matrices to transform//  the geometry with translations, rotations, scaling, and setting up our//  camera.////  Geometry is defined in model space. We can move it (translation),//  rotate it (rotation), or stretch it (scaling) using a world transform.//  The geometry is then said to be in world space. Next, we need to//  position the camera, or eye point, somewhere to look at the geometry.//  Another transform, via the view matrix, is used, to position and//  rotate our view. With the geometry then in view space, our last//  transform is the projection transform, which "projects" the 3D scene//  into our 2D viewport.////  Note that in this tutorial, we are introducing the use of D3DX, which//  is a set of helper utilities for D3D. In this case, we are using some//  of D3DX's useful matrix initialization functions. To use D3DX, simply//  include <d3dx9.h> and link with d3dx9.lib.//// Copyright (c) Microsoft Corporation. All rights reserved.//-----------------------------------------------------------------------------#include <Windows.h>#include <mmsystem.h>#include <d3dx9.h>//-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9   g_pD3D  = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9  g_pd3dDevice = NULL; // Our rendering deviceLPDIRECT3DVERTEXBUFFER9 g_pVB   = NULL; // Buffer to hold vertices// A structure for our custom vertex typestruct CUSTOMVERTEX{    FLOAT x, y, z; // The untransformed, 3D position for the vertex    DWORD color;   // The vertex color};// Our custom FVF, which describes our custom vertex structure#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)//-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    // Create the D3D object.    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )   return E_FAIL;    // Set up the structure used to create the D3DDevice    D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Create the D3DDevice    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,   D3DCREATE_SOFTWARE_VERTEXPROCESSING,   &d3dpp, &g_pd3dDevice ) ) )    {   return E_FAIL;    }    // Turn off culling, so we see the front and back of the triangle    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    // Turn off D3D lighting, since we are providing our own vertex colors    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );    return S_OK;}//-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Creates the scene geometry//-----------------------------------------------------------------------------HRESULT InitGeometry(){    // Initialize three vertices for rendering a triangle    CUSTOMVERTEX g_Vertices[] =    {   { -1.0f,-1.0f, 0.0f, 0xffff0000, },   {  1.0f,-1.0f, 0.0f, 0xff0000ff, },   {  0.0f, 1.0f, 0.0f, 0xffffffff, },    };    // Create the vertex buffer.    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {   return E_FAIL;    }    // Fill the vertex buffer.    VOID* pVertices;    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )   return E_FAIL;    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );    g_pVB->Unlock();    return S_OK;}//-----------------------------------------------------------------------------// Name: Cleanup()// Desc: Releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup(){    if( g_pVB != NULL )   g_pVB->Release();    if( g_pd3dDevice != NULL )   g_pd3dDevice->Release();    if( g_pD3D != NULL )   g_pD3D->Release();}//-----------------------------------------------------------------------------// Name: SetupMatrices()// Desc: Sets up the world, view, and projection transform matrices.//-----------------------------------------------------------------------------VOID SetupMatrices(){    // For our world matrix, we will just rotate the object about the y-axis.    D3DXMATRIXA16 matWorld;    // Set up the rotation matrix to generate 1 full rotation (2*PI radians)// every 1000 ms. To avoid the loss of precision inherent in very high// floating point numbers, the system time is modulated by the rotation// period before conversion to a radian angle.    UINT  iTime  = timeGetTime() % 1000;    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;    D3DXMatrixRotationY( &matWorld, fAngle );    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );    // Set up our view matrix. A view matrix can be defined given an eye point,    // a point to lookat, and a direction for which way is up. Here, we set the    // eye five units back along the z-axis and up three units, look at the    // origin, and define "up" to be in the y-direction.    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );    D3DXMATRIXA16 matView;    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );    // For the projection matrix, we set up a perspective transform (which    // transforms geometry from 3D view space to 2D viewport space, with    // a perspective divide making objects smaller in the distance). To build    // a perpsective transform, we need the field of view (1/4 pi is common),    // the aspect ratio, and the near and far clipping planes (which define at    // what distances geometry should be no longer be rendered).    D3DXMATRIXA16 matProj;    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );}//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//-----------------------------------------------------------------------------VOID Render(){    // Clear the backbuffer to a black color    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );    // Begin the scene    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {   // Setup the world, view, and projection matrices   SetupMatrices();   // Render the vertex buffer contents   g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );   g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );   g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );   // End the scene   g_pd3dDevice->EndScene();    }    // Present the backbuffer contents to the display    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Name: MsgProc()// Desc: The window's message handler//-----------------------------------------------------------------------------LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {   case WM_DESTROY:  Cleanup();  PostQuitMessage( 0 );  return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}//-----------------------------------------------------------------------------// Name: WinMain()// Desc: The application's entry point//-----------------------------------------------------------------------------INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,  GetModuleHandle(NULL), NULL, NULL, NULL, NULL,  "D3D Tutorial", NULL };    RegisterClassEx( &wc );    // Create the application's window    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,GetDesktopWindow(), NULL, wc.hInstance, NULL );    // Initialize Direct3D    if( SUCCEEDED( InitD3D( hWnd ) ) )    {   // Create the scene geometry   if( SUCCEEDED( InitGeometry() ) )   {  // Show the window  ShowWindow( hWnd, SW_SHOWDEFAULT );  UpdateWindow( hWnd );  // Enter the 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 ); } elseRender();  }   }    }    UnregisterClass( "D3D Tutorial", wc.hInstance );    return 0;}[/soure]    [edited by - Hansi pirman on January 1, 2003 4:37:32 PM];quote:Original post by hansi pirman  well i think vertices having a RHW format aren''t transformed by the D3d world matrix, because if you specify RHW vertices, DX assumes you have transformed them allready.when you look at the source code of tutorial 3 they have the following vertex format:#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)and here the whole tutorial3 source:   //-----------------------------------------------------------------------------// File: Matrices.cpp//// Desc: Now that we know how to create a device and render some 2D vertices,//  this tutorial goes the next step and renders 3D geometry. To deal with//  3D geometry we need to introduce the use of 4x4 matrices to transform//  the geometry with translations, rotations, scaling, and setting up our//  camera.////  Geometry is defined in model space. We can move it (translation),//  rotate it (rotation), or stretch it (scaling) using a world transform.//  The geometry is then said to be in world space. Next, we need to//  position the camera, or eye point, somewhere to look at the geometry.//  Another transform, via the view matrix, is used, to position and//  rotate our view. With the geometry then in view space, our last//  transform is the projection transform, which "projects" the 3D scene//  into our 2D viewport.////  Note that in this tutorial, we are introducing the use of D3DX, which//  is a set of helper utilities for D3D. In this case, we are using some//  of D3DX''s useful matrix initialization functions. To use D3DX, simply//  include <d3dx9.h> and link with d3dx9.lib.//// Copyright (c) Microsoft Corporation. All rights reserved.//-----------------------------------------------------------------------------#include <Windows.h>#include <mmsystem.h>#include <d3dx9.h>//-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9   g_pD3D  = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9  g_pd3dDevice = NULL; // Our rendering deviceLPDIRECT3DVERTEXBUFFER9 g_pVB   = NULL; // Buffer to hold vertices// A structure for our custom vertex typestruct CUSTOMVERTEX{    FLOAT x, y, z; // The untransformed, 3D position for the vertex    DWORD color;   // The vertex color};// Our custom FVF, which describes our custom vertex structure#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)//-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D( HWND hWnd ){    // Create the D3D object.    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )   return E_FAIL;    // Set up the structure used to create the D3DDevice    D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Create the D3DDevice    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,   D3DCREATE_SOFTWARE_VERTEXPROCESSING,   &d3dpp, &g_pd3dDevice ) ) )    {   return E_FAIL;    }    // Turn off culling, so we see the front and back of the triangle    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    // Turn off D3D lighting, since we are providing our own vertex colors    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );    return S_OK;}//-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Creates the scene geometry//-----------------------------------------------------------------------------HRESULT InitGeometry(){    // Initialize three vertices for rendering a triangle    CUSTOMVERTEX g_Vertices[] =    {   { -1.0f,-1.0f, 0.0f, 0xffff0000, },   {  1.0f,-1.0f, 0.0f, 0xff0000ff, },   {  0.0f, 1.0f, 0.0f, 0xffffffff, },    };    // Create the vertex buffer.    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {   return E_FAIL;    }    // Fill the vertex buffer.    VOID* pVertices;    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )   return E_FAIL;    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );    g_pVB->Unlock();    return S_OK;}//-----------------------------------------------------------------------------// Name: Cleanup()// Desc: Releases all previously initialized objects//-----------------------------------------------------------------------------VOID Cleanup(){    if( g_pVB != NULL )   g_pVB->Release();    if( g_pd3dDevice != NULL )   g_pd3dDevice->Release();    if( g_pD3D != NULL )   g_pD3D->Release();}//-----------------------------------------------------------------------------// Name: SetupMatrices()// Desc: Sets up the world, view, and projection transform matrices.//-----------------------------------------------------------------------------VOID SetupMatrices(){    // For our world matrix, we will just rotate the object about the y-axis.    D3DXMATRIXA16 matWorld;    // Set up the rotation matrix to generate 1 full rotation (2*PI radians)// every 1000 ms. To avoid the loss of precision inherent in very high// floating point numbers, the system time is modulated by the rotation// period before conversion to a radian angle.    UINT  iTime  = timeGetTime() % 1000;    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;    D3DXMatrixRotationY( &matWorld, fAngle );    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );    // Set up our view matrix. A view matrix can be defined given an eye point,    // a point to lookat, and a direction for which way is up. Here, we set the    // eye five units back along the z-axis and up three units, look at the    // origin, and define "up" to be in the y-direction.    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );    D3DXMATRIXA16 matView;    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );    // For the projection matrix, we set up a perspective transform (which    // transforms geometry from 3D view space to 2D viewport space, with    // a perspective divide making objects smaller in the distance). To build    // a perpsective transform, we need the field of view (1/4 pi is common),    // the aspect ratio, and the near and far clipping planes (which define at    // what distances geometry should be no longer be rendered).    D3DXMATRIXA16 matProj;    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );}//-----------------------------------------------------------------------------// Name: Render()// Desc: Draws the scene//-----------------------------------------------------------------------------VOID Render(){    // Clear the backbuffer to a black color    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );    // Begin the scene    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )    {   // Setup the world, view, and projection matrices   SetupMatrices();   // Render the vertex buffer contents   g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );   g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );   g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );   // End the scene   g_pd3dDevice->EndScene();    }    // Present the backbuffer contents to the display    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}//-----------------------------------------------------------------------------// Name: MsgProc()// Desc: The window''s message handler//-----------------------------------------------------------------------------LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){    switch( msg )    {   case WM_DESTROY:  Cleanup();  PostQuitMessage( 0 );  return 0;    }    return DefWindowProc( hWnd, msg, wParam, lParam );}//-----------------------------------------------------------------------------// Name: WinMain()// Desc: The application''s entry point//-----------------------------------------------------------------------------INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ){    // Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,  GetModuleHandle(NULL), NULL, NULL, NULL, NULL,  "D3D Tutorial", NULL };    RegisterClassEx( &wc );    // Create the application''s window    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,GetDesktopWindow(), NULL, wc.hInstance, NULL );    // Initialize Direct3D    if( SUCCEEDED( InitD3D( hWnd ) ) )    {   // Create the scene geometry   if( SUCCEEDED( InitGeometry() ) )   {  // Show the window  ShowWindow( hWnd, SW_SHOWDEFAULT );  UpdateWindow( hWnd );  // Enter the 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 ); } elseRender();  }   }    }    UnregisterClass( "D3D Tutorial", wc.hInstance );    return 0;}[/soure] [edited by - Hansi pirman on January 1, 2003 4:37:32 PM] Thanks. Do you have any ideas on what I''m doing wrong though?[  c o d e  m a t r i x  ];  yes, you try to transform RHW vertices, which isnt possible i think.   ;  No, I''m not using RHWs. Look in the code. Thanks for your help so far!   [  c o d e  m a t r i x  ];>No, I''m not using RHWs. Look in the code.>>g_pD3DDevice->CreateVertexBuffer( 3*sizeof(vertex_t), 0, D3DFVF_XYZRHW;  In the source code i downloaded fromhttp://www22.brinkster.com/codematrix67/cmd3d3.rar(which can be downloaded only if you visit http://www22.brinkster.com/codematrix67/ first)you are definitly using RHW vertices:struct vertex_t{    float m_fX;   // X position    float m_fY;   // Y position    float m_fZ;   // Z position  float m_fRHW; // RHW DWORD m_dwColor;   // Color}; if( FAILED ( g_pD3DDevice->CreateVertexBuffer( 3*sizeof(vertex_t),0,D3DFVF_XYZRHW | D3DFVF_DIFFUSE,...g_pD3DDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );try this link: http://msdn.microsoft.com/library/en-us/directx9_c/directx/graphics/programmingguide/gettingstarted/vertexformats/vformats.asp