This topic is now archived and is closed to further replies.


Direct3D Newbie

Recommended Posts

Anarchi    122
I''m attempting to learn Direct3D8, please bare with me.. Basically I dont understand the concept of matrixes, 3D space positions, setting up lights, cameras etc... I have Direct3D up and running, ive loaded a terrain X file and it displays on the screen ok, though I ripped the code from one of the SDK sample apps so I dont really know how it all works. I tried to load other x files but they do not show up on the screen. Obviously I need to position the x file (mesh) within the current matrix/projection (or something). Please explain to me what the below code does, or even direct me to a tutorial that explains how it all works. (btw: The code was used from the billboards SDK sample app). This is the code I''m using at the moment: ------------------------------------------ On initialization (setting up the matrix)...
    D3DXMATRIX matProj;
    FLOAT fAspect = GfxSys.HorzSize / (FLOAT)GfxSys.VertSize;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

    // Set up the default texture states

    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP );

    g_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE ); // False

    // HACK Make a light... (doesnt show up)

    D3DLIGHT8 Light;
    D3DCOLORVALUE   color;
    D3DVECTOR Vector;
    Vector.x = 0.0f;
    Vector.y = -10.0f;
    Vector.z = 0.0f;
    color.r = 155.0f;
    color.g = 155.0f;
    color.b = 255.0f;
    color.a = 255.0f;

    Light.Type     = D3DLIGHT_POINT;
    //color = D3DCOLOR_COLORVALUE(255,255,255,255);

    Light.Diffuse  = color;
    Light.Specular = color;
    Light.Ambient  = color;
    Light.Position = Vector;

    Light.Range = 100.0f;
    Light.Falloff = 1.0f;
    Light.Attenuation0 = 10.0f;
    Light.Attenuation1 = 10.0f;
    Light.Attenuation2 = 10.0f;
    Light.Theta  = 5.0f;
    Light.Phi  = D3DX_PI;

    g_pd3dDevice->SetLight(0, &Light);
    g_pd3dDevice->LightEnable(0, true);

This is what call to rotate the terrain...

// Name: FrameMove()

// Desc: Called once per frame, the call is the entry point for animating

//       the scene.


HRESULT FrameMove()
    // Get the eye and lookat points from the camera''s path

    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXVECTOR3 vEyePt;
    D3DXVECTOR3 vLookatPt;

    //float m_fTime = g_fCalcDelta

    static int i = 0;
    if(i > 1000) i = 0;
    float m_fTime;
    m_fTime = float(i) / 1000;

    vEyePt.x = 30.0f*cosf( 0.8f * ( m_fTime ) );
    vEyePt.z = 30.0f*sinf( 0.8f * ( m_fTime ) );
    vEyePt.y = 4 + HeightField( vEyePt.x, vEyePt.z );

    vLookatPt.x = 30.0f*cosf( 0.8f * ( m_fTime + 0.5f ) );
    vLookatPt.z = 30.0f*sinf( 0.8f * ( m_fTime + 0.5f ) );
    vLookatPt.y = vEyePt.y - 1.0f;

    // Set the app view matrix for normal viewing

    D3DXMATRIX matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

    return S_OK;
I load and render the xfile like so...

// Load the x file (on init)...

lpMesh = new CD3DMesh();
HRESULT hr = lpMesh->Create( g_pd3dDevice, _T("Terrain.x");
    delete lpMesh;
    return false;

// Render the mesh (per frame)

lpMesh->Render( g_pd3dDevice );

A few questions... 1) Is the initial matrix the main world matrix? Do all other matrixes reside within the world matrix? 2) To move the camera do I move a matrix or do I move the camera (If I have one)? 3) How big (in x,y,z float values) is the world matrix? 4) Why isnt my light working? Which values are incorrect? Thanks in advance.

Downloads, D3DXSprite tutorial, New platform game: .-= The ZeroOne Realm =-.

Share this post

Link to post
Share on other sites
Biggles    122
I think you may have the wrong idea about matrices. Matricies do not store data. The world matrix does not store the world with other matricies inside it that represent objects.
Matricies are mathematical things used to transform values. They are particularly useful for geometrical transformations, ie moving things around in 2D or 3D space. The world matrix is used to transform the coordinates of an object''s vertices from being based on the object''s origin to based on the world origin. Likewise the camera matricies are used to transform things like the camera position, look-at point, etc. To move the camera you multiply it''s position coordinates with a transformation matrix.


Never eat anything bigger than your own head.

Share this post

Link to post
Share on other sites