diff --git a/app.js b/app.js index 25270a0..64245b9 100644 --- a/app.js +++ b/app.js @@ -57,49 +57,43 @@ function initWebGL(canvas){ return gl; } + function getShader(gl, id){ // Refers to external HTML - var shaderScript = document.getElementById(id); + var shaderScript; + if (id === "shader-fs") + shaderScript = require("./fs-gator.lgl"); + else + shaderScript = require("./vs-gator.lgl"); + console.log(shaderScript); if (!shaderScript){ return null; } - var str = ""; - var k = shaderScript.firstChild; - while (k){ - if (k.nodeType == 3){ - str += k.textContent; - } - k = k.nextSibling; - } - var shader; - if (shaderScript.type == "x-shader/x-fragment"){ + if (id === "shader-fs"){ shader = gl.createShader(gl.FRAGMENT_SHADER); - } else if (shaderScript.type == "x-shader/x-vertex"){ + } else { shader = gl.createShader(gl.VERTEX_SHADER); - } else{ - return null; } - gl.shaderSource(shader, str); + gl.shaderSource(shader, shaderScript); gl.compileShader(shader); - - if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){ - alert(gl.getShaderInfoLog(shader)); - return null; + throw "could not compile shader" + gl.getShaderInfoLog(shader); } return shader; } function initShaders(){ + var fragmentShader = getShader(gl, "shader-fs"); var vertexShader = getShader(gl, "shader-vs"); shaderProgram = gl.createProgram(); + console.log(fragmentShader) gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); @@ -129,7 +123,7 @@ function drawObject(obj){ Assumes that the setMatrixUniforms function exists as well as the shaderProgram has a uniform attribute called "samplerUniform" */ -// gl.useProgram(shaderProgram); + //gl.useProgram(shaderProgram); gl.bindBuffer(gl.ARRAY_BUFFER, obj.mesh.vertexBuffer); gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, obj.mesh.vertexBuffer.itemSize, gl.FLOAT, false, 0, 0); diff --git a/compiled_fragment.frag b/compiled_fragment.frag new file mode 100644 index 0000000..c1ebeb8 --- /dev/null +++ b/compiled_fragment.frag @@ -0,0 +1 @@ +precision mediump float;void main(){gl_FragColor = vec4(0., 1., 0., 1.);} \ No newline at end of file diff --git a/fs-gator.lgl b/fs-gator.lgl new file mode 100644 index 0000000..3c99625 --- /dev/null +++ b/fs-gator.lgl @@ -0,0 +1,6 @@ +// using "glsl_defs.lgl"; + +void main() { + float[4] color = [0.0, 0.0, 0.0, 1.0]; + float[4] gl_FragColor = color; +} diff --git a/glsl_defs.lgl b/glsl_defs.lgl new file mode 100644 index 0000000..301f446 --- /dev/null +++ b/glsl_defs.lgl @@ -0,0 +1,255 @@ +// GLSL Type Declarations +declare type vec2 is float[2]; +declare type vec3 is float[3]; +declare type vec4 is float[4]; +declare type mat2 is float[2][2]; +declare type mat3 is float[3][3]; +declare type mat4 is float[4][4]; +with float[4] T: +declare type sampler2D; +with float[4] T: +declare type samplerCube; + +type scalar is float; +type angle is scalar; + +// Numeric Operator Types +with float T: declare T +(T f1, T f2); +with float T: declare T -(T f1, T f2); +with float T: declare T *(T f1, T f2); +with float T: declare T /(T f1, T f2); +with float T: declare bool ==(T f1, T f2); +with float T: declare bool >=(T f1, T f2); +with float T: declare bool <=(T f1, T f2); +with float T: declare bool >(T f1, T f2); +with float T: declare bool <(T f1, T f2); + +with int T: declare T +(T f1, T f2); +with int T: declare T -(T f1, T f2); +with int T: declare T *(T f1, T f2); +with int T: declare T /(T f1, T f2); +with int T: declare bool ==(T f1, T f2); +with int T: declare bool >=(T f1, T f2); +with int T: declare bool <=(T f1, T f2); +with int T: declare bool >(T f1, T f2); +with int T: declare bool <(T f1, T f2); + +// GLSL Vector/Matrix Types +with vec3 T: declare T +(T v1, T v2); +with vec3 T: declare T -(T v1, T v2); +with vec3 T: declare T -(T v); +with vec3 T: with float U: declare T *(T v, U f); +with vec3 T: with float U: declare T *(U f, T v); +with vec3 T: with float U: declare T /(T f1, U f2); + +with vec4 T: declare T +(T v1, T v2); +with vec4 T: declare T -(T v1, T v2); +with vec4 T: declare T -(T v); +with vec4 T: with float U: declare T *(T v, U f); +with vec4 T: with float U: declare T *(U f, T v); + +with float T: declare T +(T f1, T f2); +with float T: declare T -(T f1, T f2); +with float T: declare T -(T f); +with float T: declare T *(T f1, T f2); +with float T: declare T /(T f1, T f2); + +with mat3 T: with vec3 U: declare vec3 *(T m, U v); +with mat3 T: with mat3 U: declare mat3 +(T m, U v); +with mat3 T: with mat3 U: declare mat3 *(T m, U v); + +with mat4 T: with vec4 U: declare vec4 *(T m, U v); +with mat4 T: with mat4 U: declare mat4 +(T m, U v); +with mat4 T: with mat4 U: declare mat4 *(T m, U v); + + +// GLSL function types +declare float cos(float f); +declare float sqrt(float f); +declare float acos(float f); + +with float[4] T: +declare T texture2D(sampler2D texture, float[2] coord); +with float[4] T: +declare T textureCube(samplerCube texture, float[2] coord); + +with float[3] T: with float U: declare vec4 vec4(T v, U f); +with float[4] T: declare vec3 vec3(T v); +with float[4][4] T: declare mat3 mat3(T v); +with float[3][3] T: declare mat4 mat4(T v); + +with vec3 T: declare float dot(T v1, T v2); +// declare T normalize(T x); +declare vec3 normalize(vec3 x); +with float T: declare T max(T f1, T f2); +with vec3 T: declare T reflect(T v1, T v2); +with float T: declare T pow(T f1, T f2); + +// Geometric Objects and Operations + +prototype geometry { + object point; + object vector; + object direction; + with frame() r: object transformation; + + vector +(vector x, vector y); + vector -(vector x, vector y); + vector -(vector x); + direction -(direction x); + vector *(vector v, scalar s); + vector *(scalar s, vector v); + point +(point p, vector v); + point +(vector p, point v); + vector -(point x, point y); + vector -(point x); + with frame() target: + this.vector *(transformation m, vector v); + + with frame() target: + this.vector *(transformation m, direction d); + + with frame() target: + this.point *(transformation m, point p); + + with frame() target: + transformation +(transformation m1, transformation m2); + + with frame() middle, target: + transformation *(transformation m1, this.transformation m2); + + angle dot(direction v1, direction v2); + direction normalize(vector v); + direction normalize(direction v); + vector reflect(direction v1, direction v2); +} + +// Coordinate Scheme Definitions + +with frame(3) r: +coordinate cart3 : geometry { + object point is float[3]; + object vector is float[3]; + object direction is float[3]; + with frame(3) r2: object transformation is float[3][3]; + + vector +(vector x, vector y) { + return (x as! vec3 + y as! vec3) as! vector; + } + vector -(vector x, vector y) { + return (x as! vec3 - y as! vec3) as! vector; + } + vector *(vector v, scalar s) { + return (v as! vec3 * s) as! vector; + } + vector *(scalar s, vector v) { + return (s * v as! vec3) as! vector; + } + vector -(vector v) { + return (-v as! vec3) as! vector; + } + direction -(direction v) { + return (-v as! vec3) as! direction; + } + point +(point p, vector v) { + return (p as! vec3 + v as! vec3) as! point; + } + point +(vector v, point p) { + return (p as! vec3 + v as! vec3) as! point; + } + vector -(point x, point y) { + return (x as! vec3 - y as! vec3) as! vector; + } + vector -(point v) { + return (-v as! vec3) as! vector; + } + with frame(3) target: + this.vector *(transformation m, vector v) { + return (m as! mat3 * v as! vec3) as! this.vector; + } + with frame(3) target: + this.vector *(transformation m, direction d) { + return (m as! mat3 * d as! vec3) as! this.vector; + } + with frame(3) target: + this.point *(transformation m, point p) { + return (m as! mat3 * p as! vec3) as! this.point; + } + with frame(3) target: + transformation +(transformation m1, transformation m2) { + return (m1 as! mat3 + m2 as! mat3) as! transformation; + } + with frame(3) middle, target: + transformation *(transformation m1, this.transformation m2) { + return (m1 as! mat3 * m2 as! mat3) as! transformation; + } + + angle dot(direction v1, direction v2) { + return (dot(v1 as! vec3, v2 as! vec3) as! angle); + } + direction normalize(vector v) { + return (normalize(v as! vec3) as! direction); + } + direction normalize(direction v) { + return v; + } + vector reflect(direction v1, direction v2) { + return (reflect(v1 as! vec3, v2 as! vec3) as! vector); + } +} + +with frame(3) r: +coordinate hom : geometry { + object point is float[4]; + object vector is float[4]; + object direction is float[4]; + with frame(3) r2: object transformation is float[4][4]; + + point +(point p, vector v) { + return (p as! vec4 + v as! vec4 * p[3]) as! point; + } + point +(vector v, point p) { + return (p as! vec4 + v as! vec4 * p[3]) as! point; + } + vector -(point x, point y) { + return (x as! vec4 * y[3] - y as! vec4 * x[3]) as! vector; + } + with frame(3) target: + this.vector *(transformation m, vector v) { + return (m as! mat4 * v as! vec4) as! this.vector; + } + with frame(3) target: + this.vector *(transformation m, direction d) { + return (m as! mat4 * d as! vec4) as! this.vector; + } + with frame(3) target: + this.point *(transformation m, point p) { + return (m as! mat4 * p as! vec4) as! this.point; + } + with frame(3) target: + transformation +(transformation m1, transformation m2) { + return (m1 as! mat4 + m2 as! mat4) as! transformation; + } + with frame(3) middle, target: + transformation *(transformation m1, this.transformation m2) { + return (m1 as! mat4 * m2 as! mat4) as! transformation; + } +} + +// Transformation Functions +with frame(3) r: +canon hom.point homify(cart3.point v) { + return vec4(v, 1.) as! hom.point; +} +with frame(3) r: +canon hom.vector homify(cart3.vector v) { + return vec4(v, 0.) as! hom.vector; +} +with frame(3) r: +canon cart3.point hom_reduce(hom.point v) { + return (vec3(v) / v[3]) as! cart3.point; +} +with frame(3) r: +canon cart3.vector hom_reduce(hom.vector v) { + return vec3(v) as! cart3.vector; +} \ No newline at end of file diff --git a/index.html b/index.html index 4b39785..9e4de8c 100644 --- a/index.html +++ b/index.html @@ -3,32 +3,6 @@ webgl-obj-loader dev - - diff --git a/node_modules/.bin/r.js b/node_modules/.bin/r.js deleted file mode 100755 index 7269d27..0000000 --- a/node_modules/.bin/r.js +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") - -case `uname` in - *CYGWIN*) basedir=`cygpath -w "$basedir"`;; -esac - -if [ -x "$basedir/node" ]; then - "$basedir/node" "$basedir/../requirejs/bin/r.js" "$@" - ret=$? -else - node "$basedir/../requirejs/bin/r.js" "$@" - ret=$? -fi -exit $ret diff --git a/node_modules/.bin/r.js b/node_modules/.bin/r.js new file mode 120000 index 0000000..2075b60 --- /dev/null +++ b/node_modules/.bin/r.js @@ -0,0 +1 @@ +../requirejs/bin/r.js \ No newline at end of file diff --git a/node_modules/.bin/r_js b/node_modules/.bin/r_js deleted file mode 100755 index 7269d27..0000000 --- a/node_modules/.bin/r_js +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") - -case `uname` in - *CYGWIN*) basedir=`cygpath -w "$basedir"`;; -esac - -if [ -x "$basedir/node" ]; then - "$basedir/node" "$basedir/../requirejs/bin/r.js" "$@" - ret=$? -else - node "$basedir/../requirejs/bin/r.js" "$@" - ret=$? -fi -exit $ret diff --git a/node_modules/.bin/r_js b/node_modules/.bin/r_js new file mode 120000 index 0000000..2075b60 --- /dev/null +++ b/node_modules/.bin/r_js @@ -0,0 +1 @@ +../requirejs/bin/r.js \ No newline at end of file diff --git a/package.json b/package.json index 552fb0a..066ab95 100755 --- a/package.json +++ b/package.json @@ -13,7 +13,12 @@ "teapot": "^1.0.0", "webgl-obj-loader": "^1.1.3" }, - "devDependencies": {}, + "devDependencies": { + "glslify-bundle": "^5.1.1", + "glslify-deps": "^1.3.1", + "parcel-plugin-lgl": "file:./parcel-plugin-lgl", + "parcel-bundler": "^1.9.7" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/vertex_orig.vert b/vertex_orig.vert new file mode 100644 index 0000000..0b584f9 --- /dev/null +++ b/vertex_orig.vert @@ -0,0 +1,15 @@ +attribute vec3 aVertexPosition; +attribute vec3 aVertexNormal; +attribute vec2 aTextureCoord; +uniform mat4 uMVMatrix; +uniform mat4 uPMatrix; +uniform mat3 uNMatrix; +varying vec2 vTextureCoord; +varying vec3 vTransformedNormal; +varying vec4 vPosition; +void main(void) { + vPosition = uMVMatrix * vec4(aVertexPosition, 1.0); + gl_Position = uPMatrix * vPosition; + vTextureCoord = aTextureCoord; + vTransformedNormal = uNMatrix * aVertexNormal; +} \ No newline at end of file diff --git a/vs-gator.lgl b/vs-gator.lgl new file mode 100644 index 0000000..e922997 --- /dev/null +++ b/vs-gator.lgl @@ -0,0 +1,20 @@ +using "glsl_defs.lgl"; + +attribute vec3 aVertexPosition; +attribute vec3 aVertexNormal; +attribute vec2 aTextureCoord; +uniform mat4 uMVMatrix; +uniform mat4 uPMatrix; +uniform mat3 uNMatrix; +varying vec2 vTextureCoord; +varying vec3 vTransformedNormal; +varying vec4 vPosition; + +void main() { + vPosition = uMVMatrix * vec4(aVertexPosition, 1.0); + auto gl_Position = uPMatrix * vPosition; + vTextureCoord = aTextureCoord; + vTransformedNormal = uNMatrix * aVertexNormal; +} + +