Game Development Reference
In-Depth Information
We also calculate the polar coordinates for the longitude, scaling them by the radius of the
current ring given by Δϕ xz .
The vertex location is then given by:
During the geometry generation we can also calculate the texture coordinates, the vertical
texture coordinate v can be calculated during the latitude iteration and is given by the in-
verse factor of the latitude and the number or latitudinal segments.
The u coordinate is given by the ratio of the current longitudinal segment to the number of
segments.
For convenience we can use an STL vector to store the generated vertices and use it to cre-
ate the vertex buffer.
const float radius = 0.5f; // We are creating a unit sphere
const unsigned short verticalSegments = 20;
const unsigned short horizontalSegments = verticalSegments * 2;
for (size_t i = 0; i <= verticalSegments; ++i)
{
const float v = 1.f - static_cast<float>(i) / verticalSegments;
const float latitude = (i * math::Pi / verticalSegments) - math::HalfPi;
const float dy = sinf(latitude);
const float dxz = cosf(latitude);
for (size_t j = 0; j <= horizontalSegments; ++j)
{
const float u = static_cast<float>(j) / horizontalSegments;
const float longitude = j * math::TwoPi / horizontalSegments;
Search WWH ::




Custom Search