/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2022                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#ifndef POWERSCALING_VS_H_HGLSL
#define POWERSCALING_VS_H_HGLSL

uniform vec4 campos;
uniform mat4 camrot;
uniform vec2 scaling;
uniform vec4 objpos;

#include "powerScalingMath.hglsl"

vec4 psc_to_meter(vec4 v1, vec2 v2) {
  float factor = v2.x * pow(k,v2.y + v1.w);
  return vec4(v1.xyz * factor, 1.0);
}

// vertexPosition is returned as the transformed vertex in OS Camera Rig Space in PSC
vec4 pscTransform(inout vec4 vertexPosition, mat4 modelTransform) {
  vec3 local_vertex_pos = mat3(modelTransform) * vertexPosition.xyz;
    
  // PSC addition; local vertex position and the object power scaled world position
  vertexPosition = psc_addition(vec4(local_vertex_pos,vertexPosition.w),objpos);
    
  // PSC addition; rotated and viewscaled vertex and the cameras negative position
  vertexPosition = psc_addition(vertexPosition,vec4(-campos.xyz,campos.w));
    
  // rotate the camera
  vertexPosition.xyz =  mat3(camrot) * vertexPosition.xyz;
  vec4 tmp = vertexPosition;

  float ds = scaling.y - vertexPosition.w;
  if (ds >= 0) {
    vertexPosition = vec4(vertexPosition.xyz * scaling.x * pow(k, vertexPosition.w), scaling.y);
  }
  else {
    vertexPosition = vec4(vertexPosition.xyz * scaling.x * pow(k, scaling.y), vertexPosition.w);
  }
    
  // project using the rescaled coordinates,
  tmp = psc_to_meter(tmp, scaling);
    
  // Return the vertex tranformed to OS Camera Rig Space in meters
  return tmp;
}

#endif
