diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt index 09a80e7..e6d6f74 100644 --- a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt +++ b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt @@ -17,64 +17,17 @@ } -private val vertexShaderSource = """ - attribute vec2 a_position; - attribute vec2 a_texCoord; - - uniform mat4 u_projectionView; - - varying vec2 v_textCoord; - - void main(void) { - v_textCoord = a_texCoord; - - gl_Position = u_projectionView * vec4(a_position, -1, 1.0); - } -""" - -private val fragmentShaderSource = """ - precision mediump float; - - uniform sampler2D u_sampler; - - varying vec2 v_textCoord; - - void main(void) { - gl_FragColor = texture2D(u_sampler, v_textCoord); - } -""" - class SpriteBatch { - val program: ShaderProgram; - val vainfo = arrayOf( - VertextAttributeInfo("a_position", 2), - VertextAttributeInfo("a_texCoord", 2) - ) - // TODO: replace with Float32Array when it supports [] or set - private val spriteArray = Array(6 * 4, { 0f }) - - init { - program = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) - } - - fun begin() { - program.begin() - } fun draw(sprite: Sprite, x: Float, y: Float) { - spriteArray[0] = 1f + var texture = Textures.get(sprite.textureName) - Textures.get(sprite.textureName).bind() + texture.queueDraw(x, y) - program.queueVertices(Float32Array(spriteArray)) } - fun flush() { - program.flush() + fun render() { + Textures.render() } - fun end() { - flush() - program.end() - } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt index 09a80e7..e6d6f74 100644 --- a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt +++ b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt @@ -17,64 +17,17 @@ } -private val vertexShaderSource = """ - attribute vec2 a_position; - attribute vec2 a_texCoord; - - uniform mat4 u_projectionView; - - varying vec2 v_textCoord; - - void main(void) { - v_textCoord = a_texCoord; - - gl_Position = u_projectionView * vec4(a_position, -1, 1.0); - } -""" - -private val fragmentShaderSource = """ - precision mediump float; - - uniform sampler2D u_sampler; - - varying vec2 v_textCoord; - - void main(void) { - gl_FragColor = texture2D(u_sampler, v_textCoord); - } -""" - class SpriteBatch { - val program: ShaderProgram; - val vainfo = arrayOf( - VertextAttributeInfo("a_position", 2), - VertextAttributeInfo("a_texCoord", 2) - ) - // TODO: replace with Float32Array when it supports [] or set - private val spriteArray = Array(6 * 4, { 0f }) - - init { - program = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) - } - - fun begin() { - program.begin() - } fun draw(sprite: Sprite, x: Float, y: Float) { - spriteArray[0] = 1f + var texture = Textures.get(sprite.textureName) - Textures.get(sprite.textureName).bind() + texture.queueDraw(x, y) - program.queueVertices(Float32Array(spriteArray)) } - fun flush() { - program.flush() + fun render() { + Textures.render() } - fun end() { - flush() - program.end() - } } diff --git a/lib/kotludens/com/persesgames/texture/Sprites.kt b/lib/kotludens/com/persesgames/texture/Sprites.kt index 47230f4..1b0fd0a 100644 --- a/lib/kotludens/com/persesgames/texture/Sprites.kt +++ b/lib/kotludens/com/persesgames/texture/Sprites.kt @@ -5,51 +5,3 @@ * 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/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt index 09a80e7..e6d6f74 100644 --- a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt +++ b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt @@ -17,64 +17,17 @@ } -private val vertexShaderSource = """ - attribute vec2 a_position; - attribute vec2 a_texCoord; - - uniform mat4 u_projectionView; - - varying vec2 v_textCoord; - - void main(void) { - v_textCoord = a_texCoord; - - gl_Position = u_projectionView * vec4(a_position, -1, 1.0); - } -""" - -private val fragmentShaderSource = """ - precision mediump float; - - uniform sampler2D u_sampler; - - varying vec2 v_textCoord; - - void main(void) { - gl_FragColor = texture2D(u_sampler, v_textCoord); - } -""" - class SpriteBatch { - val program: ShaderProgram; - val vainfo = arrayOf( - VertextAttributeInfo("a_position", 2), - VertextAttributeInfo("a_texCoord", 2) - ) - // TODO: replace with Float32Array when it supports [] or set - private val spriteArray = Array(6 * 4, { 0f }) - - init { - program = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) - } - - fun begin() { - program.begin() - } fun draw(sprite: Sprite, x: Float, y: Float) { - spriteArray[0] = 1f + var texture = Textures.get(sprite.textureName) - Textures.get(sprite.textureName).bind() + texture.queueDraw(x, y) - program.queueVertices(Float32Array(spriteArray)) } - fun flush() { - program.flush() + fun render() { + Textures.render() } - fun end() { - flush() - program.end() - } } diff --git a/lib/kotludens/com/persesgames/texture/Sprites.kt b/lib/kotludens/com/persesgames/texture/Sprites.kt index 47230f4..1b0fd0a 100644 --- a/lib/kotludens/com/persesgames/texture/Sprites.kt +++ b/lib/kotludens/com/persesgames/texture/Sprites.kt @@ -5,51 +5,3 @@ * 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/com/persesgames/texture/Textures.kt b/lib/kotludens/com/persesgames/texture/Textures.kt index 4bcaaf8..524ec8c 100644 --- a/lib/kotludens/com/persesgames/texture/Textures.kt +++ b/lib/kotludens/com/persesgames/texture/Textures.kt @@ -1,6 +1,10 @@ package com.persesgames.texture import com.persesgames.game.Game +import com.persesgames.math.Matrix4 +import com.persesgames.shader.ShaderProgram +import com.persesgames.shader.ShaderProgramMesh +import com.persesgames.shader.VertextAttributeInfo import org.khronos.webgl.WebGLRenderingContext import org.khronos.webgl.WebGLTexture import org.w3c.dom.HTMLImageElement @@ -13,19 +17,80 @@ * Time: 14:52 */ -class Texture(val glTexture: WebGLTexture) { +private val vertexShaderSource = """ + attribute vec2 a_position; + attribute vec3 a_color; - fun bind() { - Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) - Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + uniform mat4 u_projectionView; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + v_color = a_color; + v_textCoord = a_position.xy; + + gl_Position = u_projectionView * vec4(a_position, -1, 1.0); + } +""" + +private val fragmentShaderSource = """ + precision mediump float; + + uniform sampler2D u_sampler; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + gl_FragColor = texture2D(u_sampler, v_textCoord) * vec4(v_color, 1.0); + } +""" + +class TextureData( + val vMatrix: Matrix4 +) { + + +} + +class Texture(val glTexture: WebGLTexture, val shaderProgram: ShaderProgram) { + val shaderProgramMesh: ShaderProgramMesh + + init { + shaderProgramMesh = ShaderProgramMesh(shaderProgram) } + fun queueDraw(x: Float, y: Float) { + // shaderProgramMesh.queue(x, y, etc) + } + + fun render(userdata: TextureData) { + Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) + Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + + shaderProgramMesh.render(userdata) + } } object Textures { var textures = HashMap(); var startedLoading = 0 var loaded = 0 + val shaderProgram: ShaderProgram + + init { + val setter = { program: ShaderProgram, data: TextureData -> + program.setUniformMatrix4fv("", data.vMatrix.getFloat32Array()) + } + + val vainfo = arrayOf( + VertextAttributeInfo("a_position", 2), + VertextAttributeInfo("a_color", 3) + ) + + shaderProgram = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo, setter) + } fun loadSpriteSheet(name: String, filename: String) { @@ -41,7 +106,7 @@ var image = document.createElement("img") as HTMLImageElement image.onload = { textureLoaded(webGlTexture, image) - textures.put(name, Texture(webGlTexture)) + textures.put(name, Texture(webGlTexture, shaderProgram)) loaded++ println("loaded texture $loaded/$startedLoading ${ready()}") } @@ -70,4 +135,11 @@ // delete and unbind all textures... } + fun render() { + var textureData = TextureData(Game.view.vMatrix) + for ((key, value) in textures) { + value.render(textureData) + } + } + } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt index 09a80e7..e6d6f74 100644 --- a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt +++ b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt @@ -17,64 +17,17 @@ } -private val vertexShaderSource = """ - attribute vec2 a_position; - attribute vec2 a_texCoord; - - uniform mat4 u_projectionView; - - varying vec2 v_textCoord; - - void main(void) { - v_textCoord = a_texCoord; - - gl_Position = u_projectionView * vec4(a_position, -1, 1.0); - } -""" - -private val fragmentShaderSource = """ - precision mediump float; - - uniform sampler2D u_sampler; - - varying vec2 v_textCoord; - - void main(void) { - gl_FragColor = texture2D(u_sampler, v_textCoord); - } -""" - class SpriteBatch { - val program: ShaderProgram; - val vainfo = arrayOf( - VertextAttributeInfo("a_position", 2), - VertextAttributeInfo("a_texCoord", 2) - ) - // TODO: replace with Float32Array when it supports [] or set - private val spriteArray = Array(6 * 4, { 0f }) - - init { - program = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) - } - - fun begin() { - program.begin() - } fun draw(sprite: Sprite, x: Float, y: Float) { - spriteArray[0] = 1f + var texture = Textures.get(sprite.textureName) - Textures.get(sprite.textureName).bind() + texture.queueDraw(x, y) - program.queueVertices(Float32Array(spriteArray)) } - fun flush() { - program.flush() + fun render() { + Textures.render() } - fun end() { - flush() - program.end() - } } diff --git a/lib/kotludens/com/persesgames/texture/Sprites.kt b/lib/kotludens/com/persesgames/texture/Sprites.kt index 47230f4..1b0fd0a 100644 --- a/lib/kotludens/com/persesgames/texture/Sprites.kt +++ b/lib/kotludens/com/persesgames/texture/Sprites.kt @@ -5,51 +5,3 @@ * 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/com/persesgames/texture/Textures.kt b/lib/kotludens/com/persesgames/texture/Textures.kt index 4bcaaf8..524ec8c 100644 --- a/lib/kotludens/com/persesgames/texture/Textures.kt +++ b/lib/kotludens/com/persesgames/texture/Textures.kt @@ -1,6 +1,10 @@ package com.persesgames.texture import com.persesgames.game.Game +import com.persesgames.math.Matrix4 +import com.persesgames.shader.ShaderProgram +import com.persesgames.shader.ShaderProgramMesh +import com.persesgames.shader.VertextAttributeInfo import org.khronos.webgl.WebGLRenderingContext import org.khronos.webgl.WebGLTexture import org.w3c.dom.HTMLImageElement @@ -13,19 +17,80 @@ * Time: 14:52 */ -class Texture(val glTexture: WebGLTexture) { +private val vertexShaderSource = """ + attribute vec2 a_position; + attribute vec3 a_color; - fun bind() { - Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) - Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + uniform mat4 u_projectionView; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + v_color = a_color; + v_textCoord = a_position.xy; + + gl_Position = u_projectionView * vec4(a_position, -1, 1.0); + } +""" + +private val fragmentShaderSource = """ + precision mediump float; + + uniform sampler2D u_sampler; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + gl_FragColor = texture2D(u_sampler, v_textCoord) * vec4(v_color, 1.0); + } +""" + +class TextureData( + val vMatrix: Matrix4 +) { + + +} + +class Texture(val glTexture: WebGLTexture, val shaderProgram: ShaderProgram) { + val shaderProgramMesh: ShaderProgramMesh + + init { + shaderProgramMesh = ShaderProgramMesh(shaderProgram) } + fun queueDraw(x: Float, y: Float) { + // shaderProgramMesh.queue(x, y, etc) + } + + fun render(userdata: TextureData) { + Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) + Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + + shaderProgramMesh.render(userdata) + } } object Textures { var textures = HashMap(); var startedLoading = 0 var loaded = 0 + val shaderProgram: ShaderProgram + + init { + val setter = { program: ShaderProgram, data: TextureData -> + program.setUniformMatrix4fv("", data.vMatrix.getFloat32Array()) + } + + val vainfo = arrayOf( + VertextAttributeInfo("a_position", 2), + VertextAttributeInfo("a_color", 3) + ) + + shaderProgram = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo, setter) + } fun loadSpriteSheet(name: String, filename: String) { @@ -41,7 +106,7 @@ var image = document.createElement("img") as HTMLImageElement image.onload = { textureLoaded(webGlTexture, image) - textures.put(name, Texture(webGlTexture)) + textures.put(name, Texture(webGlTexture, shaderProgram)) loaded++ println("loaded texture $loaded/$startedLoading ${ready()}") } @@ -70,4 +135,11 @@ // delete and unbind all textures... } + fun render() { + var textureData = TextureData(Game.view.vMatrix) + for ((key, value) in textures) { + value.render(textureData) + } + } + } diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt index 1c4f983..5f99768 100644 --- a/src/com/persesgames/Test.kt +++ b/src/com/persesgames/Test.kt @@ -14,7 +14,7 @@ * Time: 13:17 */ -val vertexShaderSource = """ +private val vertexShaderSource = """ attribute vec2 a_position; attribute vec3 a_color; @@ -31,7 +31,7 @@ } """ -val fragmentShaderSource = """ +private val fragmentShaderSource = """ precision mediump float; uniform sampler2D u_sampler; @@ -56,7 +56,7 @@ var mMatrix = Matrix4() var vMatrix = Matrix4() var pMatrix = Matrix4() - var program: ShaderProgram + var program: ShaderProgram var triangle: Float32Array init { @@ -65,7 +65,7 @@ VertextAttributeInfo("a_color", 3) ) - program = ShaderProgram(webgl, WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) + program = ShaderProgram(webgl, WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo, {a,b -> }) triangle = Float32Array(arrayOf( 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, @@ -123,15 +123,15 @@ // pMatrix.mul(mMatrix) mMatrix.mul(vMatrix); mMatrix.mul(pMatrix); - +/* program.begin() Textures.get("SHIP").bind() program.setUniform1i("u_sampler", 0) program.setUniformMatrix4fv("u_projectionView", mMatrix.getFloat32Array()) - program.queueVertices(triangle) - program.end() + //program.queueVertices(triangle) + program.end()*/ } fun resize() { diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt index 8d03190..0ce59cc 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgram.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgram.kt @@ -8,7 +8,13 @@ * Time: 15:15 */ -class ShaderProgram(val webgl: WebGLRenderingContext, val mode: Int, vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array) { +class ShaderProgram( + val webgl: WebGLRenderingContext, + val drawType: Int, + vertexShaderSource: String, + fragmentShaderSource: String, + val vainfo: Array, + val setter: (program: ShaderProgram, data: T) -> Unit) { var shaderProgram: WebGLProgram var vertex: WebGLShader @@ -19,8 +25,6 @@ var verticesLength = 0 var vertices = Float32Array(0) - var attribBuffer: WebGLBuffer - init { vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) @@ -58,9 +62,6 @@ 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"); } @@ -78,28 +79,9 @@ return result; } - fun queueVertices(verts: Float32Array) { - if((currentIndex + verts.length) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.length - } - - fun queueVertices(verts: Array) { - if((currentIndex + verts.size) >= verticesLength) { - flush(); - } - - vertices.set(verts, currentIndex) - currentIndex += verts.size - } - - fun begin() { + fun begin(attribBuffer: WebGLBuffer, userdata: T) { webgl.useProgram(shaderProgram); webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - currentIndex = 0; // set attribute locations... for (info in vainfo.iterator()) { @@ -107,18 +89,10 @@ 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; - } + setter(this, userdata) } fun end() { - flush() webgl.useProgram(null) } @@ -126,9 +100,9 @@ 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); } + fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } + fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt index 4d821ef..f83a457 100644 --- a/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt +++ b/lib/kotludens/com/persesgames/shader/ShaderProgramMesh.kt @@ -1,7 +1,7 @@ package com.persesgames.shader -import com.persesgames.game.Game -import java.util.* +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLRenderingContext /** * User: rnentjes @@ -15,24 +15,44 @@ } class ShaderProgramMesh( - val shaderProgram: ShaderProgram, - val meshSetter: (ShaderProgram, T) -> Unit, - val type: Int, - val vainfo: Array) { - val data: Array = Array(32796, { 0f }); - var count: Int = 0 - var blockLength: Int = 0 + val shaderProgram: ShaderProgram +) { + val webgl = shaderProgram.webgl + val data: Array = Array(32796, { 0f }) + var currentIndex: Int = 0 + val attribBuffer: WebGLBuffer init { - for (info in vainfo.iterator()) { - info.location = shaderProgram.getAttribLocation(info.locationName) - info.offset = blockLength; - - blockLength += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - println("verticesBlockSize $blockLength"); + attribBuffer = webgl.createBuffer() ?: throw IllegalStateException("Unable to create webgl buffer!") + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); } -} \ No newline at end of file + fun queue(vararg vert: Float) { + queue(*vert) + } + + fun queue(vertices: Array) { + // shaderProgram.verticesBlockSize + for (index in 0..vertices.size-1) { + data[currentIndex + index] = vertices[index] + } + + currentIndex += vertices.size + } + + fun render(userdata: T) { + if (currentIndex > 0) { + if (currentIndex % shaderProgram.verticesBlockSize != 0) { + throw IllegalStateException("Number of vertices not a multiple of the attribute block size!") + } + + shaderProgram.begin(attribBuffer, userdata) + + webgl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, data, WebGLRenderingContext.DYNAMIC_DRAW); + webgl.drawArrays(shaderProgram.drawType, 0, (currentIndex / shaderProgram.verticesBlockSize).toInt()) + currentIndex = 0; + + shaderProgram.end() + } + } +} diff --git a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt index 09a80e7..e6d6f74 100644 --- a/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt +++ b/lib/kotludens/com/persesgames/sprite/SpriteBatch.kt @@ -17,64 +17,17 @@ } -private val vertexShaderSource = """ - attribute vec2 a_position; - attribute vec2 a_texCoord; - - uniform mat4 u_projectionView; - - varying vec2 v_textCoord; - - void main(void) { - v_textCoord = a_texCoord; - - gl_Position = u_projectionView * vec4(a_position, -1, 1.0); - } -""" - -private val fragmentShaderSource = """ - precision mediump float; - - uniform sampler2D u_sampler; - - varying vec2 v_textCoord; - - void main(void) { - gl_FragColor = texture2D(u_sampler, v_textCoord); - } -""" - class SpriteBatch { - val program: ShaderProgram; - val vainfo = arrayOf( - VertextAttributeInfo("a_position", 2), - VertextAttributeInfo("a_texCoord", 2) - ) - // TODO: replace with Float32Array when it supports [] or set - private val spriteArray = Array(6 * 4, { 0f }) - - init { - program = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) - } - - fun begin() { - program.begin() - } fun draw(sprite: Sprite, x: Float, y: Float) { - spriteArray[0] = 1f + var texture = Textures.get(sprite.textureName) - Textures.get(sprite.textureName).bind() + texture.queueDraw(x, y) - program.queueVertices(Float32Array(spriteArray)) } - fun flush() { - program.flush() + fun render() { + Textures.render() } - fun end() { - flush() - program.end() - } } diff --git a/lib/kotludens/com/persesgames/texture/Sprites.kt b/lib/kotludens/com/persesgames/texture/Sprites.kt index 47230f4..1b0fd0a 100644 --- a/lib/kotludens/com/persesgames/texture/Sprites.kt +++ b/lib/kotludens/com/persesgames/texture/Sprites.kt @@ -5,51 +5,3 @@ * 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/com/persesgames/texture/Textures.kt b/lib/kotludens/com/persesgames/texture/Textures.kt index 4bcaaf8..524ec8c 100644 --- a/lib/kotludens/com/persesgames/texture/Textures.kt +++ b/lib/kotludens/com/persesgames/texture/Textures.kt @@ -1,6 +1,10 @@ package com.persesgames.texture import com.persesgames.game.Game +import com.persesgames.math.Matrix4 +import com.persesgames.shader.ShaderProgram +import com.persesgames.shader.ShaderProgramMesh +import com.persesgames.shader.VertextAttributeInfo import org.khronos.webgl.WebGLRenderingContext import org.khronos.webgl.WebGLTexture import org.w3c.dom.HTMLImageElement @@ -13,19 +17,80 @@ * Time: 14:52 */ -class Texture(val glTexture: WebGLTexture) { +private val vertexShaderSource = """ + attribute vec2 a_position; + attribute vec3 a_color; - fun bind() { - Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) - Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + uniform mat4 u_projectionView; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + v_color = a_color; + v_textCoord = a_position.xy; + + gl_Position = u_projectionView * vec4(a_position, -1, 1.0); + } +""" + +private val fragmentShaderSource = """ + precision mediump float; + + uniform sampler2D u_sampler; + + varying vec3 v_color; + varying vec2 v_textCoord; + + void main(void) { + gl_FragColor = texture2D(u_sampler, v_textCoord) * vec4(v_color, 1.0); + } +""" + +class TextureData( + val vMatrix: Matrix4 +) { + + +} + +class Texture(val glTexture: WebGLTexture, val shaderProgram: ShaderProgram) { + val shaderProgramMesh: ShaderProgramMesh + + init { + shaderProgramMesh = ShaderProgramMesh(shaderProgram) } + fun queueDraw(x: Float, y: Float) { + // shaderProgramMesh.queue(x, y, etc) + } + + fun render(userdata: TextureData) { + Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) + Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture); + + shaderProgramMesh.render(userdata) + } } object Textures { var textures = HashMap(); var startedLoading = 0 var loaded = 0 + val shaderProgram: ShaderProgram + + init { + val setter = { program: ShaderProgram, data: TextureData -> + program.setUniformMatrix4fv("", data.vMatrix.getFloat32Array()) + } + + val vainfo = arrayOf( + VertextAttributeInfo("a_position", 2), + VertextAttributeInfo("a_color", 3) + ) + + shaderProgram = ShaderProgram(Game.gl(), WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo, setter) + } fun loadSpriteSheet(name: String, filename: String) { @@ -41,7 +106,7 @@ var image = document.createElement("img") as HTMLImageElement image.onload = { textureLoaded(webGlTexture, image) - textures.put(name, Texture(webGlTexture)) + textures.put(name, Texture(webGlTexture, shaderProgram)) loaded++ println("loaded texture $loaded/$startedLoading ${ready()}") } @@ -70,4 +135,11 @@ // delete and unbind all textures... } + fun render() { + var textureData = TextureData(Game.view.vMatrix) + for ((key, value) in textures) { + value.render(textureData) + } + } + } diff --git a/src/com/persesgames/Test.kt b/src/com/persesgames/Test.kt index 1c4f983..5f99768 100644 --- a/src/com/persesgames/Test.kt +++ b/src/com/persesgames/Test.kt @@ -14,7 +14,7 @@ * Time: 13:17 */ -val vertexShaderSource = """ +private val vertexShaderSource = """ attribute vec2 a_position; attribute vec3 a_color; @@ -31,7 +31,7 @@ } """ -val fragmentShaderSource = """ +private val fragmentShaderSource = """ precision mediump float; uniform sampler2D u_sampler; @@ -56,7 +56,7 @@ var mMatrix = Matrix4() var vMatrix = Matrix4() var pMatrix = Matrix4() - var program: ShaderProgram + var program: ShaderProgram var triangle: Float32Array init { @@ -65,7 +65,7 @@ VertextAttributeInfo("a_color", 3) ) - program = ShaderProgram(webgl, WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo) + program = ShaderProgram(webgl, WebGLRenderingContext.TRIANGLES, vertexShaderSource, fragmentShaderSource, vainfo, {a,b -> }) triangle = Float32Array(arrayOf( 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, @@ -123,15 +123,15 @@ // pMatrix.mul(mMatrix) mMatrix.mul(vMatrix); mMatrix.mul(pMatrix); - +/* program.begin() Textures.get("SHIP").bind() program.setUniform1i("u_sampler", 0) program.setUniformMatrix4fv("u_projectionView", mMatrix.getFloat32Array()) - program.queueVertices(triangle) - program.end() + //program.queueVertices(triangle) + program.end()*/ } fun resize() { diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index 009e13b..f795246 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -13,8 +13,8 @@ class WelcomeScreen: Screen() { var sprites = SpriteBatch() - var x = 100f - var y = 100f + var x = 1f + var y = 1f var sprite = Sprite("SHIP") override fun loadResources() { @@ -30,9 +30,8 @@ Game.gl().clearColor(0f, 1f, 1f, 1f) Game.gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - sprites.begin() sprites.draw(sprite, x, y); - sprites.end(); + sprites.render() } }