Last active
August 29, 2015 13:57
-
-
Save fluffels/9870944 to your computer and use it in GitHub Desktop.
HOWTO: Create sphere geometry using trigonometry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The amount to change the angle by with each step, determined by | |
resolution.*/ | |
const float ANGLE_DELTA = (float)REVOLUTION / (float)_resolution; | |
/* Convert the delta to radians, since that's what C++ works in. */ | |
const float R_ANGLE_DELTA = ANGLE_DELTA * (float)M_PI / 180.0f; | |
/* Allocate vertices for each step along the x-axis. */ | |
_vertexCount = (int) ceilf(abs(X_ANGLE_STOP - X_ANGLE_START) / ANGLE_DELTA); | |
/* Allocate vertices for each step along the y-axis. */ | |
_vertexCount *= (int) ceilf(abs(Y_ANGLE_STOP - Y_ANGLE_START) / ANGLE_DELTA); | |
/* Each step requires two triangles. */ | |
_vertexCount *= 2; | |
/* Each triangle has three vertices. */ | |
_vertexCount *= 3; | |
/* Each vertex has four elements. */ | |
_positions = new float[_vertexCount * 4]; | |
unsigned index = 0; | |
/* Step through x / y combinations, and use sin / cos to determine the | |
coordinates for each step. */ | |
for (float longitude = X_ANGLE_START; longitude < X_ANGLE_STOP; | |
longitude += ANGLE_DELTA) | |
{ | |
float rlong = longitude * (float)M_PI / 180.0f; | |
for (float latitude = Y_ANGLE_START; latitude < Y_ANGLE_STOP; | |
latitude += ANGLE_DELTA) | |
{ | |
float rlat = latitude * (float)M_PI / 180.0f; | |
/* Bottom left. */ | |
_positions[index + X] = sinf(rlong) * cosf(rlat); | |
_positions[index + Y] = sinf(rlat); | |
_positions[index + Z] = cosf(rlong) * cosf(rlat); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
/* Top right. */ | |
_positions[index + X] = | |
sinf(rlong + R_ANGLE_DELTA) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + Y] = | |
sinf(rlat + R_ANGLE_DELTA); | |
_positions[index + Z] = | |
cosf(rlong + R_ANGLE_DELTA) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
/* Top left. */ | |
_positions[index + X] = sinf(rlong) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + Y] = sinf(rlat + R_ANGLE_DELTA); | |
_positions[index + Z] = cosf(rlong) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
/* Bottom left. */ | |
_positions[index + X] = sinf(rlong) * cosf(rlat); | |
_positions[index + Y] = sinf(rlat); | |
_positions[index + Z] = cosf(rlong) * cosf(rlat); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
/* Bottom right. */ | |
_positions[index + X] = | |
sinf(rlong + R_ANGLE_DELTA) * cosf(rlat); | |
_positions[index + Y] = | |
sinf(rlat); | |
_positions[index + Z] = | |
cosf(rlong + R_ANGLE_DELTA) * cosf(rlat); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
/* Top right. */ | |
_positions[index + X] = | |
sinf(rlong + R_ANGLE_DELTA) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + Y] = | |
sinf(rlat + R_ANGLE_DELTA); | |
_positions[index + Z] = | |
cosf(rlong + R_ANGLE_DELTA) * cosf(rlat + R_ANGLE_DELTA); | |
_positions[index + W] = 1.0f; | |
index += W + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment