Выбрать главу

  {m_rX – (m_rWidth/2), m_rY + (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,}, //Vertex 5 – Blue

  {m_rX + (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,}, //Vertex 6 – Green

  {m_rX + (m_rWidth/2), m_rY + (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,}, //Vertex 7 – Red

  //Face 2

  {m_rX + (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 1.0f,}, //Vertex 8 – Blue

  {m_rX + (m_rWidth/2), m_rY + (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 0.0f,}, //Vertex 9 – Green

  //Face 3

  {m_rX – (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,}, //Vertex 10 – Green

  {m_rX – (m_rWidth/2), m_rY + (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,}, //Vertex 11 – Red

  //Face 4

  {m_rX – (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 1.0f,}, //Vertex 12 – Red

  {m_rX – (m_rWidth/2), m_rY + (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,}, //Vertex 13 – Blue

  //Bottom Face

  {m_rX + (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 1.0f,}, //Vertex 14 – Green

  {m_rX + (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,}, //Vertex 15 – Blue

  {m_rX – (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ – (m_rDepth/2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,}, //Vertex 16 – Red

  {m_rX – (m_rWidth/2), m_rY – (m_rHeight/2), m_rZ + (m_rDepth/2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0.0f,}, //Vertex 17 – Green

 };

 //Get a pointer to the vertex buffer vertices and lock the vertex buffer

 if (FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0))) {

  return false;

 }

 //Copy our stored vertices values into the vertex buffer

 memcpy(pVertices, cvVertices, sizeof(cvVertices));

 //Unlock the vertex buffer

 m_pVertexBuffer->Unlock();

 return true;

}

Step 4: Rendering

The final code change is in the Render method of CCuboid. We need to set the texture that we want to render using SetTexture. Then we need to set how the texture should be rendered by using the SetTextureStageState method of our device. We have selected that our texture should be rendered without any blending or similar effects. You can blend your texture with other textures or the colour of the vertices depending on which flags to select.

//Set how the texture should be rendered.

if (m_pTexture != NULL) {

 //A texture has been set. We don't want to blend our texture with

 //the colours of our vertices, so use D3DTOP_SELECTARG1

 m_pD3DDevice->SetTexture(0, m_pTexture);

 m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

} else {

 //No texture has been set. So we will disable texture rendering.

 m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);

}

Once you have made these changes, you should finish up with five rotating cubes, each with a different texture (shown below).

Summary

In this tutorial we've learnt all about textures. There is more to textures in DirectX than that, and we'll take a look at these topics in a future tutorial. In the next tutorial, we'll look into lighting.

DirectX Tutorial 7: Lighting and Materials

Introduction

In this tutorial we will learn about lighting and materials in DirectX. We'll create four cubes and place them around the origin (based on the last tutorial) then rotate them. We'll place a light source in the middle of these cubes, near the origin. You will now be able to see how lighting affects your objects in 3D space. You can download the full source code by clicking the "Download Source" link above.

DirectX Lighting vs. Real World Lighting

In DirectX you can create different types of lights that will make your scene seem more realistic. But the lighting model that DirectX uses is only an approximation of light in the real world. In the real world, light is emitted from a source like a light bulb or torch and travels in a straight line until it fades out or enters your eye. As light travels, it can hit objects and be reflected in a different direction. When it is reflected, the object may absorb some of the light. In fact, light can be reflected hundreds, thousands or even millions of times before it fades out or reaches your eye. Light is reflected differently by each object depending on the material that it is made of. Shiny materials reflect more of the light than non-shiny materials. The amount of calculations to model this in virtual 3D space is too large for real-time rendering. So DirectX approximates lighting.

Attributes of a light

For different lights you can specify different attributes. Not all lights use all of the attributes that are listed below:

Position

This is the position in 3D space where the light source is located. This will be a coordinate in 3D space such as (0, 10, 0).

Direction

This is the direction in which light is emitted from the light source. This will be a vector such as (0, –1, 0).

This is the maximum distance from the light source that the light will travel. Any objects that are out of range will not receive light from this light source.

Attenuation

This is how light changes over distance. This is normally the rate that light fades out between the light source and lights range. You can specify that light does not fade out or that it gets brighter over distance if you want to.

Diffuse Light

This is the colour of diffuse light that is emitted by the light. Diffuse light is light that has been scattered, but it still has direction as opposed to ambient light that does not.

Ambient Light

This is the colour of ambient light that is emitted by the light. Ambient light is general background light. Ambient light has been scattered so much that it does not have a direction or source and is at the same everywhere in the scene.

Specular Light

This is the colour of specular light that is emitted by the light. Specular light is the opposite of diffuse light. Specular light is not scattered at all, you can use specular light to create highlights on your objects.

Types of lighting

There are four types of lights that you can create in your scene, each of which have their own behaviours and attributes.

Ambient Light

As well as being an attribute of light, you can create a general ambient light level for the scene that is independent of other lights. You can specify how much ambient light there is and what colour it is. You can define the colour by specifying it's red, green and blue (RGB) values.

Point Light

An example of a point light is a light bulb. It has a position but no direction because light is emitted in all directions equally. It also has colour, range and attenuation attributes that can be set. Fig 7.1 below, shows how light is emitted from a point light.