-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add software 3D renderer in Kotlin
- Loading branch information
1 parent
932f33a
commit 01cf27b
Showing
31 changed files
with
1,893 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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") | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/software3d/src/commonMain/kotlin/org/teavm/samples/software3d/geometry/Face.kt
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
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) |
64 changes: 64 additions & 0 deletions
64
...s/software3d/src/commonMain/kotlin/org/teavm/samples/software3d/geometry/GeneralMatrix.kt
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
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] } | ||
} | ||
} | ||
} |
Oops, something went wrong.