www.xbdev.net xbdev - software development
Saturday May 25, 2019
home | about | contact | Donations

     
 

ShaderX..

Making our graphics card work for us..

 

Vertex Shaders - Prt1 - Basics

by bkenwright@xbdev.net

 

 

 

 

 

 

 

DownLoadCode
////////////////////////////////////////////////////////////////////////////////////

//                                                                                \\

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

// our simple triangle... notice how all our vertex shader code is all in this    \\

// one function...I did that so you could see the birth and death of how it works \\

// in one easy step.... eventually you could put the creation..and destruction    \\

// code in seperate function..etc...and optimise your code for better performance \\

// ...                                                                            \\

//                                                                                \\

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

 

void Render()

{

      if(!g_pd3dDevice)return;

 

      //<-------------------Start of our vertex shader code-------------------------->

      const char SimpleShader[] =

      {

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

            "mov     oPos,  v0          // oPos-Output register, v0-Input register\n"

      };

 

      DWORD dwDecl[] =

      {

            D3DVSD_STREAM(0),

            D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT2),

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

 

      // Don't forget to put these lines in your code...I've added them to the bottom

      // of the function!...always release resources after creating them.

      //g_pd3dDevice->DeleteVertexShader(dwDecl);

      //pVS->Release();

 

      //<-------------------End of our vertex shader code---------------------------->

 

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

   

      //<-------------------Using our Vertex Shader Here ---------------------------->

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

}

 
 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.