Demo entry 6624276

Box normal calculation

   

Submitted by John Novak on Jun 15, 2017 at 06:07
Language: Nimrod. Code size: 704 Bytes.

import glm

type
  AABB* = ref object
    vmin*, vmax*: Vec4[float]

type
  Geometry* = ref object of RootObj
    objectToWorld*: Mat4x4[float]
    worldToObject*: Mat4x4[float]

  Sphere* = ref object of Geometry
    r*: float

  Plane* = ref object of Geometry
    discard

  Box* = ref object of Geometry
    aabb*: AABB

method normal*(b: Box, hit: Vec4[float]): Vec4[float] =
  let
    c = (b.aabb.vmin + b.aabb.vmax) * 0.5
    p = hit - c
    d = (b.aabb.vmin - b.aabb.vmax) * 0.5
    bias = 1.000001

  result = vec(((p.x / abs(d.x) * bias).int).float,
               ((p.y / abs(d.y) * bias).int).float,
               ((p.z / abs(d.z) * bias).int).float).normalize

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).