Skip to content

Instantly share code, notes, and snippets.

@Munksgaard
Created March 13, 2011 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Munksgaard/868216 to your computer and use it in GitHub Desktop.
Save Munksgaard/868216 to your computer and use it in GitHub Desktop.
#ifndef FRAGMENT_PROGRAM_H
#define FRAGMENT_PROGRAM_H
//
// Graphics Framework.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//
#include <iostream>
#include <iomanip>
#include "graphics/graphics.h"
#include <cmath>
namespace graphics {
/*******************************************************************\
* *
* P a s s T h r o u g h *
* *
\*******************************************************************/
template<typename math_types>
class MyIdentityFragmentProgram : public FragmentProgram<math_types>
{
public:
typedef typename FragmentProgram<math_types>::graphics_state_type graphics_state_type;
typedef typename math_types::vector3_type vector3_type;
typedef typename math_types::real_type real_type;
public:
void run(graphics_state_type const& state,
vector3_type const& in_position,
vector3_type const& in_normal,
vector3_type const& in_color,
vector3_type& out_color)
{
// >> TODO ADD YOUR OWN MAGIC HERE <<
//std::cout << "fragment: [" << in_position << "]" << std::endl;
out_color = in_color;
}
};
/*******************************************************************\
* *
* P h o n g *
* *
\*******************************************************************/
template<typename math_types>
class MyPhongFragmentProgram : public FragmentProgram<math_types>
{
public:
typedef typename FragmentProgram<math_types>::graphics_state_type graphics_state_type;
typedef typename math_types::vector3_type vector3_type;
typedef typename math_types::real_type real_type;
private:
vector3_type multvector(vector3_type v1, vector3_type v2)
{
vector3_type ret;
ret[1] = v1[1] * v2[1];
ret[2] = v1[2] * v2[2];
ret[3] = v1[3] * v2[3];
return ret;
}
real_type s(real_type Z) {
real_type s_b = 1.0;
real_type s_f = 0.0;
if (Z < s_b) {
return s_b;
} else if (Z > s_f) {
return s_f;
} else {
//s_b + (s_f - s_b) / (
}
}
public:
void run(graphics_state_type const& state,
vector3_type const& in_position,
vector3_type const& in_normal,
vector3_type const& in_color,
vector3_type& out_color)
{
// >> TODO ADD YOUR OWN MAGIC HERE <<
// Implement the Phong reflection model using the values saved in state
vector3_type V_unit, L_unit, R_unit, N_unit;
vector3_type V = state.eye_position() - in_position;
real_type Norm_V = Norm(V);
if (!Zero(Norm_V)) {
V_unit = V / Norm_V;
} else {
V_unit = V;
}
vector3_type N = in_normal;
real_type Norm_N = Norm(N);
if (!Zero(Norm_N)) {
N_unit = N / Norm_N;
} else {
N_unit = N;
}
vector3_type L = state.light_position();
real_type Norm_L = Norm(L);
if (!Zero(Norm_L)) {
L_unit = L / Norm_L;
} else {
L_unit = L;
}
vector3_type R = (N * 2) * (N * L) - L;
real_type Norm_R = Norm(R);
if (!Zero(Norm_R)) {
R_unit = R / Norm_R;
} else {
R_unit = R;
}
// Ambient
vector3_type Ambient =
state.ambient_intensity() *
multvector(state.ambient_color(), state.I_a());
// Diffuse
vector3_type Diffuse = multvector(state.I_p(),
state.diffuse_intensity() *
state.diffuse_color() *
Clamp((N_unit * L_unit)));
// Specular
vector3_type Specular = multvector(state.I_p(),
state.specular_intensity() *
state.specular_color() *
pow(Clamp(R_unit * V_unit), state.fall_off()));
vector3_type IPhongLambda;
IPhongLambda += Ambient;
IPhongLambda += Diffuse;
IPhongLambda += Specular;
vector3_type ILambda = IPhongLambda;
//printf("dybde: %f. ", in_position[3]);
out_color = ILambda;
out_color[1] = Clamp(out_color[1]);
out_color[2] = Clamp(out_color[2]);
out_color[3] = Clamp(out_color[3]);
}
private:
/*******************************************************************\
* *
* C l a m p ( r e a l _ t y p e ) *
* *
\*******************************************************************/
real_type Clamp(real_type const& value)
{
real_type result = value;
if (value < 0.0) result = 0.0;
if (value > 1.0) result = 1.0;
return result;
}
};
}// end namespace graphics
// FRAGMENT_PROGRAM_H
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment