Time for action — using colors to see interpolation

To see how color interpolation works we need to change our code a bit.

  1. Again, copy the material and make sure to adjust all names.
  2. The only thing we need to change in the material is that we don't need a texture unit. We can just delete it.
  3. In the application code, we need to replace the textureCoord() with color():
    manual->position(5.0, 0.0, 0.0);
    manual->color(0,0,1);
    manual->position(-5.0, 10.0, 0.0);
    manual->color(0,1,0);
    manual->position(-5.0, 0.0, 0.0);
    manual->color(0,1,0);
    manual->position(5.0, 10.0, 0.0);
    manual->color(0,0,1);
    
  4. The vertex shader also needs some adjustments. Replace the two texture coordinate parameters with color parameters and also change the assignment line:
    void MyVertexShader4(
    float4 position : POSITION,
    out float4 oPosition : POSITION,
    float4 color :COLOR,
    out float4 ocolor :COLOR,
    uniform float4x4 worldViewMatrix)
    {
    oPosition = mul(worldViewMatrix, position);
    ocolor = color;
    }
    
  5. The fragment shader now has two color parameters one incoming and one outgoing:
    void MyFragmentShader4( float4 color : COLOR,
    out float4 oColor : COLOR)
    {
    oColor = color;
    }
    
  6. Compile and run the application. You should see the quad with the right side blue and the left side green and the colors should fade into each other in between.
    Time for action — using colors to see interpolation

What just happened?

In step 3, we saw another function of the manual object, namely, adding color to a vertex using three float values for red, green, and blue. Step 4 replaced the texture coordinates with color parameters this time we wanted colors not textures. The same is true for step 5. This example wasn't really difficult or exciting, but it shows how interpolation works. This gives us a better understanding of how the vertex and fragment shader also work together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.151