blob: ecd6f4949506cf3ee8456f715eed6f4034103734 (
plain) (
tree)
|
|
/******************************************************************/
/* 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 << "}";
}
|