00001 #include <gra/camera/Projection.h>
00002
00003 using namespace gra;
00004
00005 void Projection::_set_clipping_planes(double left,
00006 double right,
00007 double bottom,
00008 double top,
00009 double nr,
00010 double fr)
00011 {
00012 _left = left;
00013 _right = right;
00014 _top = top;
00015 _bottom = bottom;
00016 _near = nr;
00017 _far = fr;
00018 }
00019
00020
00021
00022
00023 Projection::Projection(double left,
00024 double right,
00025 double bottom,
00026 double top,
00027 double nr,
00028 double fr)
00029 {
00030 _set_clipping_planes(left, right, bottom, top, nr, fr);
00031 _projMat.setIdentity();
00032 }
00033
00034 Projection::Projection(const Projection& proj)
00035 {
00036 set(proj);
00037 }
00038
00039 void Projection::set(const Projection& proj)
00040 {
00041 _set_clipping_planes(proj._left, proj._right, proj._bottom,
00042 proj._top, proj._near, proj._far);
00043 _projMat.set(proj._projMat);
00044 }
00045
00046 Vector4 Projection::project(const Vector4& point) const
00047 {
00048 Vector4 ret(point);
00049 _projMat.transform(ret);
00050 return ret;
00051 }
00052
00053 Vector3 Projection::project(const Vector3& point) const
00054 {
00055 Vector4 aux(point.x, point.y, point.z, 1.0);
00056 _projMat.transform(aux);
00057 return Vector3(aux.x / aux.w, aux.y / aux.w, aux.z / aux.w);
00058 }
00059
00060 bool Projection::projectAndClip(const Vector4& point, Vector4* res) const
00061 {
00062 if (res) {
00063 res->set(project(point));
00064 return clip(*res);
00065 } else {
00066 Vector4 a(project(point));
00067 return clip(a);
00068 }
00069 }
00070
00071 bool Projection::projectAndClip(const Vector3& point, Vector3* res) const
00072 {
00073 if (res) {
00074 res->set(project(point));
00075 return clip(*res);
00076 } else {
00077 Vector3 a(project(point));
00078 return clip(a);
00079 }
00080 }
00081
00082 bool Projection::clip(const Vector4& v) const
00083 {
00084 register float xw = v.x * v.w;
00085 register float yw = v.y * v.w;
00086 return (xw > _left && xw < _right && yw > _bottom && yw < _top);
00087 }
00088
00089 bool Projection::clip(const Vector3& v) const
00090 {
00091 return (v.x > _left && v.x < _right && v.y > _bottom && v.y < _top);
00092 }
00093
00094 bool Projection::projectToPlane(const Vector3& point3, Vector2& point2) const
00095 {
00096 Vector3 aux;
00097 bool inside = projectAndClip(point3, &aux);
00098
00099 point2.x = (aux.x - _left) / (_right - _left);
00100 point2.y = (aux.y - _bottom) / (_top - _bottom);
00101
00102 return inside;
00103 }
00104
00105 void Projection::getProjectionArea(Vector3& bottom_left,
00106 Vector3& top_left,
00107 Vector3& top_right,
00108 Vector3& bottom_right) const
00109 {
00110 bottom_left.set(_left, _bottom, -_near);
00111 top_left.set(_left, _top, -_near);
00112 top_right.set(_right, _top, -_near);
00113 bottom_right.set(_right, _bottom, -_near);
00114 }