xbdev - software development
Sunday March 26, 2023
home | about | contact | Donations

Prt2. DirectX3D - Triangle (Textured)..
by bkenwright@xbdev.net

Using the simple arrangment I spoke about earlier.. .putting all the repetative unchangine windows code in main.cpp ... and letting us just change the code in game.cpp... (download full source code.)

What do we have here I here you ask!.. Its a very simple textured triangle :)

Its not much to look at our results... but its kept to the simplest.. basic.. only what you need code.  I just wanted to show you that you can draw textured shapes to the screen relatively easy.

Our code that accomplishes this!  The game.cpp file:



/*                                                                         */

/* File: game.cpp                                                          */

/*                                                                         */



#include <d3dx8.h>

#include <d3d8.h>


// Each point in 3D space will be represented by this structure which we are

// defining here!  So it will have an x,y,z point, a colour, and a texture

// placement... we could really leave out the colour part... but I'm lazy!



    FLOAT x, y, z;

      DWORD colour;

      FLOAT tu, tv;



CUSTOMVERTEX cvVertices[] ={

      // x      y      z           colour           tu     tv

   { -1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 0.0f, 1.0f },  //Front face

   { -1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 0.0f, 0.0f },

   {  1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 1.0f, 0.0f },

   {  1.0f,  1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 1.0f, 0.0f },

   {  1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 1.0f, 1.0f },

   { -1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255,0,0), 0.0f, 1.0f }




// Display code

void Render()





      IDirect3DVertexBuffer8* g_pVertexBuffer = NULL; // Buffer to hold vertices

      IDirect3DTexture8* pTexture = NULL; //texture


      // This is where we define what is in our vertice points...as I said earlier

      // there made up of [1].xyz position, [2].colour and [3].texture placement.

      UINT D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);//added textures to the vertexes


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


      // Begin the scene.




      D3DXMATRIX view_matrix, matProj;

      D3DXMatrixLookAtLH(&view_matrix,&D3DXVECTOR3( 0.0f, 0.0f,-8.0f ),

                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),

                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ));


      D3DXMatrixPerspectiveFovLH(&matProj, //Result Matrix

                              D3DX_PI/4,//Field of View, in radians. (PI/4) is typical

                              ((float)600 / (float)400),     //Aspect ratio

                              1.0f,     //Near view plane

                              100.0f ); // Far view plane

      g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );

//end camera



      g_pD3DDevice->CreateVertexBuffer(sizeof(cvVertices), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVertexBuffer);

      VOID* pVertices;

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

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



      // Here is the API that loads in our image.

      D3DXCreateTextureFromFile(g_pD3DDevice, "image.bmp", &pTexture);


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


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



      // End the scene.



      // After rendering the scene we display it.

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







// The mainloop() function here is just called repeately from the main.cpp file...e.g.

// its a loop so that when no messages are being processed by windows.. we come here

// you could have just put a while loop... but how would you exit the program!  Must play

// nice with the other programs on your computer as well!.

void mainloop()





Usually when your designing anything that uses textures... you would load the texture once and keep the reference pointer to it safe somewhere... as in the above code I'm loading and destroying it all the time... same with the vertices... you would create them and them just refer to them and draw them whenever you needed to.



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