November 28, 2020
Hot Topics:

Creating an Interactive 3D world with C# and DirectX

  • By Fatima Ahmed
  • Send Email »
  • More Articles »

4. Apply Special Effects

Special effects are nothing but the real-world look and feel given to the 3D scene. This includes the following effects applied:

  • Texture mapping
  • Material mapping
  • Shading
  • Lighting
  • Reflection
  • Shadows
  • Blending
  • Fog
  • Anti-aliasing
  • Sound effects

In this example application, not many of the effects are utilized, but you can add these as per the requirements of your own applications. Here, texture, material, and lighting effects are used.

//Load the texture
//Declaring a light
DxVBLibA.D3DLIGHT8 light;
//Initializing the light with values
light.ambient.a    = 0.8f;
light.ambient.r    = 0.8f;
light.ambient.g    = 0.8f;
light.ambient.b    = 0.8f;
light.diffuse.a    = 1.0f;
light.diffuse.r    = 1.0f;
light.diffuse.g    = 1.0f;
light.diffuse.b    = 1.0f;
light.specular.a   = 1f;
light.specular.r   = 0.2f;
light.specular.g   = 0.2f;
light.specular.b   = 0.2f;
light.Direction    = global.Vector3(0.0f,1.0f,0.0f);
light.Position     = global.Vector3(0.0f, 400.0f, 10.0f);
light.Range        = 1000.0f;
light.Attenuation0 = 0.5f;
light.Falloff      = 1;
light.Phi          = 3.14f / 4.0f;
light.Theta        = light.Phi / 2;
//Create the light in the scene
TVLightEngine.CreateLight(ref light,"light", false);

Figure 10: Applying special effects

Here, the table is given a material of glass in 3D Studio Max and loaded with its material preserved, as seen in the above picture.

Figure 11: Setting material properties in 3D studio Max

5. Define User Input Controls

In the Form1 class, define the variables for mouse and keyboard movement:

// Now, for the mouse input...
int tmpMouseX,tmpMouseY;
short tmpMouseB1,tmpMouseB2,tmpMouseB3;
int tmpMouseScrollOld,tmpMouseScrollNew;
// We could have done this in many ways, but we added some smoothing to
// the movement see we need to declare two additional variables.
float sngWalk;
float sngStrafe;

I have made a Check_Input method and have called it in the main loop:

private void Check_Input()
   sngWalk = 0.0f;
   sngStrafe = 0.0f;
   // Check if we pressed the PAGEUP key, if so, then scene moves in +y direction
      TVScene.SetCamera(sngPositionX, sngPositionY+=0.5f, sngPositionZ,
                        snglookatX, snglookatY+=0.5f, snglookatZ);
   // Check if we pressed the PAGEDOWN key, if so, then scene moves in -y direction
      TVScene.SetCamera(sngPositionX, sngPositionY-=0.5f, sngPositionZ,
                        snglookatX, snglookatY-=0.5f, snglookatZ);
   // Check if we pressed the UP arrow key, if so, then we are
   // walking forward.
      sngWalk = 0.1f;
   // If we are not walking forward, maybe we are walking backward
   // by using the DOWN arrow? If so, set walk speed to negative.
      sngWalk = -0.1f;
   // Check if we pressed the LEFT arrow key, if so, then strafe
   // on the left.
      sngStrafe = 0.1f;
   // If we are not strafing left, maybe we want to strafe to the
   // right, using the RIGHT arrow? If so, set strafe to negative.
      sngStrafe = -0.1f;
   // Actual value to old mouse scroller value.
   tmpMouseScrollOld = tmpMouseScrollNew;
   // Get the movement of the mouse.
   Inp.GetMouseState(ref tmpMouseX, ref tmpMouseY, ref tmpMouseB1,
                     ref tmpMouseB2, ref tmpMouseB3,
                     ref tmpMouseScrollNew);
   // Update the camera angles.
   sngAngleX = sngAngleX -( (float)tmpMouseY / 100);
   sngAngleY = sngAngleY -( (float)tmpMouseX / 100);

Page 5 of 7

This article was originally published on February 3, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date