MCPcopy Create free account
hub / github.com/NazaraEngine/NazaraEngine / GenerateUvSphere

Function GenerateUvSphere

src/Nazara/Utility/AlgorithmUtility.cpp:994–1046  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

992 }
993
994 void GenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackCount, const Matrix4f& matrix, const Rectf& textureCoords, VertexPointers vertexPointers, IndexIterator indices, Boxf* aabb, unsigned int indexOffset)
995 {
996 // http://stackoverflow.com/questions/14080932/implementing-opengl-sphere-example-code
997 float invSliceCount = 1.f / (sliceCount-1);
998 float invStackCount = 1.f / (stackCount-1);
999
1000 const float pi = static_cast<float>(M_PI); // Pour éviter toute promotion en double
1001 const float pi2 = pi * 2.f;
1002 const float pi_2 = pi / 2.f;
1003
1004 for (unsigned int stack = 0; stack < stackCount; ++stack)
1005 {
1006 float stackVal = stack * invStackCount;
1007 float stackValPi = stackVal * pi;
1008 float sinStackValPi = std::sin(stackValPi);
1009
1010 for (unsigned int slice = 0; slice < sliceCount; ++slice)
1011 {
1012 float sliceVal = slice * invSliceCount;
1013 float sliceValPi2 = sliceVal * pi2;
1014
1015 Vector3f normal;
1016 normal.y = std::sin(-pi_2 + stackValPi);
1017 normal.x = std::cos(sliceValPi2) * sinStackValPi;
1018 normal.z = std::sin(sliceValPi2) * sinStackValPi;
1019
1020 *vertexPointers.positionPtr++ = matrix.Transform(size * normal);
1021
1022 if (vertexPointers.normalPtr)
1023 *vertexPointers.normalPtr++ = matrix.Transform(normal, 0.f);
1024
1025 if (vertexPointers.uvPtr)
1026 *vertexPointers.uvPtr++ = Vector2f(textureCoords.x + textureCoords.width*(1.f - sliceVal), textureCoords.y + textureCoords.height*stackVal);
1027
1028 if (stack != stackCount-1 && slice != sliceCount-1)
1029 {
1030 *indices++ = (stack+0)*sliceCount + (slice+0) + indexOffset;
1031 *indices++ = (stack+1)*sliceCount + (slice+0) + indexOffset;
1032 *indices++ = (stack+0)*sliceCount + (slice+1) + indexOffset;
1033
1034 *indices++ = (stack+0)*sliceCount + (slice+1) + indexOffset;
1035 *indices++ = (stack+1)*sliceCount + (slice+0) + indexOffset;
1036 *indices++ = (stack+1)*sliceCount + (slice+1) + indexOffset;
1037 }
1038 }
1039 }
1040
1041 if (aabb)
1042 {
1043 Vector3f totalSize = size * matrix.GetScale();
1044 aabb->Set(-totalSize, totalSize);
1045 }
1046 }
1047
1048 /**********************************Optimize*********************************/
1049

Callers 1

BuildSubMeshMethod · 0.85

Calls 3

TransformMethod · 0.80
GetScaleMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected