Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palini #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions compiled_fragment.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
precision mediump float;void main(){gl_FragColor = vec4(0., 1., 0., 1.);}
6 changes: 6 additions & 0 deletions fs-gator.lgl
Original file line number Diff line number Diff line change
@@ -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;
}
255 changes: 255 additions & 0 deletions glsl_defs.lgl
Original file line number Diff line number Diff line change
@@ -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<T> texture, float[2] coord);
with float[4] T:
declare T textureCube(samplerCube<T> 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 : vec3>(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<target>.vector *(transformation<target> m, vector v);

with frame() target:
this<target>.vector *(transformation<target> m, direction d);

with frame() target:
this<target>.point *(transformation<target> m, point p);

with frame() target:
transformation<target> +(transformation<target> m1, transformation<target> m2);

with frame() middle, target:
transformation<target> *(transformation<middle> m1, this<middle>.transformation<target> 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<target>.vector *(transformation<target> m, vector v) {
return (m as! mat3 * v as! vec3) as! this<target>.vector;
}
with frame(3) target:
this<target>.vector *(transformation<target> m, direction d) {
return (m as! mat3 * d as! vec3) as! this<target>.vector;
}
with frame(3) target:
this<target>.point *(transformation<target> m, point p) {
return (m as! mat3 * p as! vec3) as! this<target>.point;
}
with frame(3) target:
transformation<target> +(transformation<target> m1, transformation<target> m2) {
return (m1 as! mat3 + m2 as! mat3) as! transformation<target>;
}
with frame(3) middle, target:
transformation<target> *(transformation<middle> m1, this<middle>.transformation<target> m2) {
return (m1 as! mat3 * m2 as! mat3) as! transformation<target>;
}

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<target>.vector *(transformation<target> m, vector v) {
return (m as! mat4 * v as! vec4) as! this<target>.vector;
}
with frame(3) target:
this<target>.vector *(transformation<target> m, direction d) {
return (m as! mat4 * d as! vec4) as! this<target>.vector;
}
with frame(3) target:
this<target>.point *(transformation<target> m, point p) {
return (m as! mat4 * p as! vec4) as! this<target>.point;
}
with frame(3) target:
transformation<target> +(transformation<target> m1, transformation<target> m2) {
return (m1 as! mat4 + m2 as! mat4) as! transformation<target>;
}
with frame(3) middle, target:
transformation<target> *(transformation<middle> m1, this<middle>.transformation<target> m2) {
return (m1 as! mat4 * m2 as! mat4) as! transformation<target>;
}
}

// Transformation Functions
with frame(3) r:
canon hom<r>.point homify(cart3<r>.point v) {
return vec4(v, 1.) as! hom<r>.point;
}
with frame(3) r:
canon hom<r>.vector homify(cart3<r>.vector v) {
return vec4(v, 0.) as! hom<r>.vector;
}
with frame(3) r:
canon cart3<r>.point hom_reduce(hom<r>.point v) {
return (vec3(v) / v[3]) as! cart3<r>.point;
}
with frame(3) r:
canon cart3<r>.vector hom_reduce(hom<r>.vector v) {
return vec3(v) as! cart3<r>.vector;
}
26 changes: 0 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
<head>
<title>webgl-obj-loader dev</title>
<meta charset="UTF-8">
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
varying vec3 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
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;
}
</script>
<!-- <script type="text/javascript" src="gl-matrix.js"></script> -->

</head>
Expand Down
15 changes: 0 additions & 15 deletions node_modules/.bin/r.js

This file was deleted.

1 change: 1 addition & 0 deletions node_modules/.bin/r.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading