New sample renderer and UI (#10)

No changes to simulation
This commit is contained in:
Erin Catto 2026-06-06 10:06:05 -07:00 • committed by GitHub
commit 9e02b7f3b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
196 changed files with 116992 additions and 29969 deletions

View file

@ -1,17 +1,15 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#include "camera.h"
#include "mesh_loader.h"
#include "renderer.h"
#include "sample.h"
#include "scene.h"
#include "mesh_loader.h"
#include "gfx/draw.h"
#include "gfx/keycodes.h"
#include "box3d/box3d.h"
#include <glad/glad.h>
// prevent clang format sorting glad.h with glfw3.h
#include <GLFW/glfw3.h>
#include <imgui.h>
class CapsulePlane : public Sample
@ -59,24 +57,20 @@ public:
{
Sample::Render();
DrawGrid( m_scene, 10 );
DrawLine( m_scene, b3Vec3_zero, 2.0f * b3Vec3_axisX, b3_colorRed );
DrawLine( m_scene, b3Vec3_zero, 2.0f * b3Vec3_axisY, b3_colorGreen );
DrawLine( m_scene, b3Vec3_zero, 2.0f * b3Vec3_axisZ, b3_colorBlue );
DrawGroundGrid( 10 );
DrawLine( b3Vec3_zero, 2.0f * b3Vec3_axisX, MakeColor( b3_colorRed ) );
DrawLine( b3Vec3_zero, 2.0f * b3Vec3_axisY, MakeColor( b3_colorGreen ) );
DrawLine( b3Vec3_zero, 2.0f * b3Vec3_axisZ, MakeColor( b3_colorBlue ) );
}
void UpdateUI() override
bool DrawControls() override
{
ImGui::SetNextWindowPos( ImVec2( 10.0f, 600.0f ) );
ImGui::SetNextWindowSize( ImVec2( 240.0f, 80.0f ) );
ImGui::Begin( "Capsule Plane", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize );
if ( ImGui::Button( "Solve" ) )
{
Solve();
}
ImGui::End();
return true;
}
static bool PlaneResultFcn( b3ShapeId shape, const b3PlaneResult* results, int planeCount, void* context )
@ -92,7 +86,7 @@ public:
void MouseDown( b3Vec2 p, int button, int modifiers ) override
{
if ( button == 0 && ( modifiers & GLFW_MOD_ALT ) == 0 )
if ( button == 0 && ( modifiers & MOD_ALT ) == 0 )
{
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
m_origin = pickRay.origin + 10.0f * b3Normalize( pickRay.translation );
@ -120,7 +114,7 @@ public:
{
m_planeCount = 0;
DrawCapsule( m_scene, m_transform, m_capsule, b3_colorGreen );
DrawSolidCapsule( m_transform, m_capsule, MakeColor( b3_colorGreen ) );
b3QueryFilter filter = b3DefaultQueryFilter();
b3Capsule capsule = { m_capsule.center1 + m_transform.p, m_capsule.center2 + m_transform.p, m_capsule.radius };
@ -131,8 +125,8 @@ public:
b3Plane plane = m_planes[i].plane;
b3Vec3 p1 = m_transform.p + ( plane.offset - m_capsule.radius ) * plane.normal;
b3Vec3 p2 = p1 + 0.1f * plane.normal;
DrawPoint( m_scene, p1, 5.0f, b3_colorYellow );
DrawLine( m_scene, p1, p2, b3_colorYellow );
DrawPoint( p1, 5.0f, MakeColor( b3_colorYellow ) );
DrawLine( p1, p2, MakeColor( b3_colorYellow ) );
}
}
@ -150,7 +144,7 @@ public:
bool m_tracking;
};
static int sampleCapsulePlane = SampleManager::Register( "Character", "CapsulePlane", CapsulePlane::Create );
static int sampleCapsulePlane = RegisterSample( "Character", "CapsulePlane", CapsulePlane::Create );
// Exercises the deep-overlap path of b3World_CollideMover against each primitive
// shape type: drag the mover capsule (yellow) into the static sphere, capsule,
@ -216,7 +210,7 @@ public:
void Render() override
{
Sample::Render();
DrawGrid( m_scene, 12 );
DrawGroundGrid( 12 );
}
static bool PlaneResultFcn( b3ShapeId /*shape*/, const b3PlaneResult* results, int planeCount, void* context )
@ -232,7 +226,7 @@ public:
void MouseDown( b3Vec2 p, int button, int modifiers ) override
{
if ( button == 0 && ( modifiers & GLFW_MOD_ALT ) == 0 )
if ( button == 0 && ( modifiers & MOD_ALT ) == 0 )
{
PickRay pickRay = m_camera->BuildPickRay( p.x, p.y );
m_origin = pickRay.origin + 10.0f * b3Normalize( pickRay.translation );
@ -267,7 +261,7 @@ public:
b3World_CollideMover( m_worldId, &worldMover, filter, PlaneResultFcn, this );
// Mover at the queried position.
DrawCapsule( m_scene, m_transform, m_capsule, b3_colorYellow );
DrawSolidCapsule( m_transform, m_capsule, MakeColor( b3_colorYellow ) );
// One arrow per returned plane, drawn from the contact point along the
// normal. A degenerate (zero) normal is drawn red to surface the bug.
@ -277,8 +271,8 @@ public:
b3PlaneResult r = m_results[i];
bool valid = b3IsNormalized( r.plane.normal );
b3HexColor color = valid ? b3_colorLimeGreen : b3_colorRed;
DrawPoint( m_scene, r.point, 6.0f, color );
DrawArrow( m_scene, r.point, r.point + 0.5f * r.plane.normal, 0.05f, color );
DrawPoint( r.point, 6.0f, MakeColor( color ) );
DrawArrow( r.point, r.point + 0.5f * r.plane.normal, MakeColor( color ) );
if ( valid == false )
{
m_zeroNormalCount += 1;
@ -289,22 +283,18 @@ public:
// Solve the planes and show the pushed-out capsule pose.
b3PlaneSolverResult solved = b3SolvePlanes( b3Vec3_zero, solverPlanes, m_planeCount );
b3Transform pushed = { m_transform.p + solved.delta, m_transform.q };
DrawCapsule( m_scene, pushed, m_capsule, b3_colorCyan );
DrawSolidCapsule( pushed, m_capsule, MakeColor( b3_colorCyan ) );
DrawTextLine( "drag the capsule with the left mouse to push it into the shapes" );
DrawTextLine( "yellow = queried pose, cyan = solved push-out, lime = valid plane normals" );
DrawTextLine( "planes: %d degenerate normals: %d", m_planeCount, m_zeroNormalCount );
}
void UpdateUI() override
bool DrawControls() override
{
float fontSize = ImGui::GetFontSize();
ImGui::SetNextWindowPos( ImVec2( 10.0f, 600.0f ), ImGuiCond_Once );
ImGui::SetNextWindowSize( ImVec2( 18.0f * fontSize, 5.0f * fontSize ) );
ImGui::Begin( "Mover Overlap", nullptr, ImGuiWindowFlags_NoResize );
ImGui::Text( "planes: %d", m_planeCount );
ImGui::Text( "degenerate normals: %d", m_zeroNormalCount );
ImGui::End();
return true;
}
static constexpr int m_planeCapacity = 32;
@ -320,7 +310,7 @@ public:
bool m_tracking;
};
static int sampleMoverOverlap = SampleManager::Register( "Character", "MoverOverlap", MoverOverlap::Create );
static int sampleMoverOverlap = RegisterSample( "Character", "MoverOverlap", MoverOverlap::Create );
class BasicMover : public Sample
{
@ -527,9 +517,9 @@ public:
// Body->AddSphere( &ShapeDef, Sphere );
//}
m_camera->m_thirdPerson = true;
m_camera->m_thirdPerson = false;
m_clipVelocity = true;
glfwSetInputMode( m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED );
// sapp_lock_mouse( true );
//m_haveMouseLast = false;
//m_mouseLast = { 0.0f, 0.0f };
@ -539,7 +529,7 @@ public:
~BasicMover() override
{
m_camera->m_thirdPerson = false;
glfwSetInputMode( m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL );
sapp_lock_mouse( false );
b3DestroyMesh( m_levelMesh );
b3DestroyMesh( m_stairs );
b3DestroyMesh( m_torus );
@ -549,32 +539,25 @@ public:
void Render() override
{
Sample::Render();
DrawTransform( m_scene, { { 0.0f, 0.0f, 0.02f }, b3Quat_identity }, 2.0f );
DrawAxes( { { 0.0f, 0.0f, 0.02f }, b3Quat_identity }, 2.0f );
}
void UpdateUI() override
bool DrawControls() override
{
float fontSize = ImGui::GetFontSize();
float height = 9.0f * fontSize;
ImGui::SetNextWindowPos( ImVec2( 0.5f * fontSize, m_camera->m_height - height - 2.0f * fontSize ), ImGuiCond_Once );
ImGui::SetNextWindowSize( ImVec2( 19.0f * fontSize, height ) );
ImGui::Begin( "Mover", nullptr, ImGuiWindowFlags_NoResize );
bool thirdPerson = m_camera->m_thirdPerson;
if ( ImGui::Checkbox( "Third Person (Key: T)", &thirdPerson ) )
if ( ImGui::Checkbox( "Third Person (T)", &thirdPerson ) )
{
ToggleThirdPerson();
}
ImGui::Checkbox( "Clip Velocity", &m_clipVelocity );
ImGui::End();
return true;
}
void Keyboard( int key, int action, int mods ) override
{
if ( key == GLFW_KEY_T && action == GLFW_PRESS )
if ( key == KEY_T && action == ACTION_PRESS )
{
ToggleThirdPerson();
}
@ -605,7 +588,7 @@ public:
bool m_clipVelocity;
};
static int sampleMover = SampleManager::Register( "Character", "Mover", BasicMover::Create );
static int sampleMover = RegisterSample( "Character", "Mover", BasicMover::Create );
struct ClosestShapeCastContext
{
@ -893,7 +876,6 @@ struct RigidbodyCharacter
// --- CategorizeGround: s&box-style box cast with radius shrinking ---
void CategorizeGround()
{
Scene* scene = m_sample->m_scene;
b3Vec3 feet = GetFeetPosition();
// s&box: from = WorldPosition + Up*4, to = WorldPosition + Down*2 (Source units)
@ -910,7 +892,7 @@ struct RigidbodyCharacter
if ( radiusScale < 0.7f )
{
UpdateGround( false, b3Vec3_axisY );
DrawLine( scene, from, to, b3_colorRed );
DrawLine( from, to, MakeColor( b3_colorRed ) );
return;
}
tr = TraceBody( from, to, radiusScale, 0.5f );
@ -919,13 +901,13 @@ struct RigidbodyCharacter
if ( !tr.startedSolid && tr.hit && IsStandableSurface( tr.normal ) && m_jumpCooldown <= 0.0f )
{
UpdateGround( true, tr.normal );
DrawLine( scene, from, tr.hitPoint, b3_colorGreen );
DrawPoint( scene, tr.hitPoint, 5.0f, b3_colorGreen );
DrawLine( from, tr.hitPoint, MakeColor( b3_colorGreen ) );
DrawPoint( tr.hitPoint, 5.0f, MakeColor( b3_colorGreen ) );
}
else
{
UpdateGround( false, b3Vec3_axisY );
DrawLine( scene, from, to, b3_colorGray );
DrawLine( from, to, MakeColor( b3_colorGray ) );
}
}
@ -947,7 +929,6 @@ struct RigidbodyCharacter
return;
}
Scene* scene = m_sample->m_scene;
b3Vec3 pos = b3Body_GetPosition( m_bodyId );
b3Vec3 from = { pos.x, pos.y + 0.05f, pos.z };
@ -982,7 +963,7 @@ struct RigidbodyCharacter
b3Body_SetLinearVelocity( m_bodyId, vel );
}
DrawLine( scene, from, tr.endPosition, b3_colorCyan );
DrawLine( from, tr.endPosition, MakeColor( b3_colorCyan ) );
}
}
@ -990,7 +971,6 @@ struct RigidbodyCharacter
// Returns true if a step was taken and m_stepPosition was set.
bool TryStep( float maxStepHeight )
{
Scene* scene = m_sample->m_scene;
b3Vec3 pos = b3Body_GetPosition( m_bodyId );
b3Vec3 vel = b3Body_GetLinearVelocity( m_bodyId );
@ -1023,7 +1003,7 @@ struct RigidbodyCharacter
radiusScale -= 0.1f;
if ( radiusScale < 0.6f )
{
DrawLine( scene, forwardFrom, forwardTo, b3_colorRed );
DrawLine( forwardFrom, forwardTo, MakeColor( b3_colorRed ) );
return false;
}
trForward = TraceBody( forwardFrom, forwardTo, radiusScale );
@ -1035,7 +1015,7 @@ struct RigidbodyCharacter
return false;
}
DrawLine( scene, forwardFrom, trForward.endPosition, b3_colorYellow );
DrawLine( forwardFrom, trForward.endPosition, MakeColor( b3_colorYellow ) );
// Remaining velocity direction after hit
b3Vec3 hitPos = trForward.endPosition;
@ -1047,7 +1027,7 @@ struct RigidbodyCharacter
if ( trUp.startedSolid )
{
DrawLine( scene, upFrom, upTo, b3_colorRed );
DrawLine( upFrom, upTo, MakeColor( b3_colorRed ) );
return false;
}
@ -1056,11 +1036,11 @@ struct RigidbodyCharacter
if ( upDistance < 0.005f )
{
// Too tight to step up
DrawLine( scene, upFrom, topPos, b3_colorRed );
DrawLine( upFrom, topPos, MakeColor( b3_colorRed ) );
return false;
}
DrawLine( scene, upFrom, topPos, b3_colorYellow );
DrawLine( upFrom, topPos, MakeColor( b3_colorYellow ) );
// Phase 3 — ACROSS: from top position, trace in move direction
float acrossDist = forwardDist * ( 1.0f - trForward.fraction ) + m_bodyRadius * 0.5f;
@ -1070,12 +1050,12 @@ struct RigidbodyCharacter
if ( trAcross.startedSolid )
{
DrawLine( scene, acrossFrom, acrossTo, b3_colorRed );
DrawLine( acrossFrom, acrossTo, MakeColor( b3_colorRed ) );
return false;
}
b3Vec3 acrossPos = trAcross.hit ? trAcross.endPosition : acrossTo;
DrawLine( scene, acrossFrom, acrossPos, b3_colorYellow );
DrawLine( acrossFrom, acrossPos, MakeColor( b3_colorYellow ) );
// Phase 4 — DOWN: from across position, trace straight down
b3Vec3 downFrom = acrossPos;
@ -1084,13 +1064,13 @@ struct RigidbodyCharacter
if ( !trDown.hit )
{
DrawLine( scene, downFrom, downTo, b3_colorRed );
DrawLine( downFrom, downTo, MakeColor( b3_colorRed ) );
return false;
}
if ( !IsStandableSurface( trDown.normal ) )
{
DrawLine( scene, downFrom, trDown.endPosition, b3_colorRed );
DrawLine( downFrom, trDown.endPosition, MakeColor( b3_colorRed ) );
return false;
}
@ -1101,8 +1081,8 @@ struct RigidbodyCharacter
return false;
}
DrawLine( scene, downFrom, trDown.endPosition, b3_colorYellow );
DrawPoint( scene, trDown.endPosition, 8.0f, b3_colorYellow );
DrawLine( downFrom, trDown.endPosition, MakeColor( b3_colorYellow ) );
DrawPoint( trDown.endPosition, 8.0f, MakeColor( b3_colorYellow ) );
// Teleport body to step position
b3Vec3 stepPos = { trDown.endPosition.x, trDown.endPosition.y + 0.01f, trDown.endPosition.z };
@ -1316,24 +1296,23 @@ struct RigidbodyCharacter
void DrawDebug() const
{
Scene* scene = m_sample->m_scene;
b3Vec3 pos = b3Body_GetPosition( m_bodyId );
b3Vec3 vel = b3Body_GetLinearVelocity( m_bodyId );
// Draw velocity vector (purple)
DrawLine( scene, pos, pos + vel, b3_colorPurple );
DrawLine( pos, pos + vel, MakeColor( b3_colorPurple ) );
// Draw wish velocity (orange)
DrawLine( scene, pos, pos + m_lastWishVelocity, b3_colorOrange );
DrawLine( pos, pos + m_lastWishVelocity, MakeColor( b3_colorOrange ) );
// Draw mass center (yellow dot)
DrawPoint( scene, m_massCenterWorld, 8.0f, b3_colorYellow );
DrawPoint( m_massCenterWorld, 8.0f, MakeColor( b3_colorYellow ) );
// Draw ground indicator
if ( m_onGround )
{
b3Vec3 bottom = { pos.x, pos.y - m_totalHeight * 0.5f, pos.z };
DrawLine( scene, bottom, bottom + 0.3f * m_groundNormal, b3_colorGreen );
DrawLine( bottom, bottom + 0.3f * m_groundNormal, MakeColor( b3_colorGreen ) );
}
}
};
@ -1514,13 +1493,13 @@ public:
m_camera->m_thirdPerson = true;
m_showDebug = true;
glfwSetInputMode( m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED );
sapp_lock_mouse( true );
}
~RigidBodyCharacter() override
{
m_camera->m_thirdPerson = false;
glfwSetInputMode( m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL );
sapp_lock_mouse( false );
b3DestroyMesh( m_levelMesh );
b3DestroyMesh( m_stairs );
b3DestroyMesh( m_building );
@ -1531,12 +1510,12 @@ public:
void Keyboard( int key, int action, int mods ) override
{
if ( key == GLFW_KEY_T && action == GLFW_PRESS )
if ( key == KEY_T && action == ACTION_PRESS )
{
ToggleThirdPerson();
}
if ( key == GLFW_KEY_V && action == GLFW_PRESS )
if ( key == KEY_V && action == ACTION_PRESS )
{
m_showDebug = !m_showDebug;
}
@ -1562,29 +1541,29 @@ public:
if ( m_camera->m_thirdPerson )
{
if ( glfwGetKey( m_window, GLFW_KEY_W ) )
if ( IsKeyDown( KEY_W ) )
{
throttle.x += 1.0f;
}
if ( glfwGetKey( m_window, GLFW_KEY_S ) )
if ( IsKeyDown( KEY_S ) )
{
throttle.x -= 1.0f;
}
if ( glfwGetKey( m_window, GLFW_KEY_A ) )
if ( IsKeyDown( KEY_A ) )
{
throttle.y -= 1.0f;
}
if ( glfwGetKey( m_window, GLFW_KEY_D ) )
if ( IsKeyDown( KEY_D ) )
{
throttle.y += 1.0f;
}
if ( glfwGetKey( m_window, GLFW_KEY_SPACE ) )
if ( IsKeyDown( KEY_SPACE ) )
{
m_character.Jump();
}
m_character.m_sprint = m_character.m_onGround && glfwGetKey( m_window, GLFW_KEY_LEFT_SHIFT ) != 0;
m_character.m_sprint = m_character.m_onGround && IsKeyDown( KEY_LEFT_SHIFT );
}
// Pre-step: manipulate velocity before physics
@ -1654,25 +1633,18 @@ public:
void Render() override
{
Sample::Render();
DrawTransform( m_scene, { { 0.0f, 0.0f, 0.02f }, b3Quat_identity }, 2.0f );
DrawAxes( { { 0.0f, 0.0f, 0.02f }, b3Quat_identity }, 2.0f );
}
void UpdateUI() override
bool DrawControls() override
{
float fontSize = ImGui::GetFontSize();
float height = 12.0f * fontSize;
ImGui::SetNextWindowPos( ImVec2( 0.5f * fontSize, m_camera->m_height - height - 2.0f * fontSize ), ImGuiCond_Once );
ImGui::SetNextWindowSize( ImVec2( 22.0f * fontSize, height ) );
ImGui::Begin( "Rigidbody Character", nullptr, ImGuiWindowFlags_NoResize );
bool thirdPerson = m_camera->m_thirdPerson;
if ( ImGui::Checkbox( "Third Person (Key: T)", &thirdPerson ) )
if ( ImGui::Checkbox( "Third Person (T)", &thirdPerson ) )
{
ToggleThirdPerson();
}
ImGui::Checkbox( "Debug Visualization (Key: V)", &m_showDebug );
ImGui::Checkbox( "Debug (V)", &m_showDebug );
ImGui::Separator();
ImGui::Text( "Ground: %s", m_character.m_onGround ? "YES" : "NO" );
@ -1686,7 +1658,7 @@ public:
b3Vec3 pos = b3Body_GetPosition( m_character.m_bodyId );
ImGui::Text( "Mass center offset: %.2f", mc.y - pos.y );
ImGui::End();
return true;
}
static Sample* Create( SampleContext* context )
@ -1704,4 +1676,4 @@ public:
bool m_showDebug;
};
static int sampleRBCharacter = SampleManager::Register( "Character", "Rigid Body", RigidBodyCharacter::Create );
static int sampleRBCharacter = RegisterSample( "Character", "Rigid Body", RigidBodyCharacter::Create );