Andy Pike - DirectX 8 Programming Tutorial
Здесь есть возможность читать онлайн «Andy Pike - DirectX 8 Programming Tutorial» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.
- Название:DirectX 8 Programming Tutorial
- Автор:
- Жанр:
- Год:неизвестен
- ISBN:нет данных
- Рейтинг книги:5 / 5. Голосов: 1
-
Избранное:Добавить в избранное
- Отзывы:
-
Ваша оценка:
- 100
- 1
- 2
- 3
- 4
- 5
DirectX 8 Programming Tutorial: краткое содержание, описание и аннотация
Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «DirectX 8 Programming Tutorial»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.
DirectX 8 Programming Tutorial — читать онлайн бесплатно полную книгу (весь текст) целиком
Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «DirectX 8 Programming Tutorial», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.
Интервал:
Закладка:
In this tutorial we've look at index buffers and how they can make a massive saving on how many vertices need to be processed. In the next tutorial we will look at how to create other shapes such as spheres, cylinders and cones.
DirectX Tutorial 9: Textured Spheres, Cylinders and Cones
In this tutorial we will look at how to create a textured sphere, cylinder and cone. We'll set-up the normals so that the objects are shaded correctly. Also, we'll derive all of our classes from one base class called CBase, which has methods for HTML logging and Normal calculations. Our example application for this tutorial will consist of two cylinders, two cones and a simple model of the Moon rotating around Earth, which is rotating around the Sun. You can download the full source code by clicking the "Download Source" link above.
Our cylinder will be made up from two triangle fans, one for the top and one for the bottom. The sides of the cylinder will be made up from a triangle strip. Fig 9.1 below shows a diagram of the vertices and polygons for a cylinder. It also includes the normals for the four corners of one segment. To create our cylinder we have not used an index buffer, only a vertex buffer. This is because; although we do have shared vertices (between the sides and the top/bottom faces) we want different normals so the edges around the top and bottom appear sharp.
Fig 9.1
To create the cylinder we need to specify the number of segments. There are 8 segments in the diagram above. The more segments there are, the smoother and rounder the cylinder will appear. We also need to know the height and radius of the cylinder. Once we know the height, radius and number of segments, we can defined the position of our vertices together with their Normal value and texture coordinates. The following code snippet shows how this is done.
bool CCylinder::UpdateVertices() {
CYLINDER_CUSTOMVERTEX* pVertex;
WORD wVertexIndex = 0;
int nCurrentSegment;
//Lock the vertex buffer
if (FAILED(m_pVertexBuffer->Lock(0, 0, (BYTE**)&pVertex, 0))) {
LogError("
CCylinder: Unable to lock vertex buffer.");
return false;
}
float rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);
float rSegmentLength = 1.0f / (float)m_nSegments;
//Create the sides triangle strip
for (nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++) {
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
pVertex->x = x0;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = x0;
pVertex->ny = 0.0f;
pVertex->nz = z0;
pVertex->tu = 1.0f – (rSegmentLength * (float)nCurrentSegment);
pVertex->tv = 0.0f;
pVertex++;
pVertex->x = x0;
pVertex->y = 0.0f – (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = x0;
pVertex->ny = 0.0f;
pVertex->nz = z0;
pVertex->tu = 1.0f – (rSegmentLength * (float)nCurrentSegment);
pVertex->tv = 1.0f;
pVertex++;
}
//Create the top triangle fan: Center
pVertex->x = 0.0f;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = 0.0f;
pVertex->nx = 0.0f;
pVertex->ny = 1.0f;
pVertex->nz = 0.0f;
pVertex->tu = 0.5f;
pVertex->tv = 0.5f;
pVertex++;
//Create the top triangle fan: Edges
for (nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++) {
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
pVertex->x = x0;
pVertex->y = 0.0f + (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = 0.0f;
pVertex->ny = 1.0f;
pVertex->nz = 0.0f;
float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
pVertex->tu = tu0;
pVertex->tv = tv0;
pVertex++;
}
//Create the bottom triangle fan: Center
pVertex->x = 0.0f;
pVertex->y = 0.0f – (m_rHeight / 2.0f);
pVertex->z = 0.0f;
pVertex->nx = 0.0f;
pVertex->ny = –1.0f;
pVertex->nz = 0.0f;
pVertex->tu = 0.5f;
pVertex->tv = 0.5f;
pVertex++;
//Create the bottom triangle fan: Edges
for (nCurrentSegment = m_nSegments; nCurrentSegment >= 0; nCurrentSegment--) {
float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
pVertex->x = x0;
pVertex->y = 0.0f – (m_rHeight / 2.0f);
pVertex->z = z0;
pVertex->nx = 0.0f;
pVertex->ny = –1.0f;
pVertex->nz = 0.0f;
float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
pVertex->tu = tu0;
pVertex->tv = tv0;
pVertex++;
}
if (FAILED(m_pVertexBuffer->Unlock())) {
LogError("
CCylinder: Unable to unlock vertex buffer.");
return false;
}
return true;
}
So, what's going on here? Well, after locking our vertex buffer (so we can write to it) we calculate the angle for each segment. This is done by dividing the number of radians in a circle (2*PI) by the number of segments, this angle is stored in rDeltaSegAngle for use later when defining the vertices position. We then calculate the length of each segment for use when defining the texture coordinates for the triangle strip (sides). This is done by dividing the length of our texture (1.0) by the number of segments.
Once this is done, we can define the sides of our cylinder. We loop around, once for each segment + 1. In this loop we calculate the x and z position for the current segment and write these values for the top and bottom vertex for that segment. The y value is simply + or – half the height. We use the segment length to calculate the texture coordinates for the current vertex.
Once that is done, we can define the two triangle fans, one for the top and one for the bottom. First we define the centre point for the fan and then we use the same calculations to define the edge vertices of the fan. Once this is complete for the top and bottom fans, the vertex buffer is full and then unlocked ready for rendering.
Интервал:
Закладка:
Похожие книги на «DirectX 8 Programming Tutorial»
Представляем Вашему вниманию похожие книги на «DirectX 8 Programming Tutorial» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.
Обсуждение, отзывы о книге «DirectX 8 Programming Tutorial» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.