www.xbdev.net
xbdev - software development
Thursday November 14, 2024
Home | Contact | Support | XBOX Programming.. More than just a toy...
     
 

XBOX Programming..

More than just a toy...

 

Prt4 - Textured Triangle, I'm scared...

Author: Ben_3D@xbdev.net

 

Well the codes not that much different than a simple coloured triangle, just added a couple of new lines, ... now the codes starting to get long and if you understand it good, if not, sit down with a good cup of coffee and go over it a few times.  These are the basics that all the other examples will work on.

 

//Application entry point

void __cdecl main()

{

      InitialiseD3D();

      while(true)

      {

            //Clear the backbuffer to black

            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 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

                  DrawTexturedTriangle();

 

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

}

 

Well as I promised practically anything has changed in the main() function, anything new has been commented with "//NEW NEW" at the start and end.

 

//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 and a zbuffer

      d3dpp.BackBufferCount = 1;

      d3dpp.EnableAutoDepthStencil = TRUE;

      d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

 

 

 

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

 

 

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

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

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

 

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

      g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

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

 

}

 

 

 

void CleanUp()

{

    g_pD3DDevice->Release();

    g_pD3D->Release();

}

 

Well below shows the juicy part of the code which actually makes the drawing of a textured triangle possible.  I can see the excitement in your eyes - just think of the possibilities.

 

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

LPDIRECT3DTEXTURE8 pTexture = NULL;

 

struct CUSTOMVERTEX

{

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

    DWORD colour; // The vertex colour.

    FLOAT tu, tv;

};

 

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

 

 

void DrawTexturedTriangle()

{

    VOID* pVertices;

   

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

    CUSTOMVERTEX cvVertices[] =

    {

        { -1.0f, -1.0f, 0.0f, 0x00FF0000, 0.0f, 1.0f }, // x, y, z, color

            { -1.0f,  1.0f, 0.0f, 0x0000FF00, 0.0f, 0.0f },

            {  1.0f,  1.0f, 0.0f, 0x000000FF, 1.0f, 0.0f }

    };

     

      //FileName is "D:\\xfactordev.bmp"

      D3DXCreateTextureFromFile(g_pD3DDevice, "D:\\xfactordev.bmp", &pTexture);

 

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

    //Set our background to use our texture buffer

    g_pD3DDevice->SetTexture(0, pTexture);

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

 

      g_pVertexBuffer->Release();

      pTexture->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

 

An important thing not to forget when testing this code, don't forget to put the bitmap (e.g. xfactordev.bmp) in the same folder that your running the .xbe from.  Or else it will just crash your xbox and you'll have to re-start it.

Its a simple piece of code, the only real difference between this piece of code and the code from earlier (e.g. a basic triangle) is that we have a DirectX texture buffer, and have added texture coordinates to our CUSTOMVERTEX structure.

When you run this code, you'll get half a triangle pasted across your screen which is blue, and on the other half, you'll get a textured triangle with the contents of the bitmap stretched onto it.

I can see you drooling with excitement....Oooo...yeah...just think, where getting closer and closer to that polygon drawn character with a texture mapped onto him.

 

(Back Tutorials Page)

 

 

 

 

 

 
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.