Created
April 21, 2010 10:04
-
-
Save BlockoS/373649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
struct vec3 | |
{ | |
vec3(const vec3& v) : x(v.x), y(v.y), z(v.z) {} | |
vec3(float ix, float iy, float iz) : x(ix), y(iy), z(iz) {} | |
vec3(float f) : x(f), y(f), z(f) {} | |
vec3& operator(const vec3& v) | |
{ | |
x = v.x, y = v.y, z = v.z; | |
return *this; | |
} | |
vec3& operator+= (const vec3& v) | |
{ | |
x += v.x, y += v.y, z += v.z; | |
return *this; | |
} | |
vec3& operator-= (const vec3& v) | |
{ | |
x -= v.x, y -= v.y, z -= v.z; | |
return *this; | |
} | |
vec3& operator*= (const vec3& v) | |
{ | |
x *= v.x, y *= v.y, z *= v.z; | |
return *this; | |
} | |
vec3& operator*= (float f) | |
{ | |
x *= f, y *= f, z *= f; | |
return *this; | |
} | |
vec3& operator/= (const vec3& v) | |
{ | |
x /= v.x, y /= v.y, z /= v.z; | |
return *this; | |
} | |
vec3& operator/= (float f) | |
{ | |
x /= f, y /= f, z /= f; | |
return *this; | |
} | |
friend vec3 operator+ (const vec3& u, const vec3& v) | |
{ | |
return vec3(u.x+v.x, u.y+v.y, u.z+v.z); | |
} | |
friend vec3 operator- (const vec3& u, const vec3& v) | |
{ | |
return vec3(u.x-v.x, u.y-v.y, u.z-v.z); | |
} | |
friend vec3 operator* (const vec3& u, const vec3& v) | |
{ | |
return vec3(u.x*v.x, u.y*v.y, u.z*v.z); | |
} | |
friend vec3 operator* (const vec3& u, float f) | |
{ | |
return vec3(u.x*f, u.y*f, u.z*f); | |
} | |
friend vec3 operator* (float f, const vec3& u) | |
{ | |
return vec3(u.x*f, u.y*f, u.z*f); | |
} | |
friend vec3 operator/ (const vec3& u, const vec3& v) | |
{ | |
return vec3(u.x/v.x, u.y/v.y, u.z/v.z); | |
} | |
friend vec3 operator/ (const vec3& u, float f) | |
{ | |
return vec3(u.x/f, u.y/f, u.z/f); | |
} | |
float magnitude() const | |
{ | |
return u.x*u.x + u.y*u.y + u.z*u.z; | |
} | |
float length() const | |
{ | |
return sqrtf( magnitude() ); | |
} | |
void normalize() | |
{ | |
*this /= length(); | |
} | |
friend float distance(const vec3& u, const vec3& v) | |
{ | |
return (u - v).length(); | |
} | |
friend float dot(const vec3& u, const vec3& v) | |
{ | |
return u.x*v.x + u.y*v.y + u.z*v.z; | |
} | |
friend vec3 cross(const vec3& u, const vec3& v) | |
{ | |
return vec3( u.y*v.z - u.z*v.y, u.z*v.x - u.x*v.z, u.x*v.y - u.y*v.x); | |
} | |
float x, y, z; | |
}; | |
class Camera | |
{ | |
public: | |
Camera(); | |
~Camera(); | |
void LookAt(const vec3& eye, const vec3& target, const vec3& up = vec3(0.0, 1.0, 0.0)); | |
void Perspective(float fovy, float aspect); | |
void Sample(float x, float y, vec3& out); | |
private: | |
vec3 _eye; | |
vec3 _direction; | |
vec3 _up; | |
vec3 _right; | |
float _fov, halfTan; | |
float _aspect; | |
}; | |
Camera::Camera() : | |
_eye(0.0), | |
_direction(0.0, 0.0, 1.0), | |
_up(0.0, 1.0, 0.0), | |
_right(1.0, 0.0, 0.0), | |
_fov(M_PI*0.5), | |
_halfTan( tan(M_PI*0.25) ), | |
_aspect( 4.0/3.0 ) | |
{} | |
Camera::~Camera() {} | |
void Camera::LookAt(const vec3& eye, const vec3& target, const vec3& up) | |
{ | |
_eye = eye; | |
_direction = target - eye; | |
_direction.normalize(); | |
_right = cross( up, _direction ); | |
_right.normalize(); | |
_up = cross( _direction, _right ); | |
_up.normalize(); | |
} | |
void Camera::Perspective(float fovy, float aspect) | |
{ | |
_fov = fovy; | |
_halfTan = tan(_fov * 0.5); | |
_aspect = aspect; | |
} | |
void Camera::Sample(float x, float y, vec3& out) | |
{ | |
out = _direction + ((_right * x) + (_up * (y/aspect))) * _halfTan; | |
out.normalize(); | |
} | |
class Triangle | |
{ | |
}; | |
int main() | |
{ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment