Archived

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

D_JildQuantum

Camera Code

Recommended Posts

Ok, since noone replied to my post, i just went ahead and tried to write it on my own, so it may be buggy, but heres my problem: I have the CCamera class written, and i have the Move Function written, but i cant get it to actually move if theres a key down... heres my code:
//3D Firefly Engine.  3D Engine for the 2D Game, built with 3D objects, and 2D translation math, Firefly, and TenTwist Firefly.

//Created on July 21st 2003

//Created by Damian Jimmy-Ian Lanningham


//INCLUDES

#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d8.h>
#include <d3dx8.h>
#include <mmsystem.h>
//#include "Main.h"

#define KEYDOWN(vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
#define KEYUP  (vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 0:1)
//Prototypes

//Initialization

bool InitGame(HWND g_hWnd);
bool InitTextures();
//Shutdown

void ShutdownGame();
//Loop

void GameMain();
//STRUCTS

class CQuantumVertex
{
public:
	//Variables

	float LocX, LocY, LocZ;
	float fU, fV;
	//Class Functions

};

//CLASSES

class CCamera
{
private:
	
	float EyeX, EyeY, EyeZ;
	float LookX,LookY,LookZ;
	float UpX, UpY, UpZ;
public:
	bool Move();
	float GetEyeX(){return(EyeX);}
	float GetEyeY(){return(EyeY);}
	float GetEyeZ(){return(EyeZ);}

	float GetLookX(){return(LookX);}
	float GetLookY(){return(LookY);}
	float GetLookZ(){return(LookZ);}

	float GetUpX(){return(UpX);}
	float GetUpY(){return(UpY);}
	float GetUpZ(){return(UpZ);}
	CCamera(float EX, float EY, float EZ, float LX, float LY, float LZ, float UX, float UY, float UZ)
	{
		EyeX=EX;
		EyeY=EY;
		EyeZ=EZ;

		LookX=LX;
		LookY=LY;
		LookZ=LZ;

		UpX=UX;
		UpY=UY;
		UpZ=UZ;
	}
};



//////////////////////////////////////////////////////////////////////////////////

//CTERRAIN CODE																	//

//////////////////////////////////////////////////////////////////////////////////

class CTerrainEngine
{
public:

    LPDIRECT3DTEXTURE8 m_pTextureHeightmap;         // heightmap texture

    LPDIRECT3DTEXTURE8 m_pTextureGround;            // texture for ground level


    LPDIRECT3DVERTEXBUFFER8 m_pVertexBuffer;        // VB for terrain mesh

    LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;          // Index buffer for terrain


    int m_nHeightArray[256][256];                   // 0-255*0-255 array for Y vals of each heightmap pixel

    CQuantumVertex m_vTerrain[128*128];              // 6 verts per quad, one quad per pixel

    short m_sIndices[(6*(128*128))];                // Index buffer indices


    CTerrainEngine();
    ~CTerrainEngine();

    HRESULT Initialise(LPDIRECT3DDEVICE8& pDevice, char* pszHeightmap);

    HRESULT Render(LPDIRECT3DDEVICE8& pDevice, LPDIRECT3DSURFACE8& pBackBuffer);
};

 ////////////////////////////////////////////////////////////////////////////////

//End of CTerrain Code															//

 ////////////////////////////////////////////////////////////////////////////////

//CONSTANTS

const int  WINDOW_HEIGHT		= 480;
const int  WINDOW_WIDTH		= 640;
const int  WINDOW_Y				= 0;
const int  WINDOW_X				= 0;
const char *WINDOW_TITLE		= "Quantum Window";
const char *WINDOW_CLASS_NAME	= "QUANTUMWINDOW";
const DWORD D3DFVF_CQuantumVertex=D3DFVF_XYZ | D3DFVF_TEX1;
//GLOBAL


//Application Globals

HINSTANCE g_hInst;
HWND g_hWnd;
bool g_bFullscreen;
//D3D Globals

IDirect3D8			*g_pDirect3D;	//Pointer to Direct3D

IDirect3DDevice8	*g_pDevice;		//Pointer to the Device

HRESULT				g_hr;			//For Storing the result of function calls

//Geometry Globals

CQuantumVertex CQVTriangle[]=	{	{0.0f,1.0f,0.0f,0.5f,0.0f},{-1.0f,-1.0f,-1.0f,0.0f,1.0f},{1.0f,-1.0f,-1.0f,1.0f,1.0f},
									{0.0f,1.0f,0.0f,0.5f,0.0f},{-1.0f,-1.0f,1.0f,0.0f,1.0f},{-1.0f,-1.0f,-1.0f,1.0f,1.0f},
									{0.0f,1.0f,0.0f,0.5f,0.0f},{1.0f,-1.0f,1.0f,0.0f,1.0f},{-1.0f,-1.0f,1.0f,1.0f,1.0f},
									{0.0f,1.0f,0.0f,0.5f,0.0f},{1.0f,-1.0f,-1.0f,0.0f,1.0f},{1.0f,-1.0f,1.0f,1.0f,1.0f}};


CQuantumVertex CQVQuad[]=  {	{-1.0f,-1.0f,-1.0f,0.0f,0.0f},
								{1.0f,-1.0f,-1.0f,1.0f,0.0f},
								{-1.0f,-1.0f,1.0f,0.0f,1.0f},
								{1.0f,-1.0f,1.0f,1.0f,1.0f}};

//Drawing and Texture Globals

LPDIRECT3DVERTEXBUFFER8 g_pQuantumVertexBufferQuad;
LPDIRECT3DVERTEXBUFFER8	g_pQuantumVertexBufferTriangle;
IDirect3DTexture8		*g_pPyramidTexture = NULL;
IDirect3DTexture8		*g_pDessertTexture = NULL;
IDirect3DTexture8		*g_pForestTexture = NULL;
IDirect3DTexture8		*g_pLavaTexture = NULL;
CCamera					Camera(-1.5f, 1.7f, -10.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
D3DXMATRIX MatrixView;
//PROTOTYPES


//FUNCTIONS

 ////////////////////////////////////////////////////////////////////////////////

//InitD3D()																		//

//Purpose:  Sets up the Direct3D and creates a window either fullscreen or		//

//			windowed															//

 ////////////////////////////////////////////////////////////////////////////////

bool InitD3D(HWND g_hWnd)
{ 
        g_pDirect3D=Direct3DCreate8(D3D_SDK_VERSION); 
        if(g_pDirect3D==NULL) 
                return(false); 
		
		D3DDISPLAYMODE DisplayMode; 
        if(g_bFullscreen==false) 
        { 
                g_hr=g_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode); 
                if(FAILED(g_hr)) 
                        return(false); 
        } 
        else 
        { 
                DisplayMode.Width=      WINDOW_WIDTH; 
                DisplayMode.Height=     WINDOW_HEIGHT; 
                DisplayMode.RefreshRate=0; 
                DisplayMode.Format=     D3DFMT_R5G6B5; 
        } 
        
		D3DPRESENT_PARAMETERS   PresentParameters; 
        memset(&PresentParameters, 0, sizeof(D3DPRESENT_PARAMETERS)); 
        if(g_bFullscreen==false) 
        { 
                PresentParameters.Windowed = true; 
        } 
        else if(g_bFullscreen==true) 
        { 
                PresentParameters.Windowed = false; 
        } 
        PresentParameters.SwapEffect	   = D3DSWAPEFFECT_DISCARD;
		PresentParameters.BackBufferWidth = 640;
		PresentParameters.BackBufferHeight = 480;
		PresentParameters.BackBufferFormat = D3DFMT_X8R8G8B8; 

		PresentParameters.BackBufferCount  = 1;
		PresentParameters.hDeviceWindow    = g_hWnd;
		PresentParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
		PresentParameters.EnableAutoDepthStencil = true;
		PresentParameters.AutoDepthStencilFormat = D3DFMT_D16;
		PresentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
		
        g_hr=g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, 
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
                                  &PresentParameters, &g_pDevice); 
        if(FAILED(g_hr)) 
        { 
                return(false); 
        } 
		

		D3DXMATRIX MatrixProjection;
		ZeroMemory(&MatrixProjection, sizeof(MatrixProjection));
		D3DXMatrixPerspectiveFovLH(&MatrixProjection, D3DX_PI/6, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 100.0);
		g_pDevice->SetTransform(D3DTS_PROJECTION, &(D3DMATRIX)MatrixProjection);

		ZeroMemory(&MatrixView, sizeof(MatrixView));
		D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(Camera.GetEyeX(),Camera.GetEyeY(), Camera.GetEyeZ()),
										&D3DXVECTOR3(Camera.GetLookX(),Camera.GetLookY(),Camera.GetLookZ()),
										&D3DXVECTOR3(Camera.GetUpX(), Camera.GetUpY(), Camera.GetUpZ()));
		g_pDevice->SetTransform(D3DTS_VIEW, &MatrixView);

		D3DXMATRIX MatrixWorld;
		D3DXMatrixTranslation(&MatrixWorld, 3.0f, 0.0f, 2.0f);
		g_pDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);

		
		g_hr=g_pDevice->CreateVertexBuffer(sizeof(CQVQuad), 0, D3DFVF_CQuantumVertex, D3DPOOL_DEFAULT,
                                         &g_pQuantumVertexBufferQuad);
		if(FAILED(g_hr))
			return false;
		BYTE* pVerticeLock=0;
		g_hr=g_pQuantumVertexBufferQuad->Lock(0, sizeof(CQVQuad), &pVerticeLock, 0);
		if(FAILED(g_hr))
			return false;
		CopyMemory(pVerticeLock, &CQVQuad, sizeof(CQVQuad));
		g_pQuantumVertexBufferQuad->Unlock();
		
		g_hr=g_pDevice->CreateVertexBuffer(sizeof(CQVTriangle), 0, D3DFVF_CQuantumVertex, D3DPOOL_DEFAULT, &g_pQuantumVertexBufferTriangle);
		if(FAILED(g_hr))
			return(false);
		pVerticeLock=0;
		g_hr=g_pQuantumVertexBufferTriangle->Lock(0, sizeof(CQVTriangle), &pVerticeLock, 0);
		if(FAILED(g_hr))
			return(false);
		CopyMemory(pVerticeLock, &CQVTriangle, sizeof(CQVTriangle));
		g_pQuantumVertexBufferTriangle->Unlock();
		
		g_pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
		g_pDevice->SetRenderState( D3DRS_LIGHTING, false);
		return(true);
}//End InitD3D() 



 ////////////////////////////////////////////////////////////////////////////////

//DrawScene()																	//

//Purpose: Render the scene to the back buffer and then copy it to the monitor	//

 ////////////////////////////////////////////////////////////////////////////////

void DrawScene()
{
	g_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
	if(g_pDevice->BeginScene() == D3D_OK)
	{
	g_pDevice->SetTexture(0, g_pPyramidTexture);
	g_pDevice->SetStreamSource(0,g_pQuantumVertexBufferQuad,sizeof(CQuantumVertex));
	g_pDevice->SetVertexShader(D3DFVF_CQuantumVertex);
	g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, CQVQuad, sizeof(CQuantumVertex));
	g_pDevice->SetStreamSource(0,g_pQuantumVertexBufferTriangle, sizeof(CQuantumVertex));
	g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 4, CQVTriangle, sizeof(CQuantumVertex));
	g_pDevice->EndScene();
	}
	g_pDevice->Present(NULL, NULL, NULL, NULL);
}
 ////////////////////////////////////////////////////////////////////////////////

//ShutdownD3D()																	//

//Purpose:  To release all of the Direct3D objects and destroy out widnow		//

 ////////////////////////////////////////////////////////////////////////////////

void ShutdownD3D()
{
	if(g_pLavaTexture != NULL)
	{    
		g_pLavaTexture->Release();    
		g_pLavaTexture = NULL;
	}
	if(g_pForestTexture != NULL)
	{    
		g_pForestTexture->Release();    
		g_pForestTexture = NULL;
	}
	if(g_pDessertTexture != NULL)
	{    
		g_pDessertTexture->Release();    
		g_pDessertTexture = NULL;
	}
	if(g_pPyramidTexture != NULL)
	{    
		g_pPyramidTexture->Release();    
		g_pPyramidTexture = NULL;
	}
	
	if(g_pQuantumVertexBufferTriangle != NULL)
	{
		g_pQuantumVertexBufferTriangle->Release();
		g_pQuantumVertexBufferTriangle=NULL;
	}
	if(g_pQuantumVertexBufferQuad != NULL)
	{    
		g_pQuantumVertexBufferQuad->Release();    
		g_pQuantumVertexBufferQuad = NULL;
	}
	if(g_pDevice)
		g_pDevice->Release();
	g_pDevice=NULL;
	if(g_pDirect3D)
		g_pDirect3D->Release();
	g_pDirect3D=NULL;
}

 ////////////////////////////////////////////////////////////////////////////////

//UpdateScene																	//

//Purpose: To Update the Positions and then render afterward					//

 ////////////////////////////////////////////////////////////////////////////////

void UpdateScene()
{
	if(GetAsyncKeyState(VK_ESCAPE))
			PostQuitMessage(0);
}

 ////////////////////////////////////////////////////////////////////////////////

//WinProc()																		//

//Purpose: Windows CallBack														//

 ////////////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		}break;
	default:break;
	}
	return(DefWindowProc(hwnd, msg, wparam, lparam));
}
 ////////////////////////////////////////////////////////////////////////////////

//WinMain()																		//

//Purpose: Main Function, with the game Loop in it.								//

 ////////////////////////////////////////////////////////////////////////////////

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX	WinClass;
	MSG msg;

	g_hInst=hInstance;
	WinClass.cbSize			= sizeof(WNDCLASSEX);
	WinClass.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	WinClass.lpfnWndProc	= WinProc; 
	WinClass.cbClsExtra		= 0;
	WinClass.cbWndExtra		= 0; 
	WinClass.hInstance		= g_hInst; 
	WinClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION); 
	WinClass.hCursor		= LoadCursor(NULL, IDC_ARROW); 
	WinClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH); 
	WinClass.lpszMenuName	= NULL; 
	WinClass.lpszClassName	= WINDOW_CLASS_NAME; 
	WinClass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
	if(!RegisterClassEx(&WinClass))
		return(0);
	if(MessageBox(NULL, "Would you like to run in Fullscreen mode?", "Fullscreen?", MB_YESNO)==IDYES)
		g_bFullscreen=true;
	else
		g_bFullscreen=false;
	if (g_bFullscreen==false)
	{
		g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,  
							 WINDOW_CLASS_NAME,     
							 WINDOW_TITLE, 
							 WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE, 
					 		 WINDOW_X, WINDOW_Y ,
							 WINDOW_WIDTH, WINDOW_HEIGHT,
							 NULL,
							 NULL,
							 g_hInst,  
							 NULL);	
		ShowCursor(true);
	}
	else
	{
		g_hWnd = CreateWindowEx(NULL,  
							 WINDOW_CLASS_NAME,     
							 WINDOW_TITLE, 
							 WS_POPUP | WS_VISIBLE, 
					 		 WINDOW_X, WINDOW_Y ,
							 WINDOW_WIDTH, WINDOW_HEIGHT,
							 NULL,
							 NULL,
							 g_hInst,  
							 NULL);		
		
		ShowCursor(false);
	}
	if(!g_hWnd)
		return(0);
	if(InitGame(g_hWnd) == false)
	{
		ShutdownGame();
		return(0);
	}
	while(true)
	{
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			if(msg.message==WM_QUIT)
			break;
		}
		if(msg.message==WM_QUIT)
			break;
		
		GameMain();
	}
	ShutdownD3D();
	return(0);
}
//Loop Functions

bool InitGame(HWND g_hWnd)
{
	if(FAILED(InitD3D(g_hWnd)))
		return(false);
	if(FAILED(InitTextures()))
		return(false);
	return(true);
}

void GameMain()
{
	UpdateScene();
	DrawScene();
	if(FAILED(Camera.Move()))
		return;
if(GetAsyncKeyState(VK_ESCAPE))
	
			PostQuitMessage(0);
}

void ShutdownGame()
{
	ShutdownD3D();
}

//Init Funcitons

bool InitTextures()
{
	g_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
		g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
		g_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
		
		if(FAILED(D3DXCreateTextureFromFile(g_pDevice, "C:\\Damian\\PROGRAMMINGPROJECTS\\3DFirefly\\Media\\Graphics\\pyratext.jpg", &g_pPyramidTexture)))
			return(false);
		if(FAILED(D3DXCreateTextureFromFile(g_pDevice, "C:\\Damian\\PROGRAMMINGPROJECTS\\3DFirefly\\Media\\Graphics\\sandtext.jpg",&g_pDessertTexture)))
			return(false);
		if(FAILED(D3DXCreateTextureFromFile(g_pDevice, "C:\\Damian\\PROGRAMMINGPROJECTS\\3DFirefly\\Media\\Graphics\\grasstext.jpg",&g_pForestTexture)))
			return(false);
		if(FAILED(D3DXCreateTextureFromFile(g_pDevice, "C:\\Damian\\PROGRAMMINGPROJECTS\\3DFirefly\\Media\\Graphics\\lavatext.jpg",&g_pLavaTexture)))
			return(false);
		return(true);
}
//MainFunctions

//ShutdownFunctions

bool CCamera::Move()
{
	if(KEYDOWN(VK_UP))
	{
		Camera.EyeZ += 1.0f;
		Camera.LookZ+= 1.0f;
	}
	if(KEYDOWN(VK_DOWN))
	{
		Camera.EyeZ -=1.0f;
		Camera.LookZ-=1.0f;
	}
	if(FAILED(D3DXMatrixLookAtLH(&MatrixView, &D3DXVECTOR3(Camera.GetEyeX(),Camera.GetEyeY(), Camera.GetEyeZ()),
									&D3DXVECTOR3(Camera.GetLookX(),Camera.GetLookY(),Camera.GetLookZ()),
									&D3DXVECTOR3(Camera.GetUpX(), Camera.GetUpY(), Camera.GetUpZ()))))
									return(false);
return(true);
}
If you want any specific parts and dont want to spend the energy finding them, tell me and ill post that up... Quantum

Share this post


Link to post
Share on other sites
Like most others, I guess, I didn''t read all your code because the relevant portions are the camera placement (which I assume is the default 0,0,0) and the only camera movement is its lookat along the z axis.

Since by default the camera is looking straight down the z axis, moving its lookat along that axis will have very insignificant results until the lookat changes sign.

I''d suggest offsetting the camera along the x axis to confirm or refute this suggestion.

Hope this is the answer, anyway.



Stevie

Don''t follow me, I''m lost.

Share this post


Link to post
Share on other sites