Andy Pike - DirectX 8 Programming Tutorial

Здесь есть возможность читать онлайн «Andy Pike - DirectX 8 Programming Tutorial» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

DirectX 8 Programming Tutorial: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «DirectX 8 Programming Tutorial»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

DirectX 8 Programming Tutorial — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «DirectX 8 Programming Tutorial», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать
How do I use transformation matrices in DirectX?

In the code for this tutorial, we will have 5 cubes in different positions all rotating differently. Here is a walkthtough of the code and how it works.

Step 1: Create objects

The first thing we need to do is create and define our five cubes. I have defined 5 cubes as member variables and modified the InitialiseGame method of CGame to create and set their centre position. Their default size is 10x10x10 which I haven't changed. The InitialiseGame method is now as follows:

bool CGame::InitialiseGame() {

//Setup games objects here

m_pCube1 = new CCuboid(m_pD3DDevice);

m_pCube1->SetPosition(-27.0, 0.0, 0.0);

m_pCube2 = new CCuboid(m_pD3DDevice);

m_pCube2->SetPosition(-9.0, 0.0, 0.0);

m_pCube3 = new CCuboid(m_pD3DDevice);

m_pCube3->SetPosition(9.0, 0.0, 0.0);

m_pCube4 = new CCuboid(m_pD3DDevice);

m_pCube4->SetPosition(27.0, 0.0, 0.0);

m_pCube5 = new CCuboid(m_pD3DDevice);

m_pCube5->SetPosition(0.0, 15.0, 0.0);

return true;

}

Fig 5.4 below shows the initial positions of our five cubes. We would normally create all of these objects at the origin, and the translate them. But for the purpose of this tutorial, we'll create them in the positions show below. The centre coordinates are shown with each cube.

Fig 5.4

Step 2: Create Transformation Matrices

The next step is to create our transformation matrices. We want our cubes to all rotate differently, so we need a different transformation matrix for each one. Cube 1, 2 and 3 will rotate around the x, y, and z axis respectively. Cube 4 will rotate around a user defined axis and cube 5 will rotate around the x, y and z axis, and it will be enlarged (scaled).

To create the x, y and z rotation matrices, we will use the DirectX functions D3DXMatrixRotationX, D3DXMatrixRotationY and D3DXMatrixRotationZ. The first parameter is a pointer to a D3DXMATRIX structure that will hold the rotation matrix, the second parameter is the angle to rotate in radians. The code snippet below, taken from our new Render method, shows these calls.

//Create the rotation transformation matrices around the x, y and z axis

D3DXMatrixRotationX(&matRotationX, timeGetTime()/400.0f);

D3DXMatrixRotationY(&matRotationY, timeGetTime()/400.0f);

D3DXMatrixRotationZ(&matRotationZ, timeGetTime()/400.0f);

The next thing to do is create the rotation matrix around a user define axis. To do this, we will use the D3DXMatrixRotationAxis function of DirectX. The first parameter is a pointer to a D3DXMATRIX structure that will hold the rotation matrix. The second parameter is a pointer to a D3DXVECTOR3 structure that defines our user defined axis. We want our axis to be a 45 degree angle between the x and y axis, so we define our access by using the following vector (1, 1, 0). Fig 5.5 below shows how to define our axis. The third parameter is the angle to rotate in radians.

//Create the rotation transformation matrices around our user defined axis

D3DXMatrixRotationAxis(&matRotationUser1, &D3DXVECTOR3(1.0f, 1.0f, 0.0f), timeGetTime()/400.0f);

Fig 5.5

In addition to rotation, we will need to create some other matrices. We will need a matrix to scale cube 5 so that it is 50% larger. We will also need some matrices to translate (move) our cube to the origin and back again (I'll explain why later). So, we use the following code to create these matrices:

//Create the translation (move) matrices

D3DXMatrixTranslation(&matMoveRight27, 27.0, 0.0, 0.0);

D3DXMatrixTranslation(&matMoveLeft27, –27.0, 0.0, 0.0);

D3DXMatrixTranslation(&matMoveRight9, 9.0, 0.0, 0.0);

D3DXMatrixTranslation(&matMoveLeft9, –9.0, 0.0, 0.0);

D3DXMatrixTranslation(&matMoveDown15, 0.0, –15.0, 0.0);

D3DXMatrixTranslation(&matMoveUp15, 0.0, 15.0, 0.0);

//Create a scale transformation

D3DXMatrixScaling(&matScaleUp1p5, 1.5, 1.5, 1.5);

Step 3: Multiply Matrices

Now that we have our matrices, we need to multiply them together to create one transformation matrix for each cube. We will use the DirectX function D3DXMatrixMultiply to do this. Cube 1 is easy, we want it to rotate around the x axis. Becuase it's center is on the x axis, all we need to do is use the x axis rotation matrix, that means that we don't need to multiply any matrices together because we already have this matrix defined. Cubes 2-5 are a bit different, they are not on the axis that we want to rotate them around. So, what we need to do is move them on to their rotation axis, rotate them, then move them back to their starting positions. If we just rotate them without moving them, the cubes will just swing around the axis rather than staying in the same position and rotating. Fig 5.6 shows a cube swinging around the y axis. Fig 5.7 shows a cube being moved to the origin, rotated, them moved back.

Fig 5.6

Fig 5.7

We have the matrix for cube 1, so now we need to create a matrix for cubes 2, 3, 4 and 5. The follow code snippet shows how to do this. Notice the order in which the matrices are multiplied together. If you change this order, you will get a different result.

//Combine the matrices to form 4 transformation matrices

D3DXMatrixMultiply(&matTransformation2, &matMoveRight9, &matRotationY);

D3DXMatrixMultiply(&matTransformation2, &matTransformation2, &matMoveLeft9);

D3DXMatrixMultiply(&matTransformation3, &matMoveLeft9, &matRotationZ);

D3DXMatrixMultiply(&matTransformation3, &matTransformation3, &matMoveRight9);

D3DXMatrixMultiply(&matTransformation4, &matMoveLeft27, &matRotationUser1);

D3DXMatrixMultiply(&matTransformation4, &matTransformation4, &matMoveRight27);

D3DXMatrixMultiply(&matTransformation5, &matMoveDown15, &matRotationY);

D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationX);

D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matRotationZ);

D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matMoveUp15);

D3DXMatrixMultiply(&matTransformation5, &matTransformation5, &matScaleUp1p5);

Step 4: Applying the Transformations

Now that we have a transformation matrix for each cube, we need to apply them and then render the cubes. To apply a transformation matix, we use the SetTransform method. When you call SetTransform with a transformation matrix, all further objects that are rendered will have that matrix appied to them. So, for each cube we need to call SetTransform with that cubes transformation matrix, and then render that cube. The code snippet below shows this:

//Apply the transformations and render our objects

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matRotationX);

m_pCube1->Render();

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation2);

m_pCube2->Render();

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation3);

m_pCube3->Render();

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation4);

m_pCube4->Render();

m_pD3DDevice->SetTransform(D3DTS_WORLD, &matTransformation5);

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «DirectX 8 Programming Tutorial»

Представляем Вашему вниманию похожие книги на «DirectX 8 Programming Tutorial» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «DirectX 8 Programming Tutorial»

Обсуждение, отзывы о книге «DirectX 8 Programming Tutorial» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x