/***************************************************************************/
/*
*/
/* File: game.cpp
*/
/*
*/
/***************************************************************************/
// Lights camera and action!!!
/***************************************************************************/
/* *
**** *** **
************ *** CAMERA CAMERA
**************** CAMERA CAMERA
************ *** CAMERA CAMERA
**** *** **
*/
/***************************************************************************/
#include
<d3dx8.h>
#include
<d3d8.h>
// We are going to setup a basic camera in here! No when you advance yo'ull
// probably develop a CCamera class which contains loads of additional
// functionality etc... but this is the basics!
void
set_camera()
{
// Add a bit of fun, allowing you to move forwards
and backwards in our 3D world
static float
z_pos = -5.0f;
if( GetKeyState(VK_UP) & 0x80 )
z_pos += 0.001f;
if(GetKeyState(VK_DOWN) & 0x80 )
z_pos -= 0.001f;
//\\ PART-1-
D3DXMATRIX view_matrix; // empty here.
D3DXVECTOR3 pEye = D3DXVECTOR3(0,0,z_pos); //
Position of camera in 3D space.
D3DXVECTOR3 pAt = D3DXVECTOR3(0,0,0); // The
Point in 3D space where looking at.
D3DXVECTOR3 pUp = D3DXVECTOR3(0,1,0); // The up
direction! (normalised vector).
// Generate our cameras matrix!
D3DXMatrixLookAtLH(&view_matrix, &pEye, &pAt, &pUp);
// Set our camera's look details into the device.
g_pD3DDevice->SetTransform(D3DTS_VIEW, &view_matrix);
//\\ PART -2-
D3DXMATRIX proj_matrix;
float field_of_view = D3DX_PI / 4;
// if you set this to 2PI you can see 360 all the
way around..lol
float aspect = 1.0f;
float near_view_plane = 1.0f;
float far_view_plane = 100.0f;
D3DXMatrixPerspectiveFovLH(&proj_matrix, field_of_view,
aspect,
near_view_plane,
far_view_plane);
// Actually apply our generated matrix to the
device!.. else nothing has happened.
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &proj_matrix);
}
// I made some modifications here!.. its very difficult to see how the world
// is moving through the eyes of a person if the world is just 1 flat
triangle...
// so to make things more exciting.. .I did a pyramid...and you pass in the
x,
// y and z position and it draws it in our world... (you could have used)
matrixs
// to shift the pyramid...but I didn't want to do that incase you where new
to
// them and still get scared!.
void
draw_pyramid(float x,
float y, float
z)
{
struct my_vertex
{
FLOAT x, y, z; // D3DFVF_XYZ
DWORD colour; // D3DFVF_DIFFUSE
};
my_vertex v[] =
{
{-0.5f + x, -0.5f + y, 0.5f + z, 0xfffcff00},
//[1] - front
{ 0.0f + x, 0.5f + y, 0.0f + z, 0xff00ff00},
//[2]
{ 0.5f + x, -0.5f + y, 0.5f + z, 0xff33f9ff},
//[3]
{ 0.5f + x, -0.5f + y, -0.5f + z, 0xff55ff00},
// - back
{ 0.0f + x, 0.5f + y, 0.0f + z, 0xffff2200},
//
{-0.5f + x, -0.5f + y, -0.5f + z, 0xf4f2ff00},
//
{-0.5f + x, -0.5f + y, -0.5f + z, 0xffffff11},
// - left
{ 0.0f + x, 0.5f + y, 0.0f + z, 0xffff2200},
//
{-0.5f + x, -0.5f + y, 0.5f + z, 0xff88ff00},
//
{ 0.5f + x, -0.5f + y, 0.5f + z, 0xffffff00},
// - right
{ 0.0f + x, 0.5f + y, 0.0f + z, 0xff222200},
//
{ 0.5f + x, -0.5f + y, -0.5f + z, 0xffaaff00}
//
};
UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
IDirect3DVertexBuffer8 * DX_vb;
g_pD3DDevice->CreateVertexBuffer( sizeof(v),
0, my_vertex_description, D3DPOOL_MANAGED, &DX_vb );
// Copy our array which is in computer memory over
to the directX memory.. using that pointer we
// just created etc.
unsigned char
*temp_pointer_vb;
DX_vb->Lock(0,0, &temp_pointer_vb, 0);
memcpy(temp_pointer_vb, v, sizeof(v) );
DX_vb->Unlock();
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);
// Draw our triangle.
g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));
g_pD3DDevice->SetVertexShader(my_vertex_description);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);
DX_vb->Release();
}
// Not really done much matrix stuff yet!.. but be patient!
void
Render()
{
if(!g_pD3DDevice)return;
set_camera();
// Clear the back buffer to a blue color
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Okay I'll draw a 4 pyramids here using the
pyramid function..place
// them around the world so that as the camera
moves we'll actually
// notice it!!! :)
draw_pyramid(-1.0f, -0.5f, 1.0f);
draw_pyramid( 1.0f, -0.5f, 1.0f);
draw_pyramid(-5.0f, -0.5f, 20.0f);
draw_pyramid( 5.0f, -0.5f, 20.0f);
// After rendering the scene we display it.
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
}
void
mainloop()
{
Render();
} |