Sign in to follow this  
emforce

camera problems

Recommended Posts

hi guys i have created an app which renders very simple hillsides and i was trying to move the camera so i could go forward and backwards and left to right can u please help me? right now it goes towards the centre of the map and when i press a button to make it go left or right it goes in that direction then starts zooming out. heres the code
##include "d3dApp.h"
#include "PeaksAndValleys.h"


class PeaksAndValleysApp : public D3DApp
{
public:
	PeaksAndValleysApp(HINSTANCE hInstance);
	~PeaksAndValleysApp();

	void initApp();
	void onResize();
	void updateScene(float dt);
	void drawScene(); 

private:
	void buildFX();
	void buildVertexLayouts();
 
private:

	PeaksAndValleys mLand;

	ID3D10Effect* mFX;
	ID3D10EffectTechnique* mTech;
	ID3D10InputLayout* mVertexLayout;
	ID3D10Buffer* mVertexBuffer;
	ID3D10Buffer* mIndexBuffer;
	ID3D10EffectMatrixVariable* mfxWVPVar;

	D3DXMATRIX mView;
	D3DXMATRIX mProj;
	D3DXMATRIX mWVP;

	float mRadius;
	float mForward;
	float mSide;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
	// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif


	PeaksAndValleysApp theApp(hInstance);
	
	theApp.initApp();

	return theApp.run();
}

PeaksAndValleysApp::PeaksAndValleysApp(HINSTANCE hInstance)
: D3DApp(hInstance), mFX(0), mTech(0), mVertexLayout(0),
  mVertexBuffer(0), mIndexBuffer(0), mfxWVPVar(0), 
  mRadius(0.0f), mForward(0.0f), mSide(0.0f)
{
	D3DXMatrixIdentity(&mView);
	D3DXMatrixIdentity(&mProj);
	D3DXMatrixIdentity(&mWVP); 
}

PeaksAndValleysApp::~PeaksAndValleysApp()
{
	if( md3dDevice )
		md3dDevice->ClearState();

	ReleaseCOM(mFX);
	ReleaseCOM(mVertexLayout);
	ReleaseCOM(mVertexBuffer);
	ReleaseCOM(mIndexBuffer);
}

void PeaksAndValleysApp::initApp()
{
	D3DApp::initApp();
	
	mLand.init(md3dDevice, 200, 200, 1.0f);

	buildFX();
	buildVertexLayouts();
}

void PeaksAndValleysApp::onResize()
{
	D3DApp::onResize();

	float aspect = float(mClientWidth)/mClientHeight;
	D3DXMatrixPerspectiveFovLH(&mProj, 0.25f*PI, aspect, 1.0f, 1000.0f);
}

void PeaksAndValleysApp::updateScene(float dt)
{
	D3DApp::updateScene(dt);	
	

		if(GetAsyncKeyState('A') & 0x8000)	mSide -= 0.50f;
	if(GetAsyncKeyState('D') & 0x8000)	mSide += 0.50f;
	if(GetAsyncKeyState('W') & 0x8000)	mForward -= 0.50f;
	if(GetAsyncKeyState('S') & 0x8000)	mForward += 0.50f;



	// Convert Spherical to Cartesian coordinates: mPhi measured from +y
	// and mTheta measured counterclockwise from -z.
	float x =  75.0f + mForward;
	float z = 5.0f + mSide;
	float y =  50.0f;

	 

	// Build the view matrix.
	D3DXVECTOR3 pos(x, y, z);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}

void PeaksAndValleysApp::drawScene()
{
	D3DApp::drawScene();
	
	

	if(mRender == true)
	{
	// Restore default states, input layout and primitive topology 
	// because mFont->DrawText changes them.  Note that we can 
	// restore the default states by passing null.
	md3dDevice->OMSetDepthStencilState(0, 0);
	float blendFactors[] = {0.0f, 0.0f, 0.0f, 0.0f};
	md3dDevice->OMSetBlendState(0, blendFactors, 0xffffffff);
    md3dDevice->IASetInputLayout(mVertexLayout);
    md3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	
	
    D3D10_TECHNIQUE_DESC techDesc;
    mTech->GetDesc( &techDesc );
    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
		mWVP = mView*mProj;
		mfxWVPVar->SetMatrix((float*)&mWVP);
        mTech->GetPassByIndex( p )->Apply(0);
		mLand.draw();
    }
	}

	if(mRenderInfo == true)
	{
	// We specify DT_NOCLIP, so we do not care about width/height of the rect.
	RECT R = {5, 5, 0, 0};
	mFont->DrawText(0, mFrameStats.c_str(), -1, &R, DT_NOCLIP, BLACK);
	}
		RECT R2 = {600, 5, 0, 0};
	mFont->DrawText(0, mAppControls.c_str(), -1, &R2, DT_NOCLIP, BLACK);

	mSwapChain->Present(0, 0);
}

void PeaksAndValleysApp::buildFX()
{
	DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    shaderFlags |= D3D10_SHADER_DEBUG;
	shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif
 
	ID3D10Blob* compilationErrors = 0;
	HRESULT hr = 0;
	hr = D3DX10CreateEffectFromFile(L"color.fx", 0, 0, 
		"fx_4_0", shaderFlags, 0, md3dDevice, 0, 0, &mFX, &compilationErrors, 0);
	if(FAILED(hr))
	{
		if( compilationErrors )
		{
			MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
			ReleaseCOM(compilationErrors);
		}
		DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX10CreateEffectFromFile", true);
	} 

	mTech = mFX->GetTechniqueByName("ColorTech");
	
	mfxWVPVar = mFX->GetVariableByName("gWVP")->AsMatrix();
}

void PeaksAndValleysApp::buildVertexLayouts()
{
	// Create the vertex input layout.
	D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
	{
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
		{"COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
	};

	// Create the input layout
    D3D10_PASS_DESC PassDesc;
    mTech->GetPassByIndex(0)->GetDesc(&PassDesc);
    HR(md3dDevice->CreateInputLayout(vertexDesc, 2, PassDesc.pIAInputSignature,
		PassDesc.IAInputSignatureSize, &mVertexLayout));
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this