Crystal Space
Welcome, Guest. Please login or register.
September 01, 2014, 02:06:05 pm

Login with username, password and session length
Search:     Advanced search
9009 Posts in 2043 Topics by 8372 Members
Latest Member: Berger4
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Project Development
| |-+  Bug Reports
| | |-+  Portals, mirrors & bugs (some thoughts of how to fix)
« previous next »
Pages: [1] Print
Author Topic: Portals, mirrors & bugs (some thoughts of how to fix)  (Read 5932 times)
rvl_
Newbie
*
Posts: 27


View Profile
« on: January 09, 2006, 08:54:04 pm »

There are some problems in CS engine & OpenGL renderer which doesn't allow to draw mirrors correctly.

1. Probably one problem is common for all zfill/floating portals and described in bug #1398043 I've posted recently (see Flarge_Mirror.jpg attached to bug tracker).
It is caused by strange code in gl_render3d.cpp. That code is in Draw2DPolygon function:

Code:
void csGLGraphics3D::Draw2DPolygon (csVector2* poly, int num_poly,
const csPlane3& normal)
{
  SwapIfNeeded();

  // Get the plane normal of the polygon. Using this we can calculate
  // '1/z' at every screen space point.
  float M, N, O;
  float Dc = normal.D ();
  if (ABS (Dc) < 0.01f)
  {
    M = N = 0;
    O = 1;
  }
  else
  {
    float inv_Dc = 1.0f / Dc;
    M = -normal.A () * inv_Dc * inv_aspect;
    N = -normal.B () * inv_Dc * inv_aspect;
    O = -normal.C () * inv_Dc;
  }

  // Basically a GL ortho matrix
  const float P0 = 2.0f / (float)viewwidth;
  const float P5 = 2.0f / (float)viewheight;
  const float P10 = -2.0f / 11.0f;
  const float P11 = -9.0f / 11.0f;

  int v;
  glBegin (GL_TRIANGLE_FAN);
  csVector2* vt = poly;
  for (v = 0 ; v < num_poly ; v++)
  {
    float sx = vt->x - asp_center_x;
    float sy = vt->y - asp_center_y;
    float one_over_sz = M * sx + N * sy + O;
    float sz = 1.0f / one_over_sz;
    // This is what we would do if we'd use glOrtho():
    //glVertex4f (vt->x * sz, vt->y * sz, -1.0f, sz);

    // The vector that results from a GL ortho transform
    csVector4 bar ((vt->x * sz) * P0 - sz,
      (vt->y * sz) * P5 - sz,
      -P10 + sz * P11,
      sz);
    /* Now it can happen that a vertex of a polygon gets clipped when it's
     * very close to the near plane. In practice that causes sonme of the
     * portal magic (stencil area setup, Z fill) to go wrong when the camera
     * is close to the portal. We "fix" this by checking whether the vertex
     * would get clipped and ... */
    const float bar_w = bar.w, minus_bar_w = -bar_w;
    if ((bar.x < minus_bar_w) || (bar.x > bar_w)
      || (bar.y < minus_bar_w) || (bar.y > bar_w)
      || (bar.z < minus_bar_w) || (bar.z > bar_w))
    {
      /*If yes, "fix" the vertex sent to GL by replacing the Z value with one
      that won't cause clipping.*/
      const float hackedZ = 1.0f - EPSILON;
      glVertex3f (bar.x/bar_w, bar.y/bar_w, hackedZ);
    }
    else
    {
      // If not, proceed as usual.
      glVertex4f (bar.x, bar.y, bar.z, bar.w);
    }
    vt++;
  }
  glEnd ();
}

Strange code begins with "Now it can happen..." comment. Yes, we need some work around the case when camera "glass" crosses a portal polygon. But the vertices of portal polygon may have any z-coordinate and I don't understand why their x/y coordinates influence on "hackedZ" path.

If we use

   
Code:
if ((bar.z < minus_bar_w) || (bar.z > bar_w)) ...

instead of
   
   
Code:
if ((bar.x < minus_bar_w) || (bar.x > bar_w)
      || (bar.y < minus_bar_w) || (bar.y > bar_w)
      || (bar.z < minus_bar_w) || (bar.z > bar_w))

the function works OK with "portal magic" but the "overlay" bug #1398043 doesn't appear anymore. Maybe I'm wrong?

2. Other portal problem: portals are not visible in mirrors (it can be seen in flarge level also, but it 's not striking)

It seems, this happens because of vertex order of portal polygon is reversed in each mirror. When we use static statecache->SetCullFace (GL_FRONT) call it works Ok, while no other portals reflected. But if we have other portals the problem appears.
The temporary solution (second shot) is to disable GL culling while filling stencil, but this causes other bug - backfaced mirrors sometimes appears/flickers.

To solve the problem nicely we need to have for each portal in clipportal_stack some "mirror_flag" and calculate result culling in SetupClipPortals. But renderer knows nothing of portals' mirror property at the moment.

Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: January 09, 2006, 09:01:09 pm »

Hmm... I have to think about your post a bit more when I'm more awake but it looks like you're a good candidate to do some CS development? smiley

Care to join the team?

Greetings,
Logged
rvl_
Newbie
*
Posts: 27


View Profile
« Reply #2 on: January 11, 2006, 07:17:12 pm »

Why not... But don't wait superior activity from me. Smiley Naturally I'm interested in bug fixing of things needed by project on which I'm working.
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #3 on: January 14, 2006, 06:37:13 am »

Why not... But don't wait superior activity from me. Smiley Naturally I'm interested in bug fixing of things needed by project on which I'm working.

If you want you can get dev access. Just drop me a mail (jorrit.tyberghein@gmail.com) with your SF login.

Greetings,
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 2.164 seconds with 12 queries.