Advertisement
Guest User

Jann Poppinga

a guest
Aug 26th, 2009
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.79 KB | None | 0 0
  1. #include "OPCODE-1.2/Opcode.h"
  2. //#include "Opcode.h"
  3. #include <iostream>
  4. #include <queue>
  5.  
  6. using namespace std;
  7. using namespace Opcode;
  8.  
  9. int main() {
  10.  
  11.  
  12.     /////////////////////// Model //////////////////////
  13.     Point vertices[6];
  14.     vertices[0] = Point(1,3,0);
  15.     vertices[1] = Point(1,-1,1);
  16.     vertices[2] = Point(1,-1,-3);
  17.     vertices[3] = Point(0,-1,1);
  18.     vertices[4] = Point(0,-1,-3);
  19.     vertices[5] = Point(-1,-1,1);
  20.     IndexedTriangle triangles[3];
  21.     triangles[0] = IndexedTriangle(0,2,1);
  22.     triangles[1] = IndexedTriangle(0,4,3);
  23.     triangles[2] = IndexedTriangle(1,3,5);
  24.    
  25.     Opcode::OPCODECREATE creator;
  26.  
  27.     creator.NbTris               = 3;
  28.     //creator.NbTris               = aIndices.size()/3;
  29.     creator.NbVerts              = 6;
  30.     //creator.Tris                 = aIndices.getArray();
  31.     creator.Tris                 = (udword*) triangles;
  32.     creator.Verts                = vertices;
  33.     // Tree building settings
  34.     creator.Rules                = Opcode::SPLIT_COMPLETE | Opcode::SPLIT_SPLATTERPOINTS | Opcode::SPLIT_GEOMCENTER;
  35.     creator.NoLeaf               = false;
  36.     creator.Quantized            = false;
  37.     // Debug
  38.     creator.KeepOriginal         = true;
  39.  
  40.     for( int i=0;i<9;++i ) {
  41.     if( i>0 && i%3==0 )
  42.         cout << endl;
  43.     cout << "tri " << i << ": " << creator.Tris[i] << "  ";
  44.     }
  45.     cout << endl;
  46.  
  47.  
  48.  
  49.     // 2) Build the model
  50.     Opcode::OPCODE_Model aModel;
  51.     bool Status = aModel.Build(creator);
  52.     if( !Status ) {
  53.     cerr << "building model failed" << endl;
  54.     exit(1);
  55.     } else {
  56.     cout << "created OPCODE model" << endl;//  with %i nodes, using %i bytes from %i triangles", aModel.GetNbNodes(), aModel.GetUsedBytes(), aTriangles.size() );
  57.     }
  58.  
  59.  
  60.     const AABBTree* tree=aModel.GetSourceTree();
  61.     cout << "tree depth: " << tree->ComputeDepth() << ", is complete " << boolalpha << tree->IsComplete() << ", number of nodes: " << tree->GetNbNodes()
  62.      << ", is leaf: " << boolalpha << tree->IsLeaf() << endl;
  63.     for( int i=0; i<tree->GetNbNodes();++i ) {
  64.     cout << "  index: " << tree->GetIndices()[i] << endl;
  65.     }
  66.  
  67.  
  68.     queue<const AABBTreeNode*> trees;
  69.     const AABBTreeNode* temp;
  70.     trees.push(aModel.GetSourceTree());
  71.     while(!trees.empty()) {
  72.     cout << "queue size: " << trees.size() << ", node: " << trees.front() << endl;
  73.     cout << "  size: " << trees.front()->GetNodeSize() << endl;
  74.  
  75.     for( int i=0;i<trees.front()->GetNbPrimitives();++i ) {
  76.         int index=trees.front()->GetPrimitives()[i];
  77.         cout << " " << i << ", " << index << ": " << tree->GetIndices()[index] << endl;
  78.     }
  79.     const AABB* aabb = trees.front()->GetAABB();
  80.     Point min,max;
  81.     aabb->GetMin(min);
  82.     aabb->GetMax(max);
  83.     cout << " min: " << min.x << " " << min.y << " " << min.z << ", max: " << max.x << " " << max.y << " " << max.z << endl;
  84.  
  85.     if( (temp=trees.front()->GetPos()) != 0 ) {
  86.         trees.push(temp);
  87.     }
  88.     if( (temp=trees.front()->GetNeg()) != 0 ) {
  89.         trees.push(temp);
  90.     }
  91.     trees.pop();
  92.     }
  93.  
  94.  
  95.  
  96. ////////////////////// OBB setup /////////////////////////////////
  97.     Point center(0,0,0);
  98.     Point extents(2,2,2);
  99.     IceMaths::Matrix3x3 transform( 1,0,0, 0,1,0, 0,0,1 );
  100.     OBB obb( center, extents, transform );
  101.  
  102.     Point corners[8];
  103.     obb.ComputePoints(corners);
  104.  
  105.     cout << "OBB corners: " << endl;
  106.     for( int i=0; i<8; ++i ) {
  107.     cout << " " << i << ": " << corners[i].x << " " << corners[i].y << " " << corners[i].z << endl;
  108.     }
  109.     for( int i=0;i<(sizeof(vertices)/sizeof(Point));++i) {
  110.     cout << "  point " << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z;
  111.     if( obb.ContainsPoint(vertices[i]) ) {
  112.         cout << " contained" << endl;
  113.     } else {
  114.         cout << " not contained" << endl;
  115.     }
  116.     }
  117.    
  118.  
  119. ///////////////////// Collider ///////////////////////////////////
  120.     Opcode::OBBCollider obbCollider;
  121.     obbCollider.SetFirstContact(true);
  122.     obbCollider.SetTemporalCoherence(false);
  123.     obbCollider.SetFullBoxBoxTest(true);
  124.     //3) Setup object callback or pointers:
  125.     obbCollider.SetPointers(triangles, vertices);
  126.  
  127.     const char* problem = obbCollider.ValidateSettings();
  128.     if( problem==0 ) {
  129.     std::cerr << "settings are fine" << std::endl;
  130.     } else {
  131.     std::cerr << "setting problem: " << problem << std::endl;
  132.     }
  133.  
  134.     //4) Perform a collision query
  135.     static Opcode::OBBCache cache;
  136.     IceMaths::Matrix4x4 id( 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 );
  137.     bool IsOk = obbCollider.Collide(cache, obb, &aModel);
  138.  
  139.     if( !IsOk ) {
  140.     std::cerr << "collision detection could not be performed" << std::endl;
  141.     } else {
  142.     cout << "touched primitives: " << obbCollider.GetNbTouchedPrimitives() << ", volume-bv tests: " << obbCollider.GetNbVolumeBVTests() << ", volume-prim tests: " << obbCollider.GetNbVolumePrimTests() << std::endl;
  143.     }
  144.  
  145.     if( obbCollider.GetContactStatus() ) {
  146.     cout << endl << ":)" << "Collision!" << endl << ":)" << endl;
  147.     } else {
  148.     cout << "No collision!" << endl;
  149.     }
  150.  
  151.     if( obbCollider.ContactFound() ) {
  152.     cout << "contact found" << endl;
  153.     } else {
  154.     cout << "contact not found" << endl;
  155.     }
  156.    
  157.  
  158.  
  159. }
  160.  
  161. #include "OPCODE-1.2/Opcode.h"
  162. #include <iostream>
  163. #include <queue>
  164.  
  165. using namespace std;
  166. using namespace Opcode;
  167.  
  168. int main() {
  169.  
  170.  
  171.     /////////////////////// Model //////////////////////
  172.     Point vertices[6];
  173.     vertices[0] = Point(1,3,0);
  174.     vertices[1] = Point(1,-1,1);
  175.     vertices[2] = Point(1,-1,-3);
  176.     vertices[3] = Point(0,-1,1);
  177.     vertices[4] = Point(0,-1,-3);
  178.     vertices[5] = Point(-1,-1,1);
  179.     IndexedTriangle triangles[3];
  180.     triangles[0] = IndexedTriangle(0,2,1);
  181.     triangles[1] = IndexedTriangle(0,4,3);
  182.     triangles[2] = IndexedTriangle(1,3,5);
  183.    
  184.     Opcode::OPCODECREATE creator;
  185.  
  186.     creator.NbTris               = 3;
  187.     //creator.NbTris               = aIndices.size()/3;
  188.     creator.NbVerts              = 6;
  189.     //creator.Tris                 = aIndices.getArray();
  190.     creator.Tris                 = (udword*) triangles;
  191.     creator.Verts                = vertices;
  192.     // Tree building settings
  193.     creator.Rules                = Opcode::SPLIT_COMPLETE | Opcode::SPLIT_SPLATTERPOINTS | Opcode::SPLIT_GEOMCENTER;
  194.     creator.NoLeaf               = false;
  195.     creator.Quantized            = false;
  196.     // Debug
  197.     creator.KeepOriginal         = true;
  198.  
  199.     for( int i=0;i<9;++i ) {
  200.     if( i>0 && i%3==0 )
  201.         cout << endl;
  202.     cout << "tri " << i << ": " << creator.Tris[i] << "  ";
  203.     }
  204.     cout << endl;
  205.  
  206.  
  207.  
  208.     // 2) Build the model
  209.     Opcode::OPCODE_Model aModel;
  210.     bool Status = aModel.Build(creator);
  211.     if( !Status ) {
  212.     cerr << "building model failed" << endl;
  213.     exit(1);
  214.     } else {
  215.     cout << "created OPCODE model" << endl;//  with %i nodes, using %i bytes from %i triangles", aModel.GetNbNodes(), aModel.GetUsedBytes(), aTriangles.size() );
  216.     }
  217.  
  218.  
  219.     const AABBTree* tree=aModel.GetSourceTree();
  220.     cout << "tree depth: " << tree->ComputeDepth() << ", is complete " << boolalpha << tree->IsComplete() << ", number of nodes: " << tree->GetNbNodes()
  221.      << ", is leaf: " << boolalpha << tree->IsLeaf() << endl;
  222.     for( int i=0; i<tree->GetNbNodes();++i ) {
  223.     cout << "  index: " << tree->GetIndices()[i] << endl;
  224.     }
  225.  
  226.  
  227.     queue<const AABBTreeNode*> trees;
  228.     const AABBTreeNode* temp;
  229.     trees.push(aModel.GetSourceTree());
  230.     while(!trees.empty()) {
  231.     cout << "queue size: " << trees.size() << ", node: " << trees.front() << endl;
  232.     cout << "  size: " << trees.front()->GetNodeSize() << endl;
  233.  
  234.     for( int i=0;i<trees.front()->GetNbPrimitives();++i ) {
  235.         int index=trees.front()->GetPrimitives()[i];
  236.         cout << " " << i << ", " << index << ": " << tree->GetIndices()[index] << endl;
  237.     }
  238.     const AABB* aabb = trees.front()->GetAABB();
  239.     Point min,max;
  240.     aabb->GetMin(min);
  241.     aabb->GetMax(max);
  242.     cout << " min: " << min.x << " " << min.y << " " << min.z << ", max: " << max.x << " " << max.y << " " << max.z << endl;
  243.  
  244.     if( (temp=trees.front()->GetPos()) != 0 ) {
  245.         trees.push(temp);
  246.     }
  247.     if( (temp=trees.front()->GetNeg()) != 0 ) {
  248.         trees.push(temp);
  249.     }
  250.     trees.pop();
  251.     }
  252.  
  253.  
  254.  
  255. ////////////////////// AABB setup /////////////////////////////////
  256.     Point center(0,0,0);
  257.     Point extents(2,2,2);
  258.     IceMaths::Matrix3x3 transform( 1,0,0, 0,1,0, 0,0,1 );
  259.     AABB aabb;
  260.     aabb.SetCenterExtents(center, extents);
  261.  
  262.     Point min, max;
  263.     aabb.GetMin(min);
  264.     aabb.GetMax(max);
  265.     cout << "aabb min: " << min.x << " " << min.y << " " << min.z << ", max: " << max.x << " " << max.y << " " << max.z << endl;
  266.  
  267.    
  268.  
  269. ///////////////////// Collider ///////////////////////////////////
  270.     Opcode::AABBCollider aabbCollider;
  271.     aabbCollider.SetFirstContact(true);
  272.     aabbCollider.SetTemporalCoherence(false);
  273.     //3) Setup object callback or pointers:
  274.     aabbCollider.SetPointers(triangles, vertices);
  275.  
  276.     const char* problem = aabbCollider.ValidateSettings();
  277.     if( problem==0 ) {
  278.     std::cerr << "settings are fine" << std::endl;
  279.     } else {
  280.     std::cerr << "setting problem: " << problem << std::endl;
  281.     }
  282.  
  283.     //4) Perform a collision query
  284.     static Opcode::AABBCache cache;
  285.     IceMaths::Matrix4x4 id( 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 );
  286.     bool IsOk = aabbCollider.Collide(cache, aabb, &aModel);
  287.  
  288.     if( !IsOk ) {
  289.     std::cerr << "collision detection could not be performed" << std::endl;
  290.     } else {
  291.     cout << "touched primitives: " << aabbCollider.GetNbTouchedPrimitives() << ", volume-bv tests: " << aabbCollider.GetNbVolumeBVTests() << ", volume-prim tests: " << aabbCollider.GetNbVolumePrimTests() << std::endl;
  292.     }
  293.  
  294.     if( aabbCollider.GetContactStatus() ) {
  295.     cout << endl << ":)" << "Collision!" << endl << ":)" << endl;
  296.     } else {
  297.     cout << "No collision!" << endl;
  298.     }
  299.  
  300.     if( aabbCollider.ContactFound() ) {
  301.     cout << "contact found" << endl;
  302.     } else {
  303.     cout << "contact not found" << endl;
  304.     }
  305.    
  306.  
  307.  
  308. }
  309.  
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement