www.xbdev.net
xbdev - software development
Tuesday October 29, 2024
Home | Contact | Support | ShaderX.. Making our graphics card work for us..
>>
     
 

ShaderX..

Making our graphics card work for us..

 

Vertex Shaders - Prt3 - And then there was colour!

by bkenwright@xbdev.net

 

 

 

 

 

 

DownloadCode

////////////////////////////////////////////////////////////////////////////////////

//                                                                                \\

// This little function is called over and over again, and does the rendering of  \\

// our triangles...Its an all in one Pixel Shader function...the code has been    \\

// kept together for tutorial/explanation purposes...so you can see it in action  \\

// See how simple it really is....                                                \\

//                                                                                \\

//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

void Render()

{

      if(!g_pd3dDevice)return;

 

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

      // CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED

     

      const char SimpleShader[] =

      {

            "vs.1.1                     // Shader version 1.1 \n"

            "m4x4    oPos,  v0,  c0     // oPos-Output position, v0-Input position, c0-Constant register\n"

            "mov     oD0,   v5          // oDn-Output colour, v5-Input colour value\n"

      };

     

 

      DWORD dwDecl[] =

      {

            D3DVSD_STREAM(0),

            D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),

            D3DVSD_REG(D3DVSDE_DIFFUSE,  D3DVSDT_D3DCOLOR),

            D3DVSD_END()

      };

 

      DWORD dwShaderID;

 

      ID3DXBuffer* pVS;

 

      D3DXAssembleShader( SimpleShader, sizeof(SimpleShader)-1,

                                    0, NULL, &pVS, NULL );

 

      g_pd3dDevice->CreateVertexShader(dwDecl, (DWORD*)pVS->GetBufferPointer(),

                                                       &dwShaderID, 0);

     

      //g_pd3dDevice->SetVertexShader( dwShaderID );

 

      //g_pd3dDevice->DeleteVertexShader(dwDecl);

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

      // These few extra lines are used to generate some data and pass it to our

      // vertex shader contant register, which we have used above as c[0].

      // Again I've done the multiplication of the World and Projection matrix's here

      // so you get to see the principle of whats happening...but wait until

      // we pass more and more to our graphics card...and really make it sweat!

 

      // Setup the world, view, and projection matrices

      WorldMatrices();

 

      D3DXMATRIX mat;

      D3DXMatrixMultiply(&mat, &matWorld, &matView);

      D3DXMatrixMultiply(&mat, &mat,      &matProj);

      D3DXMatrixTranspose(&mat, &mat);

   

      //                                         +--- The data we are passing

      //                                        \|/

      //                                         |

      g_pd3dDevice->SetVertexShaderConstant(0, &mat, 4);

      //                                    |        |

      //                                   /|\      /|\

      //                                    |        +-- Number of 4 byte chunks!

      //                                    +-- Constant Register c[0]

         

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

 

 

      struct stVert

      {

            FLOAT x, y, z;  // D3DFVF_XYZ

            DWORD colour;   // D3DFVF_DIFFUSE

      };

      UINT myFVFFormat = D3DFVF_XYZ|D3DFVF_DIFFUSE ;

 

      stVert myBox[]=

      {

            {-0.5f, -0.5f, 0.0f, 0xff00ff00},

            { 0.5f,  0.5f, 0.0f, 0xffff0000},

            {-0.5f,  0.5f, 0.0f, 0xff0000ff},

 

            {-0.5f, -0.5f, 0.0f, 0xffffff00},

            { 0.5f,  0.5f, 0.0f, 0xff00ff00},

            { 0.5f, -0.5f, 0.0f, 0xffff00ff}

      };

 

      IDirect3DVertexBuffer8 * pVertexBuffer;

      g_pd3dDevice->CreateVertexBuffer( 6*sizeof(stVert), 0,

                                    myFVFFormat, D3DPOOL_MANAGED, &pVertexBuffer );

 

      unsigned char *p;

    pVertexBuffer->Lock(0,0, &p, 0);

    memcpy(p, myBox, 6*sizeof(stVert) );

    pVertexBuffer->Unlock();

 

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

    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);

    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);

 

    // Clear the backbuffer and the zbuffer

    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,

                                            D3DCOLOR_XRGB(0x00,0x00,0xff), 1.0f, 0 );

  

    // Draw our triangle.

    g_pd3dDevice->SetStreamSource(0, pVertexBuffer, sizeof(stVert));

     

    g_pd3dDevice->SetVertexShader( dwShaderID );

 

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

 

    // End the scene

    g_pd3dDevice->EndScene();

   

    // Present the backbuffer contents to the display

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

 

    g_pd3dDevice->DeleteVertexShader(dwShaderID);

    pVS->Release();

 

    pVertexBuffer->Release();

}

 

 

 

 

Well I didn't want to have to redo the code over and over again...you can change the below lines where appropriate to get a real feel of how vertex shaders work...and how you would use them....below are some example changes...which work...but you can try them anyhow...

 

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

/*                                                                                */

/* Ideas to try out... well by looking at the code above, you can think of some   */

/* simple alternatives to try out....I've put some ideas below for you...its good */

/* to experiment, and dont' worry about your program crashing etc....learning     */

/* takes time....                                                                 */

/*                                                                                */

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

/*

      // In this example, I'm using a constant colour, instead of the verice colours

      // I'm setting it into the contant register c[5]...which you can then use in

      // your Vertex Asm Code...

 

      float colour[4] = {1.0f, 1.0f, 0.0f, 1.0f};

      g_pd3dDevice->SetVertexShaderConstant(5, colour, 1);

 

      And use:

      const char SimpleShader[] =

      {

            "vs.1.1                     // Shader version 1.1 \n"

            "m4x4    oPos,  v0,  c0     // oPos-Output position, v0-Input position, c0-Constant register\n"

            "mov     oD0,   c5          // oDn-Output colour, c5-constant register we set above\n"

      };

*/

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

/*

      // OOoooo...now where doing more and more....lets modify our v5 value...

      // lets add one of the input coordinate values to it...it can't harm :)

 

      float colour[4] = {1.0f, 1.0f, 0.0f, 1.0f};

      g_pd3dDevice->SetVertexShaderConstant(5, colour, 1);

 

      And use:

      const char SimpleShader[] =

      {

            "vs.1.1                     // Shader version 1.1 \n"

            "m4x4    oPos,  v0,  c0     // oPos-Output position, v0-Input position, c0-Constant register\n"

            "mov     r0,    v5          // r0-Temporary register, v5-Input colour value\n"

            "add     r0,    r0,  c0.z   // Add our .x pos to our colour\n"

            "mov     oD0,   r0          // oDn-Output colour, putting our new colour value in to oD0\n"

      };

*/

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 
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.