Skip to content

Commit ea30ba7

Browse files
committed
* Added the 3D move gizmo rendering.
* Merged 2D/3D gizmo render function in viewport code (no need to keep two functions in sync).
1 parent 025d0aa commit ea30ba7

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

TheForceEngine/TFE_Editor/LevelEditor/Rendering/gizmo.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,48 @@ namespace LevelEditor
202202
}
203203
}
204204

205+
void gizmo_drawMove3d(Vec3f p0, Vec3f p1)
206+
{
207+
Vec3f offset0 = { p0.x - s_camera.pos.x, p0.y - s_camera.pos.y, p0.z - s_camera.pos.z };
208+
Vec3f offset1 = { p1.x - s_camera.pos.x, p1.y - s_camera.pos.y, p1.z - s_camera.pos.z };
209+
f32 scale0 = sqrtf(TFE_Math::dot(&offset0, &offset0));
210+
f32 scale1 = sqrtf(TFE_Math::dot(&offset1, &offset1));
211+
212+
const u32 colorMain = 0xff00a5ff;
213+
const u32 colorXAxis = 0xff4040ff;
214+
const u32 colorZAxis = 0xffff4040;
215+
const Vec3f vtx[] =
216+
{
217+
p0, p1
218+
};
219+
const u32 colorsMain[] = { colorMain, colorMain };
220+
const u32 colorsXAxis[] = { colorXAxis, colorXAxis };
221+
const u32 colorsZAxis[] = { colorZAxis, colorZAxis };
222+
223+
f32 dx = fabsf(p1.x - p0.x);
224+
f32 dz = fabsf(p1.z - p0.z);
225+
226+
lineDraw3d_setLineDrawMode(LINE_DRAW_DASHED);
227+
// Draw movement line.
228+
const u32* distColor = colorsMain;
229+
if (dx < 0.001f) distColor = colorsZAxis;
230+
else if (dz < 0.001f) distColor = colorsXAxis;
231+
lineDraw3d_addLine(2.5f, vtx, distColor);
232+
// Draw X & Y movement lines.
233+
if (fabsf(p1.x - p0.x) > 0.001f && fabsf(p1.z - p0.z) > 0.001f)
234+
{
235+
const Vec3f zaxis[] = { {vtx[0].x, vtx[0].y, vtx[0].z}, {vtx[0].x, vtx[0].y, vtx[1].z} };
236+
const Vec3f xaxis[] = { {vtx[0].x, vtx[0].y, vtx[1].z}, {vtx[1].x, vtx[0].y, vtx[1].z} };
237+
238+
lineDraw3d_addLine(2.5f, zaxis, colorsZAxis);
239+
lineDraw3d_addLine(2.5f, xaxis, colorsXAxis);
240+
}
241+
lineDraw3d_setLineDrawMode();
242+
243+
drawBox3d(&vtx[0], 0.0075f * scale0, 2.0f, colorMain);
244+
drawBox3d(&vtx[1], 0.0075f * scale1, 2.0f, colorMain);
245+
}
246+
205247
void gizmo_drawRotation3d(Vec3f center)
206248
{
207249
const RotationGizmoPart hoveredPart = (RotationGizmoPart)edit_getTransformRotationHover();

TheForceEngine/TFE_Editor/LevelEditor/Rendering/gizmo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace LevelEditor
2929

3030
// Transform Gizmos.
3131
void gizmo_drawMove2d(Vec3f p0, Vec3f p1);
32+
void gizmo_drawMove3d(Vec3f p0, Vec3f p1);
3233
void gizmo_drawRotation2d(Vec2f center);
3334
void gizmo_drawRotation3d(Vec3f center);
3435
f32 gizmo_getRotationRadiusWS(RotationGizmoPart part, Vec3f* center = nullptr);

TheForceEngine/TFE_Editor/LevelEditor/Rendering/viewport.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ namespace LevelEditor
173173
void drawArrow3d(f32 width, f32 lenInPixels, Vec3f pos, Vec3f dir, Vec3f nrm, u32 color);
174174
void drawArrow3d_Segment(f32 width, f32 lenInPixels, Vec3f pos0, Vec3f pos1, Vec3f nrm, u32 color);
175175
void drawNoteIcon3d(LevelNote* note, s32 id, u32 objColor, const Vec3f& cameraRgt, const Vec3f& cameraUp);
176-
void drawTransformGizmo3d();
176+
void drawTransformGizmo();
177177
bool computeSignCorners(const EditorSector* sector, const EditorWall* wall, Vec3f* corners);
178178

179179
void viewport_init()
@@ -316,21 +316,24 @@ namespace LevelEditor
316316
camera.projMtx.m3.w = 1.0f;
317317
}
318318

319-
void drawTransformGizmo2d()
319+
void drawTransformGizmo()
320320
{
321321
if (!edit_isTransformToolActive()) { return; }
322-
323-
TransformMode mode = edit_getTransformMode();
324-
if (mode == TRANS_MOVE)
325-
{
326-
const Vec3f p0 = edit_getTransformAnchor();
327-
const Vec3f p1 = edit_getTransformPos();
328-
gizmo_drawMove2d(p0, p1);
329-
}
330-
else if (mode == TRANS_ROTATE)
322+
switch (edit_getTransformMode())
331323
{
332-
const Vec3f centerWS = edit_getTransformAnchor();
333-
gizmo_drawRotation2d({ centerWS.x, centerWS.z });
324+
case TRANS_MOVE:
325+
{
326+
const Vec3f p0 = edit_getTransformAnchor();
327+
const Vec3f p1 = edit_getTransformPos();
328+
if (s_view == EDIT_VIEW_2D) { gizmo_drawMove2d(p0, p1); }
329+
else { gizmo_drawMove3d(p0, p1); }
330+
} break;
331+
case TRANS_ROTATE:
332+
{
333+
const Vec3f centerWS = edit_getTransformAnchor();
334+
if (s_view == EDIT_VIEW_2D) { gizmo_drawRotation2d({ centerWS.x, centerWS.z }); }
335+
else { gizmo_drawRotation3d(centerWS); }
336+
} break;
334337
}
335338
}
336339

@@ -532,7 +535,7 @@ namespace LevelEditor
532535
}
533536

534537
// Transform Gizmos
535-
drawTransformGizmo2d();
538+
drawTransformGizmo();
536539

537540
// Compute the camera and projection for the model draw.
538541
Camera3d camera;
@@ -1836,7 +1839,7 @@ namespace LevelEditor
18361839
triDraw3d_begin(&s_grid);
18371840
lineDraw3d_begin(s_viewportSize.x, s_viewportSize.z);
18381841

1839-
drawTransformGizmo3d();
1842+
drawTransformGizmo();
18401843

18411844
triDraw3d_draw(&s_camera, (f32)s_viewportSize.x, (f32)s_viewportSize.z, s_grid.size, 0.0f, false, false);
18421845
lineDraw3d_drawLines(&s_camera, false, false);
@@ -2133,21 +2136,7 @@ namespace LevelEditor
21332136

21342137
return u32(colorSum.x * 255.0f) | (u32(colorSum.y * 255.0f) << 8) | (u32(colorSum.z * 255.0f) << 16) | (u32(alpha * 255.0f) << 24);
21352138
}
2136-
2137-
void drawTransformGizmo3d()
2138-
{
2139-
if (!edit_isTransformToolActive()) { return; }
2140-
2141-
TransformMode mode = edit_getTransformMode();
2142-
if (mode == TRANS_ROTATE)
2143-
{
2144-
Vec3f centerWS = edit_getTransformAnchor();
2145-
Vec3f rotation = edit_getTransformRotation();
2146-
2147-
gizmo_drawRotation3d(centerWS);
2148-
}
2149-
}
2150-
2139+
21512140
void renderLevel3D()
21522141
{
21532142
viewport_updateRail();

0 commit comments

Comments
 (0)