/*
Copyright (c) 2014, Shodruky Rhyammer
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

  Neither the name of the copyright holders nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdint.h>

#define MAXREF 3
#define OBJNUM 6

#define EPSILON 0.0001f
#define FARDIST 1.0e+18f
#define NOHIT -1


typedef struct
{
  float x;
  float y;
  float z;
} vec3_t;

typedef struct
{
  vec3_t pos;
  vec3_t dir;
} ray_t;

typedef struct {
    float dist;
    int hit;
} target_t;

typedef struct
{
  vec3_t pos;
  vec3_t spd;
  vec3_t col;
  vec3_t poseye;
  float rv;
  float radsq;
  bool shadow;
  bool reflect;
} obj_t;

typedef struct {
  obj_t obj[OBJNUM];
  obj_t light;
  vec3_t eye;

  float swidth;
  float sheight;
  float ax;
  float ay;
  float ayc;

  int framew;
  int frameh;
  int stride;

  int current_time;
  int next_col;
  int next_ccol;
  int next_col_time;
  int next_ccol_time;
  int curobj;
  int curcol;
} trace_global;

static inline float max(float a, float b)
{
  return (a > b) ? a : b;
}

static inline float min(float a, float b)
{
  return (a < b) ? a : b;
}

static inline float fast_inv_sqrt(float a)
{
  union fi32_u
  {
    float f;
    int32_t i;
  };
  union fi32_u x;
  x.f = a;
  x.i = 0x5f3759df - (x.i >> 1);
  x.f = x.f * (1.5f - x.f * x.f * 0.5f * a);
  x.f = x.f * (1.5f - x.f * x.f * 0.5f * a);
  return x.f;
}

static inline float fast_sqrt(float a)
{
  union fu32_u
  {
    float f;
    uint32_t u;
  };
  union fu32_u x;
  x.f = a;
  x.u += 0x7f << 23;
  x.u >>= 1;
  return x.f;
}

static inline float fast_inv(float a)
{
  union fu32_u
  {
    float f;
    uint32_t u;
  };
  union fu32_u x;
  x.f = a;
  x.u = 0x7eeeeeee - x.u;
  x.f = x.f * (2.0f - a * x.f);
  return x.f;
}

static inline vec3_t vec3_set(float x, float y, float z)
{
  vec3_t r;
  r.x = x;
  r.y = y;
  r.z = z;
  return r;
}

static inline vec3_t vec3_add(vec3_t a, vec3_t b)
{
  vec3_t r;
  r.x = a.x + b.x;
  r.y = a.y + b.y;
  r.z = a.z + b.z;
  return r;
}

static inline vec3_t vec3_sub(vec3_t a, vec3_t b)
{
  vec3_t r;
  r.x = a.x - b.x;
  r.y = a.y - b.y;
  r.z = a.z - b.z;
  return r;
}

static inline vec3_t vec3_scale(vec3_t a, float b)
{
  vec3_t r;
  r.x = a.x * b;
  r.y = a.y * b;
  r.z = a.z * b;
  return r;
}

static inline float dot(vec3_t a, vec3_t b)
{
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

static inline float dotsq(vec3_t a, vec3_t b)
{
  float f = a.x * b.x + a.y * b.y + a.z * b.z;
  return f * f;
}

static inline float norm2(vec3_t a)
{
  return a.x * a.x + a.y * a.y + a.z * a.z;
}

static inline float norm(vec3_t a)
{
  return fast_sqrt(norm2(a));
}

static inline vec3_t normalize(vec3_t a)
{
  vec3_t r;
  float b = fast_inv_sqrt(norm2(a));
  r = vec3_scale( a, b );
  return r;
}

static inline float power_spec(float a)
{
  float b = a * a;
  float c = b * b;
  return c * c;
}
