www.xbdev.net xbdev - software development
Wednesday June 19, 2019
home | about | contact | Donations
>>
     
 

ShaderX..

Making our graphics card work for us..

 

Vertex Shaders - Prt4 - 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"

            // Rotates our square

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

            // Caluculates our new normals, and applys them to our colour

            "dp3    r0,    v3,    c4    // r0-Temp register, v3-Input normal, c4-Constant\n"

            "mul    oD0,   r0.x,  v5    // oDn-Output colour, v5-Input colour value\n"

      };

     

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

      DWORD dwDecl[] =

      {

            D3DVSD_STREAM(0),

          D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),    // D3DVSDE_POSITION=0 e.g. v0

            D3DVSD_REG(D3DVSDE_NORMAL,   D3DVSDT_FLOAT3),    // D3DVSDE_NORMAL  =3 e.g  v3

            D3DVSD_REG(D3DVSDE_DIFFUSE,  D3DVSDT_D3DCOLOR),  // D3DVSDE_DIFFUSE =5 e.g. v5

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

 

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

 

      D3DXMATRIX m;

      D3DXMatrixInverse(&m, NULL, &matWorld);

      D3DXVECTOR3 vLight = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

      D3DXVec3TransformNormal(&vLight, &vLight, &m);

      D3DXVec3Normalize(&vLight, &vLight);

      vLight= -vLight;

 

      g_pd3dDevice->SetVertexShaderConstant(4, &vLight, 1);

 

 

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

      struct stVert

      {

            float x, y, z;  // D3DFVF_XYZ

            float nx,ny,nz; // D3DFVF_NORMAL

            DWORD colour;   // D3DFVF_DIFFUSE

      };

      UINT myFVFFormat = D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL ;

 

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

      stVert myBox[]=

      {

    //      xyz position          normal          colour

    //   _________________    _______________    ________

    //  /                 \  /               \  /        \ 

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

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

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

 

            {-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000},

            { 0.5f,  0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000},

            { 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000}

      };

 

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

}

 

 

 

 

 

 

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

/*                                                                                */

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

/*                                                                                */

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

/*

    // How about this for an idea...why not have a go at passing more work along

      // to our graphics card:

      Change your original one from this:

      const char SimpleShader[] =

      {

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

            // Rotates our square

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

            // Caluculates our new normals, and applys them to our colour

            "dp3    r0,    v3,    c4    // r0-Temp register, v3-Input normal, c4-Constant\n"

            "mul    oD0,   r0.x,  v5    // oDn-Output colour, v5-Input colour value\n"

      };

 

      To this:

    const char SimpleShader[] =

      {

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

            // Rotates our square

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

            // Caluculates our new normals, and applys them to our colour

            // Transform our inverse world matrix that we passed in.

            "dp3    r3.x,   v3,   c4   \n"

            "dp3    r3.y,   v3,   c5   \n"

            "dp3    r3.z,   v3,   c6   \n"

            // These three lines normalise our r3 value for us

            "dp3    r3.w,   r3,   r3   \n"

            "rsq    r3.w,   r3.w       \n"

            "mul    r3,     r3,   r3.w \n"

        // Finally apply to the output colour

            "mul    oD0,    -r3.z,   v5   \n"

      };

 

 

      And change this:

      D3DXMATRIX m;

      D3DXMatrixInverse(&m, NULL, &matWorld);

      D3DXVECTOR3 vLight = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

      D3DXVec3TransformNormal(&vLight, &vLight, &m);

      D3DXVec3Normalize(&vLight, &vLight);

      vLight= -vLight;

      g_pd3dDevice->SetVertexShaderConstant(4, &vLight, 1);

 

      To This:

      D3DXMATRIX m;

      D3DXMatrixInverse(&m, NULL, &matWorld);

      g_pd3dDevice->SetVertexShaderConstant(4, &m, 4);

 

*/

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

 

 

 

 

 
 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.