summaryrefslogtreecommitdiff
path: root/frontend/_shared/2d/shader.js
diff options
context:
space:
mode:
authorMatthew Kosarek <matthew.kosarek@vention.cc>2021-02-23 19:39:16 -0500
committerMatthew Kosarek <matthew.kosarek@vention.cc>2021-02-23 19:39:16 -0500
commitf0d1398b0d1b1a7c5bd1d4e0b3488b7f1aa74364 (patch)
tree4279b063052fb71ef5ff34a2f2f93d5c7b870b98 /frontend/_shared/2d/shader.js
parentece2b67aa689aee0b881bac17a62c16e0469bc56 (diff)
Big rework of the directory structure to make it more orderly for my brain
Diffstat (limited to 'frontend/_shared/2d/shader.js')
-rw-r--r--frontend/_shared/2d/shader.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/frontend/_shared/2d/shader.js b/frontend/_shared/2d/shader.js
new file mode 100644
index 0000000..4deddb5
--- /dev/null
+++ b/frontend/_shared/2d/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('/_shared/2d/shaders/orthographic.vert').then(function(pResponse) {
+ if (pResponse.status === 200) {
+ return pResponse.text().then(function(pShader) {
+ lVertex = pShader;
+
+ return fetch('/_shared/2d/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