Jump to content
  • Advertisement
Sign in to follow this  
calidev234

Mimic MeshViewer Movements

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to mimic the mouse movements of the MeshViewer (or the skinnedmesh sample). For example when you right-click and drag it moves the mesh off center; as well, when you push down on the mouse scroll this changes the zoom ratio. I was looking at the SkinnedMesh sample, but I could not figure this out. Below is the code: I'm looking for help with the: // STAND ALONE void Movements(HWND hwnd) Here is the full code:
// -------------------------------------
// A. VARIABLE DEFINITION
// B. WINDOW EVENTS
// C. MODEL EVENTS (LOAD, DELETE, etc)
// D. GAME EVENTS (INIT, RUN, etc)
// E. MOVEMENT EVENTS
// -------------------------------------
#include "stdafx.h"
#include "AdvViewer.h"
#include "game.h"
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>
#include <stdio.h>
#include "dxgraphics.h"
//#include "dxaudio.h"
#include "dxinput.h"

// -------------------------------------
// A. VARIABLE DEFINITION
// -------------------------------------
#define WHITE D3DCOLOR_ARGB(0,255,255,255)
#define BLACK D3DCOLOR_ARGB(0,0,0,0)
#define GREY D3DCOLOR_ARGB(0,120,120,120)

#define CAMERA_X 3655.0f
#define CAMERA_Y 3610.0f
#define CAMERA_Z 3655.0f
#define FLT_MAX  3.402823466e+38F        /* max value */

int g_MeshCnt;
bool g_WalkThrough;

float const PI = 3.14159265358979;
float const RAD = PI / 180;
float const DEG = 180 / PI;
float g_Angle=0, g_Angle2=0;

bool bWireframe;
float g_LastMouseX;
float g_LastMouseY;
float g_Radius = 0.003;

float g_CurrentScaling=1;

float ViewDirX, ViewDirY;
float cXmove, cYmove;
long LastUpdated, systemframe;
float PosY=0, PosX, PosZ;
float LookX=0, LookY=0, LookZ=0;
 
//define the MODEL struct
struct MODEL
{
    LPD3DXMESH mesh;
    D3DMATERIAL9* materials;
    LPDIRECT3DTEXTURE9* textures;
    DWORD material_count;
	float Scale;
	int OffX;
	int OffY;
	int OffZ;
	float Rotate;
};

struct jLIGHT
{
	int type;				//0-Directional; 1-Spot

	bool DiffuseDummy;
	float D_R, D_G, D_B;

	bool SpecularDummy;
	float S_R, S_G, S_B;

	bool AmbientDummy;
	float A_R, A_G, A_B;
	
	bool DirectionalDummy;
	float Dir_Range, Dir_FallOff, Dir_Theta, Dir_Phi, Dir_Atten0, Dir_Atten1, Dir_Atten2;

	bool SpotDummy;
	float Spot_Range, Spot_FallOff, Spot_Theta, Spot_Phi, Spot_Atten0, Spot_Atten1, Spot_Atten2;

	bool PointDummy;
	float Point_Range, Point_FallOff, Point_Theta, Point_Phi, Point_Atten0, Point_Atten1, Point_Atten2;
};

MODEL *car;
MODEL *car2;
MODEL *g_MeshAry;

jLIGHT *m_Light;

CString *g_sMeshFileAry;
float *g_fMeshRotateAry;
float *g_fMeshScaleAry;
int *g_iMeshOffX;
int *g_iMeshOffY;
int *g_iMeshOffZ;

// -------------------------------------
// B. WINDOW EVENTS
// -------------------------------------
int StartDirectX(int iMeshCnt, bool bWalkThrough, CString sMeshFile[], float tmpMeshRotateAry[], float tmpMeshScaleAry[], int tmpMeshOffXAry[], int tmpMeshOffYAry[], int tmpMeshOffZAry[])
{

	g_MeshCnt = iMeshCnt;

	g_sMeshFileAry = new CString[g_MeshCnt];
	g_fMeshRotateAry = new float[g_MeshCnt];
	g_fMeshScaleAry = new float[g_MeshCnt];
	g_iMeshOffX = new int[g_MeshCnt];
	g_iMeshOffY = new int[g_MeshCnt];
	g_iMeshOffZ = new int[g_MeshCnt];

	g_sMeshFileAry = sMeshFile;
	g_fMeshRotateAry = tmpMeshRotateAry;
	g_fMeshScaleAry = tmpMeshScaleAry;
	g_iMeshOffX = tmpMeshOffXAry;
	g_iMeshOffY = tmpMeshOffYAry;
	g_iMeshOffZ = tmpMeshOffZAry;

	g_WalkThrough = bWalkThrough;

	m_Light = new jLIGHT();
	m_Light->type = atoi(m_pApp.GetValue("LIGHT_TYPE"));
	m_Light->DiffuseDummy = m_pApp.GetValue("LIGHT_DIFFUSE_DUMMY");
	m_Light->D_R = atof(m_pApp.GetValue("LIGHT_DIFFUSE_R"));
	m_Light->D_G = atof(m_pApp.GetValue("LIGHT_DIFFUSE_G"));
	m_Light->D_B = atof(m_pApp.GetValue("LIGHT_DIFFUSE_B"));

	m_Light->SpecularDummy = atof(m_pApp.GetValue("LIGHT_SPECULAR_DUMMY"));
	m_Light->S_R = atof(m_pApp.GetValue("LIGHT_SPECULAR_R"));
	m_Light->S_G = atof(m_pApp.GetValue("LIGHT_SPECULAR_G"));
	m_Light->S_B = atof(m_pApp.GetValue("LIGHT_SPECULAR_B"));

	m_Light->AmbientDummy = atof(m_pApp.GetValue("LIGHT_AMBIENT_DUMMY"));
	m_Light->A_R = atof(m_pApp.GetValue("LIGHT_AMBIENT_R"));
	m_Light->A_G = atof(m_pApp.GetValue("LIGHT_AMBIENT_G"));
	m_Light->A_B = atof(m_pApp.GetValue("LIGHT_AMBIENT_B"));

	m_Light->DirectionalDummy = m_pApp.GetValue("LIGHT_DIRECTIONAL_DUMMY");
	m_Light->Dir_Range = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_RANGE"));
	m_Light->Dir_FallOff = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_FALLOFF"));
	m_Light->Dir_Theta = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_THETA"));
	m_Light->Dir_Phi = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_PHI"));
	m_Light->Dir_Atten0 = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_ATTENUATION0"));
	m_Light->Dir_Atten1 = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_ATTENUATION1"));
	m_Light->Dir_Atten2 = atof(m_pApp.GetValue("LIGHT_DIRECTIONAL_ATTENUATION2"));

	m_Light->SpotDummy = m_pApp.GetValue("LIGHT_SPOT_DUMMY");
	m_Light->Spot_Range = atof(m_pApp.GetValue("LIGHT_SPOT_RANGE"));
	m_Light->Spot_FallOff = atof(m_pApp.GetValue("LIGHT_SPOT_FALLOFF"));
	m_Light->Spot_Theta = atof(m_pApp.GetValue("LIGHT_SPOT_THETA"));
	m_Light->Spot_Phi = atof(m_pApp.GetValue("LIGHT_SPOT_PHI"));
	m_Light->Spot_Atten0 = atof(m_pApp.GetValue("LIGHT_SPOT_ATTENUATION0"));
	m_Light->Spot_Atten1 = atof(m_pApp.GetValue("LIGHT_SPOT_ATTENUATION1"));
	m_Light->Spot_Atten2 = atof(m_pApp.GetValue("LIGHT_SPOT_ATTENUATION2"));

	m_Light->PointDummy = m_pApp.GetValue("LIGHT_POINT_DUMMY");
	m_Light->Point_Range = atof(m_pApp.GetValue("LIGHT_POINT_RANGE"));
	m_Light->Point_FallOff = atof(m_pApp.GetValue("LIGHT_POINT_FALLOFF"));
	m_Light->Point_Theta = atof(m_pApp.GetValue("LIGHT_POINT_THETA"));
	m_Light->Point_Phi = atof(m_pApp.GetValue("LIGHT_POINT_PHI"));
	m_Light->Point_Atten0 = atof(m_pApp.GetValue("LIGHT_POINT_ATTENUATION0"));
	m_Light->Point_Atten1 = atof(m_pApp.GetValue("LIGHT_POINT_ATTENUATION1"));
	m_Light->Point_Atten2 = atof(m_pApp.GetValue("LIGHT_POINT_ATTENUATION2"));

	
	if (g_WalkThrough)
	{
	}
	else
	{
	}

	int iReturn = JWinMain();
	return iReturn;
}

//entry point for a Windows program
int JWinMain()
{
	MSG msg;
    HWND hWnd;

	HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();
	int       nCmdShow=1;

	msg.message = 0;
	
	// register the class
	MyRegisterClass(hInstance);

    //set up the screen in windowed or fullscreen mode?
    DWORD style;
    if (FULLSCREEN)	style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
    //else	        style = WS_OVERLAPPEDWINDOW | WS_POPUP;
	else			style = WS_OVERLAPPEDWINDOW | WS_POPUPWINDOW;

	//CWindow gameWnd = new CWindow();
	
    //create a new window
    hWnd = CreateWindow(
       APPTITLE,				//window class
       APPTITLE,				//title bar
       style,					//window style
       CW_USEDEFAULT,			//x position of window
       CW_USEDEFAULT,			//y position of window
       SCREEN_WIDTH,			//width of the window
       SCREEN_HEIGHT,			//height of the window
       NULL,					//parent window
       NULL,					//menu
       hInstance,				//application instance
       NULL);					//window parameters
	
	CloseWindow(hWnd);
    if (!hWnd) return FALSE;	//was there an error creating the window?
    ShowWindow(hWnd, nCmdShow);	//display the window
    UpdateWindow(hWnd);
	
    //Initialize: Direct3D
	if (!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
	{
		ShowWindow(hWnd, 0);	//display the window
		return 0;
	}

    //Initialize: DirectSound
    //if (!Init_DirectSound(hWnd))
    //{
    //    MessageBox(hWnd, "Error initializing DirectSound", "Error", MB_OK);
    //    return 0;
	//}

    //Initialize: DirectInput
    if (!Init_DirectInput(hWnd))
    {
        MessageBox(hWnd, "Error initializing DirectInput", "Error", MB_OK);
        return 0;
    }
    
	//Initialize: GAME
    if (!Game_Init(hWnd))
    {
        MessageBox(hWnd, "Error initializing the game", "Error", MB_OK);
        return 0;
    }

	// ------------------------------------
    // Main Message Loop (START)
	// ------------------------------------
	while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
	    {
		    TranslateMessage(&msg);	//decode and pass messages on to WndProc
		    DispatchMessage(&msg);
	    }
        else
            Game_Run(hWnd);			//process game loop (else prevents running after window is closed)
    }
	// ------------------------------------
    // Main Message Loop (END)
	// ------------------------------------

	return msg.wParam;
}


//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX); 

    //fill the struct with info
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WinProc;
    wc.cbClsExtra	 = 0;
    wc.cbWndExtra	 = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm       = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);
}

//window event callback function
LRESULT WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            if (d3ddev != NULL) d3ddev->Release();			//release the Direct3D objects
            if (d3d != NULL) d3d->Release();
            //if (dsound != NULL) delete dsound;				//release sound objects (this is a class not a DX object)

            //release input objects
            if (dinput != NULL) dinput->Release();
            Kill_Keyboard();
            Kill_Mouse();

            //call the "front-end" shutdown function
            Game_End(hWnd);

			//------------------------------------------------------
			D3DPRESENT_PARAMETERS d3dpp;
			d3dpp.BackBufferWidth            = 800;
			d3dpp.BackBufferHeight           = 600;
			d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
			d3dpp.BackBufferCount            = 1;
			d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
			d3dpp.MultiSampleQuality         = 0;
			d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
			d3dpp.hDeviceWindow              = hWnd;
			d3dpp.Windowed                   = true; // XXX turn OFF fullscreen	mode
			d3dpp.EnableAutoDepthStencil     = true;
			d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
			d3dpp.Flags                      = 0;
			d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
			d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE; 
			d3ddev->Reset(&d3dpp);
			//------------------------------------------------------

			//tell Windows to kill this program
            PostQuitMessage(0);
            return 0;
		case WM_SIZE:
			Game_Reset(hWnd);
			break;
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

// -------------------------------------
// C. MODEL EVENTS (LOAD, DELETE, etc)
// -------------------------------------
MODEL *LoadModel(CString filename, float tScale, int tOffX, int tOffY, int tOffZ, float tRotate)
{

	CString strRootPath="";
	int iEnd=0;
	iEnd = filename.ReverseFind('\\');
	strRootPath = filename.Mid(0, iEnd + 1);

    MODEL *model = (MODEL*)malloc(sizeof(MODEL));
    LPD3DXBUFFER matbuffer;
    HRESULT result;

	model->Scale = tScale;
	model->OffX = tOffX;
	model->OffY = tOffY;
	model->OffZ = tOffZ;
	model->Rotate = tRotate;

    //load mesh from the specified file
    result = D3DXLoadMeshFromX(
        filename,               //filename
        D3DXMESH_SYSTEMMEM,     //mesh options
        d3ddev,                 //Direct3D device
        NULL,                   //adjacency buffer
        &matbuffer,             //material buffer
        NULL,                   //special effects
        &model->material_count, //number of materials
        &model->mesh);          //resulting mesh

    if (result != D3D_OK)
    {
        MessageBox(NULL, "Error loading model file", "Error", MB_OK);
        return NULL;
    }

    //extract material properties and texture names from material buffer
    LPD3DXMATERIAL d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();
    model->materials = new D3DMATERIAL9[model->material_count];
    model->textures  = new LPDIRECT3DTEXTURE9[model->material_count];

    //create the materials and textures
    for(DWORD i=0; i<model->material_count; i++)
    {
        model->materials = d3dxMaterials.MatD3D;				//grab the material
        model->materials.Ambient = model->materials.Diffuse;	//set ambient color for material 
        model->textures = NULL;

        if( d3dxMaterials.pTextureFilename != NULL && lstrlen(d3dxMaterials.pTextureFilename) > 0 )
        {
            //load texture file specified in .x file
			// Note: you must get the root path here
			// d3dxMaterials.pTextureFilename
            result = D3DXCreateTextureFromFile(d3ddev, strRootPath + d3dxMaterials.pTextureFilename, &model->textures);
            if (result != D3D_OK)
            {
                MessageBox(NULL, "Could not find texture file\n" + strRootPath + d3dxMaterials.pTextureFilename, "Error", MB_OK);
                return NULL;
            }
        }
    }
    matbuffer->Release();	//done using material buffer
    return model;
}

VOID DeleteModel(MODEL *model)
{
    if( model->materials != NULL ) delete[] model->materials;	//remove materials from memory
    if (model->textures != NULL)								//remove textures from memory
    {
        for( DWORD i = 0; i < model->material_count; i++)
        {
            if (model->textures != NULL) model->textures->Release();
        }
        delete[] model->textures;
    }
    if (model->mesh != NULL) model->mesh->Release();			//remove mesh from memory
    if (model != NULL) free(model);								//remove model struct from memory
}

void DrawModel(MODEL *model, bool jDummy)
{
	//---------------------------------------------------------------------
	//ADDED: (START)
	//---------------------------------------------------------------------
	//Rotate World

	D3DXMATRIXA16 matWorld;
	D3DXMATRIXA16 matWorld2;
	D3DXMATRIXA16 matWorldScale;

	if (!g_WalkThrough)
	{
		D3DXMatrixIdentity(&matWorld);

		//Try 1
		//D3DXVECTOR3 vRot2(1000, 0, 0);
		//D3DXMatrixRotationAxis(&matWorld, &vRot2, -g_Angle * RAD);
		//D3DXVECTOR3 vRot3(0, 1000, 0);
		//D3DXMatrixRotationAxis(&matWorld2, &vRot2, g_Angle2 * RAD);

		//Try 2
		D3DXMatrixRotationY(&matWorld, g_Angle2 * RAD);
		D3DXMatrixRotationX(&matWorld2, g_Angle * RAD);

		D3DXMatrixScaling( &matWorldScale, g_CurrentScaling, g_CurrentScaling, g_CurrentScaling);
	}

	D3DXMATRIXA16 mW;
	D3DXMatrixIdentity( &mW );

	if (jDummy)
	{
		LPVOID pVerts = NULL;
		if( SUCCEEDED( model->mesh->LockVertexBuffer( 0, &pVerts ) ) )
		{
			float fRadius;
			D3DXVECTOR3 vCtr;
			if( SUCCEEDED(D3DXComputeBoundingSphere( (const D3DXVECTOR3*)pVerts,
										model->mesh->GetNumVertices(),
										model->mesh->GetNumBytesPerVertex(),
										&vCtr, &fRadius ) ) )
			{
				//Scale
				D3DXMATRIXA16 mScale;
				D3DXMatrixScaling( &mScale, model->Scale, model->Scale, model->Scale);
                D3DXMatrixMultiply( &mW, &mW, &mScale );
				//d3ddev->SetTransform(D3DTS_WORLD, &mW);

				//Transpose
				D3DXMATRIXA16 mTrans;
				D3DXMatrixTranslation( &mTrans, model->OffX, model->OffY, model->OffZ );
				D3DXMatrixMultiply( &mW, &mW, &mTrans );
				d3ddev->SetTransform(D3DTS_WORLD, &mW);

				//Rotation (around a point)
				D3DXMATRIXA16 mRot;
				D3DXVECTOR3 vRot(model->OffX, model->OffY, model->OffZ);
				D3DXMatrixRotationAxis(&mRot, &vRot, model->Rotate);
				D3DXMatrixMultiply( &mW, &mW, &mRot );
				d3ddev->SetTransform(D3DTS_WORLD, &mW);

			}
			model->mesh->UnlockVertexBuffer();
		}
	}

	//---------------------------------------------------------------------
	//ADDED: (END)
	//---------------------------------------------------------------------
	if (!g_WalkThrough)
	{
		D3DXMatrixMultiply(&mW, &mW, &matWorld);
		D3DXMatrixMultiply(&mW, &mW, &matWorld2);
		D3DXMatrixMultiply(&mW, &mW, &matWorldScale);
	}


	d3ddev->SetTransform(D3DTS_WORLD, &mW);

	//draw each mesh subset
    for( DWORD i=0; i<model->material_count; i++ )
    {
		d3ddev->SetMaterial( &model->materials );			// Set the material and texture for this subset
        d3ddev->SetTexture( 0, model->textures );
        model->mesh->DrawSubset( i );							// Draw the mesh subset
    }
}

// -------------------------------------
// D. GAME EVENTS (INIT, RUN, etc)
// -------------------------------------
int Game_Init(HWND hwnd)
{
	if (d3ddev != NULL) d3ddev->Release();			//release the Direct3D objects
    if (d3d != NULL) d3d->Release();

	// global variables reset 
	bWireframe = false;

	ViewDirX = 90;
    ViewDirY = 0;
	PosZ = 15;
	
    if (!Init_Keyboard(hwnd))
    {
        MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
        return 0;
    }
	if (!Init_Mouse(hwnd))
    {
        MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
        return 0;
    }

	// ----------------------------------------------------------------------------
	// 1. Set the Camera and Perspective
	// ----------------------------------------------------------------------------
    SetCamera(CAMERA_X, CAMERA_Y, CAMERA_Z, 1000, 1000, 0);
    //float ratio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
    //SetPerspective(23.0f, ratio, 0.1f, 10000.0f);
	//SetPerspective(0.25f*3.141592654f, 1.3333f, 1.0f, 2000.0f);

	RECT r;
    GetClientRect( hwnd, &r );
    r.top += 28;
    r.bottom -= 20;
	float fAspect = (float)(r.right - r.left) / (float)(r.bottom - r.top);
	D3DXMATRIX mat;
	//D3DXMatrixPerspectiveFovLH(&mat, 0.25f*3.141592654f, fAspect, 3.f, 10000.0f);
	D3DXMatrixPerspectiveFovLH(&mat, 0.25f*3.141592654f, fAspect, 20.f, 8000.0f);
    d3ddev->SetTransform( D3DTS_PROJECTION, (D3DMATRIX*)&mat );

	//SetPerspective(1.287f, 1.3333f, 1.0f, 2000.0f);

	// ----------------------------------------------------------------------------
	// 2. Set up the 3D Device
	// ----------------------------------------------------------------------------
	//D3DXMATRIX matProj;
	//D3DXMatrixPerspectiveLH(&matProj, SCREEN_WIDTH, SCREEN_HEIGHT, .0001f, 100000.0f);
	//d3ddev->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&matProj);

    //use ambient lighting and z-buffering
	d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_USEW);
    d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);		//remember to fix this in ch11
    //d3ddev->SetRenderState(D3DRS_AMBIENT, GREY);		//should be ambient option instead
	d3ddev->SetRenderState( D3DRS_AMBIENT,  0xffffffff);
	d3ddev->SetRenderState(D3DRS_VERTEXBLEND, 0);
	d3ddev->SetRenderState(D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
	d3ddev->SetRenderState( D3DRS_SPECULARENABLE, TRUE );
	//d3ddev->SetNPatchMode( 0 );
	
	//ADDED
	d3ddev->SetRenderState( D3DRS_CULLMODE,         D3DCULL_CCW );
	d3ddev->SetRenderState( D3DRS_DITHERENABLE,     TRUE );
	d3ddev->SetRenderState( D3DRS_NORMALIZENORMALS, FALSE );
    d3ddev->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
    d3ddev->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	d3ddev->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);  //D3DFILL_WIREFRAME : D3DFILL_SOLID

	//d3ddev->SetFVF(D3DFVF_XYZ);

	// ----------------------------------------------------------------------------
	// 3. Load All the Mesh Files
	// ----------------------------------------------------------------------------
	// Try 2: Dynamic
	g_MeshAry = new MODEL[g_MeshCnt];
	for (int i=0;i<g_MeshCnt;i++)
	{
		g_MeshAry = (*LoadModel(g_sMeshFileAry, g_fMeshScaleAry, g_iMeshOffX, g_iMeshOffY, g_iMeshOffZ, g_fMeshRotateAry));
		if (g_MeshAry.mesh == NULL)
		{
			MessageBox(hwnd, "Error loading mesh file.", "Error", MB_OK);
			return 0;
		}
	
	}
    return 1;	//return okay
}

// MAIN GAME LOOP
void Game_Run(HWND hwnd)
{
    ClearScene(GREY);
    if (d3ddev->BeginScene())
    {
		//----------------------------------------------------------------------
		// 1. Setup the Light
		//----------------------------------------------------------------------
		D3DLIGHT9 light;
		D3DXVECTOR3 vecLightDirUnnormalized(1.0f, 1.0f, -1.0f);
		ZeroMemory( &light, sizeof(D3DLIGHT9) );
		
		//D3DLIGHT_SPOT, D3DLIGHT_POINT, D3DLIGHT_FORCE_DWORD, D3DLIGHT_DIRECTIONAL
		switch(m_Light->type)
		{
			case 0:	light.Type = D3DLIGHT_DIRECTIONAL;	break;	
			case 1:	light.Type = D3DLIGHT_SPOT;			break;
			case 2:	light.Type = D3DLIGHT_POINT;		break;
		}

		if (m_Light->DiffuseDummy)
		{
			light.Diffuse.r = m_Light->D_R;
			light.Diffuse.g = m_Light->D_G;
			light.Diffuse.b = m_Light->D_B;
		}
		if (m_Light->SpecularDummy)
		{
			light.Specular.r = m_Light->S_R;
			light.Specular.g = m_Light->S_G;
			light.Specular.b = m_Light->S_B;
		}
		if (m_Light->AmbientDummy)
		{
			light.Ambient.r = m_Light->A_R;
			light.Ambient.g = m_Light->A_G;
			light.Ambient.b = m_Light->A_B;
		}

		D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecLightDirUnnormalized );

		switch( light.Type )
		{
			case D3DLIGHT_POINT:
				light.Position     = D3DXVECTOR3(PosX, PosY, PosZ);
				light.Direction    = D3DXVECTOR3( LookX, LookY, LookZ);
				if (m_Light->SpotDummy)
				{
					light.Range        = m_Light->Point_Range;
					light.Theta        = m_Light->Point_Theta;
					light.Phi          = m_Light->Point_Phi;
					light.Falloff	   = m_Light->Point_FallOff;
					light.Attenuation0 = m_Light->Point_Atten0;
					light.Attenuation1 = m_Light->Point_Atten1;
					light.Attenuation2 = m_Light->Point_Atten2;
				}
				break;
			case D3DLIGHT_DIRECTIONAL:
				light.Position     = D3DXVECTOR3(PosX, PosY, PosZ);
				light.Direction     = D3DXVECTOR3( LookX, LookY, LookZ);
				//light.Position     = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
				//light.Direction	   = D3DXVECTOR3(1110.0f, 1110.0f, 1.0f);
				if (m_Light->DirectionalDummy)
				{
					light.Range		   = m_Light->Dir_Range;
					light.Falloff	   = m_Light->Dir_FallOff;
					light.Theta		   = m_Light->Dir_Theta;
					light.Phi		   = m_Light->Dir_Phi;
					light.Attenuation0 = m_Light->Dir_Atten0;
					light.Attenuation1 = m_Light->Dir_Atten1;
					light.Attenuation2 = m_Light->Dir_Atten2;
				}
				break;
			case D3DLIGHT_SPOT:
				light.Position     = D3DXVECTOR3(PosX, PosY, PosZ);
				light.Direction     = D3DXVECTOR3( LookX, LookY, LookZ);
				if (m_Light->SpotDummy)
				{
					light.Range        = m_Light->Spot_Range;
					light.Falloff      = m_Light->Spot_FallOff;
					light.Theta        = m_Light->Spot_Theta;
					light.Phi          = m_Light->Spot_Phi;
					light.Attenuation0 = m_Light->Spot_Atten0;
					light.Attenuation1 = m_Light->Spot_Atten1;
					light.Attenuation2 = m_Light->Spot_Atten2;
				}
		}

		// Set Light for vertex shader
		D3DXVECTOR4 vLightDir( 1.0f, 1.0f, -1.0f, 1.0f );
	    D3DXVec4Normalize( &vLightDir, &vLightDir );
		d3ddev->SetVertexShaderConstantF(1, (float*)&vLightDir, 1);

		d3ddev->SetLight(0, &light ) ;
		d3ddev->LightEnable(0, TRUE );

		//----------------------------------------------------------------------
		//  2. Draw the Model
		//----------------------------------------------------------------------
        //DrawModel(car2, false);
		for(int i=0;i<g_MeshCnt;i++)
		{ 
			DrawModel(&g_MeshAry, (i==0) ? false : true); 
		}

        d3ddev->EndScene();
    }

    d3ddev->Present(NULL, NULL, NULL, NULL);

	//----------------------------------------------------------------------
	//  3. Get Movements
	//----------------------------------------------------------------------
	if (g_WalkThrough)	Movements_WalkThrough(hwnd);
	else				Movements(hwnd);
}

void Game_Reset(HWND hwnd)
{ 
	if (d3ddev != NULL) 
	{ 
		RECT r;
		GetClientRect( hwnd, &r );
		r.top += 28;
		r.bottom -= 20;
		float fAspect = (float)(r.right - r.left) / (float)(r.bottom - r.top);
		D3DXMATRIX mat;
		D3DXMatrixPerspectiveFovLH( &mat, D3DX_PI/4, fAspect, 20.f, 8000.0f);
		d3ddev->SetTransform( D3DTS_PROJECTION, &mat );
		d3ddev->Reset;
	}
}

void Game_End(HWND hwnd)
{
	if (g_MeshCnt == 1) return;
	DeleteModel(&g_MeshAry[0]);
}

// -------------------------------------
// E. MOVEMENT EVENTS
// -------------------------------------
// STAND ALONE
void Movements(HWND hwnd)
{
	Poll_Keyboard();
	//if (Key_Down(DIK_ESCAPE)) PostMessage(hwnd, WM_DESTROY, 0, 0);

    int tAdded = (Key_Down(DIK_LSHIFT)) ? 5 : 1;
	float tZoomLevel = (Key_Down(DIK_LSHIFT)) ? 0.1f : 0.01f;
    
	if (Key_Down(DIK_UP))		g_Angle = g_Angle + tAdded;
	if (Key_Down(DIK_DOWN))		g_Angle = g_Angle - tAdded;
	if (Key_Down(DIK_RIGHT))	g_Angle2 = g_Angle2 + tAdded;
	if (Key_Down(DIK_LEFT))		g_Angle2 = g_Angle2 - tAdded;

	if (g_Angle > 360) g_Angle = 0;
	if (g_Angle < 0) g_Angle = 360;
	if (g_Angle2 > 360) g_Angle2 = 0;
	if (g_Angle2 < 0) g_Angle2 = 360;

	if (Key_Down(DIK_F)) g_CurrentScaling = g_CurrentScaling + tZoomLevel;
	if (Key_Down(DIK_B)) g_CurrentScaling = g_CurrentScaling - tZoomLevel;

	Poll_Mouse();
	float mouseX = Mouse_X();
	float mouseY = Mouse_Y();
	float mouseZ = Mouse_Z();

	if (Mouse_Button(0))
	{
		g_LastMouseX = mouseX;
		g_LastMouseY = mouseY;

		g_Angle = g_Angle - (Mouse_X() * 1);
		g_Angle2 = g_Angle2 - (Mouse_Y() * 1);
		
		UINT uLines;
        SystemParametersInfo( 104, 0, &uLines, 0 );
	}
	if (Mouse_Button(2))
	{
	}
	if (Mouse_Button(1))
	{
	}
	
	g_CurrentScaling = g_CurrentScaling + (mouseZ * .002);
}

// WALKTHROUGH
void Movements_WalkThrough(HWND hwnd)
{
	Poll_Keyboard();
	//if (Key_Down(DIK_ESCAPE)) PostMessage(hwnd, WM_DESTROY, 0, 0);

	float tempStep = (Key_Down(DIK_LSHIFT) || Key_Down(DIK_RSHIFT)) ? 20 : 10;

	//wireframe or solid
	//if (Key_Down(DIK_R))
	//{
		//bWireframe = (bWireframe) ? false : true;
	//}
	//d3ddev->SetRenderState( D3DRS_FILLMODE, (bWireframe) ? D3DFILL_WIREFRAME : D3DFILL_SOLID);  //D3DFILL_WIREFRAME : D3DFILL_SOLID

	//-------------------
	//A. CAMERA MOVEMENT
	//-------------------
	//Forward
	if (Key_Down(DIK_UP) || Key_Down(DIK_W))
	{
		cXmove = (tempStep * sin(ViewDirX)) * systemframe;
		cYmove = (tempStep * cos(ViewDirX)) * systemframe;
		PosX = PosX + cXmove;	PosY = PosY + cYmove;
	}
	//Backward
	if (Key_Down(DIK_DOWN) || Key_Down(DIK_S) )
	{
		cXmove = (tempStep * sin(ViewDirX)) * systemframe;
		cYmove = (tempStep * cos(ViewDirX)) * systemframe;
		PosX = PosX - cXmove;	PosY = PosY - cYmove;
	}
	//strafe left
	if (Key_Down(DIK_LEFT) || Key_Down(DIK_A) )
	{
		cXmove = (tempStep * sin(ViewDirX - 90)) * systemframe;
		cYmove = (tempStep * cos(ViewDirX - 90)) * systemframe;
		PosX = PosX - cXmove;	PosY = PosY - cYmove;
	}
	//strafe right
	if (Key_Down(DIK_RIGHT) || Key_Down(DIK_D))
	{
		cXmove = (tempStep * sin(ViewDirX + 90)) * systemframe;
		cYmove = (tempStep * cos(ViewDirX + 90)) * systemframe;
		PosX = PosX - cXmove; 	PosY = PosY - cYmove;
	}

	//Up and Down
	if (Key_Down(DIK_U))	PosZ = PosZ + 10;
	if (Key_Down(DIK_Y))	PosZ = PosZ - 10;

	//-------------------
	//B. POINT OF VIEW
	//-------------------
	Poll_Mouse();
	ViewDirY = ViewDirY - (Mouse_Y() * g_Radius);
	ViewDirX = ViewDirX - (Mouse_X() * g_Radius);

	if (ViewDirX < 0)		ViewDirX = 360;
	if (ViewDirX > 360)		ViewDirX = 0;
	if (ViewDirY <= -52)	ViewDirY = -52;
	if (ViewDirY > 78)		ViewDirY = 78;

	LookX = 10 * sin(ViewDirX) + PosX;
    LookY = 10 * cos(ViewDirX) + PosY;
    LookZ = 10 * sin(ViewDirY) + PosZ;

	SetCamera(PosX, PosY, PosZ, LookX, LookY, LookZ);

	LastUpdated = GetTickCount();
	systemframe = GetTickCount() - LastUpdated + 1;
}



Share this post


Link to post
Share on other sites
Advertisement
I don't have time to look through that huge listing, but what you want (w.r.t. the SDK examples) is commonly referred to as "Arc Ball" navigation.

The DirectX SDK contains an implementation of one if you want to take a look. I don't have my code to hand, but i think it's called CModelViewerCamera.

hth
Jack

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!