Original Post
I have a quick question.
In my code im trying to get the little ship like things(called Objects in the code) to move on their own axis' but they always move left, right , up , or down, no matter which direction they are pointing,
I cant figure out how to make them move , say, in the direction their nose is pointing regardless of which way there nose is pointing.
Please!!! look at my code and tell me whats going on, Ive tried forever and cant get it to work. Im still not to experienced with matrices and Transformation
PS. it compiles perfectly, no errors
[edited by - rickwi22 on April 11, 2002 1:41:36 PM]
#define STRICT
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"
// A structure for custom vertex type
struct CUSTOMVERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
FLOAT tu, tv;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
//-----------------------------------------------------------------------------
// Name: struct Object
// Desc: Structure for holding data for each object
//-----------------------------------------------------------------------------
struct Object
{
D3DXVECTOR3 vLoc; // Location/Translation
D3DXVECTOR3 vR; // Rotation vector
FLOAT fR, fG, fB; // Color of the object
D3DMATRIX matLocal;
};
struct Floor
{
D3DXVECTOR3 vLoc; // Location/Translation
D3DXVECTOR3 vR; // Rotation vector
FLOAT fR, fG, fB; // Color of the object
D3DMATRIX matLocal;
};
struct Wall
{
D3DXVECTOR3 vLoc; // Location/Translation
D3DXVECTOR3 vR; // Rotation vector
FLOAT fR, fG, fB; // Color of the object
D3DMATRIX matLocal;
};
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffers to hold vertices
LPDIRECT3DINDEXBUFFER8 m_pIB;
// Buffer to hold vertices
DWORD m_dwSizeofVertices;
DWORD m_dwSizeofIndices;
LPDIRECT3DVERTEXBUFFER8 m_pFB;
DWORD m_dwSizeofFVertices;
LPDIRECT3DVERTEXBUFFER8 m_pWB;
DWORD m_dwSizeofWVertices;
FLOAT m_fStartTimeKey, // Time reference for calculations
m_fTimeElapsed;
CUSTOMVERTEX m_pvObjectVertices[16];
WORD m_pwObjectIndices[30];
Object m_pObjects[3];
CUSTOMVERTEX m_pvFloorVertices[4];
Floor m_pFloors[1];
CUSTOMVERTEX m_pvWallVertices[4];
Wall m_pWalls[1];
LPDIRECT3DTEXTURE8 m_pFloorTexture;
LPDIRECT3DTEXTURE8 m_pWallTexture;
LPDIRECT3DTEXTURE8 m_pObjectTexture;
BYTE m_bKey[256]; // keyboard state buffer
HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
HRESULT FrameMove();
HRESULT FinalCleanup();
public:
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("First Steps to Animation");
m_bUseDepthBuffer = TRUE;
m_pWB = NULL;
m_pFB = NULL;
m_pVB = NULL;
m_pIB = NULL;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
ZeroMemory( m_bKey, 256 );
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
// Points and normals which make up a object geometry
D3DXVECTOR3 pp1 = D3DXVECTOR3( -2.0f, 0.00f, -2.0f );
D3DXVECTOR3 pp2 = D3DXVECTOR3( -2.0f, 0.00f, 2.0f );
D3DXVECTOR3 pp3 = D3DXVECTOR3( 2.0f, 0.00f, 2.0f );
D3DXVECTOR3 pp4 = D3DXVECTOR3( 2.0f, 0.00f, -2.0f );
D3DXVECTOR3 nn1 = D3DXVECTOR3( -2.0f, 0.0f, -2.0f );
D3DXVec3Normalize(&nn1, &nn1);
D3DXVECTOR3 nn2 = D3DXVECTOR3( -2.f, 0.0f, 2.0f );
D3DXVec3Normalize(&nn2, &nn2);
D3DXVECTOR3 nn3 = D3DXVECTOR3( 2.0f, 0.0f, 2.0f );
D3DXVec3Normalize(&nn3, &nn3);
D3DXVECTOR3 nn4 = D3DXVECTOR3(2.0f, 0.0f, -2.0f );
D3DXVec3Normalize(&nn4, &nn4);
m_pvFloorVertices[0].p = pp1;
m_pvFloorVertices[0].n = nn1;
m_pvFloorVertices[0].tu = 0.0f;
m_pvFloorVertices[0].tv = 0.0f;
m_pvFloorVertices[1].p = pp2;
m_pvFloorVertices[1].n = nn2;
m_pvFloorVertices[1].tu = 0.0f;
m_pvFloorVertices[1].tv = 1.0f;
m_pvFloorVertices[2].p = pp3;
m_pvFloorVertices[2].n = nn3;
m_pvFloorVertices[2].tu = 1.0f;
m_pvFloorVertices[2].tv = 1.0f;
m_pvFloorVertices[3].p = pp4;
m_pvFloorVertices[3].n = nn4;
m_pvFloorVertices[3].tu = 1.0f;
m_pvFloorVertices[3].tv = 0.0f;
D3DXVECTOR3 ppp1 = D3DXVECTOR3( -2.0f, -2.00f, 0.0f );
D3DXVECTOR3 ppp2 = D3DXVECTOR3( -2.0f, 2.00f, 0.0f );
D3DXVECTOR3 ppp3 = D3DXVECTOR3( 2.0f, 2.00f, 0.0f );
D3DXVECTOR3 ppp4 = D3DXVECTOR3( 2.0f, -2.00f, 0.0f );
D3DXVECTOR3 nnn1 = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVec3Normalize(&nnn1, &nnn1);
D3DXVECTOR3 nnn2 = D3DXVECTOR3( 0.f, 2.0f, 0.0f );
D3DXVec3Normalize(&nnn2, &nnn2);
D3DXVECTOR3 nnn3 = D3DXVECTOR3( 2.0f, 2.0f, 0.0f );
D3DXVec3Normalize(&nnn3, &nnn3);
D3DXVECTOR3 nnn4 = D3DXVECTOR3(2.0f, 0.0f, 0.0f );
D3DXVec3Normalize(&nnn4, &nnn4);
m_pvWallVertices[0].p = ppp1;
m_pvWallVertices[0].n = nnn1;
m_pvWallVertices[0].tu = 0.0f;
m_pvWallVertices[0].tv = 0.0f;
m_pvWallVertices[1].p = ppp2;
m_pvWallVertices[1].n = nnn2;
m_pvWallVertices[1].tu = 1.0f;
m_pvWallVertices[1].tv = 0.0f;
m_pvWallVertices[2].p = ppp3;
m_pvWallVertices[2].n = nnn3;
m_pvWallVertices[2].tu = 1.0f;
m_pvWallVertices[2].tv = 1.0f;
m_pvWallVertices[3].p = ppp4;
m_pvWallVertices[3].n = nnn4;
m_pvWallVertices[3].tu = 0.0f;
m_pvWallVertices[3].tv = 1.0f;
D3DXVECTOR3 p1 = D3DXVECTOR3( 0.00f, 0.00f, 0.50f );
D3DXVECTOR3 p2 = D3DXVECTOR3( 0.50f, 0.00f,-0.50f );
D3DXVECTOR3 p3 = D3DXVECTOR3( 0.15f, 0.15f,-0.35f );
D3DXVECTOR3 p4 = D3DXVECTOR3(-0.15f, 0.15f,-0.35f );
D3DXVECTOR3 p5 = D3DXVECTOR3( 0.15f,-0.15f,-0.35f );
D3DXVECTOR3 p6 = D3DXVECTOR3(-0.15f,-0.15f,-0.35f );
D3DXVECTOR3 p7 = D3DXVECTOR3(-0.50f, 0.00f,-0.50f );
D3DXVECTOR3 n1 = D3DXVECTOR3( 0.2f, 1.0f, 0.0f );
D3DXVec3Normalize(&n1, &n1);
D3DXVECTOR3 n2 = D3DXVECTOR3( 0.1f, 1.0f, 0.0f );
D3DXVec3Normalize(&n2, &n2);
D3DXVECTOR3 n3 = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXVec3Normalize(&n3, &n3);
D3DXVECTOR3 n4 = D3DXVECTOR3(-0.1f, 1.0f, 0.0f );
D3DXVec3Normalize(&n4, &n4);
D3DXVECTOR3 n5 = D3DXVECTOR3(-0.2f, 1.0f, 0.0f );
D3DXVec3Normalize(&n5, &n5);
D3DXVECTOR3 n6 = D3DXVECTOR3(-0.4f, 0.0f, -1.0f );
D3DXVec3Normalize(&n6, &n6);
D3DXVECTOR3 n7 = D3DXVECTOR3(-0.2f, 0.0f, -1.0f );
D3DXVec3Normalize(&n7, &n7);
D3DXVECTOR3 n8 = D3DXVECTOR3( 0.2f, 0.0f, -1.0f );
D3DXVec3Normalize(&n8, &n8);
D3DXVECTOR3 n9 = D3DXVECTOR3( 0.4f, 0.0f, -1.0f );
D3DXVec3Normalize(&n9, &n9);
// vertices for the top
m_pvObjectVertices[0].p = p1;
m_pvObjectVertices[0].n = n1;
m_pvObjectVertices[0].tu = 0.0f;
m_pvObjectVertices[0].tv = 0.5f;
m_pvObjectVertices[1].p = p2;
m_pvObjectVertices[1].n = n2;
m_pvObjectVertices[1].tu = 0.5f;
m_pvObjectVertices[1].tv = 1.0f;
m_pvObjectVertices[2].p = p3;
m_pvObjectVertices[2].n = n3;
m_pvObjectVertices[2].tu = 0.425f;
m_pvObjectVertices[2].tv = 0.575f;
m_pvObjectVertices[3].p = p4;
m_pvObjectVertices[3].n = n4;
m_pvObjectVertices[3].tu = 0.425f;
m_pvObjectVertices[3].tv = 0.425f;
m_pvObjectVertices[4].p = p7;
m_pvObjectVertices[4].n = n5;
m_pvObjectVertices[4].tu = 0.5f;
m_pvObjectVertices[4].tv = 0.0f;
// vertices for the bottom
m_pvObjectVertices[5].p = p1;
m_pvObjectVertices[5].n = -n5;
m_pvObjectVertices[5].tu = 1.0f;
m_pvObjectVertices[5].tv = 0.5f;
m_pvObjectVertices[6].p = p2;
m_pvObjectVertices[6].n = -n4;
m_pvObjectVertices[6].tu = 0.5f;
m_pvObjectVertices[6].tv = 1.0f;
m_pvObjectVertices[7].p = p5;
m_pvObjectVertices[7].n = -n3;
m_pvObjectVertices[7].tu = 0.575f;
m_pvObjectVertices[7].tv = 0.575f;
m_pvObjectVertices[8].p = p6;
m_pvObjectVertices[8].n = -n2;
m_pvObjectVertices[8].tu = 0.575f;
m_pvObjectVertices[8].tv = 0.425f;
m_pvObjectVertices[9].p = p7;
m_pvObjectVertices[9].n = -n1;
m_pvObjectVertices[9].tu = 0.5f;
m_pvObjectVertices[9].tv = 0.0f;
// vertices for the rear
m_pvObjectVertices[10].p = p2;
m_pvObjectVertices[10].n = n6;
m_pvObjectVertices[10].tu = 0.5f;
m_pvObjectVertices[10].tv = 1.0f;
m_pvObjectVertices[11].p = p3;
m_pvObjectVertices[11].n = n7;
m_pvObjectVertices[11].tu = 0.425f;
m_pvObjectVertices[11].tv = 0.575f;
m_pvObjectVertices[12].p = p4;
m_pvObjectVertices[12].n = n8;
m_pvObjectVertices[12].tu = 0.425f;
m_pvObjectVertices[12].tv = 0.425f;
m_pvObjectVertices[13].p = p7;
m_pvObjectVertices[13].n = n9;
m_pvObjectVertices[13].tu = 0.5f;
m_pvObjectVertices[13].tv = 0.0f;
m_pvObjectVertices[14].p = p6;
m_pvObjectVertices[14].n = n8;
m_pvObjectVertices[14].tu = 0.575f;
m_pvObjectVertices[14].tv = 0.425f;
m_pvObjectVertices[15].p = p5;
m_pvObjectVertices[15].n = n7;
m_pvObjectVertices[15].tu = 0.575f;
m_pvObjectVertices[15].tv = 0.575f;
// Vertex indices for the object
m_pwObjectIndices[ 0] = 0; m_pwObjectIndices[ 1] = 1; m_pwObjectIndices[ 2] = 2;
m_pwObjectIndices[ 3] = 0; m_pwObjectIndices[ 4] = 2; m_pwObjectIndices[ 5] = 3;
m_pwObjectIndices[ 6] = 0; m_pwObjectIndices[ 7] = 3; m_pwObjectIndices[ 8] = 4;
m_pwObjectIndices[ 9] = 5; m_pwObjectIndices[10] = 7; m_pwObjectIndices[11] = 6;
m_pwObjectIndices[12] = 5; m_pwObjectIndices[13] = 8; m_pwObjectIndices[14] = 7;
m_pwObjectIndices[15] = 5; m_pwObjectIndices[16] = 9; m_pwObjectIndices[17] = 8;
m_pwObjectIndices[18] = 10; m_pwObjectIndices[19] = 15; m_pwObjectIndices[20] = 11;
m_pwObjectIndices[21] = 11; m_pwObjectIndices[22] = 15; m_pwObjectIndices[23] = 12;
m_pwObjectIndices[24] = 12; m_pwObjectIndices[25] = 15; m_pwObjectIndices[26] = 14;
m_pwObjectIndices[27] = 12; m_pwObjectIndices[28] = 14; m_pwObjectIndices[29] = 13;
// yellow object
m_pObjects[0].vLoc = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
m_pObjects[0].vR.x = 0.0f;
m_pObjects[0].vR.y = 0.0f;
m_pObjects[0].vR.z = 0.0f;
// red object
m_pObjects[1].vLoc = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
m_pObjects[1].vR.x = 0.0f;
m_pObjects[1].vR.y = 0.0f;
m_pObjects[1].vR.z = 0.0f;
//green object
m_pObjects[2].vLoc = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
m_pObjects[2].vR.x = 0.0f;
m_pObjects[2].vR.y = 0.0f;
m_pObjects[2].vR.z = 0.0f;
//floor Object
m_pFloors[0].vLoc = D3DXVECTOR3(0.0f, -2.0f, 0.0f);
m_pFloors[0].vR.x = 0.0f;
m_pFloors[0].vR.y = 0.0f;
m_pFloors[0].vR.z = 0.0f;
//walls
m_pWalls[0].vLoc = D3DXVECTOR3(-2.0f, 0.0f, 0.0f);
m_pWalls[0].vR.x = 0.0f;
m_pWalls[0].vR.y = 1.57f;
m_pWalls[0].vR.z = 1.57f;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
// timing code:
// the object should move/rotate in the same speed
// at every possible fps
const cTimeScale = 5;
// calculate elapsed time
m_fTimeElapsed=(m_fTime-m_fStartTimeKey) * cTimeScale;
// store last time
m_fStartTimeKey=m_fTime;
//**********************************************************
// red object
//**********************************************************
if (m_bKey['U']) m_pObjects[0].vLoc.z += 1.0f * m_fTimeElapsed;
if (m_bKey['J']) m_pObjects[0].vLoc.z -= 1.0f * m_fTimeElapsed;
if (m_bKey['H']) m_pObjects[0].vLoc.x -= 1.0f * m_fTimeElapsed;
if (m_bKey['L']) m_pObjects[0].vLoc.x += 1.0f * m_fTimeElapsed;
if (m_bKey['I']) m_pObjects[0].vLoc.y += 1.0f * m_fTimeElapsed;
if (m_bKey['K']) m_pObjects[0].vLoc.y -= 1.0f * m_fTimeElapsed;
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, m_pObjects[0].vLoc.x, m_pObjects[0].vLoc.y, m_pObjects[0].vLoc.z);
D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationY( &matRotateY, -m_pObjects[0].vR.y );
D3DXMatrixRotationX( &matRotateX, -m_pObjects[0].vR.x );
D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[0].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pObjects[0].matLocal = matWorld;
//**********************************************************
// blue object
//**********************************************************
if (m_bKey['W']) m_pObjects[1].vLoc.x += 1.0f * m_fTimeElapsed;
if (m_bKey['Q']) m_pObjects[1].vLoc.x -= 1.0f * m_fTimeElapsed;
if (m_bKey['D']) m_pObjects[1].vR.z -= 1.0f * m_fTimeElapsed;
if (m_bKey['A']) m_pObjects[1].vR.z += 1.0f * m_fTimeElapsed;
if (m_bKey['S']) m_pObjects[1].vR.y -= 1.0f * m_fTimeElapsed;
if (m_bKey['E']) m_pObjects[1].vR.y += 1.0f * m_fTimeElapsed;
if (m_bKey['R']) m_pObjects[1].vR.x -= 1.0f * m_fTimeElapsed;
if (m_bKey['F']) m_pObjects[1].vR.x += 1.0f * m_fTimeElapsed;
D3DXMatrixTranslation(&matWorld, m_pObjects[1].vLoc.x, m_pObjects[1].vLoc.y, m_pObjects[1].vLoc.z);
D3DXMatrixRotationY( &matRotateY, -m_pObjects[1].vR.y );
D3DXMatrixRotationX( &matRotateX, -m_pObjects[1].vR.x );
D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[1].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pObjects[1].matLocal = matWorld;
//green object
if (m_bKey['7']) m_pObjects[2].vLoc.x -= 1.0f * m_fTimeElapsed;
if (m_bKey['8']) m_pObjects[2].vLoc.x += 1.0f * m_fTimeElapsed;
if (m_bKey['1']) m_pObjects[2].vR.z -= 1.0f * m_fTimeElapsed;
if (m_bKey['2']) m_pObjects[2].vR.z += 1.0f * m_fTimeElapsed;
if (m_bKey['3']) m_pObjects[2].vR.y -= 1.0f * m_fTimeElapsed;
if (m_bKey['4']) m_pObjects[2].vR.y += 1.0f * m_fTimeElapsed;
if (m_bKey['5']) m_pObjects[2].vR.x -= 1.0f * m_fTimeElapsed;
if (m_bKey['6']) m_pObjects[2].vR.x += 1.0f * m_fTimeElapsed;
D3DXMatrixTranslation(&matWorld, m_pObjects[2].vLoc.x, m_pObjects[2].vLoc.y, m_pObjects[2].vLoc.z);
D3DXMatrixRotationY( &matRotateY, -m_pObjects[2].vR.y );
D3DXMatrixRotationX( &matRotateX, -m_pObjects[2].vR.x );
D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[2].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pObjects[2].matLocal = matWorld;
//////////////////floor
D3DXMatrixTranslation(&matWorld, m_pFloors[0].vLoc.x, m_pFloors[0].vLoc.y, m_pFloors[0].vLoc.z);
D3DXMatrixRotationY( &matRotateY, -m_pFloors[0].vR.y );
D3DXMatrixRotationX( &matRotateX, -m_pFloors[0].vR.x );
D3DXMatrixRotationZ( &matRotateZ, -m_pFloors[0].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pFloors[0].matLocal = matWorld;
/////////////////////wall
D3DXMatrixTranslation(&matWorld, m_pWalls[0].vLoc.x, m_pWalls[0].vLoc.y, m_pWalls[0].vLoc.z);
D3DXMatrixRotationY( &matRotateY, -m_pWalls[0].vR.y );
D3DXMatrixRotationX( &matRotateX, -m_pWalls[0].vR.x );
D3DXMatrixRotationZ( &matRotateZ, -m_pWalls[0].vR.z );
D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
m_pWalls[0].matLocal = matWorld;
//************************************************************
// camera stuff
//************************************************************
static D3DXVECTOR3 vLook=D3DXVECTOR3(0.0f,0.0f,1.0);
static D3DXVECTOR3 vUp=D3DXVECTOR3(0.0f,1.0f,0.0f);
static D3DXVECTOR3 vRight=D3DXVECTOR3(1.0f,0.0f,0.0f);
static D3DXVECTOR3 vPos=D3DXVECTOR3(0.0f,0.5f,-5.0f);
FLOAT fPitch,fYaw,fRoll;
fPitch = fYaw = fRoll = 0.0f;
FLOAT fspeed= 1.0f * m_fTimeElapsed;
if (m_bKey[VK_UP]) fPitch=-0.3f * m_fTimeElapsed; // fPitch
if (m_bKey[VK_DOWN]) fPitch=+0.3f * m_fTimeElapsed;
if (m_bKey['C']) fYaw=+0.3f * m_fTimeElapsed; // fYaw
if (m_bKey['X']) fYaw=-0.3f * m_fTimeElapsed;
if (m_bKey[VK_LEFT]) fRoll=-0.3f * m_fTimeElapsed; // fRoll
if (m_bKey[VK_RIGHT]) fRoll=+0.3f * m_fTimeElapsed;
// camera forward
if (m_bKey[VK_HOME]) // Key HOME
{
vPos.x+=fspeed*vLook.x;
vPos.y+=fspeed*vLook.y;
vPos.z+=fspeed*vLook.z;
}
// camera back
if (m_bKey[VK_END]) // Key END
{
vPos.x-=fspeed*vLook.x;
vPos.y-=fspeed*vLook.y;
vPos.z-=fspeed*vLook.z;
}
// base vector regeneration
D3DXVec3Normalize(&vLook, &vLook);
D3DXVec3Cross(&vRight, &vUp, &vLook); // Cross Product of the UP and LOOK Vector
D3DXVec3Normalize(&vRight, &vRight);
D3DXVec3Cross(&vUp, &vLook, &vRight); // Cross Product of the RIGHT and LOOK Vector
D3DXVec3Normalize(&vUp, &vUp);
// Matrices for pitch, yaw and roll
D3DXMATRIX matPitch, matYaw, matRoll;
D3DXMatrixRotationAxis(&matPitch, &vRight, fPitch );
D3DXMatrixRotationAxis(&matYaw, &vUp, fYaw );
D3DXMatrixRotationAxis(&matRoll, &vLook, fRoll);
// rotate the LOOK & RIGHT Vectors about the UP Vector
D3DXVec3TransformCoord(&vLook, &vLook, &matYaw);
D3DXVec3TransformCoord(&vRight, &vRight, &matYaw);
// rotate the LOOK & UP Vectors about the RIGHT Vector
D3DXVec3TransformCoord(&vLook, &vLook, &matPitch);
D3DXVec3TransformCoord(&vUp, &vUp, &matPitch);
// rotate the RIGHT & UP Vectors about the LOOK Vector
D3DXVec3TransformCoord(&vRight, &vRight, &matRoll);
D3DXVec3TransformCoord(&vUp, &vUp, &matRoll);
D3DXMATRIX view=matWorld;
D3DXMatrixIdentity( &view );
view._11 = vRight.x; view._12 = vUp.x; view._13 = vLook.x;
view._21 = vRight.y; view._22 = vUp.y; view._23 = vLook.y;
view._31 = vRight.z; view._32 = vUp.z; view._33 = vLook.z;
view._41 = - D3DXVec3Dot( &vPos, &vRight );
view._42 = - D3DXVec3Dot( &vPos, &vUp );
view._43 = - D3DXVec3Dot( &vPos, &vLook );
m_pd3dDevice->SetTransform(D3DTS_VIEW, &view);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
// Clear the viewport | z-buffer
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x00000000, 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// yellow object
// Set the color for the object
m_pd3dDevice->SetTexture( 0, m_pObjectTexture);
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[0].matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
16, // number of vertices
0,
10); // number of primitives
// red object
// Set the color for the object
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[1].matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
16, // number of vertices
0,
10); // number of primitives
// green object
// Set the color for the object
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pObjects[2].matLocal );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetIndices( m_pIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
16, // number of vertices
0,
10);// number of primitives
/////////////////floor
m_pd3dDevice->SetTexture( 0, m_pFloorTexture);
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pFloors[0].matLocal );
m_pd3dDevice->SetStreamSource( 0,m_pFB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2); // number of primitives
//upside down floor
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
// Apply the object's local matrix
m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_pWalls[0].matLocal );
m_pd3dDevice->SetStreamSource( 0,m_pWB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2); // number of primitives
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("floor.bmp"), &m_pFloorTexture)))
return D3DAPPERR_MEDIANOTFOUND;
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("wall.bmp"), &m_pWallTexture)))
return D3DAPPERR_MEDIANOTFOUND;
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("object.bmp"), &m_pObjectTexture)))
return D3DAPPERR_MEDIANOTFOUND;
m_pFont->InitDeviceObjects( m_pd3dDevice );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();
D3DMATERIAL8 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// Set the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// Set up the lights
if( m_d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_DIRECTIONALLIGHTS )
{
D3DLIGHT8 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 1.5f, 0.0f, 0.0f );
light.Ambient.r = 0.1f;
light.Ambient.g = 0.1f;
light.Ambient.b = 0.1f;
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f, -1.5f, 0.0f );
light.Ambient.r = 0.2f;
light.Ambient.g = 0.2f;
light.Ambient.b = 0.2f;
m_pd3dDevice->SetLight( 1, &light );
m_pd3dDevice->LightEnable( 1, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
}
else
{
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x0c0c0c0c);
}
// fill the floor vertex buffer
m_dwSizeofFVertices = sizeof(m_pvFloorVertices);
// Create the floor vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofFVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pFB ) ) )
return E_FAIL;
VOID* pVertices;
if( FAILED( m_pFB->Lock( 0, m_dwSizeofFVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, m_pvFloorVertices, m_dwSizeofFVertices);
m_pFB->Unlock();
//fill wall vertex buffer
m_dwSizeofWVertices = sizeof(m_pvWallVertices);
// Create the wall vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofWVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pWB ) ) )
return E_FAIL;
if( FAILED( m_pWB->Lock( 0, m_dwSizeofWVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, m_pvWallVertices, m_dwSizeofWVertices);
m_pWB->Unlock();
// fill the vertex buffer
m_dwSizeofVertices = sizeof(m_pvObjectVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;
//VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, m_pvObjectVertices, m_dwSizeofVertices);
m_pVB->Unlock();
// fill the Index buffer
m_dwSizeofIndices = sizeof(m_pwObjectIndices);
// Create the index buffer
if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_dwSizeofIndices,
0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &m_pIB ) ) )
return E_FAIL;
VOID* pIndices;
if( FAILED( m_pIB->Lock( 0, m_dwSizeofIndices, (BYTE**)&pIndices, 0 ) ) )
return E_FAIL;
memcpy( pIndices, m_pwObjectIndices, m_dwSizeofIndices );
m_pIB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pIB );
SAFE_RELEASE( m_pFB );
SAFE_RELEASE( m_pWB );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
SAFE_RELEASE( m_pFloorTexture );
SAFE_RELEASE( m_pWallTexture );
SAFE_RELEASE( m_pObjectTexture );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
// Record key presses
if( WM_KEYDOWN == uMsg )
{
m_bKey[wParam] = 1;
}
// Perform commands when keys are released
if( WM_KEYUP == uMsg )
{
m_bKey[wParam] = 0;
}
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}