Home / Coracle / Drawings / Bad Sphere Mesh

Bad Sphere Mesh

Highlights an issue with the older sphere mesh builder. Now replaced with a triangle strip

import coracle.*
import coracle.shapes.Mesh
import coracle.shapes.Point

class SphereMesh2: Drawing() {

    lateinit var cube: Mesh

    var projectionMatrix = Matrix.projectionMatrix(450f / 450f, 90.0f, 0.1f, 1000.0f)
    var matRotZ = Matrix()
    var matRotX = Matrix()

    val camera = Point()//Camera is at 0,0,0
    val light = Point(0.0f, 0.0f, -1.0f)
    var frame = 0

    override fun setup() {
        size(450, 450)

        cube = Mesh.sphere(30)
        light.normalise()

        noFill()
        stroke(0xffffff, 0.4f)
    }

    override fun draw() {
        background(0x1d1d1d)
        frame++

        matRotZ.rotateZ(frame/240f)
        matRotX.rotateX(frame/240f)

        cube.triangles.forEach { triangle ->
            //Rotate
            val rotatedTriangle = triangle * matRotZ
            rotatedTriangle *= matRotX
            rotatedTriangle.applyZOffset(3.5f)

            val normal = rotatedTriangle.normal()

            if(normal.x * (rotatedTriangle.a.x - camera.x) + normal.y * (rotatedTriangle.a.y - camera.y) + normal.z * (rotatedTriangle.a.z - camera.z) < 0.0f) {
                // Illumination
                val dp = normal.x * light.x + normal.y * light.y + normal.z * light.z
                fill(Colour.grey((dp * 255f).toInt()))
                noStroke()

                val tri2D = rotatedTriangle.to2D(projectionMatrix)
                tri2D.scale(width/2f)
                tri2D.translate(width/2f, height/2f)
                tri2D.draw()
            }
        }
    }
}