Refactor ShaderProgram class for improved readability and structure; add missing imports. Minor HTML formatting adjustments in index.html.
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package com.persesgames.shader
|
package com.persesgames.shader
|
||||||
|
|
||||||
import org.khronos.webgl.*
|
import org.khronos.webgl.Float32Array
|
||||||
|
import org.khronos.webgl.WebGLBuffer
|
||||||
|
import org.khronos.webgl.WebGLProgram
|
||||||
|
import org.khronos.webgl.WebGLRenderingContext
|
||||||
|
import org.khronos.webgl.WebGLShader
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: rnentjes
|
* User: rnentjes
|
||||||
@@ -19,7 +23,8 @@ class ShaderProgram<T>(
|
|||||||
vertexShaderSource: String,
|
vertexShaderSource: String,
|
||||||
fragmentShaderSource: String,
|
fragmentShaderSource: String,
|
||||||
val vainfo: Array<VertextAttributeInfo>,
|
val vainfo: Array<VertextAttributeInfo>,
|
||||||
val setter: (program: ShaderProgram<T>, data: T) -> Unit) {
|
val setter: (program: ShaderProgram<T>, data: T) -> Unit
|
||||||
|
) {
|
||||||
|
|
||||||
var shaderProgram: WebGLProgram
|
var shaderProgram: WebGLProgram
|
||||||
var vertex: WebGLShader
|
var vertex: WebGLShader
|
||||||
@@ -32,7 +37,8 @@ class ShaderProgram<T>(
|
|||||||
vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
|
vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER)
|
||||||
fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
|
fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER)
|
||||||
|
|
||||||
shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!")
|
shaderProgram = webgl.createProgram()
|
||||||
|
?: throw IllegalStateException("Unable to request shader program from webgl context!")
|
||||||
webgl.attachShader(shaderProgram, vertex)
|
webgl.attachShader(shaderProgram, vertex)
|
||||||
webgl.attachShader(shaderProgram, fragment)
|
webgl.attachShader(shaderProgram, fragment)
|
||||||
webgl.linkProgram(shaderProgram)
|
webgl.linkProgram(shaderProgram)
|
||||||
@@ -59,6 +65,7 @@ class ShaderProgram<T>(
|
|||||||
WebGLRenderingContext.TRIANGLES -> {
|
WebGLRenderingContext.TRIANGLES -> {
|
||||||
drawLength = verticesBlockSize * 3
|
drawLength = verticesBlockSize * 3
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
drawLength = verticesBlockSize
|
drawLength = verticesBlockSize
|
||||||
}
|
}
|
||||||
@@ -70,14 +77,19 @@ class ShaderProgram<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun compileShader(source: String, type: Int): WebGLShader {
|
private fun compileShader(source: String, type: Int): WebGLShader {
|
||||||
val result: WebGLShader
|
val result: WebGLShader = webgl.createShader(type)
|
||||||
|
?: throw IllegalStateException("Unable to request shader from webgl context!")
|
||||||
result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
|
|
||||||
webgl.shaderSource(result, source)
|
webgl.shaderSource(result, source)
|
||||||
webgl.compileShader(result)
|
webgl.compileShader(result)
|
||||||
|
|
||||||
if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
|
if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) {
|
||||||
throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}")
|
throw IllegalStateException(
|
||||||
|
"Unable to compile shader!\n${source}\n\n${
|
||||||
|
webgl.getShaderInfoLog(
|
||||||
|
result
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -90,7 +102,14 @@ class ShaderProgram<T>(
|
|||||||
// set attribute locations...
|
// set attribute locations...
|
||||||
for (info in vainfo.iterator()) {
|
for (info in vainfo.iterator()) {
|
||||||
webgl.enableVertexAttribArray(info.location);
|
webgl.enableVertexAttribArray(info.location);
|
||||||
webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4);
|
webgl.vertexAttribPointer(
|
||||||
|
info.location,
|
||||||
|
info.numElements,
|
||||||
|
WebGLRenderingContext.FLOAT,
|
||||||
|
false,
|
||||||
|
verticesBlockSize * 4,
|
||||||
|
info.offset * 4
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
setter(this, userdata)
|
setter(this, userdata)
|
||||||
@@ -107,10 +126,19 @@ class ShaderProgram<T>(
|
|||||||
|
|
||||||
fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
|
fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
|
||||||
|
|
||||||
fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); }
|
fun setUniform1f(location: String, value: Float) {
|
||||||
fun setUniform2f(location: String, v1: Float, v2: Float) { webgl.uniform2f(getUniformLocation(location), v1, v2); }
|
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 setUniform2f(location: String, v1: Float, v2: Float) {
|
||||||
fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); }
|
webgl.uniform2f(getUniformLocation(location), v1, v2); }
|
||||||
|
|
||||||
|
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); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
padding: 0;
|
padding: 0;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas {
|
canvas {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user