Monday, November 26, 2012

Modifying the Shader Example for Assignment 5

Here are the steps I took to modify the crater shader example available on the course web page to make a wave effect.  This is part of what you had to do for Assignment 5.

  • Removed warnings. ;)
     
  • In the mesh_wave.fx file added in the following:
    uniform extern texture gWorldTex;
    uniform extern float3 direction;
    uniform extern int amplitude;
    uniform extern float wavelength;

     
  • Added the following sampler info structure for use with textures:
    sampler2D mySampler = sampler_state {
        Texture = (gWorldTex);
        MinFilter = Linear;
        MagFilter = Linear;
        AddressU = Wrap;
        AddressV = Wrap;
    };

      
  • Added or modified the following structs to the effect file:

    struct inputVS {
        float3 pos : POSITION0;
        float4 col : COLOR0;
        float2 tex0: TEXCOORD0;
    };


    struct OutputVS {
        float4 pos : POSITION0;
        float4 col : COLOR0;
        float2 tex0: TEXCOORD0;
    };

     
  • Added a new vertex shader in the effect file called directionalWaveVS:
     
    • used the following formula to determine the distance from the plane with the normal given by the direction passed in:
      d = direction.x * vin.pos.x + direction.z * vin.pos.z;
       
    • used the following formula to compute the new y position of the current vertex being processed, using the distance computed above and an omega value of 0.07:
      vin.pos.y += amplitude*cos(wavelength*d-omega*time);
       
    • transformed the current vertex using the world, view, and projection matrices
       
    • saved the input colour and texture value to be passed along to the pixel shader
       
  • Modified the pixel shader function to use the texture's colour instead of the vertex's colour. Used the following function with the input texture coordinate to accomplish this:
    tex2D(mySampler, v.tex0)
     
  • Added a wavesTechnique that uses the new vertex and pixel shaders in a single pass.
     
  • Added "waveTechnique" to the techniceName array (ignoring the misspelling...)
     
  • Added D3DXVECTOR2 tex0 to meshVertex in meshSurface.h
     
  • Added int amplitude, float waveDir and float waveLength to the meshSurface class, and initializing these values in the constructor (good defaults are amplitude=50, wavelength=0.01f, and waveDir=0.0f)
     
  • Added D3DXHANDLE hWaveTechnique, D3DXHANDLE hDirection, D3DXHANDLE hAmplitude, and D3DXHANDLE hWavelength to the meshSurface class
     
  • Added the following to the D3DVERTEXELEMENT9 decl in meshSurface.cpp:
    {0,0,D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}
     
    • And after that, assuming it was placed third in decl:
      decl[2].Offset = (char *) &v.tex0 - (char *) &v;
       
  • Set up texture usage (mTexture is already declared in the class):
     
    • released mTexture in the class destructor
    • updated the texture coordinates in the vertex structure in createSurface:
      vtx[k].tex0.x = texU;
      vtx[k].tex0.y = texV;
    • created the texture in initEffect:
      hTexture = pMeshEffect->GetParameterByName(NULL, "gWorldTex");
      D3DXCreateTextureFromFile(md3dDev, "earth.bmp", &mTexture);
  • Associated the direction, amplitude, and wavelength variables with their counterparts in the effects file in initEffect:
    hDirection = pMeshEffect->GetParameterByName(NULL, "direction");
    hAmplitude = pMeshEffect->GetParameterByName(NULL, "amplitude");
    hWavelength = pMeshEffect->GetParameterByName(NULL, "wavelength");

     
  • Associated the new wave technique in the effects file in initEffect:
    hTechnique = pMeshEffect->GetTechniqueByName(techniceName[0]);
     
  • Updated ShaderRender to compute the application-side values needed for the effect file:
     
    • computed the wave's direction (making sure to initialize the wave direction at some point - can default to zero, and if desired, can be controlled with keyboard input):
      float directionVec[3] = {sin(waveDir), 0.0f, cos(waveDir)}
       
    • copy the texture, direction, amplitude, and wavelength values into their counterparts in pMeshEffect
       
    • set the technique to the waves technique:
      pMeshEffect->SetTechnique(hWaveTechnique);

In the effects file, I also added a second wave to add onto the first one and played with the parameters to see what kinds of effects it gave me.  You can uncomment that line to see for yourself.

No comments:

Post a Comment