Skip to content

Instantly share code, notes, and snippets.

@Twometer
Created October 1, 2021 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twometer/de2acdba3bc649e46cdddff674c858fa to your computer and use it in GitHub Desktop.
Save Twometer/de2acdba3bc649e46cdddff674c858fa to your computer and use it in GitHub Desktop.
Unit sphere generation
val unitSphere: Primitive by lazy {
fun sphere(u: Float, v: Float): Vector3f =
Vector3f(MathF.cos(u) * MathF.sin(v), MathF.cos(v), MathF.sin(u) * MathF.sin(v))
val vertices = ArrayList<Float>()
val resolution = 20
val startU = 0F
val startV = 0F
val endU = MathF.PI * 2F
val endV = MathF.PI
val stepU = (endU - startU) / resolution.toFloat()
val stepV = (endV - startV) / resolution.toFloat()
for (i in 0 until resolution) {
for (j in 0 until resolution) {
val u = i * stepU + startU
val v = j * stepV + startV
val un = if (i + 1 == resolution) endU else (i + 1) * stepU + startU
val vn = if (j + 1 == resolution) endV else (j + 1) * stepV + startV
val p0 = sphere(u, v)
val p1 = sphere(u, vn)
val p2 = sphere(un, v)
val p3 = sphere(un, vn)
addTriangle(p0, p2, p1)
addTriangle(p3, p1, p2)
}
}
Primitive(vertices.toFloatArray(), GL_TRIANGLES, 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment