www.xbdev.net
xbdev - software development
Saturday February 22, 2025
Home | Contact | Support | DirectX.. Its doing all the hard work for us...
>>

PrtX. A simple coloured triangle!..

by bkenwright@xbdev.net


Well you better get used to working with vertices.. (e.g. x,y and z) points as if DirectX3D its nothing but them. (download source code.)

I thought it was not much considering the amount of work you have to do to generate such a simple triangle.... well if you understand the principles of how we are using these vertice....e.g. the dynamic way we are creating them and copying them over the a DirectX vertex buffer...then your neary home and dry.  You should spend a few minutes if not hours going over what is actually happening.  As in all 3D games/effects etc.. its allways going to boil down to vertices.

Now onto the code!  As I've said before the only code you'll really need to learn ... or that is new to this example is the code that is in game.cpp.

 

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

/*                                                                         */

/* File: game.cpp                                                          */

/*                                                                         */

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

 

#include <d3dx8.h>

#include <d3d8.h>

 

// These two items, the struct and the UINT, are the defentions of a single

// vertex point in space!.. the UINT is for directX so it knows what we want.

struct my_vertex

{

      FLOAT x, y, z;  // D3DFVF_XYZ

      DWORD colour;   // D3DFVF_DIFFUSE

};

 

UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);

 

 

// Well if we need to do any drawing or rendering triangles we can do it in here!

void Render()

{

      if(!g_pD3DDevice)return;

 

      /* 1-Create the points that will represent our triangle */

      // struct we declared earlier.

      //   |     array of elements of type my_vertex.

      //   |     |

      //  \ /   \ /

      //   |     |

      my_vertex v[] =

      {   // Three points make up the face of the triangle.

            {-1.0f,-1.0f, 0.0f, 0xffffff00},  //[1]

            {0.0f, 1.0f, 0.0f, 0xffff2200},   //[2]

            {1.0f, -1.0f, 0.0f, 0xffffff00}   //[3]

      };

                           

                    /*                    * - [2] (0,1,0)

                                         * *

                                        *   *

                                       *     *

                                      *       *

                                     *         *

                    [1] (-1,-1,0) - * * * * * * * - [3] (1,-1,0).      */

                                         

 

      // The pointer to memory on the graphics card you can think of it as.. stops you getting

      // confused... but its not exactly correct!  But we'll get to that later.

      IDirect3DVertexBuffer8 * DX_vb;

      g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex), 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();

      // Okay at this point... our graphics card has our vertices stored in it... we've just copied

      // them over :)

 

      // Some stuff to setup or graphics card!

 

      //Turn off lighting becuase we are specifying that our vertices have colour

    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

 

      g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);

 

 

 

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

 

      // Draw our triangle.

      g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));

      g_pD3DDevice->SetVertexShader(my_vertex_description);

 

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

 

      // After rendering the scene we display it.

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

 

      // As where calling the functino over and over again.. we'd be constantly creating new memory

      // without releasing the old if not for this line!

      DX_vb->Release();

}

 

void mainloop()

{

      Render();

}

 
Advert (Support Website)

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