Changeset b545cad for libcfa


Ignore:
Timestamp:
Nov 11, 2019, 2:41:41 PM (4 years ago)
Author:
Dmitry Kobets <dkobets@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
a39fd1d
Parents:
44f41997
git-author:
Dmitry Kobets <dkobets@…> (10/04/19 18:51:57)
git-committer:
Dmitry Kobets <dkobets@…> (11/11/19 14:41:41)
Message:

Added refraction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/vector.hfa

    r44f41997 rb545cad  
    11#pragma once
    2 #include "math.hfa"
     2#include <math.hfa>
    33#include <iostream.hfa>
    44
    55//---------------------- Vector Types ----------------------
    66// TODO: make generic, as per glm
    7 
    87
    98struct vec2 {
     
    2120        return os;
    2221    }
    23     void ?|?( ostype & os, const vec2 v ) {
     22    void ?|?( ostype & os, const vec2& v ) {
    2423        (ostype &)(os | v); ends( os );
    2524    }
     25}
     26
     27void ?{}(vec2& vec, zero_t) with (vec) {
     28    x = y = 0;
     29}
     30void ?{}(vec2& vec, vec2& other) with (vec) {
     31    [x,y] = other.[x,y];
    2632}
    2733
     
    3137vec2 ?*?(const vec2& v, float scalar) with (v) {
    3238    return [x * scalar, y * scalar];
     39}
     40vec2 ?*?(float scalar, const vec2& v) {
     41    return v * scalar;
    3342}
    3443vec2 ?/?(const vec2& v, float scalar) with (v) {
     
    5867    return v / sqrt(dot(v, v));
    5968}
     69
     70// project vector u onto vector v
     71static inline vec2 project(const vec2& u, const vec2& v) {
     72    vec2 v_norm = normalize(v);
     73    return v_norm * dot(u, v_norm);
     74}
     75
     76/* returns the reflection direction : v - 2.0 * project(v, n)
     77 * for incident vector v and surface normal n
     78 */
     79static inline vec2 reflect(const vec2& v, const vec2& n) {
     80    return v - 2 * project(v, n);
     81}
     82
     83// incident vector v, surface normal n
     84// eta = ratio of indices of refraction between starting material and
     85// entering material (i.e., from air to water, eta = 1/1.33)
     86static inline vec2 refract(const vec2& v, const vec2& n, float eta) {
     87    float dotValue = dot(n, v);
     88    float k = 1 - eta * eta * (1 - dotValue * dotValue);
     89    if (k < 0) {
     90        return 0;
     91    }
     92    return eta * v - (eta * dotValue + sqrt(k)) * n;
     93}
Note: See TracChangeset for help on using the changeset viewer.