diff options
Diffstat (limited to 'nGenEx/Projector.cpp')
-rw-r--r-- | nGenEx/Projector.cpp | 492 |
1 files changed, 246 insertions, 246 deletions
diff --git a/nGenEx/Projector.cpp b/nGenEx/Projector.cpp index af88d08..8af5491 100644 --- a/nGenEx/Projector.cpp +++ b/nGenEx/Projector.cpp @@ -1,15 +1,15 @@ /* Project nGenEx
- Destroyer Studios LLC
- Copyright © 1997-2004. All Rights Reserved.
+ Destroyer Studios LLC
+ Copyright © 1997-2004. All Rights Reserved.
- SUBSYSTEM: nGenEx.lib
- FILE: Projector.cpp
- AUTHOR: John DiCamillo
+ SUBSYSTEM: nGenEx.lib
+ FILE: Projector.cpp
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- 3D Projection Camera class
+ OVERVIEW
+ ========
+ 3D Projection Camera class
*/
#include "MemDebug.h"
@@ -29,12 +29,12 @@ static Camera emergency_cam; // +--------------------------------------------------------------------+
Projector::Projector(Window* window, Camera* cam)
- : camera(cam), infinite(0), depth_scale(1.0f), orthogonal(false), field_of_view(2)
+: camera(cam), infinite(0), depth_scale(1.0f), orthogonal(false), field_of_view(2)
{
- if (!camera)
- camera = &emergency_cam;
+ if (!camera)
+ camera = &emergency_cam;
- UseWindow(window);
+ UseWindow(window);
}
Projector::~Projector()
@@ -45,68 +45,68 @@ Projector::~Projector() void
Projector::UseCamera(Camera* cam)
{
- if (cam)
- camera = cam;
- else
- camera = &emergency_cam;
+ if (cam)
+ camera = cam;
+ else
+ camera = &emergency_cam;
}
void
Projector::UseWindow(Window* win)
{
- Rect r = win->GetRect();
- width = r.w;
- height = r.h;
-
- xcenter = (width / 2.0);
- ycenter = (height / 2.0);
-
- xclip0 = 0.0f;
- xclip1 = (float) width-0.5f;
- yclip0 = 0.0f;
- yclip1 = (float) height-0.5f;
-
- SetFieldOfView(field_of_view);
+ Rect r = win->GetRect();
+ width = r.w;
+ height = r.h;
+
+ xcenter = (width / 2.0);
+ ycenter = (height / 2.0);
+
+ xclip0 = 0.0f;
+ xclip1 = (float) width-0.5f;
+ yclip0 = 0.0f;
+ yclip1 = (float) height-0.5f;
+
+ SetFieldOfView(field_of_view);
}
void
Projector::SetFieldOfView(double fov)
{
- field_of_view = fov;
+ field_of_view = fov;
- xscreenscale = width / fov;
- yscreenscale = height / fov;
+ xscreenscale = width / fov;
+ yscreenscale = height / fov;
- maxscale = max(xscreenscale, yscreenscale);
+ maxscale = max(xscreenscale, yscreenscale);
- xangle = atan(2.0/fov * maxscale/xscreenscale);
- yangle = atan(2.0/fov * maxscale/yscreenscale);
+ xangle = atan(2.0/fov * maxscale/xscreenscale);
+ yangle = atan(2.0/fov * maxscale/yscreenscale);
}
double
Projector::GetFieldOfView() const
{
- return field_of_view;
+ return field_of_view;
}
void
Projector::SetDepthScale(float scale)
{
- depth_scale = scale;
+ depth_scale = scale;
}
double
Projector::GetDepthScale() const
{
- return depth_scale;
+ return depth_scale;
}
int
Projector::SetInfinite(int i)
{
- int old = infinite;
- infinite = i;
- return old;
+ int old = infinite;
+ infinite = i;
+ return old;
}
// +--------------------------------------------------------------------+
@@ -114,8 +114,8 @@ Projector::SetInfinite(int i) void
Projector::StartFrame()
{
- SetUpFrustum();
- SetWorldSpace();
+ SetUpFrustum();
+ SetWorldSpace();
}
// +--------------------------------------------------------------------+
@@ -125,19 +125,19 @@ Projector::StartFrame() void
Projector::Transform(Vec3& vec) const
{
- Vec3 tvert = vec;
+ Vec3 tvert = vec;
- // Translate into a viewpoint-relative coordinate
- if (!infinite)
- tvert -= camera->Pos();
+ // Translate into a viewpoint-relative coordinate
+ if (!infinite)
+ tvert -= camera->Pos();
- // old method:
- vec.x = (tvert * camera->vrt());
- vec.y = (tvert * camera->vup());
- vec.z = (tvert * camera->vpn());
+ // old method:
+ vec.x = (tvert * camera->vrt());
+ vec.y = (tvert * camera->vup());
+ vec.z = (tvert * camera->vpn());
- // Rotate into the view orientation
- // vec = tvert * camera->Orientation();
+ // Rotate into the view orientation
+ // vec = tvert * camera->Orientation();
}
// +--------------------------------------------------------------------+
@@ -147,19 +147,19 @@ Projector::Transform(Vec3& vec) const void
Projector::Transform(Point& point) const
{
- Point tvert = point;
+ Point tvert = point;
- // Translate into a viewpoint-relative coordinate
- if (!infinite)
- tvert -= camera->Pos();
+ // Translate into a viewpoint-relative coordinate
+ if (!infinite)
+ tvert -= camera->Pos();
- // old method:
- point.x = (tvert * camera->vrt());
- point.y = (tvert * camera->vup());
- point.z = (tvert * camera->vpn());
+ // old method:
+ point.x = (tvert * camera->vrt());
+ point.y = (tvert * camera->vup());
+ point.z = (tvert * camera->vpn());
- // Rotate into the view orientation
- // point = tvert * camera->Orientation();
+ // Rotate into the view orientation
+ // point = tvert * camera->Orientation();
}
// +--------------------------------------------------------------------+
@@ -171,7 +171,7 @@ Projector::Transform(Point& point) const float
Projector::ProjectRadius(const Vec3& v, float radius) const
{
- return (float) fabs((radius * maxscale) / v.z);
+ return (float) fabs((radius * maxscale) / v.z);
}
// +--------------------------------------------------------------------+
@@ -184,32 +184,32 @@ Projector::ProjectRadius(const Vec3& v, float radius) const void
Projector::Project(Vec3& v, bool clamp) const
{
- double zrecip;
-
- if (orthogonal) {
- double scale = field_of_view/2;
- v.x = (float) (xcenter + scale * v.x);
- v.y = (float) (height - (ycenter + scale * v.y));
- v.z = (float) (0.0f);
- }
-
- else {
- //zrecip = 2 * (1.0e5 / (1.0e5-1)) / v.z;
- //zrecip = 2 * 0.97 / v.z; -- what the heck was this version used for?
-
- zrecip = 2 / v.z;
- v.x = (float) (xcenter + maxscale * v.x * zrecip);
- v.y = (float) (height - (ycenter + maxscale * v.y * zrecip));
- v.z = (float) (1 - zrecip);
- }
-
- // clamp the point to the viewport:
- if (clamp) {
- if (v.x < xclip0) v.x = xclip0;
- if (v.x > xclip1) v.x = xclip1;
- if (v.y < yclip0) v.y = yclip0;
- if (v.y > yclip1) v.y = yclip1;
- }
+ double zrecip;
+
+ if (orthogonal) {
+ double scale = field_of_view/2;
+ v.x = (float) (xcenter + scale * v.x);
+ v.y = (float) (height - (ycenter + scale * v.y));
+ v.z = (float) (0.0f);
+ }
+
+ else {
+ //zrecip = 2 * (1.0e5 / (1.0e5-1)) / v.z;
+ //zrecip = 2 * 0.97 / v.z; -- what the heck was this version used for?
+
+ zrecip = 2 / v.z;
+ v.x = (float) (xcenter + maxscale * v.x * zrecip);
+ v.y = (float) (height - (ycenter + maxscale * v.y * zrecip));
+ v.z = (float) (1 - zrecip);
+ }
+
+ // clamp the point to the viewport:
+ if (clamp) {
+ if (v.x < xclip0) v.x = xclip0;
+ if (v.x > xclip1) v.x = xclip1;
+ if (v.y < yclip0) v.y = yclip0;
+ if (v.y > yclip1) v.y = yclip1;
+ }
}
// +--------------------------------------------------------------------+
@@ -222,29 +222,29 @@ Projector::Project(Vec3& v, bool clamp) const void
Projector::Project(Point& v, bool clamp) const
{
- double zrecip;
-
- if (orthogonal) {
- double scale = field_of_view/2;
- v.x = (xcenter + scale * v.x);
- v.y = (height - (ycenter + scale * v.y));
- v.z = 0;
- }
-
- else {
- zrecip = 1 / v.z;
- v.x = (xcenter + 2 * maxscale * v.x * zrecip);
- v.y = (height - (ycenter + 2 * maxscale * v.y * zrecip));
- v.z = (1 - zrecip);
- }
-
- // clamp the point to the viewport:
- if (clamp) {
- if (v.x < xclip0) v.x = xclip0;
- if (v.x > xclip1) v.x = xclip1;
- if (v.y < yclip0) v.y = yclip0;
- if (v.y > yclip1) v.y = yclip1;
- }
+ double zrecip;
+
+ if (orthogonal) {
+ double scale = field_of_view/2;
+ v.x = (xcenter + scale * v.x);
+ v.y = (height - (ycenter + scale * v.y));
+ v.z = 0;
+ }
+
+ else {
+ zrecip = 1 / v.z;
+ v.x = (xcenter + 2 * maxscale * v.x * zrecip);
+ v.y = (height - (ycenter + 2 * maxscale * v.y * zrecip));
+ v.z = (1 - zrecip);
+ }
+
+ // clamp the point to the viewport:
+ if (clamp) {
+ if (v.x < xclip0) v.x = xclip0;
+ if (v.x > xclip1) v.x = xclip1;
+ if (v.y < yclip0) v.y = yclip0;
+ if (v.y > yclip1) v.y = yclip1;
+ }
}
// +--------------------------------------------------------------------+
@@ -257,17 +257,17 @@ Projector::Project(Point& v, bool clamp) const void
Projector::Unproject(Point& v) const
{
- double zrecip = 1 / v.z;
+ double zrecip = 1 / v.z;
- /***
- * forward projection:
- v.x = (xcenter + maxscale * v.x * zrecip);
- v.y = (height - (ycenter + maxscale * v.y * zrecip));
- v.z = (1 - zrecip);
- ***/
+ /***
+ * forward projection:
+v.x = (xcenter + maxscale * v.x * zrecip);
+v.y = (height - (ycenter + maxscale * v.y * zrecip));
+v.z = (1 - zrecip);
+***/
- v.x = ( v.x - xcenter) / (maxscale * zrecip);
- v.y = (height - v.y - ycenter) / (maxscale * zrecip);
+ v.x = ( v.x - xcenter) / (maxscale * zrecip);
+ v.y = (height - v.y - ycenter) / (maxscale * zrecip);
}
// +--------------------------------------------------------------------+
@@ -280,24 +280,24 @@ Projector::Unproject(Point& v) const void
Projector::ProjectRect(Point& v, double& w, double& h) const
{
- double zrecip;
-
- if (orthogonal) {
- double scale = field_of_view/2;
- v.x = (xcenter + scale * v.x);
- v.y = (height - (ycenter + scale * v.y));
- v.z = 0;
- }
-
- else {
- zrecip = 1 / v.z;
- v.x = (xcenter + 2 * maxscale * v.x * zrecip);
- v.y = (height - (ycenter + 2 * maxscale * v.y * zrecip));
- v.z = (1 - Z_NEAR*zrecip);
-
- w *= maxscale * zrecip;
- h *= maxscale * zrecip;
- }
+ double zrecip;
+
+ if (orthogonal) {
+ double scale = field_of_view/2;
+ v.x = (xcenter + scale * v.x);
+ v.y = (height - (ycenter + scale * v.y));
+ v.z = 0;
+ }
+
+ else {
+ zrecip = 1 / v.z;
+ v.x = (xcenter + 2 * maxscale * v.x * zrecip);
+ v.y = (height - (ycenter + 2 * maxscale * v.y * zrecip));
+ v.z = (1 - Z_NEAR*zrecip);
+
+ w *= maxscale * zrecip;
+ h *= maxscale * zrecip;
+ }
}
// +--------------------------------------------------------------------+
@@ -307,9 +307,9 @@ Projector::ProjectRect(Point& v, double& w, double& h) const void
Projector::SetWorldspaceClipPlane(Vec3& normal, Plane& plane)
{
- // Rotate the plane normal into worldspace
- ViewToWorld(normal, plane.normal);
- plane.distance = (float) (camera->Pos() * plane.normal + CLIP_PLANE_EPSILON);
+ // Rotate the plane normal into worldspace
+ ViewToWorld(normal, plane.normal);
+ plane.distance = (float) (camera->Pos() * plane.normal + CLIP_PLANE_EPSILON);
}
// +--------------------------------------------------------------------+
@@ -319,44 +319,44 @@ Projector::SetWorldspaceClipPlane(Vec3& normal, Plane& plane) void
Projector::SetUpFrustum()
{
- double angle, s, c;
- Vec3 normal;
-
- angle = XAngle();
- s = sin(angle);
- c = cos(angle);
-
- // Left clip plane
- normal.x = (float) s;
- normal.y = (float) 0;
- normal.z = (float) c;
- view_planes[0].normal = normal;
- view_planes[0].distance = CLIP_PLANE_EPSILON;
- SetWorldspaceClipPlane(normal, world_planes[0]);
-
- // Right clip plane
- normal.x = (float) -s;
- view_planes[1].normal = normal;
- view_planes[1].distance = CLIP_PLANE_EPSILON;
- SetWorldspaceClipPlane(normal, world_planes[1]);
-
- angle = YAngle();
- s = sin(angle);
- c = cos(angle);
-
- // Bottom clip plane
- normal.x = (float) 0;
- normal.y = (float) s;
- normal.z = (float) c;
- view_planes[2].normal = normal;
- view_planes[2].distance = CLIP_PLANE_EPSILON;
- SetWorldspaceClipPlane(normal, world_planes[2]);
-
- // Top clip plane
- normal.y = (float) -s;
- view_planes[3].normal = normal;
- view_planes[3].distance = CLIP_PLANE_EPSILON;
- SetWorldspaceClipPlane(normal, world_planes[3]);
+ double angle, s, c;
+ Vec3 normal;
+
+ angle = XAngle();
+ s = sin(angle);
+ c = cos(angle);
+
+ // Left clip plane
+ normal.x = (float) s;
+ normal.y = (float) 0;
+ normal.z = (float) c;
+ view_planes[0].normal = normal;
+ view_planes[0].distance = CLIP_PLANE_EPSILON;
+ SetWorldspaceClipPlane(normal, world_planes[0]);
+
+ // Right clip plane
+ normal.x = (float) -s;
+ view_planes[1].normal = normal;
+ view_planes[1].distance = CLIP_PLANE_EPSILON;
+ SetWorldspaceClipPlane(normal, world_planes[1]);
+
+ angle = YAngle();
+ s = sin(angle);
+ c = cos(angle);
+
+ // Bottom clip plane
+ normal.x = (float) 0;
+ normal.y = (float) s;
+ normal.z = (float) c;
+ view_planes[2].normal = normal;
+ view_planes[2].distance = CLIP_PLANE_EPSILON;
+ SetWorldspaceClipPlane(normal, world_planes[2]);
+
+ // Top clip plane
+ normal.y = (float) -s;
+ view_planes[3].normal = normal;
+ view_planes[3].distance = CLIP_PLANE_EPSILON;
+ SetWorldspaceClipPlane(normal, world_planes[3]);
}
// +--------------------------------------------------------------------+
@@ -367,28 +367,28 @@ Projector::SetUpFrustum() int
Projector::IsVisible(const Vec3& v, float radius) const
{
- int visible = 1;
- int complete = 1;
-
- Plane* plane = (Plane*) frustum_planes;
- if (infinite) {
- complete = 0;
-
- for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
- visible = ((v * plane->normal) >= CLIP_PLANE_EPSILON);
- plane++;
- }
- }
- else {
- for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
- float dot = v * plane->normal;
- visible = ((dot + radius) >= plane->distance);
- complete = complete && ((dot - radius) >= plane->distance);
- plane++;
- }
- }
-
- return visible + complete;
+ int visible = 1;
+ int complete = 1;
+
+ Plane* plane = (Plane*) frustum_planes;
+ if (infinite) {
+ complete = 0;
+
+ for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
+ visible = ((v * plane->normal) >= CLIP_PLANE_EPSILON);
+ plane++;
+ }
+ }
+ else {
+ for (int i = 0; visible && (i < NUM_FRUSTUM_PLANES); i++) {
+ float dot = v * plane->normal;
+ visible = ((dot + radius) >= plane->distance);
+ complete = complete && ((dot - radius) >= plane->distance);
+ plane++;
+ }
+ }
+
+ return visible + complete;
}
// +--------------------------------------------------------------------+
@@ -401,37 +401,37 @@ Projector::IsVisible(const Vec3& v, float radius) const int
Projector::IsBoxVisible(const Point* p) const
{
- int i, j, outside = 0;
-
- // if all eight corners are outside of the same
- // frustrum plane, then the box is not visible
- Plane* plane = (Plane*) frustum_planes;
-
- if (infinite) {
- for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
- for (j = 0; j < 8; j++)
- outside += (p[j] * plane->normal) < CLIP_PLANE_EPSILON;
-
- if (outside < 8)
- outside = 0;
-
- plane++;
- }
- }
- else {
- for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
- for (j = 0; j < 8; j++)
- outside += (p[j] * plane->normal) < plane->distance;
-
- if (outside < 8)
- outside = 0;
-
- plane++;
- }
- }
-
- // if not outside, then the box is visible
- return !outside;
+ int i, j, outside = 0;
+
+ // if all eight corners are outside of the same
+ // frustrum plane, then the box is not visible
+ Plane* plane = (Plane*) frustum_planes;
+
+ if (infinite) {
+ for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
+ for (j = 0; j < 8; j++)
+ outside += (p[j] * plane->normal) < CLIP_PLANE_EPSILON;
+
+ if (outside < 8)
+ outside = 0;
+
+ plane++;
+ }
+ }
+ else {
+ for (i = 0; !outside && (i < NUM_FRUSTUM_PLANES); i++) {
+ for (j = 0; j < 8; j++)
+ outside += (p[j] * plane->normal) < plane->distance;
+
+ if (outside < 8)
+ outside = 0;
+
+ plane++;
+ }
+ }
+
+ // if not outside, then the box is visible
+ return !outside;
}
// +--------------------------------------------------------------------+
@@ -439,10 +439,10 @@ Projector::IsBoxVisible(const Point* p) const float
Projector::ApparentRadius(const Vec3& v, float radius) const
{
- Vec3 vloc = v;
-
- Transform(vloc); // transform in place
- return ProjectRadius(vloc, radius);
+ Vec3 vloc = v;
+
+ Transform(vloc); // transform in place
+ return ProjectRadius(vloc, radius);
}
@@ -453,18 +453,18 @@ Projector::ApparentRadius(const Vec3& v, float radius) const void
Projector::ViewToWorld(Point& pin, Point& pout)
{
- // Rotate into the world orientation
- pout.x = pin.x * camera->vrt().x + pin.y * camera->vup().x + pin.z * camera->vpn().x;
- pout.y = pin.x * camera->vrt().y + pin.y * camera->vup().y + pin.z * camera->vpn().y;
- pout.z = pin.x * camera->vrt().z + pin.y * camera->vup().z + pin.z * camera->vpn().z;
+ // Rotate into the world orientation
+ pout.x = pin.x * camera->vrt().x + pin.y * camera->vup().x + pin.z * camera->vpn().x;
+ pout.y = pin.x * camera->vrt().y + pin.y * camera->vup().y + pin.z * camera->vpn().y;
+ pout.z = pin.x * camera->vrt().z + pin.y * camera->vup().z + pin.z * camera->vpn().z;
}
void
Projector::ViewToWorld(Vec3& vin, Vec3& vout)
{
- // Rotate into the world orientation
- vout.x = (float) (vin.x * camera->vrt().x + vin.y * camera->vup().x + vin.z * camera->vpn().x);
- vout.y = (float) (vin.x * camera->vrt().y + vin.y * camera->vup().y + vin.z * camera->vpn().y);
- vout.z = (float) (vin.x * camera->vrt().z + vin.y * camera->vup().z + vin.z * camera->vpn().z);
+ // Rotate into the world orientation
+ vout.x = (float) (vin.x * camera->vrt().x + vin.y * camera->vup().x + vin.z * camera->vpn().x);
+ vout.y = (float) (vin.x * camera->vrt().y + vin.y * camera->vup().y + vin.z * camera->vpn().y);
+ vout.z = (float) (vin.x * camera->vrt().z + vin.y * camera->vup().z + vin.z * camera->vpn().z);
}
|