summaryrefslogtreecommitdiff
path: root/frontend/_rigidbody/shader.js
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
committerMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
commit376e1a7f9207fffb1ec3027ac1e7f32db5de4922 (patch)
tree8bc6c381a10d8b62ff33cdcff6daa1d17cee0f9a /frontend/_rigidbody/shader.js
Initial commit
Diffstat (limited to 'frontend/_rigidbody/shader.js')
-rw-r--r--frontend/_rigidbody/shader.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/frontend/_rigidbody/shader.js b/frontend/_rigidbody/shader.js
new file mode 100644
index 0000000..bf08638
--- /dev/null
+++ b/frontend/_rigidbody/shader.js
@@ -0,0 +1,60 @@
+function loadShader(pGl, pShaderType, pShaderSource) {
+ const lShader = pGl.createShader(pShaderType);
+ pGl.shaderSource(lShader, pShaderSource);
+ pGl.compileShader(lShader);
+ if (!pGl.getShaderParameter(lShader, pGl.COMPILE_STATUS)) {
+ alert('An error occurred compiling the shaders: ' + pGl.getShaderInfoLog(lShader));
+ pGl.deleteShader(lShader);
+ return null;
+ }
+
+ return lShader;
+}
+
+function loadOrthographicShader(pGl) {
+ var lVertex, lFragment;
+
+ function lLoadShaders() {
+ const lVertexShader = loadShader(pGl, pGl.VERTEX_SHADER, lVertex);
+ const lFragmentShader = loadShader(pGl, pGl.FRAGMENT_SHADER, lFragment);
+
+ const lShaderProgram = pGl.createProgram();
+ pGl.attachShader(lShaderProgram, lVertexShader);
+ pGl.attachShader(lShaderProgram, lFragmentShader);
+ pGl.linkProgram(lShaderProgram);
+
+ if (!pGl.getProgramParameter(lShaderProgram, pGl.LINK_STATUS)) {
+ alert('Unable to initialize the shader program: ' + pGl.getProgramInfoLog(lShaderProgram));
+ return null;
+ }
+
+ return {
+ program: lShaderProgram,
+ attributeLocations: {
+ position: pGl.getAttribLocation(lShaderProgram, 'position'),
+ color: pGl.getAttribLocation(lShaderProgram, 'color')
+ },
+ uniformLocations: {
+ projection: pGl.getUniformLocation(lShaderProgram, 'projection'),
+ model: pGl.getUniformLocation(lShaderProgram, 'model')
+ }
+ }
+ }
+
+ return fetch('/_rigidbody/shaders/orthographic.vert').then(function(pResponse) {
+ if (pResponse.status === 200) {
+ return pResponse.text().then(function(pShader) {
+ lVertex = pShader;
+
+ return fetch('/_rigidbody/shaders/orthographic.frag').then(function(pResponse) {
+ if (pResponse.status === 200) {
+ return pResponse.text().then(function(pShader) {
+ lFragment = pShader;
+ return lLoadShaders();
+ });
+ }
+ });
+ });
+ }
+ });
+} \ No newline at end of file