Skip to content

Commit

Permalink
Merge pull request #125 from evanbenadler/oop
Browse files Browse the repository at this point in the history
Oop
  • Loading branch information
Checkmate50 authored Jan 6, 2021
2 parents c9f796e + 8699fa2 commit f506d64
Show file tree
Hide file tree
Showing 44 changed files with 1,187 additions and 120 deletions.
6 changes: 3 additions & 3 deletions bin/gatorc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ let _ =
(String.split_on_char '.'
(List.hd (List.rev (String.split_on_char '/' f)))) in
let fs, found = Check.search_prog prog [progname] in
let typedProg =
let cx, typedProg =
Check.check_prog prog (search_progs (prog_path f) fs found) in
if !run_interp then Ops.eval_prog typedProg
else if !emit_ts then print_string (EmitTS.compile_program typedProg)
else if !emit_hl then print_string (EmitHL.compile_program typedProg)
else if !pretty_printer then
let compiled_program = EmitGL.compile_program typedProg in
let compiled_program = EmitGL.compile_program cx typedProg in
let r = Str.regexp ";\\s*}?" in
let result = Str.global_replace r "\\0\n" compiled_program in
print_string result
else print_string (EmitGL.compile_program typedProg)
else print_string (EmitGL.compile_program cx typedProg)
14 changes: 9 additions & 5 deletions examples/microfacet/fragment.lgl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ uniform int mat_hasNormalTexture;
uniform sampler2D<alphaColor> mat_normalTexture;
uniform float mat_normalTextureScale;
uniform int anisotropic;
uniform cart3<view>.position light_eyePosition;
uniform attenuation light_attenuation;
uniform color light_color;

class light {
public cart3<view>.position eyePosition;
public attenuation attenuation;
public color color;
};
uniform light uLight;

canon cart3<normal_localframe>.halfdir proj_normlocalframe (cart3<view>.halfdir m, canon cart3<view>.normal n) {
auto nhat = normalize(n);
Expand Down Expand Up @@ -144,13 +148,13 @@ vec3 promote(float f) {

void main() {
alphaColor final_color = [0., 0., 0., 1.];
auto i = normalize(light_eyePosition - geom_position);
auto i = normalize(uLight.eyePosition - geom_position);
auto o = normalize(-geom_position);
auto m = get_halfdir(i, o);
auto n = getShadingNormal();
auto kd = getDiffuseColor();
final_color += alphatize(
(kd + promote(F(i,m) * D(m,n) * G(i,o,m,n) / (4.0 * abs (dot(i,n)) * abs(dot(o,n))))) * max(0., dot(n,i))
* I(light_color, light_eyePosition, geom_position, light_attenuation));
* I(uLight.color, uLight.eyePosition, geom_position, uLight.attenuation));
gl_FragColor = final_color;
}
6 changes: 3 additions & 3 deletions examples/microfacet/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function main() {
let loc_mat_normalTexture = lgl.uniformLoc(gl, program, 'mat_normalTexture');
let loc_mat_normalTextureScale = lgl.uniformLoc(gl, program, 'mat_normalTextureScale');
let loc_anisotropic = lgl.uniformLoc(gl, program, 'anisotropic');
let loc_light_eyePosition = lgl.uniformLoc(gl, program, 'light_eyePosition');
let loc_light_attenuation = lgl.uniformLoc(gl, program, 'light_attenuation');
let loc_light_color = lgl.uniformLoc(gl, program, 'light_color');
let loc_light_eyePosition = lgl.uniformLoc(gl, program, 'uLight.eyePosition');
let loc_light_attenuation = lgl.uniformLoc(gl, program, 'uLight.attenuation');
let loc_light_color = lgl.uniformLoc(gl, program, 'uLight.color');

// Read in lpshead obj
// URL must be statically analyzable other than (__dirname) and (__filename)
Expand Down
24 changes: 17 additions & 7 deletions examples/phong/fragment.lgl
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,36 @@ varying cart3<model>.point vPosition;
uniform hom<model>.transformation<world> uModel;
uniform hom<world>.transformation<camera> uView;
varying cart3<model>.vector vNormal;
uniform cart3<world>.point uLight;

class light {
public cart3<world>.point pos;
public color diffColor;
public color specColor;
};
uniform light uLight;

class positions {
public cart3<world>.point worldPos;
public positions init(cart3<world>.point p) {
self(p);
}
};

void main() {
// vec4 gl_FragColor = vec4(uLight, .9);
hom<model>.transformation<camera> uModelView = uView * uModel;
color ambient = [0.1, 0., 0.];
color diffColor = [0.4, 0.3, 0.8];
color specColor = [1.0, 1.0, 1.0];

auto worldPos = hom_reduce(uModel*homify(vPosition));
positions p = new_positions(hom_reduce(uModel*homify(vPosition)));
auto camPos = hom_reduce(uView*uModel*homify(vPosition));
auto worldNorm = normalize(hom_reduce(uModel*homify(vNormal)));

auto lightDir = normalize(uLight - worldPos);
auto lightDir = normalize(uLight.pos - p.worldPos);
auto lightWorldDot = dot(lightDir, worldNorm);
scalar diffuse = max(lightWorldDot, 0.);

auto reflectDir = normalize(hom_reduce(uView * homify(reflect(-lightDir, worldNorm))));

scalar specular = pow(max(dot(normalize(-camPos), reflectDir), 0.), 32.);

gl_FragColor = vec4(ambient + diffuse * diffColor + specular * specColor, 1.0);
gl_FragColor = vec4(ambient + diffuse * uLight.diffColor + specular * uLight.specColor, 1.0);
}
15 changes: 13 additions & 2 deletions examples/phong/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ function main() {
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_uLight = lgl.uniformLoc(gl, program, 'uLight');
//let loc_uLight = lgl.uniformLoc(gl, program, 'uLight');
let loc_aPosition = lgl.attribLoc(gl, program, 'aPosition');
let loc_aNormal = lgl.attribLoc(gl, program, 'aNormal');
let loc_uLight1Pos = lgl.uniformLoc(gl, program, 'uLight.pos');
let loc_uLight1DiffColor = lgl.uniformLoc(gl, program, 'uLight.diffColor');
let loc_uLight1SpecColor = lgl.uniformLoc(gl, program, 'uLight.specColor');

// We'll draw a gator
let mesh = lgl.getBunny(gl);
Expand All @@ -32,6 +35,12 @@ function main() {

// Position the light source for the lighting effect.
let light = vec3.fromValues(20., 0., 20.);
let diffColor = vec3.fromValues(1., 1., 0.);
let specColor = vec3.fromValues(1., 1., 1.);

console.log(loc_uLight1Pos);
console.log(loc_uLight1DiffColor);
console.log(loc_uLight1SpecColor);

function render(view: mat4, projection: mat4) {
// Rotate the model a little bit on each frame.
Expand All @@ -44,7 +53,9 @@ function main() {
gl.uniformMatrix4fv(loc_uProjection, false, projection);
gl.uniformMatrix4fv(loc_uView, false, view);
gl.uniformMatrix4fv(loc_uModel, false, model);
gl.uniform3fv(loc_uLight, light);
gl.uniform3fv(loc_uLight1Pos, light);
gl.uniform3fv(loc_uLight1DiffColor, diffColor);
gl.uniform3fv(loc_uLight1SpecColor, specColor);

// Set the attribute arrays.
lgl.bind_attrib_buffer(gl, loc_aNormal, mesh.normals, 3);
Expand Down
2 changes: 2 additions & 0 deletions src/assoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ let to_string_sep f sep c =
String.concat sep (List.map (fun (l, r) -> l ^ " : " ^ f r) c)

let to_string f c = to_string_sep f ", " c

let hd c = List.hd c
2 changes: 2 additions & 0 deletions src/assoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ val to_string_sep : ('a -> string) -> string -> 'a context -> string

(* String representation seperated by ", " *)
val to_string : ('a -> string) -> 'a context -> string

val hd : 'a context -> string * 'a
Loading

0 comments on commit f506d64

Please sign in to comment.