resetting manifest requested domain to floor
[platform/upstream/libbullet.git] / Extras / CDTestFramework / OpcodeArraySAPTest.cpp
1 /*
2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman,  pierre@codercorner.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "stdafx.h"
17 #include "OpcodeArraySAPTest.h"
18 #include "RenderingHelpers.h"
19 #include "GLFontRenderer.h"
20
21 static udword gNbCreatedPairs;
22 static udword gNbDeletedPairs;
23 static udword gTotalNbPairs;
24
25 static void* CBData = (void*)0x12345678;
26 static void* PairUserData = (void*)0xDeadDead;
27
28 static void* CreatePairCB(const void* object0, const void* object1, void* user_data)
29 {
30         assert(user_data==CBData);
31
32         gNbCreatedPairs++;
33         return PairUserData;
34 }
35
36 static void DeletePairCB(const void* object0, const void* object1, void* user_data, void* pair_user_data)
37 {
38         assert(user_data==CBData);
39         assert(pair_user_data==PairUserData);
40
41         gNbDeletedPairs++;
42 }
43
44 OpcodeArraySAPTest::OpcodeArraySAPTest(int numBoxes) :
45         mBar                    (null),
46         mNbBoxes                (numBoxes),
47         mBoxes                  (null),
48         mHandles                (null),
49         mBoxTime                (null),
50         mAmplitude              (100.0f)
51 {
52 }
53
54 OpcodeArraySAPTest::~OpcodeArraySAPTest()
55 {
56         Release();
57 }
58
59 void OpcodeArraySAPTest::Init()
60 {
61         m_firstTime = true;
62
63         SRand(0);
64         mBoxes = new AABB[mNbBoxes];
65         mBoxTime = new float[mNbBoxes];
66         mHandles = new void*[mNbBoxes];
67         for(udword i=0;i<mNbBoxes;i++)
68         {
69                 Point Center, Extents;
70
71                 Center.x = (UnitRandomFloat()-0.5f) * 100.0f;
72                 Center.y = (UnitRandomFloat()-0.5f) * 10.0f;
73                 Center.z = (UnitRandomFloat()-0.5f) * 100.0f;
74                 Extents.x = 2.0f + UnitRandomFloat() * 2.0f;
75                 Extents.y = 2.0f + UnitRandomFloat() * 2.0f;
76                 Extents.z = 2.0f + UnitRandomFloat() * 2.0f;
77
78                 mBoxes[i].SetCenterExtents(Center, Extents);
79
80                 mBoxTime[i] = 2000.0f*UnitRandomFloat();
81         }
82
83         UpdateBoxes(mNbBoxes);
84
85         for(udword i=0;i<mNbBoxes;i++)
86         {
87                 // It is mandatory to pass a valid pointer as a first parameter. This is supposed to be the game object
88                 // associated with the AABB. In this small example I just pass a pointer to the SAP itself.
89                 mHandles[i] = (void*)mASAP.AddObject(&mASAP, i, mBoxes[i]);
90         }
91
92         udword MyNbInitialPairs = mASAP.DumpPairs(CreatePairCB, DeletePairCB, CBData);
93         gTotalNbPairs = MyNbInitialPairs;
94 }
95
96 void OpcodeArraySAPTest::Release()
97 {
98         DELETEARRAY(mHandles);
99         DELETEARRAY(mBoxTime);
100         DELETEARRAY(mBoxes);
101 }
102
103 extern float    objectSpeed;
104
105 void OpcodeArraySAPTest::Select()
106 {
107         // Create a tweak bar
108         {
109                 mBar = TwNewBar("OpcodeArraySAPTest");
110                 TwAddVarRW(mBar, "Speed", TW_TYPE_FLOAT, &objectSpeed, " min=0.0 max=0.01 step=0.00001");
111                 TwAddVarRW(mBar, "Amplitude", TW_TYPE_FLOAT, &mAmplitude, " min=10.0 max=200.0 step=0.1");
112         }
113 }
114
115 void OpcodeArraySAPTest::Deselect()
116 {
117         if(mBar)
118         {
119                 TwDeleteBar(mBar);
120                 mBar = null;
121         }
122 }
123
124 bool OpcodeArraySAPTest::UpdateBoxes(int numBoxes)
125 {
126         for(int i=0;i<numBoxes;i++)
127         {
128                 mBoxTime[i] += objectSpeed;
129
130                 Point Center,Extents;
131                 mBoxes[i].GetExtents(Extents);
132
133                 Center.x = cosf(mBoxTime[i]*2.17f)*mAmplitude + sinf(mBoxTime[i])*mAmplitude*0.5f;
134                 Center.y = cosf(mBoxTime[i]*1.38f)*mAmplitude + sinf(mBoxTime[i]*mAmplitude);
135                 Center.z = sinf(mBoxTime[i]*0.777f)*mAmplitude;
136
137                 mBoxes[i].SetCenterExtents(Center, Extents);
138         }
139         return true;
140 }
141
142 extern int      percentUpdate;
143 extern bool     enableDraw;
144
145 void OpcodeArraySAPTest::PerformTest()
146 {
147         int numBoxes = (mNbBoxes*percentUpdate)/100;
148         if (m_firstTime)
149         {
150                 numBoxes = mNbBoxes;
151                 m_firstTime = false;
152         }
153
154         mProfiler.Start();
155         UpdateBoxes(numBoxes);
156
157         gNbCreatedPairs = 0;
158         gNbDeletedPairs = 0;
159
160         for(int i=0;i<numBoxes;i++)
161         {
162                 mASAP.UpdateObject((udword)mHandles[i], mBoxes[i]);
163         }
164
165         ASAP_Pair* Pairs;
166         udword NbPairs = mASAP.DumpPairs(CreatePairCB, DeletePairCB, CBData, &Pairs);
167
168         gTotalNbPairs += gNbCreatedPairs;
169         gTotalNbPairs -= gNbDeletedPairs;
170         
171
172         mProfiler.End();
173         mProfiler.Accum();
174
175 //      printf("%d pairs colliding\r     ", mPairs.GetNbPairs());
176
177         bool* Flags = (bool*)_alloca(sizeof(bool)*mNbBoxes);
178         ZeroMemory(Flags, sizeof(bool)*mNbBoxes);
179         for(udword i=0;i<NbPairs;i++)
180         {
181                 Flags[Pairs[i].id0] = true;
182                 Flags[Pairs[i].id1] = true;
183         }
184
185         // Render boxes
186         if(enableDraw)
187                 {
188                 OBB CurrentBox;
189                 CurrentBox.mRot.Identity();
190                 for(udword i=0;i<mNbBoxes;i++)
191                 {
192                         if(Flags[i])    glColor3f(1.0f, 0.0f, 0.0f);
193                         else                    glColor3f(0.0f, 1.0f, 0.0f);
194                         mBoxes[i].GetCenter(CurrentBox.mCenter);
195                         mBoxes[i].GetExtents(CurrentBox.mExtents);
196                         DrawOBB(CurrentBox);
197                 }
198                 }
199
200         char Buffer[4096];
201         sprintf(Buffer, "OpcodeArraySAPTest:  %5.1f us (%d cycles) : %d pairs\n", mProfiler.mMsTime, mProfiler.mCycles, NbPairs);
202         GLFontRenderer::print(10.0f, 10.0f, 0.02f, Buffer);
203 }
204
205 void OpcodeArraySAPTest::KeyboardCallback(unsigned char key, int x, int y)
206 {
207 }
208
209 void OpcodeArraySAPTest::MouseCallback(int button, int state, int x, int y)
210 {
211 }
212
213 void OpcodeArraySAPTest::MotionCallback(int x, int y)
214 {
215 }