• Create Account

## Screen space rectangle (3D object axis aligned rect)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1belfegor  Members

2833
Like
0Likes
Like

Posted 04 June 2013 - 12:09 PM

I am currently debugging my code, and since i am using some third party library i need to confirm if my code is doing the right thing so i can find out if it is the source of a bug.
I have bunch of objects, and i have calculated axis aligned bounding box for each, then i frustum cull all and for those that passes this test i take their AABBs corners and transfrom them in screen space and based on that calculate screen space rectangle that i need:

// get 8 corners/points of AABB

void aabb::getCorners(std::array<float3, 8>& corners) const

{

const float3& c    = center;

const float3& e    = extent; // half extent

corners[0] = float3((c.x - e.x), (c.y - e.y), (c.z - e.z));

corners[1] = float3((c.x + e.x), (c.y - e.y), (c.z - e.z));

corners[2] = float3((c.x - e.x), (c.y + e.y), (c.z - e.z));

corners[3] = float3((c.x + e.x), (c.y + e.y), (c.z - e.z));

corners[4] = float3((c.x - e.x), (c.y - e.y), (c.z + e.z));

corners[5] = float3((c.x + e.x), (c.y - e.y), (c.z + e.z));

corners[6] = float3((c.x - e.x), (c.y + e.y), (c.z + e.z));

corners[7] = float3((c.x + e.x), (c.y + e.y), (c.z + e.z));

}

...

// DirectX like 4x4 matrix

void matrix::projectVector(float3& out, const float3& in, float w, float h) const

{

float norm;

norm  = 1.0f / (m03 * in.x + m13 * in.y + m23 * in.z + m33);

out.x = (m00 * in.x + m10 * in.y + m20 * in.z + m30) * norm;

out.y = (m01 * in.x + m11 * in.y + m21 * in.z + m31) * norm;

out.z = (m02 * in.x + m12 * in.y + m22 * in.z + m32) * norm;

out.x = ( out.x + 1.0f ) * w * 0.5f; // + viewport.X, viewport X & Y always 0

out.y = ( 1.0f - out.y ) * h * 0.5f; // + viewport.Y

out.z = ( out.z + 1.0f ) * 0.5f;

}

...

// vp is view * projection,

bool CullingManager::isOccludeeVisible(aabb* pAABB, const matrix& vp)

{

std::array<float3, 8u> in_bboxPts;

std::array<float3, 8u> out_bboxPts;

pAABB->getCorners(in_bboxPts);

float zed = 1.0f; // take closest Z as depth

for(std::size_t i = 0; i < 8u; ++i)

{

vp.projectVector(out_bboxPts[i], in_bboxPts[i], (float)occDim.w, (float)occDim.h);

if(zed > out_bboxPts[i].z)

zed = out_bboxPts[i].z;

}

// third party library data to fill

OccludeeData od;

od.boundingBox.xMin = std::numeric_limits<int>::max();

od.boundingBox.yMin = std::numeric_limits<int>::max();

od.boundingBox.xMax = std::numeric_limits<int>::min();

od.boundingBox.yMax = std::numeric_limits<int>::min();

for(std::size_t i = 0; i < 8u; ++i)

{

int currentX = (int)out_bboxPts[i].x;

int currentY = (int)out_bboxPts[i].y;

if(od.boundingBox.xMin > currentX)

od.boundingBox.xMin = currentX;

if(od.boundingBox.yMin > currentY)

od.boundingBox.yMin = currentY;

if(od.boundingBox.xMax < currentX)

od.boundingBox.xMax = currentX;

if(od.boundingBox.yMax < currentY)

od.boundingBox.yMax = currentY;

}

od.depth = saturate(zed); // clamp 0 - 1

// third party library function

if(!occEngine->testOccludeeVisibility(od))

{

return false;

}

return true;

}

Do you see any problems?
Need more details?

Edited by belfegor, 05 June 2013 - 01:40 AM.

### #2belfegor  Members

2833
Like
0Likes
Like

Posted 05 June 2013 - 01:38 AM

I am not sure if it is requisite for all points to be inside of view frustum so that "project" function have right output?

I ask this because i see that for those that are this way they are properly occluded, but for those that some points are outside it is not (at certain camera angles) but should not be occluded. Am i making sense? Do you need more detail?

### #3unbird  Members

8297
Like
1Likes
Like

Posted 05 June 2013 - 03:07 AM

I guess the problem is the clipping. For x and y it's probably enough the clamp the final rectangle to the viewport. For z you need to clip to the near and far plane. Don't know how to do this, probably trickier since you need to clip triangles or lines. I'd google "software rasterization" for further research. This looks like a good starting point.

### #4belfegor  Members

2833
Like
0Likes
Like

Posted 05 June 2013 - 04:28 AM

Thank you for your input, i will have a look at that and for some more articles.

I was thinking to avoid implementing "software rasterization" myself so i used ISOCE library to do the hard work for me.

In this library they use a concept of occluder and occludee. I have found the algorithm for clipping occluder triangles against the view frustum planes in their demo source, so i have done that part (i was doing some tests for decals also) and it works fine. Now (i think) the problem is with occludees, do i have to clip each AABBs 12 triangles also, or can i get proper screen space rectangle by just projecting AABB points with which i find 2d min-max? For occludee in documentation says:

OccludeeData

Contains the 2D axis Alined bounding box of the Occluder and a conservative depth (Farthest depth value of the whole occludee)

I think they have 2 mistakes in this statement?

1. Occluder -> Occludee

2. Farthest depth value -> Nearest/closest depth value; would make sense or i miss something?

Any more tips?

Edited by belfegor, 05 June 2013 - 04:31 AM.

### #5unbird  Members

8297
Like
1Likes
Like

Posted 05 June 2013 - 11:37 AM

The library looks interesting, but I can't help you further unless I dig deeper there . "Funny" that the library is C++ and all the samples C# and I don't seem to find any helper functions in the library interface for what you're doing. But your OP asks for problems of your code anyway.

One thing I spotted is your z-calculation. This smells like a OpenGL-style clip space to depth conversion, rather than DirectX. You could actually check against the D3DXVec3Project if your numbers make sense.

### #6belfegor  Members

2833
Like
0Likes
Like

Posted 05 June 2013 - 12:19 PM

I can run their prebuild demo, it crashes with exceptions:

also i tried to compile it but get some errors which i don't understand since i don't know C#.

So i dig deeper into their C# demo source and found (Occludee.cs) that they using almost the same thing for occludee AABB as i do:

public static bool projectBoundingBox(TgcBoundingBox box3d, OcclusionViewport viewport, out Occludee.BoundingBox2D box2D)
{
box2D = new Occludee.BoundingBox2D();

//Proyectar los 8 puntos, sin dividir aun por W
Vector3[] corners = box3d.computeCorners();
Matrix m = viewport.View * viewport.Projection;
Vector3[] projVertices = new Vector3[corners.Length];
int width = viewport.D3dViewport.Width;
int height = viewport.D3dViewport.Height;
for (int i = 0; i < corners.Length; i++)
{
Vector4 pOut = Vector3.Transform(corners[i], m);
if (pOut.W < 0) return true;
projVertices[i] = viewport.toScreenSpace(pOut, width, height);
}

//Buscar los puntos extremos
Vector2 min = new Vector2(float.MaxValue, float.MaxValue);
Vector2 max = new Vector2(float.MinValue, float.MinValue);
float minDepth = float.MaxValue;
foreach (Vector3 v in projVertices)
{
if (v.X < min.X)
{
min.X = v.X;
}
if (v.Y < min.Y)
{
min.Y = v.Y;
}
if (v.X > max.X)
{
max.X = v.X;
}
if (v.Y > max.Y)
{
max.Y = v.Y;
}

if (v.Z < minDepth)
{
minDepth = v.Z;
}
}

//Clamp
if (min.X < 0f) min.X = 0f;
if (min.Y < 0f) min.Y = 0f;
if (max.X >= width) max.X = width - 1;
if (max.Y >= height) max.Y = height - 1;

//Control de tamaño minimo
if (max.X - min.X < 1f) return true;
if (max.Y - min.Y < 1f) return true;

//Cargar valores de box2D
box2D.min = min;
box2D.max = max;
box2D.depth = minDepth;
return false;
}


then i noticed if (pOut.W < 0) return true; thing and apply changes to my code:

bool matrix::projectVector(float3& out, const float3& in, float w, float h) const
{
float norm = m03 * in.x + m13 * in.y + m23 * in.z + m33;
out.x = (m00 * in.x + m10 * in.y + m20 * in.z + m30) / norm;
out.y = (m01 * in.x + m11 * in.y + m21 * in.z + m31) / norm;
out.z = (m02 * in.x + m12 * in.y + m22 * in.z + m32) / norm;
out.x = ( out.x + 1.0f ) * w * 0.5f;
out.y = ( 1.0f - out.y ) * h * 0.5f;
out.z = ( out.z + 1.0f ) * 0.5f;
if(norm < 0.0f)
return false;
return true;
}


and now this appears to work correctly for my scenes.

Edited by belfegor, 05 June 2013 - 12:21 PM.

### #7belfegor  Members

2833
Like
0Likes
Like

Posted 05 June 2013 - 12:43 PM

I looked for a source of a D3DXVec3Project:

pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );



so if min = 0 and max = 1 then

pout->z = vec.z


Is this Z in 0 - 1 range?

Since i find in some other examples like:

out.z = ( out.z + 1.0f ) * 0.5f;


so i think they assume -1 - 1 range and then scale/bias to be in 0 - 1 range. Which one is right?

### #8unbird  Members

8297
Like
1Likes
Like

Posted 05 June 2013 - 03:35 PM

For DirectX it's [0..1], that's why I was concerned. And the OcclusionViewport.cs does it exactly so (stripped the comments)
public Vector3 toScreenSpace(Vector4 p, int width, int height)
{
p.X = p.X / p.W;
p.Y = p.Y / p.W;
p.Z = p.Z / p.W;

p.X = (int)(0.5f + ((p.X + 1) * 0.5f * width));
p.Y = (int)(0.5f + ((1 - p.Y) * 0.5f * height));

return new Vector3(p.X, p.Y, p.Z);
}

Where do you have your matrix::projectVector code from ?

### #9belfegor  Members

2833
Like
0Likes
Like

Posted 05 June 2013 - 03:59 PM

Where do you have your matrix::projectVector code from ?

Its from here D3DXVec3TransformCoord with viewport "toScreenSpace" part, i didn't want to use D3DXVec3Project since it does unnecessary world-view-projection multiply for each:

 D3DXVECTOR3* D3DXVec3Project(D3DXVECTOR3* pout, CONST D3DXVECTOR3* pv, CONST D3DVIEWPORT9* pviewport, CONST D3DXMATRIX* pprojection, CONST D3DXMATRIX* pview, CONST D3DXMATRIX* pworld)
{
D3DXMATRIX m1, m2;
D3DXVECTOR3 vec;

D3DXMatrixMultiply(&m1, pworld, pview);
D3DXMatrixMultiply(&m2, &m1, pprojection);

D3DXVec3TransformCoord(&vec, pv, &m2);
pout->x = pviewport->X + ( 1.0f + vec.x ) * pviewport->Width / 2.0f;
pout->y = pviewport->Y + ( 1.0f - vec.y ) * pviewport->Height / 2.0f;

pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );

return pout;
}


and i have lots of points to transform. Since my points are in world space already i just need to multiply view * projection once.

### #10belfegor  Members

2833
Like
0Likes
Like

Posted 06 June 2013 - 12:33 PM

I have created console application to test out Z values returned from projectVector:

#include <iostream>
#include <d3dx9.h>

bool ProjectVector(const D3DXMATRIX& m, D3DXVECTOR3& out, const D3DXVECTOR3& in, float w, float h)
{
float norm = m.m[0][3] * in.x + m.m[1][3] * in.y + m.m[2][3] * in.z + m.m[3][3];
out.x = (m.m[0][0] * in.x + m.m[1][0] * in.y + m.m[2][0] * in.z + m.m[3][0]) / norm;
out.y = (m.m[0][1] * in.x + m.m[1][1] * in.y + m.m[2][1] * in.z + m.m[3][1]) / norm;
out.z = (m.m[0][2] * in.x + m.m[1][2] * in.y + m.m[2][2] * in.z + m.m[3][2]) / norm;

out.x = (0.5f + ((out.x + 1.0f) * 0.5f * w));
out.y = (0.5f + ((1.0f - out.y) * 0.5f * h));

if(norm < 0.0f)
return false;
return true;
}

int main()
{
float nearClip = 1.0f;
float farClip  = 100.0f;

float worldZ = (farClip - nearClip) * 0.5f;
D3DXVECTOR3 inPoint = D3DXVECTOR3(0.0f, 0.0f, worldZ);

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(45.0f), 1280.0f / 720.0f, 1.0f, 100.0f);
D3DXMATRIX view;
D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 0.0f, 1.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMATRIX viewProj = view * proj;

D3DXVECTOR3 outPoint;
if(ProjectVector(viewProj, outPoint, inPoint, 1280.0f, 720.0f))
{
std::cout << outPoint.z << std::endl;
}

return 0;
}


If i put my point right in middle of near-far range i get 0.989695 as projected Z.

Is this to be expected? Z is not linear across viewing range?

### #11unbird  Members

8297
Like
1Likes
Like

Posted 06 June 2013 - 01:11 PM

Nope, it's hyperbolic, so it's expected (depends on what you chose for near and far plane).

Here's an explanation I just found through google which also has some graphs.

The characteristics of this can become a problem (precision distribution, z-fighting) and there's a whole plethora of tricks to overcome this (playing with the near/far-plane, infinite projection, reversing the depth value, using log-depth). I'm only slowly grasping all this myself, so rather search yourself. Just to give you an impression have a look at this Outerra blog entry.

Not sure if it's relevant for that culling library, since it stores full floats anyway. Also I might revoke a bit my earlier concerns: If you don't mix that occlusion depth values with the DirectX depth values (read: compare) it probably doesn't matter either as long as you project everything the same way. Also: a native float doesn't clamp like a depth buffer value.

An additional warning though: I tried to make samples running and got no luck. Even after fixing semi-hardcoded paths and some other inconsistencies, relevant samples produced SEHException (in C# this means something native went bad), so I wonder if the library actually works. At least I have no proof so far. (No offense to the author intended.)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.