www.xbdev.net
xbdev - software development
Friday March 29, 2024
home | contact | Support | ShaderX.. Making our graphics card work for us..

     
 

ShaderX..

Making our graphics card work for us..

 

Vertex Shaders - Prt2 - Rotating Square!

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"  

      };

 

      DWORD dwDecl[] =

      {

            D3DVSD_STREAM(0),

          D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),

            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

      };

      UINT myFVFFormat = D3DFVF_XYZ ;

 

      stVert myBox[]=

      {

            {-0.5f, -0.5f, 0.0f},

            { 0.5f,  0.5f, 0.0f},

            {-0.5f,  0.5f, 0.0f},

 

            {-0.5f, -0.5f, 0.0f},

            { 0.5f,  0.5f, 0.0f},

            { 0.5f, -0.5f, 0.0f}

      };

 

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

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
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.