• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
belfegor

Screen space rectangle (3D object axis aligned rect)

10 posts in this topic

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?

Thank you for your time.

Edited by belfegor
0

Share this post


Link to post
Share on other sites

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?

 

Thank you for your time.

0

Share this post


Link to post
Share on other sites

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. [url="http://www.gamasutra.com/view/news/168577/Indepth_Software_rasterizer_and_triangle_clipping.php"]This[/url] looks like a good starting point.

1

Share this post


Link to post
Share on other sites

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?

 

Thank you for your time.

Edited by belfegor
0

Share this post


Link to post
Share on other sites
The library looks interesting, but I can't help you further unless I dig deeper there wink.png. "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.
1

Share this post


Link to post
Share on other sites

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

tgcviewer.jpg

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. smile.png

Edited by belfegor
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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 ?
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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

[url="http://www.codermind.com/articles/Depth-buffer-tutorial.html"]Here's an explanation[/url] 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 [url="http://outerra.blogspot.ch/2012/11/maximizing-depth-buffer-range-and.html"]Outerra blog entry[/url].

 

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.)

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0