www.xbdev.net
xbdev - software development
Friday April 19, 2024
home | contact | Support | DirectX.. Its doing all the hard work for us...
>>

PrtX. The DirectX8 3D Camera...
by bkenwright@xbdev.net

OOooo... an actual camera...and it moves!... (download full source code).

So you'll actually be able to move around in a 'very' simple 3D world!  I've done it so that if you press the up or down arrow keys you move forwards and backwards... you could add the code so you move left and right etc... but I'm only showing you the basics!

So our actual code:

/***************************************************************************/

/*                                                                         */

/* 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();

}

 

Alternatively in future you can simply do something a lot less messy... e.g. an alternative... I did the above function so you could play around with the camera.  Once you get a feeling for it, you'll soon realise its easy to use!

void set_camera()

{

    // [1] D3DTS_VIEW

    D3DXMATRIX v;

    g_pD3DDevice->SetTransform(D3DTS_VIEW, D3DXMatrixLookAtLH(&v, &D3DXVECTOR3(0,0,-5),

                                                                  &D3DXVECTOR3(0,0,0),

                                                                  &D3DXVECTOR3(0,1,0))); 

    // [2] D3DTS_PROJECTION

    D3DXMATRIX p;

    g_pD3DDevice->SetTransform( D3DTS_PROJECTION, D3DXMatrixPerspectiveFovLH( &p,  D3DX_PI/4,  1.0f,

                                                                                                 0.0f,

                                                                                                 100.0f));

};

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.