Skip to content

Commit

Permalink
Some more refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
tanelso2 committed Apr 22, 2024
1 parent 5c0f040 commit fbccfc1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/jsMain/kotlin/HTMLInputHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ArrayOfInputsProperty(private val inputs: Array<HTMLInputElement>): ReadOn
inputs.mapIndexed { i,input ->
input.oninput = {
cache[i] = input.valueAsNumber.toFloat()
null
null // Function is required to return a js value
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/jsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,9 @@ class WebGLWrapper {
val shininessUniform = webgl.getUniformLocation(shaderProgram, "shininess")
webgl.uniform1f(shininessUniform, shininess.toFloat())

val pMatrix = Mat4()
pMatrix.perspective(PI / 3, 16.0 / 9.0, 0.1, 60.0)
val pMatrix = Mat4.perspective(PI / 3, 16.0 / 9.0, 0.1, 60.0)

val vMatrix = Mat4()
vMatrix.lookAt(Vec3(20,20,20), Vec3(0,0,0), Vec3(0,0,1))
val vMatrix = Mat4.lookAt(Vec3(20,20,20), Vec3(0,0,0), Vec3(0,0,1))

val mMatrix = Mat4()
mMatrix.scale(scaleFactor)
Expand Down
3 changes: 1 addition & 2 deletions src/jsMain/kotlin/ObjLoader.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import com.tanelso2.glmatrix.Vec3
import org.khronos.webgl.Float32Array
import org.khronos.webgl.Uint16Array
import org.khronos.webgl.get
import kotlin.random.Random

class ObjLoader(source: String) {
Expand Down Expand Up @@ -75,7 +74,7 @@ class ObjLoader(source: String) {
}

fun getVertexNormals(): Float32Array {
val floatList = normals.flatMap { listOf(it.array[0], it.array[1], it.array[2]) }
val floatList = normals.flatMap { it.list() }
return Float32Array(floatList.toTypedArray())
}

Expand Down
6 changes: 2 additions & 4 deletions src/jsMain/kotlin/ResourceLoader.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import kotlinx.browser.window
import org.w3c.fetch.*

class ResourceLoader(vararg resourceLocations: String) {
private class ResourceInfo {
var loaded = false
var value: String? = null
}
private val resourceMap: MutableMap<String, ResourceInfo> = HashMap()
private var fetchParams = js("({})")
private val fetchParams = RequestInit(method = "GET", cache = RequestCache.NO_STORE, mode = RequestMode.SAME_ORIGIN)
init {
fetchParams.method = "GET"
fetchParams.cache = "no-store"
fetchParams.mode = "same-origin"
for (loc in resourceLocations) {
resourceMap.put(loc, ResourceInfo())
makeRequest(loc)
Expand Down
13 changes: 13 additions & 0 deletions src/jsMain/kotlin/com/tanelso2/glmatrix/Mat4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ typealias Mat4JS = Float32Array
class Mat4(val array: Mat4JS) {
constructor() : this(Float32Array(mat4.create()))
constructor(a: Array<Float>) : this(Float32Array(a))
companion object {
fun lookAt(eye: Vec3, center: Vec3, up: Vec3): Mat4 {
val ret = Mat4()
ret.lookAt(eye, center, up)
return ret
}

fun perspective(fovy: Number, aspect: Number, near: Number, far: Number): Mat4 {
val ret = Mat4()
ret.perspective(fovy, aspect, near, far)
return ret
}
}

fun clone(): Mat4 = Mat4(array)
fun identity() {
Expand Down
5 changes: 5 additions & 0 deletions src/jsMain/kotlin/com/tanelso2/glmatrix/Vec3.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tanelso2.glmatrix

import org.khronos.webgl.Float32Array
import org.khronos.webgl.get

typealias Vec3JS = Float32Array

Expand All @@ -27,6 +28,10 @@ class Vec3(val array: Vec3JS) {
vec3.add(ret.array, this.array, other.array)
return ret
}

fun list(): List<Float> {
return listOf(array[0], array[1], array[2])
}
}

external open class vec3 {
Expand Down

0 comments on commit fbccfc1

Please sign in to comment.