Wednesday, June 18, 2008

BSP Portal generation some thoughts


Recently i started to learn something more about portal and PVS (potential visible set) , and decided to write a my own engine. Everyone who are going to write a BSP tree and portal generation system would get some pain in their eyes . I am sure about it.  It is not as easy as we think , BSP tree is ok , but an efficient automatic Portal generation is algorithm is difficult .

I am looking some methods to do it . BSP tree helps to divide the input geometry data in to convex polygons , that means the leaf of the node will contain the convex polygons. If the input data is not a suitable one the bsp will be an unbalanced one , and also cause to split many polygons. So an efficient BSP tree algorithm is needed ,
Which should do the following things.
1. It should select the best splitter polygon from the given polygon set
2. The criteria may be the ratio between the split count and balanced leaf count  ( Right leaf Poly count/Left leaf poly count , if it is 1 this is ideal , and you are lucky).
The next thing is to generate portals . I am stopping now .   i will update this page soon (i have no idea ),
I am thinking about an algorithm currently which can generate portals efficiently. ..
Contd...

4-7-2008
I started BSP tree works , it is not much complicated,  i need to avoid recursion to avoid stack overflow during the creation of tree.. In case of small low poly models the triangle count are normally less and i can use recursion to create tree.. anyway it is not a big problem.. The Big problem is generating the portals , it may be impossible to 100% generate correct portals automatically.. hmm..Let me see.. I want to keep the current momentum.
I just completed BSP tree works.. It works!!... I have some few problems now , like calculating the texture coordinates when triangles got cut . My friend decided to implement Sutherland-Hodgman clipping algorithm for creating portals. Now I am waiting for his results..

Tuesday, January 29, 2008

C++ overloading operator& ( Or Find the addess of a object)

Recently a friend of mine ( name : rejeesh ) asked me "how to find the address of an object if the & operator is  overloaded" ?

Example class is like this

class AddressBlocker
{

public:

  AddressBlocker*
  operator &()  {  return 0;  }
};


so if you try to execute a code like this

AddressBlocker op;
AddressBlocker* Pt = &op;

Pt will be NULL. Pt =  &op will not pass the  real address of op to Pt. Because the operator is overloaded.

So how to get the address ??

When he asked me it, i found two solutions with templates .  One deriving a new class from the required class , and the second one is
more tricky. I will explain ( by showing the code) here both two methods.

Method 1.

template<typename T>
class Addressfinder : public T
{
public:
    Addressfinder* operator&() { return this; }
};

and you use it like this
AddressBlocker
blockMe;

Addressfinder<AddressBlocker>& finder = static_cast<Addressfinder<AddressBlocker>&> ( blockMe );


and address can be retrieved by simple  "AddressBlocker* pValid = &finder;" . This will work since i overloaded the &operator in Addressfinder class.
Method 2.

I think this is more good compared to the above. Since it is not creating any more relationships with the classes. and also no need for casting to use it.

template <typename T>
class AddressfinderII
{
    T& tOb;
public:
    AddressfinderII(T& t): tOb(t){}
    T * operator&()
    {
        return reinterpret_cast<T*> ( *reinterpret_cast<int *>( this ) );
    }
};

usage is like this

AddressBlocker blocker;
AddressfinderII<AddressBlocker> finderII(blocker);AddressBlocker * pAddress = &finderII;

if you look the size of this class , it just 4 bytes. because storing reference is same as pointer.
The class object's memory will have just the real address of object. And i extracted that value in operator& and returned.

Ok.. There is a more convenient way , he told me . and it is used in boost libraries. anyway i could find some alternatives 

Method 3

template<typename T>
T* addessof(T& t)
{
    return reinterpret_cast<T*>(& reinterpret_cast<int&> ( t ) );
}

and can be use like addressof( blocker); it is more convenient. it has similarities with method 1. So now no more address hiding..


Tuesday, December 18, 2007

2d lines intersection point & Linear equations

Do you remember the linear equations studied ? and methods to solve linear equations ?  may be some of us remember it.

We know every line can be represented by a linear equation of the form Ax+by  =c . or in the point slope form y = mx + c.so if you have two lines in the graph  ( or in ur game) you can find the intersecting point by solving these equations. You can find the intersecting point by solving these equations.

For example if you have two lines corresponding to , y = 2x,  and y = 0x+ 3 ( horizontal line ) so certainly these lines will intersect. .slopes are 2 and 0 respectily. so by solving these equations 2x-y =0 , and 0x + y = 3, we will get x = 3/2 and y = 3. 

 So what is the best method for solving linear equations using computers ? There are methods like

1. Linear compostion (normal equation solving , by adding or subtracting 2 equations)
2. gauss elimination method ( using matrixes. i think everybody knows it , no need for explanation. ).
3. Cramer's Rule. ( creating the cofactor matrix and finding determinent , andby dividing with determinent).

So which is the best ? I think gauss elimination is best. Because when the matrix diamension becomes bigger , cramer's method even takes days to find the answer.But Gauss elimination method won't .