summaryrefslogtreecommitdiff
path: root/hw6/src/environmentMap.cpp
blob: 896d78e9f0cd36f8d8cb2f09484765ffa5d2fc70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#include <cmath>
#include "constants.h"
#include "environmentMap.h"


//////////////////
// Constructors //
//////////////////
environmentMap::environmentMap(const std::shared_ptr<const texture_base>& map, const transformation3d& transform)
{
  _map = map;
  _transform = transform;
}


environmentMap::environmentMap(const environmentMap& src)
{
  _map = src._map;
  _transform = src._transform;
}


///////////////
// Operators //
///////////////
environmentMap environmentMap::operator=(const environmentMap& src)
{
  _assign(src);
  return *this;
}


color environmentMap::operator()(const vec3d& direction) const
{
  // normalize & apply transformation
  vec3d dir = _transform.inverseTransformDirection(normalize(direction));

  // compute spherical coordinates
  float theta = acos(dir.z);
  float phi = atan2(dir.y, dir.x);

  if(phi < 0.0f) phi += 2.0f * PI;

  // get texel
  return _map->operator()(vec2d(0.5f * phi / PI, theta / PI));
}


///////////////////////
// Protected Methods //
///////////////////////
void environmentMap::_assign(const environmentMap& src)
{
  if(&src == this) return;
  
  _map = src._map;
  _transform = src._transform;
}


void environmentMap::_swap(environmentMap& swp)
{
  std::swap(_map, swp._map);
  swap(_transform, swp._transform);
}


void environmentMap::_print(std::ostream& s) const
{
  s << "Environment Map (" << *_map << ", " << _transform << ")";
}