Skip to content

Commit

Permalink
samples: add software 3D renderer in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Nov 19, 2023
1 parent 932f33a commit 01cf27b
Show file tree
Hide file tree
Showing 31 changed files with 1,893 additions and 1 deletion.
6 changes: 5 additions & 1 deletion samples/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

plugins {
kotlin("jvm") version "1.8.0"
kotlin("multiplatform") version "1.9.20"
war
id("org.teavm")
}
Expand All @@ -28,3 +28,7 @@ teavm.js {
addedToWebApp = true
mainClass = "org.teavm.samples.kotlin.HelloKt"
}

kotlin {
jvm()
}
1 change: 1 addition & 0 deletions samples/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ include("pi")
include("kotlin")
include("scala")
include("web-apis")
include("software3d")

gradle.allprojects {
apply<WarPlugin>()
Expand Down
67 changes: 67 additions & 0 deletions samples/software3d/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import org.teavm.gradle.api.OptimizationLevel
import org.teavm.gradle.tasks.TeaVMTask

/*
* Copyright 2023 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
kotlin("multiplatform") version "1.9.20"
war
id("org.teavm")
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
}
}

teavm.js {
addedToWebApp = true
mainClass = "org.teavm.samples.software3d.teavm.MainKt"
}

teavm.wasm {
addedToWebApp = true
mainClass = "org.teavm.samples.software3d.teavm.WasmWorkerKt"
optimization = OptimizationLevel.AGGRESSIVE
minHeapSize = 4
}

kotlin {
js {
browser {

}
binaries.executable()
}
jvm()
sourceSets.jvmMain.dependencies {
implementation(teavm.libs.jsoApis)
}
}

tasks.withType<TeaVMTask> {
classpath.from(kotlin.jvm().compilations["main"].output.classesDirs)
}

tasks.war {
dependsOn(tasks.named("jsBrowserDistribution"))
with(copySpec {
from(kotlin.js().binaries.executable().map { it.distribution.outputDirectory })
into("kjs")
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2023 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.teavm.samples.software3d.geometry

import org.teavm.samples.software3d.scene.Vertex

class Face(val a: Vertex, val b: Vertex, val c: Vertex)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.teavm.samples.software3d.geometry

interface GeneralMatrix {
val size: Int

fun get(row: Int, col: Int): Float

fun determinant(): Float {
return if (size == 2) {
get(0, 0) * get(1, 1) - get(1, 0) * get(0, 1)
} else {
(0 until size)
.map { j ->
val r = get(0, j) * exclude(0, j).determinant()
if (j % 2 == 0) r else -r
}
.sum()
}
}

fun exclude(excludeRow: Int, excludeCol: Int): GeneralMatrix {
val orig = this
return object : GeneralMatrix {
override val size: Int
get() = orig.size - 1

override fun get(row: Int, col: Int): Float {
return orig.get(if (row < excludeRow) row else row + 1, if (col < excludeCol) col else col + 1)
}
}
}

fun asString(): String {
val cells = (0 until size).map { j ->
(0 until size).map { i ->
get(i, j).toString()
}
}
val lengths = cells.map { column -> column.maxOf { it.length } }
val padCells = (cells zip lengths).map { (column, length) ->
column.map { it + " ".repeat(length - it.length) }
}

return (0 until size).joinToString("\n") { i ->
(0 until size).joinToString(" ") { j -> padCells[j][i] }
}
}
}
Loading

0 comments on commit 01cf27b

Please sign in to comment.