summaryrefslogtreecommitdiff
path: root/hw6/src/bumpMap.cpp
blob: ecd6f4949506cf3ee8456f715eed6f4034103734 (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
/******************************************************************/
/* 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 <cassert>
#include <algorithm>
#include "bumpMap.h"
#include "coordinateTransform.h"

//////////////////
// Constructors //
//////////////////
bumpMap::bumpMap(const std::shared_ptr<const texture_base>& map, float scale, unsigned int channel, const std::shared_ptr<const shader_base>& shader)
  : shadingFrameTransformation(shader)
{
  _map = map;
  _scale = scale;
  _channel = std::max(channel, 2u);
}

/////////////
// Methods //
/////////////
transformation3d bumpMap::_transformation(const vec2d& textureCoord) const
{
  // get derivative in X and Y direction
  assert(_map);
  float dx = (*_map)(textureCoord)[_channel] - (*_map)(textureCoord + vec2d(1.0f/_map->width(), 0.0f))[_channel];
  float dy = (*_map)(textureCoord)[_channel] - (*_map)(textureCoord + vec2d(0.0f, 1.0f/_map->height()))[_channel];

  // get desired local normal = (1.0, 0.0, dx) cross (0.0, 1.0, dy)
  vec3d normal(-dx, -dy, 1.0f / _scale);
  normal.normalize();

  // create coordinate system. Keep U aligned as close as possible to the X axis.
  return coordinateTransformation(normal, vec3d(1.0f, 0.0, 0.0));
}


void bumpMap::_print(std::ostream& s) const
{
  s << "bumpMap (" << _scale << " x " << *_map << "[" << _channel << "]) -> {" << *_shader << "}";
}