www.xbdev.net xbdev - software development
Monday December 10, 2018
home | about | contact | Donations

     
 

XBOX Programming..

More than just a toy...

 

Prt3 - Wow a triangle

Author: Ben_3D@xbdev.net

 

Finally something exciting - well if you understand it all up to now, you'll be a game programmer in a few days.  3D is based on vertices, e.g. points in space.  X,Y,Z value represents were in space it is located.  We just join up these points to create complex shapes, like those used in Doom, or Halo :)

A lot of the initialisation code is the same as before, you'll get used to it in time, and eventually just put it in a separate file where you can call it just once.

 

//Application entry point

void __cdecl main()

{

      InitialiseD3D();

      while(true)

      {

            //Clear the backbuffer to black

            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);

            //Begin the scene

            g_pD3DDevice->BeginScene();

 

 

                  //NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

                  DrawTriangle();

 

            //End the scene

            g_pD3DDevice->EndScene();

 

            //Filp the back and front buffers so that whatever has been rendered on the back buffer

            //will now be visible on screen (front buffer).

 

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

      }    

 

      CleanUp();

}

 

And next comes the DirectX Initilisation, and De-Initilisation.

 

//Main header file for the XDK

#include <xtl.h>

 

LPDIRECT3D8 g_pD3D = NULL;                      // DirectX Object

LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;          // Screen Object

 

void InitialiseD3D()

{

    //First of all, create the main D3D object. If it is created successfully we

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

 

    //Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

 

    //Fill the structure.

    // Set fullscreen 640x480x32 mode

 

      d3dpp.BackBufferWidth = 640;

      d3dpp.BackBufferHeight = 480;

      d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

 

 

      // Create one backbuffer

      d3dpp.BackBufferCount = 1;

 

      // Set up how the backbuffer is "presented" to the frontbuffer each time

      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

 

    //Create a Direct3D device.

    g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,

                                   D3DCREATE_HARDWARE_VERTEXPROCESSING,

                                                   &d3dpp, &g_pD3DDevice);

}

 

void CleanUp()

{

    g_pD3DDevice->Release();

    g_pD3D->Release();

}

 

Finally the Code that draws our wonderful, wonderful triangle for us.  As you can see, the DrawTriangle code has to be called within the ->BeginScene(), and ->EndScene()

member functions above in main(), I've commented the code with //NEW NEW.. so that you can see the main points I am trying to outline.

 

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

 

LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Vertices Buffer

 

struct CUSTOMVERTEX

{

    FLOAT x, y, z, rhw; // The transformed position for the vertex.

    DWORD colour; // The vertex colour.

};

 

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

 

 

void DrawTriangle()

{

    VOID* pVertices;

   

    //Store each point of the triangle together with it's colour

    CUSTOMVERTEX cvVertices[] =

    {

        {250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)

        {400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 2 - Green (400, 350)

        {100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 3 - Blue (100, 350)

    };

 

    //Create the vertex buffer from our device

    g_pD3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX),

                                               0,

                                                                     D3DFVF_CUSTOMVERTEX,

                                               D3DPOOL_DEFAULT,

                                                                     &g_pVertexBuffer);

    //Get a pointer to the vertex buffer vertices and lock the vertex buffer

    g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);

 

    //Copy our stored vertices values into the vertex buffer

    memcpy(pVertices, cvVertices, sizeof(cvVertices));

 

    //Unlock the vertex buffer

    g_pVertexBuffer->Unlock();

 

      //Rendering our triangle

    g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));

    g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);

    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

 

             // Every time we Create a vertex buffer, we must release one!.

             g_pVertexBuffer->Release();

 

}

 

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

//NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

 

Yup, I can hear you say it from here, "Wow", a triangle, not just any triangle, a funky coloured textured one on a blue background.  Well when you've just started out in the big bad world of xdk development I think it seems pretty rewarding.

You can see from the DrawTriangle() function, that we set some vertices up (e.g. x,y,z points) and just copy them into our directX buffer and render them to the screen.  DirectX buffer?  Whats that?  Well your graphics card has memory onboard, and if we use the directX buffer it will put it in there so that we can obtain better performance.

 

(Back Tutorials Page)

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.