diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
-
+
-
+
-
+
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt
index 938c02c..a2ecaa9 100644
--- a/src/com/persesgames/Test.kt
+++ b/src/com/persesgames/Test.kt
@@ -112,7 +112,7 @@
return
}
- webgl.clearColor(red, green, blue, 1f)
+ webgl.clearColor(red, green, blue, 0.9f)
webgl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT)
webgl.enable(WebGLRenderingContext.BLEND);
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt
index 938c02c..a2ecaa9 100644
--- a/src/com/persesgames/Test.kt
+++ b/src/com/persesgames/Test.kt
@@ -112,7 +112,7 @@
return
}
- webgl.clearColor(red, green, blue, 1f)
+ webgl.clearColor(red, green, blue, 0.9f)
webgl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT)
webgl.enable(WebGLRenderingContext.BLEND);
diff --git a/src/com/persesgames/math/Matrix4.kt b/src/com/persesgames/math/Matrix4.kt
deleted file mode 100644
index 316ce41..0000000
--- a/src/com/persesgames/math/Matrix4.kt
+++ /dev/null
@@ -1,170 +0,0 @@
-package com.persesgames.math
-
-import org.khronos.webgl.Float32Array
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:43
- */
-class Matrix4 {
-
- internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- internal var temp = FloatArray(16)
-
- private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- fun get(): FloatArray {
- return matrix
- }
-
- fun getFloat32Array() = Float32Array(get().toTypedArray())
-
- fun set(values: FloatArray) {
- if (values.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- matrix = values
- }
-
- fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
- val r = (angle / 180f * Math.PI).toFloat()
- val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
-
- matrix[0] = f / imageAspectRatio
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
-
- matrix[4] = 0.0f
- matrix[5] = f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
-
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = -(far + near) / (far - near)
- matrix[11] = -1.0f
-
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = -(2.0f * far * near) / (far - near)
- matrix[15] = 0.0f
- }
-
- fun setToIdentity() {
- matrix[0] = 1.0f
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
- matrix[4] = 0.0f
- matrix[5] = 1.0f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = 1.0f
- matrix[11] = 0.0f
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = 0.0f
- matrix[15] = 1.0f
- }
-
- fun mul(other: Matrix4) {
- mul(other.get())
- }
-
- protected fun mul(other: FloatArray) {
- if (other.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
- temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
- temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
- temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
- temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
- temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
- temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
- temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
- temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
- temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
- temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
- temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
- temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
- temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
- temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
- temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
-
- matrix[0] = temp[0]
- matrix[1] = temp[1]
- matrix[2] = temp[2]
- matrix[3] = temp[3]
- matrix[4] = temp[4]
- matrix[5] = temp[5]
- matrix[6] = temp[6]
- matrix[7] = temp[7]
- matrix[8] = temp[8]
- matrix[9] = temp[9]
- matrix[10] = temp[10]
- matrix[11] = temp[11]
- matrix[12] = temp[12]
- matrix[13] = temp[13]
- matrix[14] = temp[14]
- matrix[15] = temp[15]
- }
-
- fun translate(x: Float, y: Float, z: Float) {
- translateMatrix[12] = x
- translateMatrix[13] = y
- translateMatrix[14] = z
-
- mul(translateMatrix)
- }
-
- fun scale(x: Float, y: Float, z: Float) {
- scaleMatrix[0] = x
- scaleMatrix[5] = y
- scaleMatrix[10] = z
-
- mul(scaleMatrix)
- }
-
- fun rotateX(angle: Float) {
- rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
- rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
- rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
- rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateXMatrix)
- }
-
- fun rotateY(angle: Float) {
- rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
- rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
- rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateYMatrix)
- }
-
- fun rotateZ(angle: Float) {
- rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
- rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
- rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateZMatrix)
- }
-}
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt
index 938c02c..a2ecaa9 100644
--- a/src/com/persesgames/Test.kt
+++ b/src/com/persesgames/Test.kt
@@ -112,7 +112,7 @@
return
}
- webgl.clearColor(red, green, blue, 1f)
+ webgl.clearColor(red, green, blue, 0.9f)
webgl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT)
webgl.enable(WebGLRenderingContext.BLEND);
diff --git a/src/com/persesgames/math/Matrix4.kt b/src/com/persesgames/math/Matrix4.kt
deleted file mode 100644
index 316ce41..0000000
--- a/src/com/persesgames/math/Matrix4.kt
+++ /dev/null
@@ -1,170 +0,0 @@
-package com.persesgames.math
-
-import org.khronos.webgl.Float32Array
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:43
- */
-class Matrix4 {
-
- internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- internal var temp = FloatArray(16)
-
- private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- fun get(): FloatArray {
- return matrix
- }
-
- fun getFloat32Array() = Float32Array(get().toTypedArray())
-
- fun set(values: FloatArray) {
- if (values.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- matrix = values
- }
-
- fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
- val r = (angle / 180f * Math.PI).toFloat()
- val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
-
- matrix[0] = f / imageAspectRatio
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
-
- matrix[4] = 0.0f
- matrix[5] = f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
-
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = -(far + near) / (far - near)
- matrix[11] = -1.0f
-
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = -(2.0f * far * near) / (far - near)
- matrix[15] = 0.0f
- }
-
- fun setToIdentity() {
- matrix[0] = 1.0f
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
- matrix[4] = 0.0f
- matrix[5] = 1.0f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = 1.0f
- matrix[11] = 0.0f
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = 0.0f
- matrix[15] = 1.0f
- }
-
- fun mul(other: Matrix4) {
- mul(other.get())
- }
-
- protected fun mul(other: FloatArray) {
- if (other.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
- temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
- temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
- temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
- temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
- temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
- temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
- temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
- temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
- temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
- temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
- temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
- temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
- temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
- temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
- temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
-
- matrix[0] = temp[0]
- matrix[1] = temp[1]
- matrix[2] = temp[2]
- matrix[3] = temp[3]
- matrix[4] = temp[4]
- matrix[5] = temp[5]
- matrix[6] = temp[6]
- matrix[7] = temp[7]
- matrix[8] = temp[8]
- matrix[9] = temp[9]
- matrix[10] = temp[10]
- matrix[11] = temp[11]
- matrix[12] = temp[12]
- matrix[13] = temp[13]
- matrix[14] = temp[14]
- matrix[15] = temp[15]
- }
-
- fun translate(x: Float, y: Float, z: Float) {
- translateMatrix[12] = x
- translateMatrix[13] = y
- translateMatrix[14] = z
-
- mul(translateMatrix)
- }
-
- fun scale(x: Float, y: Float, z: Float) {
- scaleMatrix[0] = x
- scaleMatrix[5] = y
- scaleMatrix[10] = z
-
- mul(scaleMatrix)
- }
-
- fun rotateX(angle: Float) {
- rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
- rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
- rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
- rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateXMatrix)
- }
-
- fun rotateY(angle: Float) {
- rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
- rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
- rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateYMatrix)
- }
-
- fun rotateZ(angle: Float) {
- rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
- rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
- rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateZMatrix)
- }
-}
diff --git a/src/com/persesgames/shader/ShaderProgram.kt b/src/com/persesgames/shader/ShaderProgram.kt
deleted file mode 100644
index 1657c83..0000000
--- a/src/com/persesgames/shader/ShaderProgram.kt
+++ /dev/null
@@ -1,130 +0,0 @@
-package com.persesgames.shader
-
-import org.khronos.webgl.*
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:15
- */
-
-class VertextAttributeInfo(val locationName: String, val numElements: Int) {
- var location = 0
- var offset = 0
-}
-
-class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
-
- var shaderProgram: WebGLProgram
- var vertex: WebGLShader
- var fragment: WebGLShader
-
- var verticesBlockSize = 0
- var currentIndex = 0
- var verticesLength = 0
- var vertices = Float32Array(0)
-
- var attribBuffer: WebGLBuffer
-
- init {
- vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
- fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
-
- shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
- webgl.attachShader(shaderProgram, vertex)
- webgl.attachShader(shaderProgram, fragment)
- webgl.linkProgram(shaderProgram)
-
- if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
- println(webgl.getProgramInfoLog(shaderProgram))
- throw IllegalStateException("Unable to compile program!")
- }
-
- webgl.useProgram(shaderProgram)
-
- this.verticesBlockSize = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
- info.offset = verticesBlockSize;
-
- verticesBlockSize += info.numElements;
- println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
- }
-
- println("verticesBlockSize $verticesBlockSize");
-
- this.currentIndex = 0;
-
- // create vertices buffer
- verticesLength = 4096 - (4096 % verticesBlockSize);
- vertices = Float32Array(verticesLength);
-
- println("vertices.length ${vertices.length}");
-
- attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
-
- println("ShaderProgram constructor done");
- }
-
- private fun compileShader(source: String, type: Int): WebGLShader {
- var result: WebGLShader
-
- result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
- webgl.shaderSource(result, source)
- webgl.compileShader(result)
-
- if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
- throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
- }
-
- return result;
- }
-
- fun queueVertices(verts: Float32Array) {
- if((currentIndex + verts.length) >= verticesLength) {
- flush();
- }
-
- vertices.set(verts, currentIndex)
- currentIndex += verts.length
- }
-
- fun begin() {
- webgl.useProgram(shaderProgram);
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
- currentIndex = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- webgl.enableVertexAttribArray(info.location);
- webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
- }
-
- }
-
- fun flush() {
- if (currentIndex > 0) {
- webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
- webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
- currentIndex = 0;
- }
- }
-
- fun end() {
- flush()
- webgl.useProgram(null)
- }
-
- fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
-
- fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
-
- fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
- fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
- fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
- fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
-
-}
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt
index 938c02c..a2ecaa9 100644
--- a/src/com/persesgames/Test.kt
+++ b/src/com/persesgames/Test.kt
@@ -112,7 +112,7 @@
return
}
- webgl.clearColor(red, green, blue, 1f)
+ webgl.clearColor(red, green, blue, 0.9f)
webgl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT)
webgl.enable(WebGLRenderingContext.BLEND);
diff --git a/src/com/persesgames/math/Matrix4.kt b/src/com/persesgames/math/Matrix4.kt
deleted file mode 100644
index 316ce41..0000000
--- a/src/com/persesgames/math/Matrix4.kt
+++ /dev/null
@@ -1,170 +0,0 @@
-package com.persesgames.math
-
-import org.khronos.webgl.Float32Array
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:43
- */
-class Matrix4 {
-
- internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- internal var temp = FloatArray(16)
-
- private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- fun get(): FloatArray {
- return matrix
- }
-
- fun getFloat32Array() = Float32Array(get().toTypedArray())
-
- fun set(values: FloatArray) {
- if (values.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- matrix = values
- }
-
- fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
- val r = (angle / 180f * Math.PI).toFloat()
- val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
-
- matrix[0] = f / imageAspectRatio
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
-
- matrix[4] = 0.0f
- matrix[5] = f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
-
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = -(far + near) / (far - near)
- matrix[11] = -1.0f
-
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = -(2.0f * far * near) / (far - near)
- matrix[15] = 0.0f
- }
-
- fun setToIdentity() {
- matrix[0] = 1.0f
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
- matrix[4] = 0.0f
- matrix[5] = 1.0f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = 1.0f
- matrix[11] = 0.0f
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = 0.0f
- matrix[15] = 1.0f
- }
-
- fun mul(other: Matrix4) {
- mul(other.get())
- }
-
- protected fun mul(other: FloatArray) {
- if (other.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
- temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
- temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
- temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
- temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
- temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
- temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
- temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
- temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
- temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
- temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
- temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
- temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
- temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
- temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
- temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
-
- matrix[0] = temp[0]
- matrix[1] = temp[1]
- matrix[2] = temp[2]
- matrix[3] = temp[3]
- matrix[4] = temp[4]
- matrix[5] = temp[5]
- matrix[6] = temp[6]
- matrix[7] = temp[7]
- matrix[8] = temp[8]
- matrix[9] = temp[9]
- matrix[10] = temp[10]
- matrix[11] = temp[11]
- matrix[12] = temp[12]
- matrix[13] = temp[13]
- matrix[14] = temp[14]
- matrix[15] = temp[15]
- }
-
- fun translate(x: Float, y: Float, z: Float) {
- translateMatrix[12] = x
- translateMatrix[13] = y
- translateMatrix[14] = z
-
- mul(translateMatrix)
- }
-
- fun scale(x: Float, y: Float, z: Float) {
- scaleMatrix[0] = x
- scaleMatrix[5] = y
- scaleMatrix[10] = z
-
- mul(scaleMatrix)
- }
-
- fun rotateX(angle: Float) {
- rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
- rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
- rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
- rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateXMatrix)
- }
-
- fun rotateY(angle: Float) {
- rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
- rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
- rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateYMatrix)
- }
-
- fun rotateZ(angle: Float) {
- rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
- rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
- rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateZMatrix)
- }
-}
diff --git a/src/com/persesgames/shader/ShaderProgram.kt b/src/com/persesgames/shader/ShaderProgram.kt
deleted file mode 100644
index 1657c83..0000000
--- a/src/com/persesgames/shader/ShaderProgram.kt
+++ /dev/null
@@ -1,130 +0,0 @@
-package com.persesgames.shader
-
-import org.khronos.webgl.*
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:15
- */
-
-class VertextAttributeInfo(val locationName: String, val numElements: Int) {
- var location = 0
- var offset = 0
-}
-
-class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
-
- var shaderProgram: WebGLProgram
- var vertex: WebGLShader
- var fragment: WebGLShader
-
- var verticesBlockSize = 0
- var currentIndex = 0
- var verticesLength = 0
- var vertices = Float32Array(0)
-
- var attribBuffer: WebGLBuffer
-
- init {
- vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
- fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
-
- shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
- webgl.attachShader(shaderProgram, vertex)
- webgl.attachShader(shaderProgram, fragment)
- webgl.linkProgram(shaderProgram)
-
- if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
- println(webgl.getProgramInfoLog(shaderProgram))
- throw IllegalStateException("Unable to compile program!")
- }
-
- webgl.useProgram(shaderProgram)
-
- this.verticesBlockSize = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
- info.offset = verticesBlockSize;
-
- verticesBlockSize += info.numElements;
- println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
- }
-
- println("verticesBlockSize $verticesBlockSize");
-
- this.currentIndex = 0;
-
- // create vertices buffer
- verticesLength = 4096 - (4096 % verticesBlockSize);
- vertices = Float32Array(verticesLength);
-
- println("vertices.length ${vertices.length}");
-
- attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
-
- println("ShaderProgram constructor done");
- }
-
- private fun compileShader(source: String, type: Int): WebGLShader {
- var result: WebGLShader
-
- result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
- webgl.shaderSource(result, source)
- webgl.compileShader(result)
-
- if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
- throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
- }
-
- return result;
- }
-
- fun queueVertices(verts: Float32Array) {
- if((currentIndex + verts.length) >= verticesLength) {
- flush();
- }
-
- vertices.set(verts, currentIndex)
- currentIndex += verts.length
- }
-
- fun begin() {
- webgl.useProgram(shaderProgram);
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
- currentIndex = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- webgl.enableVertexAttribArray(info.location);
- webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
- }
-
- }
-
- fun flush() {
- if (currentIndex > 0) {
- webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
- webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
- currentIndex = 0;
- }
- }
-
- fun end() {
- flush()
- webgl.useProgram(null)
- }
-
- fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
-
- fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
-
- fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
- fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
- fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
- fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
-
-}
diff --git a/src/com/persesgames/texture/Sprites.kt b/src/com/persesgames/texture/Sprites.kt
deleted file mode 100644
index 47230f4..0000000
--- a/src/com/persesgames/texture/Sprites.kt
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.persesgames.texture
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:44
- */
-
-val vertexShaderSource = """
- attribute vec2 a_position;
-
- attribute float a_imagesX;
- attribute float a_imagesY;
- attribute float a_currentImage;
-
- uniform mat4 u_projectionView;
-
- varying float v_imagesX;
- varying float v_imagesY;
- varying float v_currentImage;
-
- void main(void) {
- gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
- gl_PointSize = 50.0 / gl_Position.w;
-
- v_imagesX = a_imagesX;
- v_imagesY = a_imagesY;
- v_currentImage = a_currentImage;
- }
-"""
-
-val fragmentShaderSource = """
- precision mediump float;
-
- uniform sampler2D uSampler;
-
- varying float v_imagesX;
- varying float v_imagesY;
- varying float v_currentImage;
-
- void main(void) {
- // calculate current texture coords depending on current image number
- float blockX = 1.0 / v_imagesX;
- float blockY = 1.0 / v_imagesY;
-
- float x = blockX * (mod(v_currentImage, v_imagesX));
- float y = blockY * floor(v_currentImage / v_imagesY);
-
- vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
- //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
-
- gl_FragColor = texture2D(uSampler, textCoord);
- }
-"""
-
diff --git a/KotlinTest.iml b/KotlinTest.iml
index 3c71942..f6bea00 100644
--- a/KotlinTest.iml
+++ b/KotlinTest.iml
@@ -4,6 +4,7 @@
+
diff --git a/KotlinTest.ipr b/KotlinTest.ipr
index 759df4e..5e0b3c1 100644
--- a/KotlinTest.ipr
+++ b/KotlinTest.ipr
@@ -47,7 +47,7 @@
-
+
diff --git a/build.gradle b/build.gradle
index cf42b8e..49c583c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,12 +2,11 @@
version '1.0.0-SNAPSHOT'
apply plugin: 'kotlin2js'
-apply plugin: 'kotlin'
apply plugin: 'idea'
sourceSets {
+ main.kotlin.srcDirs += 'lib/kotludens'
main.kotlin.srcDirs += 'src'
- main.java.srcDirs += 'src/main/kotlin'
}
repositories {
@@ -18,7 +17,7 @@
}
buildscript {
- ext.kotlin_version = '1.0.0'
+ ext.kotlin_version = '1.+'
repositories {
maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -35,6 +34,16 @@
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
+task jarSources(type:Jar){
+ from sourceSets.main.allSource
+ classifier = 'source'
+}
+artifacts {
+ compile jarSources
+}
+
+def outDir = "${buildDir}/kotlin2js/main/"
+
compileKotlin2Js {
kotlinOptions.metaInfo = true
@@ -43,3 +52,20 @@
compileKotlin2Js.kotlinOptions.suppressWarnings = true
compileKotlin2Js.kotlinOptions.verbose = true
}
+
+jar {
+ from sourceSets.main.allSource
+ include "**/*.kt"
+
+ from outDir
+ include "**/*.js"
+
+ manifest {
+ attributes(
+ "Specification-Title": "Kotlin JavaScript Lib",
+ "Kotlin-JS-Module-Name": "test-library"
+ )
+ }
+}
+
+jar.dependsOn(compileKotlin2Js)
\ No newline at end of file
diff --git a/lib/kotludens/persesgames/math/Matrix4.kt b/lib/kotludens/persesgames/math/Matrix4.kt
new file mode 100644
index 0000000..316ce41
--- /dev/null
+++ b/lib/kotludens/persesgames/math/Matrix4.kt
@@ -0,0 +1,170 @@
+package com.persesgames.math
+
+import org.khronos.webgl.Float32Array
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:43
+ */
+class Matrix4 {
+
+ internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ internal var temp = FloatArray(16)
+
+ private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
+
+ fun get(): FloatArray {
+ return matrix
+ }
+
+ fun getFloat32Array() = Float32Array(get().toTypedArray())
+
+ fun set(values: FloatArray) {
+ if (values.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ matrix = values
+ }
+
+ fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
+ val r = (angle / 180f * Math.PI).toFloat()
+ val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
+
+ matrix[0] = f / imageAspectRatio
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+
+ matrix[4] = 0.0f
+ matrix[5] = f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = -(far + near) / (far - near)
+ matrix[11] = -1.0f
+
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = -(2.0f * far * near) / (far - near)
+ matrix[15] = 0.0f
+ }
+
+ fun setToIdentity() {
+ matrix[0] = 1.0f
+ matrix[1] = 0.0f
+ matrix[2] = 0.0f
+ matrix[3] = 0.0f
+ matrix[4] = 0.0f
+ matrix[5] = 1.0f
+ matrix[6] = 0.0f
+ matrix[7] = 0.0f
+ matrix[8] = 0.0f
+ matrix[9] = 0.0f
+ matrix[10] = 1.0f
+ matrix[11] = 0.0f
+ matrix[12] = 0.0f
+ matrix[13] = 0.0f
+ matrix[14] = 0.0f
+ matrix[15] = 1.0f
+ }
+
+ fun mul(other: Matrix4) {
+ mul(other.get())
+ }
+
+ protected fun mul(other: FloatArray) {
+ if (other.size != 16) {
+ throw IllegalArgumentException("Matrix size should be 16!")
+ }
+
+ temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
+ temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
+ temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
+ temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
+ temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
+ temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
+ temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
+ temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
+ temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
+ temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
+ temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
+ temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
+ temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
+ temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
+ temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
+ temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
+
+ matrix[0] = temp[0]
+ matrix[1] = temp[1]
+ matrix[2] = temp[2]
+ matrix[3] = temp[3]
+ matrix[4] = temp[4]
+ matrix[5] = temp[5]
+ matrix[6] = temp[6]
+ matrix[7] = temp[7]
+ matrix[8] = temp[8]
+ matrix[9] = temp[9]
+ matrix[10] = temp[10]
+ matrix[11] = temp[11]
+ matrix[12] = temp[12]
+ matrix[13] = temp[13]
+ matrix[14] = temp[14]
+ matrix[15] = temp[15]
+ }
+
+ fun translate(x: Float, y: Float, z: Float) {
+ translateMatrix[12] = x
+ translateMatrix[13] = y
+ translateMatrix[14] = z
+
+ mul(translateMatrix)
+ }
+
+ fun scale(x: Float, y: Float, z: Float) {
+ scaleMatrix[0] = x
+ scaleMatrix[5] = y
+ scaleMatrix[10] = z
+
+ mul(scaleMatrix)
+ }
+
+ fun rotateX(angle: Float) {
+ rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+ rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
+ rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateXMatrix)
+ }
+
+ fun rotateY(angle: Float) {
+ rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
+ rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateYMatrix)
+ }
+
+ fun rotateZ(angle: Float) {
+ rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
+ rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
+ rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
+ rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
+
+ mul(rotateZMatrix)
+ }
+}
diff --git a/lib/kotludens/persesgames/shader/ShaderProgram.kt b/lib/kotludens/persesgames/shader/ShaderProgram.kt
new file mode 100644
index 0000000..1657c83
--- /dev/null
+++ b/lib/kotludens/persesgames/shader/ShaderProgram.kt
@@ -0,0 +1,130 @@
+package com.persesgames.shader
+
+import org.khronos.webgl.*
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:15
+ */
+
+class VertextAttributeInfo(val locationName: String, val numElements: Int) {
+ var location = 0
+ var offset = 0
+}
+
+class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
+
+ var shaderProgram: WebGLProgram
+ var vertex: WebGLShader
+ var fragment: WebGLShader
+
+ var verticesBlockSize = 0
+ var currentIndex = 0
+ var verticesLength = 0
+ var vertices = Float32Array(0)
+
+ var attribBuffer: WebGLBuffer
+
+ init {
+ vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
+ fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
+
+ shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
+ webgl.attachShader(shaderProgram, vertex)
+ webgl.attachShader(shaderProgram, fragment)
+ webgl.linkProgram(shaderProgram)
+
+ if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
+ println(webgl.getProgramInfoLog(shaderProgram))
+ throw IllegalStateException("Unable to compile program!")
+ }
+
+ webgl.useProgram(shaderProgram)
+
+ this.verticesBlockSize = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
+ info.offset = verticesBlockSize;
+
+ verticesBlockSize += info.numElements;
+ println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
+ }
+
+ println("verticesBlockSize $verticesBlockSize");
+
+ this.currentIndex = 0;
+
+ // create vertices buffer
+ verticesLength = 4096 - (4096 % verticesBlockSize);
+ vertices = Float32Array(verticesLength);
+
+ println("vertices.length ${vertices.length}");
+
+ attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+
+ println("ShaderProgram constructor done");
+ }
+
+ private fun compileShader(source: String, type: Int): WebGLShader {
+ var result: WebGLShader
+
+ result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
+ webgl.shaderSource(result, source)
+ webgl.compileShader(result)
+
+ if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
+ throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
+ }
+
+ return result;
+ }
+
+ fun queueVertices(verts: Float32Array) {
+ if((currentIndex + verts.length) >= verticesLength) {
+ flush();
+ }
+
+ vertices.set(verts, currentIndex)
+ currentIndex += verts.length
+ }
+
+ fun begin() {
+ webgl.useProgram(shaderProgram);
+ webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
+ currentIndex = 0;
+
+ // set attribute locations...
+ for (info in vainfo.iterator()) {
+ webgl.enableVertexAttribArray(info.location);
+ webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
+ }
+
+ }
+
+ fun flush() {
+ if (currentIndex > 0) {
+ webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
+ webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
+ currentIndex = 0;
+ }
+ }
+
+ fun end() {
+ flush()
+ webgl.useProgram(null)
+ }
+
+ fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
+
+ fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
+
+ fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
+ fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
+ fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
+ fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
+
+}
diff --git a/lib/kotludens/persesgames/texture/Sprites.kt b/lib/kotludens/persesgames/texture/Sprites.kt
new file mode 100644
index 0000000..47230f4
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Sprites.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 15:44
+ */
+
+val vertexShaderSource = """
+ attribute vec2 a_position;
+
+ attribute float a_imagesX;
+ attribute float a_imagesY;
+ attribute float a_currentImage;
+
+ uniform mat4 u_projectionView;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
+ gl_PointSize = 50.0 / gl_Position.w;
+
+ v_imagesX = a_imagesX;
+ v_imagesY = a_imagesY;
+ v_currentImage = a_currentImage;
+ }
+"""
+
+val fragmentShaderSource = """
+ precision mediump float;
+
+ uniform sampler2D uSampler;
+
+ varying float v_imagesX;
+ varying float v_imagesY;
+ varying float v_currentImage;
+
+ void main(void) {
+ // calculate current texture coords depending on current image number
+ float blockX = 1.0 / v_imagesX;
+ float blockY = 1.0 / v_imagesY;
+
+ float x = blockX * (mod(v_currentImage, v_imagesX));
+ float y = blockY * floor(v_currentImage / v_imagesY);
+
+ vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
+ //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
+
+ gl_FragColor = texture2D(uSampler, textCoord);
+ }
+"""
+
diff --git a/lib/kotludens/persesgames/texture/Textures.kt b/lib/kotludens/persesgames/texture/Textures.kt
new file mode 100644
index 0000000..79e26a8
--- /dev/null
+++ b/lib/kotludens/persesgames/texture/Textures.kt
@@ -0,0 +1,55 @@
+package com.persesgames.texture
+
+import org.khronos.webgl.WebGLRenderingContext
+import org.khronos.webgl.WebGLTexture
+import org.w3c.dom.HTMLImageElement
+import java.util.*
+import kotlin.browser.document
+
+/**
+ * User: rnentjes
+ * Date: 17-4-16
+ * Time: 14:52
+ */
+
+object Textures {
+ var textures = HashMap();
+ var startedLoading = 0
+ var loaded = 0
+
+ fun load(gl: WebGLRenderingContext, name: String, filename: String) {
+ startedLoading++
+
+ var webGlTexture = gl.createTexture()
+ if (webGlTexture != null) {
+ var image = document.createElement("img") as HTMLImageElement
+ image.onload = {
+ textureLoaded(gl, webGlTexture, image)
+ textures.put(name, webGlTexture)
+ loaded++
+ }
+ image.src = filename
+ } else {
+ println("Couldn't create webgl texture!")
+ }
+
+ }
+
+ fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
+ gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
+ gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
+ gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
+ gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
+ }
+
+ fun ready() = loaded == startedLoading
+
+ fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
+
+ fun clear() {
+ // delete and unbind all textures...
+ }
+
+}
\ No newline at end of file
diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt
index 938c02c..a2ecaa9 100644
--- a/src/com/persesgames/Test.kt
+++ b/src/com/persesgames/Test.kt
@@ -112,7 +112,7 @@
return
}
- webgl.clearColor(red, green, blue, 1f)
+ webgl.clearColor(red, green, blue, 0.9f)
webgl.clear(WebGLRenderingContext.COLOR_BUFFER_BIT)
webgl.enable(WebGLRenderingContext.BLEND);
diff --git a/src/com/persesgames/math/Matrix4.kt b/src/com/persesgames/math/Matrix4.kt
deleted file mode 100644
index 316ce41..0000000
--- a/src/com/persesgames/math/Matrix4.kt
+++ /dev/null
@@ -1,170 +0,0 @@
-package com.persesgames.math
-
-import org.khronos.webgl.Float32Array
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:43
- */
-class Matrix4 {
-
- internal var matrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- internal var temp = FloatArray(16)
-
- private val translateMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val scaleMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateXMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateYMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- private val rotateZMatrix = floatArrayOf(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
-
- fun get(): FloatArray {
- return matrix
- }
-
- fun getFloat32Array() = Float32Array(get().toTypedArray())
-
- fun set(values: FloatArray) {
- if (values.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- matrix = values
- }
-
- fun setPerspectiveProjection(angle: Float, imageAspectRatio: Float, near: Float, far: Float) {
- val r = (angle / 180f * Math.PI).toFloat()
- val f = (1.0f / Math.tan((r / 2.0f).toDouble())).toFloat()
-
- matrix[0] = f / imageAspectRatio
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
-
- matrix[4] = 0.0f
- matrix[5] = f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
-
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = -(far + near) / (far - near)
- matrix[11] = -1.0f
-
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = -(2.0f * far * near) / (far - near)
- matrix[15] = 0.0f
- }
-
- fun setToIdentity() {
- matrix[0] = 1.0f
- matrix[1] = 0.0f
- matrix[2] = 0.0f
- matrix[3] = 0.0f
- matrix[4] = 0.0f
- matrix[5] = 1.0f
- matrix[6] = 0.0f
- matrix[7] = 0.0f
- matrix[8] = 0.0f
- matrix[9] = 0.0f
- matrix[10] = 1.0f
- matrix[11] = 0.0f
- matrix[12] = 0.0f
- matrix[13] = 0.0f
- matrix[14] = 0.0f
- matrix[15] = 1.0f
- }
-
- fun mul(other: Matrix4) {
- mul(other.get())
- }
-
- protected fun mul(other: FloatArray) {
- if (other.size != 16) {
- throw IllegalArgumentException("Matrix size should be 16!")
- }
-
- temp[0] = matrix[0] * other[0] + matrix[1] * other[4] + matrix[2] * other[8] + matrix[3] * other[12]
- temp[1] = matrix[0] * other[1] + matrix[1] * other[5] + matrix[2] * other[9] + matrix[3] * other[13]
- temp[2] = matrix[0] * other[2] + matrix[1] * other[6] + matrix[2] * other[10] + matrix[3] * other[14]
- temp[3] = matrix[0] * other[3] + matrix[1] * other[7] + matrix[2] * other[11] + matrix[3] * other[15]
- temp[4] = matrix[4] * other[0] + matrix[5] * other[4] + matrix[6] * other[8] + matrix[7] * other[12]
- temp[5] = matrix[4] * other[1] + matrix[5] * other[5] + matrix[6] * other[9] + matrix[7] * other[13]
- temp[6] = matrix[4] * other[2] + matrix[5] * other[6] + matrix[6] * other[10] + matrix[7] * other[14]
- temp[7] = matrix[4] * other[3] + matrix[5] * other[7] + matrix[6] * other[11] + matrix[7] * other[15]
- temp[8] = matrix[8] * other[0] + matrix[9] * other[4] + matrix[10] * other[8] + matrix[11] * other[12]
- temp[9] = matrix[8] * other[1] + matrix[9] * other[5] + matrix[10] * other[9] + matrix[11] * other[13]
- temp[10] = matrix[8] * other[2] + matrix[9] * other[6] + matrix[10] * other[10] + matrix[11] * other[14]
- temp[11] = matrix[8] * other[3] + matrix[9] * other[7] + matrix[10] * other[11] + matrix[11] * other[15]
- temp[12] = matrix[12] * other[0] + matrix[13] * other[4] + matrix[14] * other[8] + matrix[15] * other[12]
- temp[13] = matrix[12] * other[1] + matrix[13] * other[5] + matrix[14] * other[9] + matrix[15] * other[13]
- temp[14] = matrix[12] * other[2] + matrix[13] * other[6] + matrix[14] * other[10] + matrix[15] * other[14]
- temp[15] = matrix[12] * other[3] + matrix[13] * other[7] + matrix[14] * other[11] + matrix[15] * other[15]
-
- matrix[0] = temp[0]
- matrix[1] = temp[1]
- matrix[2] = temp[2]
- matrix[3] = temp[3]
- matrix[4] = temp[4]
- matrix[5] = temp[5]
- matrix[6] = temp[6]
- matrix[7] = temp[7]
- matrix[8] = temp[8]
- matrix[9] = temp[9]
- matrix[10] = temp[10]
- matrix[11] = temp[11]
- matrix[12] = temp[12]
- matrix[13] = temp[13]
- matrix[14] = temp[14]
- matrix[15] = temp[15]
- }
-
- fun translate(x: Float, y: Float, z: Float) {
- translateMatrix[12] = x
- translateMatrix[13] = y
- translateMatrix[14] = z
-
- mul(translateMatrix)
- }
-
- fun scale(x: Float, y: Float, z: Float) {
- scaleMatrix[0] = x
- scaleMatrix[5] = y
- scaleMatrix[10] = z
-
- mul(scaleMatrix)
- }
-
- fun rotateX(angle: Float) {
- rotateXMatrix[5] = Math.cos(angle.toDouble()).toFloat()
- rotateXMatrix[6] = (-Math.sin(angle.toDouble())).toFloat()
- rotateXMatrix[9] = Math.sin(angle.toDouble()).toFloat()
- rotateXMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateXMatrix)
- }
-
- fun rotateY(angle: Float) {
- rotateYMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateYMatrix[2] = Math.sin(angle.toDouble()).toFloat()
- rotateYMatrix[8] = (-Math.sin(angle.toDouble())).toFloat()
- rotateYMatrix[10] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateYMatrix)
- }
-
- fun rotateZ(angle: Float) {
- rotateZMatrix[0] = Math.cos(angle.toDouble()).toFloat()
- rotateZMatrix[1] = Math.sin(angle.toDouble()).toFloat()
- rotateZMatrix[4] = (-Math.sin(angle.toDouble())).toFloat()
- rotateZMatrix[5] = Math.cos(angle.toDouble()).toFloat()
-
- mul(rotateZMatrix)
- }
-}
diff --git a/src/com/persesgames/shader/ShaderProgram.kt b/src/com/persesgames/shader/ShaderProgram.kt
deleted file mode 100644
index 1657c83..0000000
--- a/src/com/persesgames/shader/ShaderProgram.kt
+++ /dev/null
@@ -1,130 +0,0 @@
-package com.persesgames.shader
-
-import org.khronos.webgl.*
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:15
- */
-
-class VertextAttributeInfo(val locationName: String, val numElements: Int) {
- var location = 0
- var offset = 0
-}
-
-class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) {
-
- var shaderProgram: WebGLProgram
- var vertex: WebGLShader
- var fragment: WebGLShader
-
- var verticesBlockSize = 0
- var currentIndex = 0
- var verticesLength = 0
- var vertices = Float32Array(0)
-
- var attribBuffer: WebGLBuffer
-
- init {
- vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
- fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
-
- shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
- webgl.attachShader(shaderProgram, vertex)
- webgl.attachShader(shaderProgram, fragment)
- webgl.linkProgram(shaderProgram)
-
- if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) {
- println(webgl.getProgramInfoLog(shaderProgram))
- throw IllegalStateException("Unable to compile program!")
- }
-
- webgl.useProgram(shaderProgram)
-
- this.verticesBlockSize = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- info.location = webgl.getAttribLocation(shaderProgram, info.locationName)
- info.offset = verticesBlockSize;
-
- verticesBlockSize += info.numElements;
- println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}");
- }
-
- println("verticesBlockSize $verticesBlockSize");
-
- this.currentIndex = 0;
-
- // create vertices buffer
- verticesLength = 4096 - (4096 % verticesBlockSize);
- vertices = Float32Array(verticesLength);
-
- println("vertices.length ${vertices.length}");
-
- attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!")
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
-
- println("ShaderProgram constructor done");
- }
-
- private fun compileShader(source: String, type: Int): WebGLShader {
- var result: WebGLShader
-
- result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
- webgl.shaderSource(result, source)
- webgl.compileShader(result)
-
- if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
- throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
- }
-
- return result;
- }
-
- fun queueVertices(verts: Float32Array) {
- if((currentIndex + verts.length) >= verticesLength) {
- flush();
- }
-
- vertices.set(verts, currentIndex)
- currentIndex += verts.length
- }
-
- fun begin() {
- webgl.useProgram(shaderProgram);
- webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer);
- currentIndex = 0;
-
- // set attribute locations...
- for (info in vainfo.iterator()) {
- webgl.enableVertexAttribArray(info.location);
- webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
- }
-
- }
-
- fun flush() {
- if (currentIndex > 0) {
- webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingContext.DYNAMIC_DRAW);
- webgl.drawArrays(mode, 0, (currentIndex / verticesBlockSize).toInt());
- currentIndex = 0;
- }
- }
-
- fun end() {
- flush()
- webgl.useProgram(null)
- }
-
- fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location);
-
- fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
-
- fun setUniform1f(location: String, value: Float) { flush(); webgl.uniform1f(getUniformLocation(location), value); }
- fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { flush(); webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); }
- fun setUniform1i(location: String, value: Int) { flush(); webgl.uniform1i(getUniformLocation(location), value); }
- fun setUniformMatrix4fv(location: String, value: Float32Array) { flush(); webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
-
-}
diff --git a/src/com/persesgames/texture/Sprites.kt b/src/com/persesgames/texture/Sprites.kt
deleted file mode 100644
index 47230f4..0000000
--- a/src/com/persesgames/texture/Sprites.kt
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.persesgames.texture
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 15:44
- */
-
-val vertexShaderSource = """
- attribute vec2 a_position;
-
- attribute float a_imagesX;
- attribute float a_imagesY;
- attribute float a_currentImage;
-
- uniform mat4 u_projectionView;
-
- varying float v_imagesX;
- varying float v_imagesY;
- varying float v_currentImage;
-
- void main(void) {
- gl_Position = u_projectionView * vec4(a_position, -1, 1.0);
- gl_PointSize = 50.0 / gl_Position.w;
-
- v_imagesX = a_imagesX;
- v_imagesY = a_imagesY;
- v_currentImage = a_currentImage;
- }
-"""
-
-val fragmentShaderSource = """
- precision mediump float;
-
- uniform sampler2D uSampler;
-
- varying float v_imagesX;
- varying float v_imagesY;
- varying float v_currentImage;
-
- void main(void) {
- // calculate current texture coords depending on current image number
- float blockX = 1.0 / v_imagesX;
- float blockY = 1.0 / v_imagesY;
-
- float x = blockX * (mod(v_currentImage, v_imagesX));
- float y = blockY * floor(v_currentImage / v_imagesY);
-
- vec2 textCoord = vec2(x + blockX * gl_PointCoord.s, y + blockY - blockY * gl_PointCoord.t);
- //vec2 textCoord = vec2((x + blockX) * 0.0001 + gl_PointCoord.s, (y + blockY) * 0.0001 + gl_PointCoord.t);
-
- gl_FragColor = texture2D(uSampler, textCoord);
- }
-"""
-
diff --git a/src/com/persesgames/texture/Textures.kt b/src/com/persesgames/texture/Textures.kt
deleted file mode 100644
index 79e26a8..0000000
--- a/src/com/persesgames/texture/Textures.kt
+++ /dev/null
@@ -1,55 +0,0 @@
-package com.persesgames.texture
-
-import org.khronos.webgl.WebGLRenderingContext
-import org.khronos.webgl.WebGLTexture
-import org.w3c.dom.HTMLImageElement
-import java.util.*
-import kotlin.browser.document
-
-/**
- * User: rnentjes
- * Date: 17-4-16
- * Time: 14:52
- */
-
-object Textures {
- var textures = HashMap();
- var startedLoading = 0
- var loaded = 0
-
- fun load(gl: WebGLRenderingContext, name: String, filename: String) {
- startedLoading++
-
- var webGlTexture = gl.createTexture()
- if (webGlTexture != null) {
- var image = document.createElement("img") as HTMLImageElement
- image.onload = {
- textureLoaded(gl, webGlTexture, image)
- textures.put(name, webGlTexture)
- loaded++
- }
- image.src = filename
- } else {
- println("Couldn't create webgl texture!")
- }
-
- }
-
- fun textureLoaded(gl: WebGLRenderingContext, texture: WebGLTexture, image: HTMLImageElement) {
- gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, texture);
- gl.pixelStorei(WebGLRenderingContext.UNPACK_FLIP_Y_WEBGL, 1); // second argument must be an int
- gl.texImage2D(WebGLRenderingContext.TEXTURE_2D, 0, WebGLRenderingContext.RGBA, WebGLRenderingContext.RGBA, WebGLRenderingContext.UNSIGNED_BYTE, image);
- gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MAG_FILTER, WebGLRenderingContext.NEAREST);
- gl.texParameteri(WebGLRenderingContext.TEXTURE_2D, WebGLRenderingContext.TEXTURE_MIN_FILTER, WebGLRenderingContext.NEAREST);
- gl.bindTexture(WebGLRenderingContext.TEXTURE_2D, null);
- }
-
- fun ready() = loaded == startedLoading
-
- fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!")
-
- fun clear() {
- // delete and unbind all textures...
- }
-
-}
\ No newline at end of file