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
|
||||
|
||||
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
|
||||
@@ -19,7 +23,8 @@ class ShaderProgram<T>(
|
||||
vertexShaderSource: String,
|
||||
fragmentShaderSource: String,
|
||||
val vainfo: Array<VertextAttributeInfo>,
|
||||
val setter: (program: ShaderProgram<T>, data: T) -> Unit) {
|
||||
val setter: (program: ShaderProgram<T>, data: T) -> Unit
|
||||
) {
|
||||
|
||||
var shaderProgram: WebGLProgram
|
||||
var vertex: WebGLShader
|
||||
@@ -32,7 +37,8 @@ class ShaderProgram<T>(
|
||||
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!")
|
||||
shaderProgram = webgl.createProgram()
|
||||
?: throw IllegalStateException("Unable to request shader program from webgl context!")
|
||||
webgl.attachShader(shaderProgram, vertex)
|
||||
webgl.attachShader(shaderProgram, fragment)
|
||||
webgl.linkProgram(shaderProgram)
|
||||
@@ -59,6 +65,7 @@ class ShaderProgram<T>(
|
||||
WebGLRenderingContext.TRIANGLES -> {
|
||||
drawLength = verticesBlockSize * 3
|
||||
}
|
||||
|
||||
else -> {
|
||||
drawLength = verticesBlockSize
|
||||
}
|
||||
@@ -70,14 +77,19 @@ class ShaderProgram<T>(
|
||||
}
|
||||
|
||||
private fun compileShader(source: String, type: Int): WebGLShader {
|
||||
val result: WebGLShader
|
||||
|
||||
result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!")
|
||||
val result: WebGLShader = 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)}")
|
||||
throw IllegalStateException(
|
||||
"Unable to compile shader!\n${source}\n\n${
|
||||
webgl.getShaderInfoLog(
|
||||
result
|
||||
)
|
||||
}"
|
||||
)
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -90,7 +102,14 @@ class ShaderProgram<T>(
|
||||
// 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);
|
||||
webgl.vertexAttribPointer(
|
||||
info.location,
|
||||
info.numElements,
|
||||
WebGLRenderingContext.FLOAT,
|
||||
false,
|
||||
verticesBlockSize * 4,
|
||||
info.offset * 4
|
||||
);
|
||||
}
|
||||
|
||||
setter(this, userdata)
|
||||
@@ -107,10 +126,19 @@ class ShaderProgram<T>(
|
||||
|
||||
fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location);
|
||||
|
||||
fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); }
|
||||
fun setUniform2f(location: String, v1: Float, v2: Float) { 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); }
|
||||
fun setUniform1f(location: String, value: Float) {
|
||||
webgl.uniform1f(getUniformLocation(location), value); }
|
||||
|
||||
fun setUniform2f(location: String, v1: Float, v2: Float) {
|
||||
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;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
canvas {
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user