-
-
Save Twometer/de2acdba3bc649e46cdddff674c858fa to your computer and use it in GitHub Desktop.
Unit sphere generation
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
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