Skip to content

Commit c3f1547

Browse files
committed
[upstream] Custom text renderer (erincatto/box2d#998)
Added font rendering code independent of imgui. Converted drawing code to C. `b2DebugDraw::DrawSegmentFcn` renamed to `b2DebugDraw::DrawLineFcn`.
1 parent bb5a9bb commit c3f1547

File tree

179 files changed

+2258
-1867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+2258
-1867
lines changed

data/font.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330 core
5+
6+
in vec4 color;
7+
in vec2 uv;
8+
9+
uniform sampler2D FontAtlas;
10+
11+
out vec4 fragColor;
12+
13+
void main()
14+
{
15+
fragColor = vec4(color.rgb, color.a * texture(FontAtlas, uv).r);
16+
}

data/font.vs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330 core
5+
6+
layout (location = 0) in vec2 aPosition;
7+
layout (location = 1) in vec2 aUV;
8+
layout (location = 2) in vec4 aColor;
9+
10+
out vec4 color;
11+
out vec2 uv;
12+
13+
uniform mat4 ProjectionMatrix;
14+
15+
void main()
16+
{
17+
gl_Position = ProjectionMatrix * vec4(aPosition, 0.0, 1.0);
18+
19+
color = aColor;
20+
uv = aUV;
21+
}

data/line.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330
5+
6+
in vec4 f_color;
7+
out vec4 color;
8+
9+
void main(void)
10+
{
11+
color = f_color;
12+
}

data/line.vs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2024 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330
5+
6+
uniform mat4 projectionMatrix;
7+
layout(location = 0) in vec2 v_position;
8+
layout(location = 1) in vec4 v_color;
9+
10+
out vec4 f_color;
11+
12+
void main(void)
13+
{
14+
f_color = v_color;
15+
gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);
16+
}

data/point.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330
5+
6+
in vec4 f_color;
7+
out vec4 color;
8+
9+
void main(void)
10+
{
11+
color = f_color;
12+
}

data/point.vs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: 2024 Erin Catto
2+
// SPDX-License-Identifier: MIT
3+
4+
#version 330
5+
6+
uniform mat4 projectionMatrix;
7+
layout(location = 0) in vec2 v_position;
8+
layout(location = 1) in float v_size;
9+
layout(location = 2) in vec4 v_color;
10+
11+
out vec4 f_color;
12+
13+
void main(void)
14+
{
15+
f_color = v_color;
16+
gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);
17+
gl_PointSize = v_size;
18+
}

src/Box2D.NET.Samples/Camera.cs

Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,115 +2,12 @@
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi([email protected])
33
// SPDX-License-Identifier: MIT
44

5-
using System;
6-
using static Box2D.NET.B2MathFunction;
7-
8-
95
namespace Box2D.NET.Samples;
106

11-
public class Camera
7+
public struct Camera
128
{
13-
public B2Vec2 m_center;
14-
public float m_zoom;
15-
public float m_width;
16-
public float m_height;
17-
18-
public Camera()
19-
{
20-
m_width = 1920;
21-
m_height = 1080;
22-
23-
ResetView();
24-
}
25-
26-
public void ResetView()
27-
{
28-
m_center = new B2Vec2(0.0f, 20.0f);
29-
m_zoom = 1.0f;
30-
}
31-
32-
public B2Vec2 ConvertScreenToWorld(B2Vec2 ps)
33-
{
34-
float w = m_width;
35-
float h = m_height;
36-
float u = ps.X / w;
37-
float v = (h - ps.Y) / h;
38-
39-
float ratio = w / h;
40-
B2Vec2 extents = new B2Vec2(m_zoom * ratio, m_zoom);
41-
42-
B2Vec2 lower = b2Sub(m_center, extents);
43-
B2Vec2 upper = b2Add(m_center, extents);
44-
45-
B2Vec2 pw = new B2Vec2((1.0f - u) * lower.X + u * upper.X, (1.0f - v) * lower.Y + v * upper.Y);
46-
return pw;
47-
}
48-
49-
public B2Vec2 ConvertWorldToScreen(B2Vec2 pw)
50-
{
51-
float w = m_width;
52-
float h = m_height;
53-
float ratio = w / h;
54-
55-
B2Vec2 extents = new B2Vec2(m_zoom * ratio, m_zoom);
56-
57-
B2Vec2 lower = b2Sub(m_center, extents);
58-
B2Vec2 upper = b2Add(m_center, extents);
59-
60-
float u = (pw.X - lower.X) / (upper.X - lower.X);
61-
float v = (pw.Y - lower.Y) / (upper.Y - lower.Y);
62-
63-
B2Vec2 ps = new B2Vec2(u * w, (1.0f - v) * h);
64-
return ps;
65-
}
66-
67-
// Convert from world coordinates to normalized device coordinates.
68-
// http://www.songho.ca/opengl/gl_projectionmatrix.html
69-
// This also includes the view transform
70-
public void BuildProjectionMatrix(Span<float> m, float zBias)
71-
{
72-
float ratio = (float)m_width / (float)m_height;
73-
B2Vec2 extents = new B2Vec2(m_zoom * ratio, m_zoom);
74-
75-
B2Vec2 lower = b2Sub(m_center, extents);
76-
B2Vec2 upper = b2Add(m_center, extents);
77-
float w = upper.X - lower.X;
78-
float h = upper.Y - lower.Y;
79-
80-
m[0] = 2.0f / w;
81-
m[1] = 0.0f;
82-
m[2] = 0.0f;
83-
m[3] = 0.0f;
84-
85-
m[4] = 0.0f;
86-
m[5] = 2.0f / h;
87-
m[6] = 0.0f;
88-
m[7] = 0.0f;
89-
90-
m[8] = 0.0f;
91-
m[9] = 0.0f;
92-
m[10] = -1.0f;
93-
m[11] = 0.0f;
94-
95-
m[12] = -2.0f * m_center.X / w;
96-
m[13] = -2.0f * m_center.Y / h;
97-
m[14] = zBias;
98-
m[15] = 1.0f;
99-
}
100-
101-
public B2AABB GetViewBounds()
102-
{
103-
if (m_height == 0.0f || m_width == 0.0f)
104-
{
105-
B2AABB bounds = new B2AABB(b2Vec2_zero, b2Vec2_zero);
106-
return bounds;
107-
}
108-
109-
{
110-
B2AABB bounds;
111-
bounds.lowerBound = ConvertScreenToWorld(new B2Vec2(0.0f, (float)m_height));
112-
bounds.upperBound = ConvertScreenToWorld(new B2Vec2((float)m_width, 0.0f));
113-
return bounds;
114-
}
115-
}
116-
}
9+
public B2Vec2 center;
10+
public float zoom;
11+
public float width;
12+
public float height;
13+
}

src/Box2D.NET.Samples/Cameras.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System;
2+
using static Box2D.NET.B2MathFunction;
3+
4+
namespace Box2D.NET.Samples;
5+
6+
public static class Cameras
7+
{
8+
public static Camera GetDefaultCamera()
9+
{
10+
var camera = new Camera();
11+
camera.center = new B2Vec2(0.0f, 20.0f);
12+
camera.zoom = 1.0f;
13+
camera.width = 1920.0f;
14+
camera.height = 1080.0f;
15+
return camera;
16+
}
17+
18+
public static void ResetView(ref Camera camera)
19+
{
20+
camera.center = new B2Vec2(0.0f, 20.0f);
21+
camera.zoom = 1.0f;
22+
}
23+
24+
public static B2Vec2 ConvertScreenToWorld(ref Camera camera, B2Vec2 ps)
25+
{
26+
float w = camera.width;
27+
float h = camera.height;
28+
float u = ps.X / w;
29+
float v = (h - ps.Y) / h;
30+
31+
float ratio = w / h;
32+
B2Vec2 extents = new B2Vec2(camera.zoom * ratio, camera.zoom);
33+
34+
B2Vec2 lower = b2Sub(camera.center, extents);
35+
B2Vec2 upper = b2Add(camera.center, extents);
36+
37+
B2Vec2 pw = new B2Vec2((1.0f - u) * lower.X + u * upper.X, (1.0f - v) * lower.Y + v * upper.Y);
38+
return pw;
39+
}
40+
41+
public static B2Vec2 ConvertWorldToScreen(ref Camera camera, B2Vec2 pw)
42+
{
43+
float w = camera.width;
44+
float h = camera.height;
45+
float ratio = w / h;
46+
47+
B2Vec2 extents = new B2Vec2(camera.zoom * ratio, camera.zoom);
48+
49+
B2Vec2 lower = b2Sub(camera.center, extents);
50+
B2Vec2 upper = b2Add(camera.center, extents);
51+
52+
float u = (pw.X - lower.X) / (upper.X - lower.X);
53+
float v = (pw.Y - lower.Y) / (upper.Y - lower.Y);
54+
55+
B2Vec2 ps = new B2Vec2(u * w, (1.0f - v) * h);
56+
return ps;
57+
}
58+
59+
// Convert from world coordinates to normalized device coordinates.
60+
// http://www.songho.ca/opengl/gl_projectionmatrix.html
61+
// This also includes the view transform
62+
public static void BuildProjectionMatrix(ref Camera camera, Span<float> m, float zBias)
63+
{
64+
float ratio = (float)camera.width / (float)camera.height;
65+
B2Vec2 extents = new B2Vec2(camera.zoom * ratio, camera.zoom);
66+
67+
B2Vec2 lower = b2Sub(camera.center, extents);
68+
B2Vec2 upper = b2Add(camera.center, extents);
69+
float w = upper.X - lower.X;
70+
float h = upper.Y - lower.Y;
71+
72+
m[0] = 2.0f / w;
73+
m[1] = 0.0f;
74+
m[2] = 0.0f;
75+
m[3] = 0.0f;
76+
77+
m[4] = 0.0f;
78+
m[5] = 2.0f / h;
79+
m[6] = 0.0f;
80+
m[7] = 0.0f;
81+
82+
m[8] = 0.0f;
83+
m[9] = 0.0f;
84+
m[10] = -1.0f;
85+
m[11] = 0.0f;
86+
87+
m[12] = -2.0f * camera.center.X / w;
88+
m[13] = -2.0f * camera.center.Y / h;
89+
m[14] = zBias;
90+
m[15] = 1.0f;
91+
}
92+
93+
public static B2AABB GetViewBounds(ref Camera camera)
94+
{
95+
if (camera.height == 0.0f || camera.width == 0.0f)
96+
{
97+
B2AABB bounds = new B2AABB(b2Vec2_zero, b2Vec2_zero);
98+
return bounds;
99+
}
100+
101+
{
102+
B2AABB bounds;
103+
bounds.lowerBound = ConvertScreenToWorld(ref camera, new B2Vec2(0.0f, (float)camera.height));
104+
bounds.upperBound = ConvertScreenToWorld(ref camera, new B2Vec2((float)camera.width, 0.0f));
105+
return bounds;
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)