Changeset e1a0945


Ignore:
Timestamp:
Nov 11, 2019, 2:41:41 PM (2 years ago)
Author:
Dmitry Kobets <dkobets@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
af0bf71
Parents:
a39fd1d
git-author:
Dmitry Kobets <dkobets@…> (10/11/19 15:13:21)
git-committer:
Dmitry Kobets <dkobets@…> (11/11/19 14:41:41)
Message:

Implement faceforward and some primitive operations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/vector.hfa

    ra39fd1d re1a0945  
    22#include <math.hfa>
    33#include <iostream.hfa>
    4 
    5 //---------------------- Vector Types ----------------------
    6 // TODO: make generic, as per glm
    74
    85struct vec2 {
     
    107};
    118
     9// Constructors
     10
    1211void ?{}( vec2 & v, float x, float y) {
    1312    v.[x, y] = [x, y];
    1413}
     14void ?{}(vec2& vec, zero_t) with (vec) {
     15    x = y = 0;
     16}
     17void ?{}(vec2& vec, float val) with (vec) {
     18    x = y = val;
     19}
     20void ?{}(vec2& vec, vec2& other) with (vec) {
     21    [x,y] = other.[x,y];
     22}
    1523
     24// Assignment
     25void ?=?(vec2& vec, const vec2& other) with (vec) {
     26    [x,y] = other.[x,y];
     27}
     28
     29// Primitive mathematical operations
     30
     31// Subtraction
     32vec2 ?-?(const vec2& u, const vec2& v) {
     33    return [u.x - v.x, u.y - v.y];
     34}
     35vec2& ?-=?(vec2& u, const vec2& v) {
     36    u = u - v;
     37    return u;
     38}
     39vec2 -?(const vec2& v) with (v) {
     40    return [-x, -y];
     41}
     42
     43// Addition
     44vec2 ?+?(const vec2& u, const vec2& v) {
     45    return [u.x + v.x, u.y + v.y];
     46}
     47vec2& ?+=?(vec2& u, const vec2& v) {
     48    u = u + v;
     49    return u;
     50}
     51
     52// Scalar Multiplication
     53vec2 ?*?(const vec2& v, float scalar) with (v) {
     54    return [x * scalar, y * scalar];
     55}
     56vec2& ?*=?(vec2& v, float scalar) with (v) {
     57    v = v * scalar;
     58    return v;
     59}
     60vec2 ?*?(float scalar, const vec2& v) {
     61    return v * scalar;
     62}
     63
     64
     65// Scalar Division
     66vec2 ?/?(const vec2& v, float scalar) with (v) {
     67    return [x / scalar, y / scalar];
     68}
     69vec2& ?/=?(vec2& v, float scalar) with (v) {
     70    v = v / scalar;
     71    return v;
     72}
     73
     74// Relational Operators
     75bool ?==?(const vec2& u, const vec2& v) with (u) {
     76    return x == v.x && y == v.y;
     77}
     78bool ?!=?(const vec2& u, const vec2& v) {
     79    return !(u == v);
     80}
     81
     82// Printing the vector (ostream)
    1683forall( dtype ostype | ostream( ostype ) ) {
    1784    ostype & ?|?( ostype & os, const vec2& v) with (v) {
     
    2592}
    2693
    27 void ?{}(vec2& vec, zero_t) with (vec) {
    28     x = y = 0;
    29 }
    30 void ?{}(vec2& vec, vec2& other) with (vec) {
    31     [x,y] = other.[x,y];
    32 }
    33 
    34 vec2 ?-?(const vec2& u, const vec2& v) {
    35     return [u.x - v.x, u.y - v.y];
    36 }
    37 vec2 ?*?(const vec2& v, float scalar) with (v) {
    38     return [x * scalar, y * scalar];
    39 }
    40 vec2 ?*?(float scalar, const vec2& v) {
    41     return v * scalar;
    42 }
    43 vec2 ?/?(const vec2& v, float scalar) with (v) {
    44     return [x / scalar, y / scalar];
    45 }
    46 vec2 -?(const vec2& v) with (v) {
    47     return [-x, -y];
    48 }
    49 
    5094/* //---------------------- Geometric Functions ---------------------- */
    51 /* // These functions implement the Geometric Functions section of GLSL */
     95/* // These functions implement the Geometric Functions section of GLSL for 2D vectors*/
    5296
    5397static inline float dot(const vec2& u, const vec2& v) {
     
    59103}
    60104
    61 // Returns the distance betwwen v1 and v2, i.e., length(p0 - p1).
    62105static inline float distance(const vec2& v1, const vec2& v2) {
    63106    return length(v1 - v2);
     
    65108
    66109static inline vec2 normalize(const vec2& v) {
    67     // TODO(dkobets) -- show them inversesqrt
    68     // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl
    69     /* return v * inversesqrt(dot(v, v)); */
    70110    return v / sqrt(dot(v, v));
    71111}
    72112
    73 // project vector u onto vector v
     113// Project vector u onto vector v
    74114static inline vec2 project(const vec2& u, const vec2& v) {
    75115    vec2 v_norm = normalize(v);
     
    77117}
    78118
    79 /* returns the reflection direction : v - 2.0 * project(v, n)
    80  * for incident vector v and surface normal n
    81  */
     119// Reflect incident vector v with respect to surface with normal n
    82120static inline vec2 reflect(const vec2& v, const vec2& n) {
    83121    return v - 2 * project(v, n);
    84122}
    85123
    86 // incident vector v, surface normal n
    87 // eta = ratio of indices of refraction between starting material and
     124// Refract incident vector v with respect to surface with normal n
     125// eta is the ratio of indices of refraction between starting material and
    88126// entering material (i.e., from air to water, eta = 1/1.33)
    89127static inline vec2 refract(const vec2& v, const vec2& n, float eta) {
     
    96134}
    97135
    98 // TODO: I don't quite understand the use-case for faceforward
     136// Used to render perturbed surfaces by ensuring that a perturbed normal
     137// is pointing in the same direction as the geometric normal of the
     138// surface.
     139// n is the perturbed vector that we want to align
     140// i is the incident vector
     141// ng is the geometric normal of the surface
     142static inline vec2 faceforward(const vec2& n, const vec2& i, float ng) {
     143    return dot(n, i) < 0 ? ng : -ng;
     144}
Note: See TracChangeset for help on using the changeset viewer.