Skip to content

Commit

Permalink
Merged with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Checkmate50 committed Nov 9, 2018
2 parents 7887920 + 500fc73 commit 20a933a
Show file tree
Hide file tree
Showing 20 changed files with 199,018 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -7,6 +7,8 @@ lingl
This is a language with a type system that enforces the correctness of linear algebra operations.
There is a compiler, pretty printer, and interpreter.

OCaml 4.04 or higher required


Set Up
------
Expand Down
20 changes: 11 additions & 9 deletions bin/lingc.ml
Expand Up @@ -27,15 +27,17 @@ let _ =
let lexbuf = Lexing.from_channel ch in
try
Parser.main Lexer.read lexbuf
with _ ->
begin
close_in ch;
let pos = lexbuf.Lexing.lex_curr_p in
let tok = (Lexing.lexeme lexbuf) in
(* let line = pos.Lexing.pos_lnum in *)
let cnum = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
failwith ("Parsing error at character " ^ tok ^ ", character " ^ string_of_int cnum)
end in
with
| _ ->
begin
close_in ch;
let pos = lexbuf.Lexing.lex_curr_p in
let tok = (Lexing.lexeme lexbuf) in
(* let line = pos.Lexing.pos_lnum in *)
let cnum = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
failwith ("Parsing error at token '" ^ tok ^ "', line "
^ (string_of_int pos.Lexing.pos_lnum) ^ ", column " ^ string_of_int cnum)
end in
close_in ch;
let (typedProg, params) = Check.check_prog prog in
if !run_interp then Ops.eval_prog typedProg
Expand Down
74 changes: 74 additions & 0 deletions examples/bump/fragment.lgl
@@ -0,0 +1,74 @@
tag model is vec3;
tag modelHom is vec4;
tag world is vec3;
tag worldHom is vec4;
tag camera is vec3;
tag cameraHom is vec4;
tag color is vec3;
tag alphaColor is vec4;

declare bool gl_FrontFacing;
declare float abs(float x);
declare float exp(float x);
declare float sqrt(float x);
declare float max(float v1, float v2);
declare float length<`t : vec>(`t x);
declare float dot<`t: genType>(`t v1, `t v2);
declare `t normalize<`t : genType>(`t x);
declare `t pow<`t : genType>(`t v1, `t v2);
declare vec4 vec4<`t : genType>(`t v, float x);
declare `t cross<`t : genType>(`t x1, `t x2);

declare vec4 texture2D<`t : vec3>(sampler2D<`t> txt, vec2 texCoord);

vec3 to_sRGB(vec3 c) { return pow<vec3>(c, [1.0/2.2, 1.0/2.2, 1.0/2.2]); }
vec3 from_sRGB(vec3 c) { return pow<vec3>(c, [2.2, 2.2, 2.2]); }

void main(sampler2D<vec3> uDiffuseTexture, sampler2D<vec3> uDisplacementMap, vec2 vTexCoord, vec2 vUv, vec3 vNormal, vec4 vPosition, vec3->vec3 vNormalMatrix, vec3 vDerivU, vec3 vDerivV) {
float displacementScale = -4.;
// Compute displaced normals as in the
// displacement vertex shader, and use them for shading.
// Lastly, implement Phong reflectance function.
float delta = 0.001;
// Approximate the partial derivatives of a texture
vec2 d_u = [delta, 0];
vec2 d_v = [0, delta];
vec4 dh_du = (texture2D<vec4>(uDisplacementMap, vUv - d_u) - texture2D<vec4>(uDisplacementMap, vUv + d_u)) / (2.0 * delta);
vec4 dh_dv = (texture2D<vec4>(uDisplacementMap, vUv - d_v) - texture2D<vec4>(uDisplacementMap, vUv + d_v)) / (2.0 * delta);

// Calculate derivatives of the surface
vec4 t_u = vec4<vec3>(vDerivU, 0.) + uDisplacementScale * dh_du .* vec4<vec3>(vNormal, 0.);
vec4 t_v = vec4<vec3>(vDerivV, 0.) + uDisplacementScale * dh_dv .* vec4<vec3>(vNormal, 0.);

// Calculate displaced normal
vec3 dispNorm = cross<vec3>(t_u.xyz, t_v.xyz);
dispNorm = normalize<vec3>((vNormalMatrix * dispNorm).xyz);
// interpolating normals will change the length of the normal, so renormalize the normal.
vec3 N = normalize<vec3>(dispNorm);
vec3 V = normalize<vec3>(-vPosition.xyz);

float roughness = 0.2;
vec3 finalColor = [0.0, 0.0, 0.0];
vec3 lightPosition = [10., 20., 30.];
vec3 lightColor = [0., 0.3, 0.5];
for (int i = 0; i <= 1; i++) {
float r = length<vec3>(lightPosition - vPosition.xyz);
vec3 L = normalize<vec3>(lightPosition - vPosition.xyz);
vec3 H = normalize<vec3>(L + V);
// calculate diffuse term
// vec3 Idiff = from_sRGB(texture2D<vec3>(uDiffuseTexture, vUv).xyz) * max(dot(N, L), 0.0);
// calculate specular term
vec3 Ispec = [1.0, 1.0, 1.0] * pow<float>(max(dot<vec3>(N, H), 0.0), 1.0 / roughness);
//finalColor = finalColor + lightColor * (Idiff + Ispec) / (r*r);
lightPosition = [-10., 0., -10.];
}

float exposure = 0.2;
// Only shade if facing the light
// Color the back faces an identifiable color
//if (gl_FrontFacing) {
vec4 gl_FragColor = vec4<vec3>(to_sRGB(finalColor * exposure), 1.0);
//} else {
// gl_FragColor = vec4(170.0/255.0, 160.0/255.0, 0.0, 1.0);
//}
}
14 changes: 14 additions & 0 deletions examples/bump/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Linguine</title>
</head>

<body>
<canvas width="700px" height="500px" id="c"></canvas>
<script src="main.ts"></script>
</body>

</html>
78 changes: 78 additions & 0 deletions examples/bump/main.ts
@@ -0,0 +1,78 @@
import * as lgl from '../lglexample';
import { mat4 } from 'gl-matrix';

// Textures
const earthmap1k : string = require('../resources/textures/earthmap1k.jpg');
const earthbump1k : string = require('../resources/textures/earthbump1k.jpg');

// Loads file system implementation in parcel
// * can only call synchronous functions *
const fs : any = require('fs');

var __dirname : string;

function main() {
let gl = lgl.setup(render);

// Compile our shaders.
let program = lgl.compileProgram(gl,
require('./vertex.lgl'),
require('./fragment.lgl')
);

// Uniform and attribute locations.
let loc_uProjection = lgl.uniformLoc(gl, program, 'uProjection');
let loc_uView = lgl.uniformLoc(gl, program, 'uView');
let loc_uModel = lgl.uniformLoc(gl, program, 'uModel');
let loc_aPosition = lgl.attribLoc(gl, program, 'aPosition');
let loc_uNormal = lgl.attribLoc(gl, location, 'uNormal');


// Texture things
let loc_aTexCoord = lgl.attribLoc(gl, program, 'aTexCoord');
let loc_uTexture = lgl.uniformLoc(gl, program, 'uTexture');
let loc_aDerivU = lgl.uniformLoc(gl, program, 'aDerivU');
let loc_aDerivV = lgl.uniformLoc(gl, program, 'aDerivV');
let loc_aNormal = lgl.uniformLoc(gl, program, 'aNormal');
let loc_aUv = lgl.uniformLoc(gl, program, 'aUv');



// Read in lpshead obj
// URL must be statically analyzable other than (__dirname) and (__filename)
let src = fs.readFileSync(__dirname + './../resources/OBJ/sphere_highres.obj', 'utf8');

let mesh = lgl.load_obj (gl, src);

// Initialize the model position.
let model = mat4.create();

// Load image texture
lgl.load_texture(gl, earthmap1k);

function render(view: mat4, projection: mat4) {
// Rotate the model a little bit on each frame.
mat4.rotateY(model, model, .01);

// Use our shader pair.
gl.useProgram(program);

// Set the shader "uniform" parameters.
gl.uniformMatrix4fv(loc_uProjection, false, projection);
gl.uniformMatrix4fv(loc_uView, false, view);
gl.uniformMatrix4fv(loc_uModel, false, model);

// Use texture unit 0 for uTexture
gl.uniform1i(loc_uTexture, 0);

// Set the attribute arrays.
lgl.bind_attrib_buffer(gl, loc_aPosition, mesh.positions, 3);
lgl.bind_attrib_buffer(gl, loc_aTexCoord, mesh.texcoords, 2);
// lgl.bind_attrib_buffer(gl, loc_aDerivU, mesh.derivU, 2); // TODO

// Draw the object.
lgl.drawMesh(gl, mesh);
}
}

main();
18 changes: 18 additions & 0 deletions examples/bump/vertex.lgl
@@ -0,0 +1,18 @@
declare vec4 vec4<`t : genType>(`t v, float x);

void main(vec3 aPosition, vec3 aDerivU, vec3 aDerivV, vec3 aNormal, vec3->vec3 uNormal, vec2 aUv, vec4->vec4 uProjection, vec4->vec4 uView, vec4->vec4 uModel, vec2 aTexCoord, vec2 vTexCoord, vec2 vUv, vec3 vNormal, vec4 vPosition, vec3->vec3 vNormalMatrix, vec3 vDerivU, vec3 vDerivV) {
vec3->vec4 v3_v4 = [
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]
];

vec4 gl_Position = uProjection * uView * vec4<vec3>(aPosition, 1.);
vUv = aUv;
vNormal = aNormal;
vPosition = uView * vec4<vec3>(aPosition, 1.);
vNormalMatrix = uNormal;
vDerivU = aDerivU;
vDerivV = aDerivV;
}

0 comments on commit 20a933a

Please sign in to comment.