Skip to content

Commit

Permalink
Add Grid Floor for better orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
sercero committed Apr 22, 2024
1 parent 9c4cdb7 commit d7165cb
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
85 changes: 85 additions & 0 deletions ogre_grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python

import Ogre

GRID_MATERIAL = "MeshViewer/VertexColour"

class GridFloor:
def __init__(self, scene, resource_group):
self.scn_mgr = scene
self.resource_group = resource_group
self.plane_names = ["YZPlane", "XZPlane", "XYPlane"]
self.normals = [
(1, 0, 0), # YZPlane normal
(0, 1, 0), # XZPlane normal
(0, 0, 1) # XYPlane normal
]
self.axis_colors = [
Ogre.ColourValue(1, 0, 0, 1),
Ogre.ColourValue(0, 1, 0, 1),
Ogre.ColourValue(0, 0, 1, 1)
]

def show_plane(self, plane):
for p in range(0, 3):
plane_name = "MeshViewer/" + self.plane_names[p]
plane_node = self.scn_mgr.getSceneNode(plane_name)
plane_node.setVisible(p == plane)

def create_material(self):
material = Ogre.MaterialManager.getSingleton().create(GRID_MATERIAL, self.resource_group);
p = material.getTechnique(0).getPass(0);
p.setLightingEnabled(False);
p.setVertexColourTracking(Ogre.TVC_AMBIENT);

def create_planes(self, diam):
self.create_material()
for plane in range(0, 3):
plane_name = "MeshViewer/" + self.plane_names[plane]
self.create_plane(plane, -diam, diam)

def create_plane(self, plane, min_val, max_val):
cl = None
axis_color = None
plane_name = "MeshViewer/" + self.plane_names[plane]

normal = self.normals[plane]

color_X = self.axis_colors[0]
color_Y = self.axis_colors[1]
color_Z = self.axis_colors[2]

if plane == 0:
axis_color = [color_Y, color_Z]
elif plane == 1:
axis_color = [color_X, color_Z]
elif plane == 2:
axis_color = [color_Y, color_X]

grid_color = Ogre.ColourValue(0.2, 0.2, 0.2, 0.4)

# Compute the other axes based on the normal vector
axis = [
Ogre.Vector3(normal[1], normal[2], normal[0]),
Ogre.Vector3(normal[2], normal[0], normal[1])
]

o = self.scn_mgr.createManualObject(plane_name);
o.begin(GRID_MATERIAL, Ogre.RenderOperation.OT_LINE_LIST, self.resource_group)

for i in range(0, 2):
for j in range(min_val, max_val + 1):
if j == 0:
cl = axis_color[i]
else:
cl = grid_color
o.position(axis[i] * min_val + axis[1 - i] * j)
o.colour(cl)
o.position(axis[i] * max_val + axis[1 - i] * j)
o.colour(cl)

o.end()

plane_node = self.scn_mgr.getRootSceneNode().createChildSceneNode(plane_name)
plane_node.attachObject(o)
plane_node.setVisible(False)
39 changes: 36 additions & 3 deletions ogre_mesh_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import Ogre.RTShader as OgreRTShader
import Ogre.Bites as OgreBites
import Ogre.Overlay

import Ogre.ImGui as ImGui

import ogre_grid

RGN_MESHVIEWER = "OgreMeshViewer"
RGN_USERDATA = "UserData"

Expand Down Expand Up @@ -258,6 +259,8 @@ def preRenderTargetUpdate(self, evt):
ImGui.Separator()
if ImGui.MenuItem("Show Axes", "A", self.app.axes_visible):
self.app._toggle_axes()
if ImGui.MenuItem("Show Grid", "G", self.app.grid_visible):
self.app._toggle_grid()
if ImGui.MenuItem("Show Bounding Box", "B", enode.getShowBoundingBox()):
self.app._toggle_bbox()
if ImGui.MenuItem("Wireframe Mode", "W", app.cam.getPolygonMode() == Ogre.PM_WIREFRAME):
Expand Down Expand Up @@ -458,6 +461,8 @@ def __init__(self, infile, rescfg):
self.fixed_yaw_axes = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
self.fixed_yaw_axis = 1
self.default_tilt = Ogre.Degree(20)
self.grid_floor = None
self.grid_visible = True

self.active_controllers = {}

Expand All @@ -476,6 +481,8 @@ def keyPressed(self, evt):
self.gui.side_panel_visible = not self.gui.side_panel_visible
elif evt.keysym.sym == ord("a"):
self._toggle_axes()
elif evt.keysym.sym == ord("g"):
self._toggle_grid()
elif evt.keysym.sym == ord("p"):
self._save_screenshot()
elif evt.keysym.sym == ord("w"):
Expand All @@ -498,6 +505,11 @@ def mousePressed(self, evt):
return True

new_entity = hit.movable.castEntity()

# Avoid crashing when the user accidentaly clicks the grid movable object
if not new_entity:
return True

if self.attach_node and new_entity and evt.button == OgreBites.BUTTON_LEFT:
if self.entity is not None:
self.entity.getParentSceneNode().showBoundingBox(False)
Expand Down Expand Up @@ -528,11 +540,20 @@ def _toggle_axes(self):

self.axes_visible = not self.axes_visible

def _toggle_grid(self):
self.grid_visible = not self.grid_visible

if self.grid_visible:
self.grid_floor.show_plane(self.fixed_yaw_axis)
else:
self.grid_floor.show_plane(-1)

def _save_screenshot(self):
name = os.path.splitext(self.filename)[0]
outpath = os.path.join(self.filedir, f"screenshot_{name}_")

Ogre.LogManager.getSingleton().logMessage(f"Screenshot saved to folder: {self.filedir}")
Ogre.LogManager.getSingleton().logMessage(f"Screenshot saved to folder: {os.path.normpath(self.filedir)}")
print(f"Screenshot saved to folder: {os.path.normpath(self.filedir)}")

self.cam.getViewport().setOverlaysEnabled(False)
self.getRoot().renderOneFrame()
Expand All @@ -545,6 +566,8 @@ def set_orientation(self):
camnode.setOrientation(Ogre.Quaternion.IDENTITY)
camnode.setFixedYawAxis(self.fixed_yaw_axis != -1, self.fixed_yaw_axes[self.fixed_yaw_axis])
self.camman.setFixedYaw(self.fixed_yaw_axis != -1)
if self.grid_visible:
self.grid_floor.show_plane(self.fixed_yaw_axis)

if self.fixed_yaw_axis == 0:
self.camman.setYawPitchDist(0, 0, diam)
Expand Down Expand Up @@ -584,7 +607,7 @@ def locateResources(self):

# add fonts to default resource group
rgm.addResourceLocation(os.path.dirname(__file__) + "/fonts", "FileSystem", RGN_MESHVIEWER)

def loadResources(self):
rgm = Ogre.ResourceGroupManager.getSingleton()
rgm.initialiseResourceGroup(Ogre.RGN_INTERNAL)
Expand Down Expand Up @@ -652,6 +675,9 @@ def setup(self):
self.getRoot().renderOneFrame()
self.getRoot().renderOneFrame()

Ogre.LogManager.getSingleton().logMessage(f"Opening file: {os.path.normpath(self.infile)}")
print(f"*** Opening file: {os.path.normpath(self.infile)} ***")

if self.filename.lower().endswith(".scene"):
self.attach_node = scn_mgr.getRootSceneNode().createChildSceneNode()
self.attach_node.loadChildren(self.filename)
Expand All @@ -667,6 +693,7 @@ def setup(self):
diam = c.getDerivedPosition().length()
break
else:
self.attach_node = None
self.entity = scn_mgr.createEntity(self.filename)
scn_mgr.getRootSceneNode().createChildSceneNode().attachObject(self.entity)
diam = self.entity.getBoundingBox().getSize().length()
Expand All @@ -685,9 +712,15 @@ def setup(self):
light.setSpecularColour(Ogre.ColourValue.White)
camnode.attachObject(light)

self.grid_floor = ogre_grid.GridFloor(scn_mgr, RGN_MESHVIEWER)
self.grid_floor.create_planes(int(diam))
if self.grid_visible:
self.grid_floor.show_plane(self.fixed_yaw_axis)

self.camman = OgreBites.CameraMan(camnode)
self.camman.setStyle(OgreBites.CS_ORBIT)
self.camman.setYawPitchDist(0, self.default_tilt, diam)
self.set_orientation()

self.input_dispatcher = OgreBites.InputListenerChain([self.getImGuiInputListener(), self.camman, self])
self.addInputListener(self.input_dispatcher)
Expand Down

0 comments on commit d7165cb

Please sign in to comment.