From 08c40b5156cbd131defea5fce9221e02117193dd Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Mon, 19 Dec 2005 05:08:05 +0000
Subject: [PATCH] better z-clipping planes for perspective proj (should also
 work for objects far off the z=0 plane now)

---
 Common/Options.cpp | 11 ++++++++---
 Graphics/Draw.cpp  | 26 +++++++++++++-------------
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/Common/Options.cpp b/Common/Options.cpp
index ad8bd09fb5..1a1d23b3c8 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -1,4 +1,4 @@
-// $Id: Options.cpp,v 1.265 2005-12-18 21:10:54 geuzaine Exp $
+// $Id: Options.cpp,v 1.266 2005-12-19 05:08:05 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -2651,8 +2651,13 @@ double opt_general_scale2(OPT_ARGS_NUM)
 
 double opt_general_clip_factor(OPT_ARGS_NUM)
 {
-  if(action & GMSH_SET)
-    CTX.clip_factor = val;
+  if(action & GMSH_SET){
+    // should NEVER be zero (or negative)
+    if(val < 0.01) 
+      CTX.clip_factor = 0.01;
+    else
+      CTX.clip_factor = val;    
+  }
 #if defined(HAVE_FLTK)
   if(WID && (action & GMSH_GUI))
     WID->gen_value[14]->value(CTX.clip_factor);
diff --git a/Graphics/Draw.cpp b/Graphics/Draw.cpp
index f83e881bf4..558de666bd 100644
--- a/Graphics/Draw.cpp
+++ b/Graphics/Draw.cpp
@@ -1,4 +1,4 @@
-// $Id: Draw.cpp,v 1.87 2005-12-19 02:24:56 geuzaine Exp $
+// $Id: Draw.cpp,v 1.88 2005-12-19 05:08:05 geuzaine Exp $
 //
 // Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
 //
@@ -169,21 +169,20 @@ void InitProjection(int x, int y)
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
 
+  // restrict to 5x5 pixel viewport when in SELECT mode
   if(CTX.render_mode == GMSH_SELECT)
     gluPickMatrix((GLdouble) x, (GLdouble) (CTX.viewport[3] - y),
                   5.0, 5.0, (GLint *) CTX.viewport);
 
-  // we should generalize the following for cases when the object is
-  // located far from the z=0 plane
-
   double grad_z, grad_xy;
+  double zmax = MAX(fabs(CTX.min[2]), fabs(CTX.max[2]));
+  if(zmax < CTX.lc) zmax = CTX.lc;
+
   if(CTX.ortho) {
     // setting up the near and far clipping planes so that the box is
     // large enough to manipulate the model and zoom, but not too big
     // (the z-buffer resolution, e.g., on software Mesa can become
     // insufficient)
-    double zmax = MAX(fabs(CTX.min[2]), fabs(CTX.max[2]));
-    if(zmax < CTX.lc) zmax = CTX.lc;
     double clip = zmax * CTX.s[2] * CTX.clip_factor;
     glOrtho(CTX.vxmin, CTX.vxmax, CTX.vymin, CTX.vymax, -clip, clip);
     glMatrixMode(GL_MODELVIEW);
@@ -192,8 +191,9 @@ void InitProjection(int x, int y)
     grad_xy = 1.;
   }
   else {
-    double znear = 0.75 * CTX.clip_factor * CTX.lc;
-    double zfar = 75. * CTX.clip_factor * CTX.lc;
+    double clip_near = 0.75 * CTX.clip_factor * zmax;
+    double clip_far = 75. * CTX.clip_factor * zmax;
+    double coef = (75./0.75) / 3.;
     // recenter the model such that the perspective is always at the
     // center of gravity (we should maybe add an option to choose
     // this, as we do for the rotation center)
@@ -203,13 +203,13 @@ void InitProjection(int x, int y)
     CTX.vxmax -= CTX.t_init[0];
     CTX.vymin -= CTX.t_init[1];
     CTX.vymax -= CTX.t_init[1];
-    glFrustum(CTX.vxmin, CTX.vxmax, CTX.vymin, CTX.vymax, znear, zfar);
+    glFrustum(CTX.vxmin, CTX.vxmax, CTX.vymin, CTX.vymax, clip_near, clip_far);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
-    glTranslated(-10 * CTX.t_init[0], -10 * CTX.t_init[1], -10 * znear);
-    glScaled(10., 10., 10.);
-    grad_z = 0.99 * zfar;
-    grad_xy = zfar / znear;
+    glTranslated(-coef * CTX.t_init[0], -coef * CTX.t_init[1], -coef * clip_near);
+    glScaled(coef, coef, coef);
+    grad_z = 0.99 * clip_far;
+    grad_xy = clip_far / clip_near;
   }
 
   // draw background gradient
-- 
GitLab