add hello world example and edit source tree

This commit is contained in:
Irlan
2017-02-27 18:59:58 -03:00
parent ed0159f4b5
commit 71ee97aaa0
49 changed files with 177 additions and 14 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,120 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef DEBUG_DRAW_H
#define DEBUG_DRAW_H
#include <bounce/bounce.h>
#include "mat44.h"
struct DrawPoints;
struct DrawLines;
struct DrawTriangles;
struct DrawWire;
struct DrawSolid;
class Camera
{
public:
Camera()
{
m_center.Set(0.0f, 5.0f, 0.0f);
m_q.SetIdentity();
m_width = 1024.0f;
m_height = 768.0f;
m_zNear = 1.0f;
m_zFar = 500.0f;
m_fovy = 0.25f * B3_PI;
m_zoom = 10.0f;
}
Mat44 BuildProjectionMatrix() const;
Mat44 BuildViewMatrix() const;
b3Transform BuildViewTransform() const;
Mat44 BuildWorldMatrix() const;
b3Transform BuildWorldTransform() const;
b3Vec2 ConvertWorldToScreen(const b3Vec3& pw) const;
Ray3 ConvertScreenToWorld(const b3Vec2& ps) const;
float32 m_zoom;
b3Vec3 m_center;
b3Quat m_q;
float32 m_width, m_height;
float32 m_fovy;
float32 m_zNear;
float32 m_zFar;
};
class DebugDraw : public b3Draw
{
public:
DebugDraw();
~DebugDraw();
void DrawPoint(const b3Vec3& p, float32 size, const b3Color& color);
void DrawSegment(const b3Vec3& p1, const b3Vec3& p2, const b3Color& color);
void DrawTriangle(const b3Vec3& p1, const b3Vec3& p2, const b3Vec3& p3, const b3Color& color);
void DrawSolidTriangle(const b3Vec3& normal, const b3Vec3& p1, const b3Vec3& p2, const b3Vec3& p3, const b3Color& color);
void DrawPolygon(const b3Vec3* vertices, u32 count, const b3Color& color);
void DrawSolidPolygon(const b3Vec3& normal, const b3Vec3* vertices, u32 count, const b3Color& color);
void DrawCircle(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSolidCircle(const b3Vec3& normal, const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSphere(const b3Vec3& center, float32 radius, const b3Color& color);
void DrawSolidSphere(const b3Vec3& center, float32 radius, const b3Color& color);
void DrawAABB(const b3AABB3& aabb, const b3Color& color);
void DrawTransform(const b3Transform& xf);
//
void DrawString(const char* string, const b3Color& color, ...);
void DrawSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf);
void DrawCapsule(const b3CapsuleShape* s, const b3Color& c, const b3Transform& xf);
void DrawHull(const b3HullShape* s, const b3Color& c, const b3Transform& xf);
void DrawMesh(const b3MeshShape* s, const b3Color& c, const b3Transform& xf);
void DrawShape(const b3Shape* s, const b3Color& c, const b3Transform& xf);
void Draw(const b3World& world);
void Submit();
private:
friend struct DrawShapes;
DrawPoints* m_points;
DrawLines* m_lines;
DrawTriangles* m_triangles;
DrawWire* m_wire;
DrawSolid* m_solid;
};
#endif

View File

@ -0,0 +1,493 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <glad/glad.h>
#include <glfw/glfw3.h>
#include <imgui/imgui.h>
#include <imgui/imgui_impl_glfw_gl3.h>
#include <testbed/tests/test.h>
GLFWwindow* g_window;
Settings g_settings;
Test* g_test;
u32 g_testCount;
Camera g_camera;
DebugDraw* g_debugDraw;
Profiler* g_profiler;
bool g_leftDown;
bool g_rightDown;
bool g_shiftDown;
b3Vec2 g_ps0;
void WindowSize(int w, int h)
{
g_camera.m_width = float32(w);
g_camera.m_height = float32(h);
}
void MouseMove(GLFWwindow* w, double x, double y)
{
b3Vec2 ps;
ps.Set(float32(x), float32(y));
b3Vec2 dp = ps - g_ps0;
g_ps0 = ps;
Ray3 pw = g_camera.ConvertScreenToWorld(ps);
float32 nx = b3Clamp(dp.x, -1.0f, 1.0f);
float32 ny = b3Clamp(dp.y, -1.0f, 1.0f);
if (g_shiftDown)
{
if (g_leftDown)
{
// Negate angles to do positive rotations (CCW) of the world.
float32 angleX = 0.005f * B3_PI * -nx;
float32 angleY = 0.005f * B3_PI * -ny;
b3Quat qx(b3Vec3(1.0f, 0.0f, 0.0f), angleY);
b3Quat qy(b3Vec3(0.0f, 1.0f, 0.0f), angleX);
g_camera.m_q = qy * g_camera.m_q;
g_camera.m_q = g_camera.m_q * qx;
g_camera.m_q.Normalize();
}
if (g_rightDown)
{
b3Transform xf = g_camera.BuildWorldTransform();
g_camera.m_center += 0.2f * nx * xf.rotation.x;
g_camera.m_center += 0.2f * -ny * xf.rotation.y;
}
}
else
{
g_test->MouseMove(pw);
}
}
void MouseWheel(GLFWwindow* w, double dx, double dy)
{
float32 n = b3Clamp(float32(dy), -1.0f, 1.0f);
if (g_shiftDown)
{
g_camera.m_zoom += 0.5f * -n;
}
}
void MouseButton(GLFWwindow* w, int button, int action, int mods)
{
double x, y;
glfwGetCursorPos(w, &x, &y);
b3Vec2 p;
p.Set(float32(x), float32(y));
Ray3 pw = g_camera.ConvertScreenToWorld(p);
switch (action)
{
case GLFW_PRESS:
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
g_leftDown = true;
if (g_shiftDown == false)
{
g_test->MouseLeftDown(pw);
}
}
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
g_rightDown = true;
}
break;
}
case GLFW_RELEASE:
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
g_leftDown = false;
if (g_shiftDown == false)
{
g_test->MouseLeftUp(pw);
}
}
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
g_rightDown = false;
}
break;
}
default:
{
break;
}
}
}
void KeyButton(GLFWwindow* w, int button, int scancode, int action, int mods)
{
switch (action)
{
case GLFW_PRESS:
{
if (button == GLFW_KEY_LEFT_SHIFT)
{
g_shiftDown = true;
g_test->KeyDown(button);
}
if (g_shiftDown)
{
if (button == GLFW_KEY_DOWN)
{
g_camera.m_zoom += 0.05f;
}
if (button == GLFW_KEY_UP)
{
g_camera.m_zoom -= 0.05f;
}
}
else
{
g_test->KeyDown(button);
}
break;
}
case GLFW_RELEASE:
{
if (button == GLFW_KEY_LEFT_SHIFT)
{
g_shiftDown = false;
}
if (g_shiftDown == false)
{
g_test->KeyUp(button);
}
break;
}
default:
{
break;
}
}
}
void Char(GLFWwindow* w, unsigned int codepoint)
{
ImGui_ImplGlfwGL3_CharCallback(w, codepoint);
}
void CreateInterface()
{
ImGui_ImplGlfwGL3_Init(g_window, false);
ImGuiIO& io = ImGui::GetIO();
io.Fonts[0].AddFontDefault();
}
void DestroyInterface()
{
ImGui_ImplGlfwGL3_Shutdown();
}
bool GetTestName(void*, int idx, const char** name)
{
*name = g_tests[idx].name;
return true;
}
void Interface()
{
ImGui::SetNextWindowPos(ImVec2(g_camera.m_width, 0.0f));
ImGui::SetNextWindowSize(ImVec2(250.0f, g_camera.m_height));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::Begin("Controls", NULL, ImVec2(0.0f, 0.0f), 0.25f, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
ImGui::PushItemWidth(-1.0f);
ImGui::Text("Test");
if (ImGui::Combo("##Test", &g_settings.testID, GetTestName, NULL, g_testCount, g_testCount))
{
delete g_test;
g_test = g_tests[g_settings.testID].create();
g_settings.lastTestID = -1;
}
ImVec2 buttonSize = ImVec2(-1, 0);
if (ImGui::Button("Restart", buttonSize))
{
g_settings.lastTestID = -1;
}
if (ImGui::Button("Previous", buttonSize))
{
g_settings.testID = b3Clamp(g_settings.testID - 1, 0, int(g_testCount) - 1);
g_settings.lastTestID = -1;
}
if (ImGui::Button("Next", buttonSize))
{
g_settings.testID = b3Clamp(g_settings.testID + 1, 0, int(g_testCount) - 1);
g_settings.lastTestID = -1;
}
if (ImGui::Button("Exit", buttonSize))
{
glfwSetWindowShouldClose(g_window, true);
}
ImGui::Separator();
ImGui::Text("Step");
ImGui::Text("Hertz");
ImGui::SliderFloat("##Hertz", &g_settings.hertz, 0.0f, 240.0f, "%.1f");
ImGui::Text("Velocity Iterations");
ImGui::SliderInt("##Velocity Iterations", &g_settings.velocityIterations, 0, 50);
ImGui::Text("Position Iterations");
ImGui::SliderInt("#Position Iterations", &g_settings.positionIterations, 0, 50);
ImGui::Checkbox("Sleep", &g_settings.sleep);
ImGui::Checkbox("Warm Start", &g_settings.warmStart);
//ImGui::Checkbox("Convex Cache", &g_settings.convexCache);
if (ImGui::Button("Play/Pause", buttonSize))
{
g_settings.pause = !g_settings.pause;
}
if (ImGui::Button("Single Step", buttonSize))
{
g_settings.pause = true;
g_settings.singleStep = true;
}
ImGui::Separator();
ImGui::Text("View");
ImGui::Checkbox("Grid", &g_settings.drawGrid);
ImGui::Checkbox("Vertices and Edges", &g_settings.drawVerticesEdges);
ImGui::Checkbox("Faces", &g_settings.drawFaces);
ImGui::Checkbox("Center of Masses", &g_settings.drawCenterOfMasses);
ImGui::Checkbox("Bounding Boxes", &g_settings.drawBounds);
ImGui::Checkbox("Joints", &g_settings.drawJoints);
ImGui::Checkbox("Contact Points", &g_settings.drawContactPoints);
ImGui::Checkbox("Contact Normals", &g_settings.drawContactNormals);
ImGui::Checkbox("Contact Tangents", &g_settings.drawContactTangents);
ImGui::Checkbox("Statistics", &g_settings.drawStats);
ImGui::Checkbox("Profile", &g_settings.drawProfile);
ImGui::End();
ImGui::PopStyleVar();
}
void Step()
{
if (g_settings.drawGrid)
{
int n = 20;
b3Vec3 t;
t.x = -0.5f * float32(n);
t.y = 0.0f;
t.z = -0.5f * float32(n);
b3Color color(0.5f, 0.5f, 0.5f, 1.0f);
for (int i = 0; i < n; i += 1)
{
for (int j = 0; j < n; j += 1)
{
b3Vec3 vs[4];
vs[0] = b3Vec3((float)i, 0.0f, (float)j);
vs[1] = b3Vec3((float)i, 0.0f, (float)j + 1);
vs[2] = b3Vec3((float)i + 1, 0.0f, (float)j + 1);
vs[3] = b3Vec3((float)i + 1, 0.0f, (float)j);
for (u32 k = 0; k < 4; ++k)
{
vs[k] += t;
}
g_debugDraw->DrawPolygon(vs, 4, color);
}
}
}
if (g_settings.testID != g_settings.lastTestID)
{
delete g_test;
g_settings.lastTestID = g_settings.testID;
g_test = g_tests[g_settings.testID].create();
g_settings.pause = true;
}
g_test->Step();
g_debugDraw->Submit();
}
void Run()
{
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClearDepth(1.0f);
double t1 = glfwGetTime();
double frameTime = 0.0;
while (glfwWindowShouldClose(g_window) == 0)
{
int width, height;
glfwGetWindowSize(g_window, &width, &height);
g_camera.m_width = float32(width) - 250.0f;
g_camera.m_height = float32(height);
glViewport(0, 0, width - 250, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplGlfwGL3_NewFrame();
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2((float)g_camera.m_width, (float)g_camera.m_height));
ImGui::Begin("Overlay", NULL, ImVec2(0, 0), 0.0f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImGui::SetCursorPos(ImVec2(5, (float)g_camera.m_height - 20));
ImGui::Text("%.1f ms", 1000.0 * frameTime);
ImGui::End();
Interface();
Step();
double t = glfwGetTime();
frameTime = t - t1;
t1 = t;
ImGui::Render();
glfwSwapBuffers(g_window);
glfwPollEvents();
}
}
int main(int argc, char** args)
{
#if defined(_WIN32)
// Report memory leaks
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
#endif
if (glfwInit() == 0)
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
// Create window
extern b3Version b3_version;
char title[256];
sprintf(title, "Bounce Testbed Version %d.%d.%d", b3_version.major, b3_version.minor, b3_version.revision);
#if defined(__APPLE__)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
g_window = glfwCreateWindow(1024, 768, title, NULL, NULL);
if (g_window == NULL)
{
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(g_window);
glfwSetCursorPosCallback(g_window, MouseMove);
glfwSetScrollCallback(g_window, MouseWheel);
glfwSetMouseButtonCallback(g_window, MouseButton);
glfwSetKeyCallback(g_window, KeyButton);
glfwSetCharCallback(g_window, Char);
glfwSwapInterval(1);
if (gladLoadGL() == 0)
{
fprintf(stderr, "Failed to load OpenGL extensions\n");
fprintf(stderr, "Error: %d\n", glad_glGetError());
glfwTerminate();
return -1;
}
printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
g_leftDown = false;
g_rightDown = false;
g_shiftDown = false;
g_ps0.SetZero();
// Create UI
CreateInterface();
// Create profiler
g_profiler = new Profiler();
// Create renderer
g_debugDraw = new DebugDraw();
// Run the testbed
g_testCount = 0;
while (g_tests[g_testCount].create != NULL)
{
++g_testCount;
}
g_test = NULL;
Run();
// Destroy the last test
if (g_test)
{
delete g_test;
g_test = NULL;
}
// Destroy renderer
delete g_debugDraw;
// Destroy profiler
delete g_profiler;
// Destroy UI
DestroyInterface();
// Destroy g_window
glfwTerminate();
return 0;
}

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef MAT44_H
#define MAT44_H
#include <bounce/bounce.h>
struct Vec4
{
Vec4() { }
Vec4(float32 _x, float32 _y, float32 _z, float32 _w) : x(_x), y(_y), z(_z), w(_w) { }
void SetZero()
{
x = y = z = w = 0.0f;
}
void Set(float32 _x, float32 _y, float32 _z, float32 _w)
{
x = _x;
y = _y;
z = _z;
w = _w;
}
float32 x, y, z, w;
};
inline Vec4 operator+(const Vec4& a, const Vec4& b)
{
return Vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
inline Vec4 operator*(float32 s, const Vec4& v)
{
return Vec4(s * v.x, s * v.y, s * v.z, s * v.w);
}
struct Mat44
{
Mat44() { }
Mat44(const Vec4& _x, const Vec4& _y, const Vec4& _z, const Vec4& _w) : x(_x), y(_y), z(_z), w(_w) { }
void SetIdentity()
{
x.Set(1.0f, 0.0f, 0.0f, 0.0f);
y.Set(0.0f, 1.0f, 0.0f, 0.0f);
z.Set(0.0f, 0.0f, 1.0f, 0.0f);
w.Set(0.0f, 0.0f, 0.0f, 1.0f);
}
Vec4 x, y, z, w;
};
inline Vec4 operator*(const Mat44& A, const Vec4& v)
{
return v.x * A.x + v.y * A.y + v.z * A.z + v.w * A.w;
}
inline b3Vec3 operator*(const Mat44& A, const b3Vec3& v)
{
Vec4 q = v.x * A.x + v.y * A.y + v.z * A.z + A.w;
return b3Vec3(q.x, q.y, q.z);
}
inline Mat44 operator*(const Mat44& A, const Mat44& B)
{
return Mat44(A * B.x, A * B.y, A * B.z, A * B.w);
}
inline Mat44 GetMat44(const b3Transform& T)
{
return Mat44(
Vec4(T.rotation.x.x, T.rotation.x.y, T.rotation.x.z, 0.0f),
Vec4(T.rotation.y.x, T.rotation.y.y, T.rotation.y.z, 0.0f),
Vec4(T.rotation.z.x, T.rotation.z.y, T.rotation.z.z, 0.0f),
Vec4(T.position.x, T.position.y, T.position.z, 1.0f));
}
inline b3Transform GetTransform(const Mat44& T)
{
b3Transform xf;
xf.rotation.x.Set(T.x.x, T.x.y, T.x.z);
xf.rotation.y.Set(T.y.x, T.y.y, T.y.z);
xf.rotation.z.Set(T.z.x, T.z.y, T.z.z);
xf.position.Set(T.w.x, T.w.y, T.w.z);
return xf;
}
inline float32 RandomFloat(float32 a, float32 b)
{
float32 x = float32(rand()) / float32(RAND_MAX);
float32 diff = b - a;
float32 r = x * diff;
return a + r;
}
struct Ray3
{
b3Vec3 Start() const
{
return origin;
}
b3Vec3 End() const
{
return origin + fraction * direction;
}
b3Vec3 direction;
b3Vec3 origin;
float32 fraction;
};
#endif

View File

@ -0,0 +1,217 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <testbed/framework/profiler.h>
#include <bounce/common/profiler.h>
#include <bounce/common/time.h>
#include <bounce/common/template/queue.h>
struct Event
{
i32 tid;
i32 pid;
const char* name;
float64 t0;
float64 t1;
Event* parent;
};
static b3Time time;
static b3BoundedQueue<Event, 256> events;
static Event* top = NULL;
bool b3PushProfileScope(const char* name)
{
time.Update();
Event e;
e.tid = -1;
e.pid = -1;
e.t0 = time.GetCurrentMilis();
e.t1 = 0;
e.name = name;
e.parent = top;
Event* back = events.Push(e);
if (back)
{
top = back;
}
return back != NULL;
}
void b3PopProfileScope()
{
B3_ASSERT(top);
B3_ASSERT(top->t1 == 0);
time.Update();
top->t1 = time.GetCurrentMilis();
B3_ASSERT(top->t1 != 0);
top = top->parent;
}
void ProfileBegin()
{
B3_ASSERT(events.IsEmpty());
}
void ProfileEnd()
{
ProfileBeginEvents();
while (events.IsEmpty() == false)
{
const Event& e = events.Front();
events.Pop();
ProfileEvent(e.tid, e.pid, e.name, e.t0, e_begin);
ProfileEvent(e.tid, e.pid, e.name, e.t1, e_end);
ProfileEvent(e.tid, e.pid, e.name, e.t1 - e.t0);
}
B3_ASSERT(events.IsEmpty());
ProfileEndEvents();
}
//
#define PROFILER_SCREEN 1
#define PROFILER_JSON 2
#define PROFILER_OUTPUT PROFILER_SCREEN
#if PROFILER_OUTPUT == PROFILER_SCREEN
extern Profiler* g_profiler;
void ProfileBeginEvents()
{
}
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 time, ProfileType type)
{
}
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 elapsed)
{
g_profiler->Add(name, elapsed);
}
void ProfileEndEvents()
{
}
#elif PROFILER_OUTPUT == PROFILER_JSON
#include <rapidjson/filewritestream.h>
#include <rapidjson/writer.h>
using namespace rapidjson;
static FILE* file = NULL;
static FileWriteStream* stream = NULL;
static Writer<FileWriteStream>* writer = NULL;
#define STRING(x) String(x, sizeof(x) - 1)
void ProfileBeginEvents()
{
if (file)
{
return;
}
file = fopen("profile.json", "wt");
if (!file)
{
return;
}
static char buffer[512];
stream = new FileWriteStream(file, buffer, sizeof(buffer));
writer = new Writer<FileWriteStream>(*stream);
writer->StartObject();
writer->STRING("traceEvents");
writer->StartArray();
}
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 time, ProfileType type)
{
if (!writer)
{
return;
}
const char* phase = 0;
switch (type)
{
case ProfileType::e_begin: phase = "B"; break;
case ProfileType::e_end: phase = "E"; break;
default: B3_ASSERT(false);
}
float64 scale = 1000.0;
writer->StartObject();
writer->STRING("pid"); writer->Int(pid);
writer->STRING("tid"); writer->Int(tid);
writer->STRING("ts"); writer->Int64((u64)(time * scale));
writer->STRING("ph"); writer->String(phase, 1);
writer->STRING("cat"); writer->STRING("physics");
writer->STRING("name"); writer->String(name, strlen(name));
writer->STRING("args"); writer->StartObject(); writer->EndObject();
writer->EndObject();
}
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 elapsed)
{
}
void ProfileEndEvents()
{
if (!writer)
{
return;
}
writer->EndArray();
writer->EndObject();
delete writer;
writer = NULL;
delete stream;
stream = NULL;
fclose(file);
file = NULL;
}
#undef STRING
#else
#endif

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef PROFILER_H
#define PROFILER_H
#include <bounce/common/math/math.h>
#include <bounce/common/template/array.h>
enum ProfileType
{
e_begin,
e_end
};
void ProfileBeginEvents();
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 time, ProfileType type);
void ProfileEvent(i32 tid, i32 pid, const char* name, float64 elapsed);
void ProfileEndEvents();
void ProfileBegin();
void ProfileEnd();
struct ProfileRecord
{
float64 elapsed;
float64 maxElapsed;
const char* name;
};
class Profiler
{
public:
void Clear()
{
for (u32 i = 0; i < m_records.Count(); ++i)
{
m_records[i].elapsed = 0;
}
}
void Add(const char* name, float64 elapsed)
{
for (u32 i = 0; i < m_records.Count(); ++i)
{
ProfileRecord& r = m_records[i];
if (r.name == name)
{
r.elapsed += elapsed;
r.maxElapsed = b3Max(r.maxElapsed, r.elapsed);
return;
}
}
ProfileRecord r;
r.elapsed = elapsed;
r.maxElapsed = 0;
r.name = name;
m_records.PushBack(r);
}
b3StackArray<ProfileRecord, 256> m_records;
};
#endif

View File

@ -0,0 +1,499 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <testbed/tests/test.h>
extern u32 b3_allocCalls, b3_maxAllocCalls;
extern u32 b3_gjkCalls, b3_gjkIters, b3_gjkMaxIters;
extern u32 b3_convexCalls, b3_convexCacheHits;
extern bool b3_enableConvexCache;
extern b3Draw* b3_debugDraw;
extern Settings g_settings;
extern DebugDraw* g_debugDraw;
extern Camera g_camera;
extern Profiler* g_profiler;
Test::Test()
{
b3_allocCalls = 0;
b3_gjkCalls = 0;
b3_gjkIters = 0;
b3_gjkMaxIters = 0;
b3_convexCalls = 0;
b3_convexCacheHits = 0;
b3_enableConvexCache = g_settings.convexCache;
b3_debugDraw = g_debugDraw;
m_world.SetContactListener(this);
g_camera.m_q = b3Quat(b3Vec3(0.0f, 1.0f, 0.0f), 0.15f * B3_PI);
g_camera.m_q = g_camera.m_q * b3Quat(b3Vec3(1.0f, 0.0f, 0.0f), -0.15f * B3_PI);
g_camera.m_zoom = 50.0f;
g_camera.m_center.SetZero();
m_rayHit.shape = NULL;
m_mouseJoint = NULL;
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(50.0f, 1.0f, 50.0f);
m_groundHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(1.0f, 1.0f, 1.0f);
m_boxHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(1.0f, 5.0f, 1.0f);
m_tallHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(2.0f, 4.0f, 0.5f);
m_doorHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(25.0f, 0.5f, 25.0f);
m_rampHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(1.0f, 0.5f, 3.0f);
m_plankHull.SetTransform(xf);
}
{
b3Transform xf;
xf.position.SetZero();
xf.rotation = b3Diagonal(4.05f, 2.0f * B3_LINEAR_SLOP, 4.05f);
m_thinHull.SetTransform(xf);
}
{
const u32 w = 5;
const u32 h = 5;
b3Vec3 t;
t.x = -0.5f * float32(w);
t.y = 0.0f;
t.z = -0.5f * float32(h);
b3Mesh* mesh = m_meshes + e_clothMesh;
mesh->vertexCount = w * h;
mesh->vertices = (b3Vec3*)b3Alloc(mesh->vertexCount * sizeof(b3Vec3));
for (u32 i = 0; i < w; ++i)
{
for (u32 j = 0; j < h; ++j)
{
u32 v1 = i * w + j;
b3Vec3 v;
v.x = float32(i);
v.y = RandomFloat(0.0f, 0.5f);
v.z = float32(j);
v += t;
mesh->vertices[v1] = v;
}
}
mesh->triangleCount = 2 * (w - 1) * (h - 1);
mesh->triangles = (b3Triangle*)b3Alloc(mesh->triangleCount * sizeof(b3Triangle));
u32 triangleCount = 0;
for (u32 i = 0; i < w - 1; ++i)
{
for (u32 j = 0; j < h - 1; ++j)
{
u32 v1 = i * w + j;
u32 v2 = (i + 1) * w + j;
u32 v3 = (i + 1) * w + (j + 1);
u32 v4 = i * w + (j + 1);
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t1 = mesh->triangles + triangleCount;
++triangleCount;
t1->v1 = v3;
t1->v2 = v2;
t1->v3 = v1;
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t2 = mesh->triangles + triangleCount;
++triangleCount;
t2->v1 = v1;
t2->v2 = v4;
t2->v3 = v3;
}
}
B3_ASSERT(triangleCount == mesh->triangleCount);
mesh->BuildTree();
}
{
const u32 w = 50;
const u32 h = 50;
b3Vec3 t;
t.x = -0.5f * float32(w);
t.y = 0.0f;
t.z = -0.5f * float32(h);
b3Mesh* mesh = m_meshes + e_gridMesh;
mesh->vertexCount = w * h;
mesh->vertices = (b3Vec3*)b3Alloc(mesh->vertexCount * sizeof(b3Vec3));
for (u32 i = 0; i < w; ++i)
{
for (u32 j = 0; j < h; ++j)
{
u32 v1 = i * w + j;
b3Vec3 v;
v.x = float32(i);
v.y = 0.0f;
v.z = float32(j);
v += t;
mesh->vertices[v1] = v;
}
}
// 2 triangles per quad
mesh->triangleCount = 2 * (w - 1) * (h - 1);
mesh->triangles = (b3Triangle*)b3Alloc(mesh->triangleCount * sizeof(b3Triangle));
u32 triangleCount = 0;
for (u32 i = 0; i < w - 1; ++i)
{
for (u32 j = 0; j < h - 1; ++j)
{
u32 v1 = i * w + j;
u32 v2 = (i + 1) * w + j;
u32 v3 = (i + 1) * w + (j + 1);
u32 v4 = i * w + (j + 1);
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t1 = mesh->triangles + triangleCount;
++triangleCount;
t1->v1 = v3;
t1->v2 = v2;
t1->v3 = v1;
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t2 = mesh->triangles + triangleCount;
++triangleCount;
t2->v1 = v1;
t2->v2 = v4;
t2->v3 = v3;
}
}
B3_ASSERT(triangleCount == mesh->triangleCount);
mesh->BuildTree();
}
{
const u32 w = 50;
const u32 h = 50;
b3Vec3 t;
t.x = -0.5f * float32(w);
t.y = 0.0f;
t.z = -0.5f * float32(h);
b3Mesh* mesh = m_meshes + e_terrainMesh;
mesh->vertexCount = w * h;
mesh->vertices = (b3Vec3*)b3Alloc(mesh->vertexCount * sizeof(b3Vec3));
for (u32 i = 0; i < w; ++i)
{
for (u32 j = 0; j < h; ++j)
{
u32 v1 = i * w + j;
b3Vec3 v;
v.x = 2.0f * float32(i);
v.y = RandomFloat(0.0f, 0.5f);
v.z = 2.0f *float32(j);
v += t;
mesh->vertices[v1] = v;
}
}
mesh->triangleCount = 2 * (w - 1) * (h - 1);
mesh->triangles = (b3Triangle*)b3Alloc(mesh->triangleCount * sizeof(b3Triangle));
u32 triangleCount = 0;
for (u32 i = 0; i < w - 1; ++i)
{
for (u32 j = 0; j < h - 1; ++j)
{
u32 v1 = i * w + j;
u32 v2 = (i + 1) * w + j;
u32 v3 = (i + 1) * w + (j + 1);
u32 v4 = i * w + (j + 1);
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t1 = mesh->triangles + triangleCount;
++triangleCount;
t1->v1 = v3;
t1->v2 = v2;
t1->v3 = v1;
B3_ASSERT(triangleCount < mesh->triangleCount);
b3Triangle* t2 = mesh->triangles + triangleCount;
++triangleCount;
t2->v1 = v1;
t2->v2 = v4;
t2->v3 = v3;
}
}
B3_ASSERT(triangleCount == mesh->triangleCount);
mesh->BuildTree();
}
}
Test::~Test()
{
for (u32 i = 0; i < e_maxMeshes; ++i)
{
b3Free(m_meshes[i].vertices);
b3Free(m_meshes[i].triangles);
}
}
void Test::BeginContact(b3Contact* contact)
{
}
void Test::EndContact(b3Contact* contact)
{
}
void Test::PreSolve(b3Contact* contact)
{
}
void Test::Step()
{
float32 dt = g_settings.hertz > 0.0f ? 1.0f / g_settings.hertz : 0.0f;
if (g_settings.pause)
{
if (g_settings.singleStep)
{
g_settings.singleStep = false;
}
else
{
dt = 0.0f;
}
}
b3_allocCalls = 0;
b3_gjkCalls = 0;
b3_gjkIters = 0;
b3_gjkMaxIters = 0;
b3_convexCalls = 0;
b3_convexCacheHits = 0;
b3_enableConvexCache = g_settings.convexCache;
// Step
ProfileBegin();
m_world.SetSleeping(g_settings.sleep);
m_world.SetWarmStart(g_settings.warmStart);
m_world.Step(dt, g_settings.velocityIterations, g_settings.positionIterations);
ProfileEnd();
// Draw World
u32 drawFlags = 0;
drawFlags += g_settings.drawBounds * b3Draw::e_aabbsFlag;
drawFlags += g_settings.drawVerticesEdges * b3Draw::e_shapesFlag;
drawFlags += g_settings.drawCenterOfMasses * b3Draw::e_centerOfMassesFlag;
drawFlags += g_settings.drawJoints * b3Draw::e_jointsFlag;
drawFlags += g_settings.drawContactPoints * b3Draw::e_contactPointsFlag;
drawFlags += g_settings.drawContactNormals * b3Draw::e_contactNormalsFlag;
drawFlags += g_settings.drawContactTangents * b3Draw::e_contactTangentsFlag;
g_debugDraw->SetFlags(drawFlags);
m_world.DebugDraw();
g_debugDraw->Submit();
if (g_settings.drawFaces)
{
g_debugDraw->Draw(m_world);
}
// Draw Statistics
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::Begin("Log", NULL, ImVec2(0, 0), 0.0f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
if (g_settings.pause)
{
ImGui::Text("*PAUSED*");
}
if (g_settings.drawStats)
{
ImGui::Text("Bodies %d", m_world.GetBodyList().m_count);
ImGui::Text("Joints %d", m_world.GetJointList().m_count);
ImGui::Text("Contacts %d", m_world.GetContactList().m_count);
float32 avgGjkIters = 0.0f;
if (b3_gjkCalls > 0)
{
avgGjkIters = float32(b3_gjkIters) / float32(b3_gjkCalls);
}
ImGui::Text("GJK Calls %d", b3_gjkCalls);
ImGui::Text("GJK Iterations %d (%d) (%f)", b3_gjkIters, b3_gjkMaxIters, avgGjkIters);
float32 convexCacheHitRatio = 0.0f;
if (b3_convexCalls > 0)
{
convexCacheHitRatio = float32(b3_convexCacheHits) / float32(b3_convexCalls);
}
ImGui::Text("Convex Calls %d", b3_convexCalls);
ImGui::Text("Convex Cache Hits %d (%f)", b3_convexCacheHits, convexCacheHitRatio);
ImGui::Text("Frame Allocations %d (%d)", b3_allocCalls, b3_maxAllocCalls);
}
if (g_settings.drawProfile)
{
for (u32 i = 0; i < g_profiler->m_records.Count(); ++i)
{
const ProfileRecord& r = g_profiler->m_records[i];
ImGui::Text("%s %.4f (%.4f) [ms]", r.name, r.elapsed, r.maxElapsed);
}
}
g_profiler->Clear();
ImGui::End();
}
void Test::MouseMove(const Ray3& pw)
{
if (m_mouseJoint)
{
float32 t = m_rayHit.fraction;
float32 w1 = 1.0f - t;
float32 w2 = t;
b3Vec3 target = w1 * pw.Start() + w2 * pw.End();
m_mouseJoint->SetTarget(target);
}
}
void Test::MouseLeftDown(const Ray3& pw)
{
// Clear the current hit
m_rayHit.shape = NULL;
if (m_mouseJoint)
{
b3Body* groundBody = m_mouseJoint->GetBodyA();
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = NULL;
m_world.DestroyBody(groundBody);
}
b3Vec3 p1 = pw.Start();
b3Vec3 p2 = pw.End();
RayCastListener listener;
listener.hit.shape = NULL;
// Perform the ray cast
b3RayCastSingleOutput out;
if (m_world.RayCastSingle(&out, p1, p2))
{
m_rayHit = out;
RayHit();
}
}
void Test::MouseLeftUp(const Ray3& pw)
{
m_rayHit.shape = NULL;
if (m_mouseJoint)
{
b3Body* groundBody = m_mouseJoint->GetBodyA();
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = NULL;
m_world.DestroyBody(groundBody);
}
}
void Test::RayHit()
{
b3BodyDef bdef;
b3Body* bodyA = m_world.CreateBody(bdef);
b3Body* bodyB = m_rayHit.shape->GetBody();
b3MouseJointDef def;
def.bodyA = bodyA;
def.bodyB = bodyB;
def.target = m_rayHit.point;
def.maxForce = 2000.0f * bodyB->GetMass();
m_mouseJoint = (b3MouseJoint*)m_world.CreateJoint(def);
bodyB->SetAwake(true);
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <testbed/tests/test.h>
#include <testbed/tests/quickhull_test.h>
#include <testbed/tests/cluster_test.h>
#include <testbed/tests/distance_test.h>
#include <testbed/tests/capsule_distance.h>
#include <testbed/tests/collide_test.h>
#include <testbed/tests/capsule_collision.h>
#include <testbed/tests/capsule_and_hull_collision.h>
#include <testbed/tests/hull_collision.h>
#include <testbed/tests/linear_motion.h>
#include <testbed/tests/angular_motion.h>
#include <testbed/tests/multiple_shapes.h>
#include <testbed/tests/quadric_shapes.h>
#include <testbed/tests/spring.h>
#include <testbed/tests/newton_cradle.h>
#include <testbed/tests/hinge_motor.h>
#include <testbed/tests/hinge_chain.h>
#include <testbed/tests/ragdoll.h>
#include <testbed/tests/mesh_contact_test.h>
#include <testbed/tests/sphere_stack.h>
#include <testbed/tests/capsule_stack.h>
#include <testbed/tests/box_stack.h>
#include <testbed/tests/shape_stack.h>
#include <testbed/tests/jenga.h>
#include <testbed/tests/thin.h>
#include <testbed/tests/pyramid.h>
#include <testbed/tests/pyramids.h>
#include <testbed/tests/ray_cast.h>
#include <testbed/tests/sensor_test.h>
#include <testbed/tests/character_test.h>
#include <testbed/tests/body_types.h>
#include <testbed/tests/varying_friction.h>
#include <testbed/tests/varying_restitution.h>
#include <testbed/tests/cloth_test.h>
TestEntry g_tests[] =
{
{ "Quickhull Test", &QuickhullTest::Create },
{ "Cluster Test", &Cluster::Create },
{ "Distance Test", &Distance::Create },
{ "Capsule Distance", &CapsuleDistance::Create },
{ "Capsule Collision", &CapsuleCollision::Create },
{ "Capsule and Hull Collision", &CapsuleAndHull::Create },
{ "Hull Collision", &HullAndHull::Create },
{ "Linear Motion", &LinearMotion::Create },
{ "Angular Motion", &AngularMotion::Create },
{ "Multiple Shapes", &MultipleShapes::Create },
{ "Quadric Shapes", &QuadricShapes::Create },
{ "Springs", &Spring::Create },
{ "Newton's Cradle", &NewtonCradle::Create },
{ "Hinge Motor", &HingeMotor::Create },
{ "Hinge Chain", &HingeChain::Create },
{ "Ragdoll", &Ragdoll::Create },
{ "Thin Boxes", &Thin::Create },
{ "Mesh Contact Test", &MeshContactTest::Create },
{ "Sphere Stack", &SphereStack::Create },
{ "Capsule Stack", &CapsuleStack::Create },
{ "Box Stack", &BoxStack::Create },
{ "Shape Stack", &ShapeStack::Create },
{ "Jenga", &Jenga::Create },
{ "Box Pyramid", &Pyramid::Create },
{ "Box Pyramid Rows", &Pyramids::Create },
{ "Ray Cast", &RayCast::Create },
{ "Sensor Test", &SensorTest::Create },
{ "Character Test", &Character::Create },
{ "Body Types", &BodyTypes::Create },
{ "Varying Friction", &VaryingFriction::Create },
{ "Varying Restitution", &VaryingRestitution::Create },
{ "Cloth", &Cloth::Create },
{ NULL, NULL }
};