View Single Post
  #5  
Old 09-01-2015, 07:24 PM
jeremy jeremy is offline
Member
 
Join Date: Dec 2012
Posts: 2
Axes indicator sample script

Hello Walter,

Thank you for your post. I like the feature idea

I've gone and put together a sample script, here is the code:

Code:
"""Two examples of ways to provide a visual indicator of world
orientation which is always in view. A compass of sorts, effectively
setup as a "Heads-up display" or HUD.

Two options are provided:
The first sets up the HUD as a group node which is linked to the view.
The second sets up a VizGUICanvas object, which is a new feature in
Vizard 5 providing a flat render node designed for displaying GUI
elements or other nodes. (In this example the canvas uses the
"WorldOverlay" render mode).

For both of these examples, the HUD is a node in the world (as opposed
to being in an ortho window, or screen space). While it is possible to
set up a HUD in window or screen space, an advantage to having a HUD
in world space is that rendering can be simplified as there's no 
need for special treatment for such aspects as lighting, depth 
testing, or stereo.
"""

import viz
import vizshape

viz.go()
dojo = viz.addChild('dojo.osgb')
viz.MainView.setPosition(0.5, 1, -3)

# Set to 1 or 2 as desired
example = 1


if example == 1:
	# Set up a group node to be the HUD.
	# Other geometry will be parented to this node.
	viewGroup = viz.addGroup()
	
	# Link the HUD to the main view so it's always in view
	viewGroupLink = viz.link(viz.MainView, viewGroup)
	
	# Shift the HUD a meter in front and down and to the left a bit
	viewGroupLink.preTrans([-0.3, -0.2, 1])
	
	# Add a small 20x20cm grid to provide a bit of frame of reference
	grid = vizshape.addGrid(size=(0.2, 0.2), step=0.05, parent=viewGroup)
	
	# Add small (10cm long) axes which will serve as the indicator
	axes = vizshape.addAxes(length=0.1, parent=viewGroup)


if example == 2:
	# Setup a GUI canvas to serve as the HUD
	# One advantage of the GUI canvas is that, as a separate render
	# node, it is always drawn on top. So the HUD won't be occluded
	# when the view gets up close to an object in the scene.
	canvas = viz.addGUICanvas()
	
	# Define the dimensions of the canvas
	canvasDimensions = [800, 600]  # width, height in (virtual) pixels
	
	# Canvas field of view will be slightly smaller than the window's
	canvasFOV = viz.MainWindow.getVerticalFOV() - 15
	
	# Depth of 1m
	canvasDepth = 1  # distance out in front (meters)
	
	# Set up the canvas render parameters
	canvas.setRenderWorldOverlay(canvasDimensions, canvasFOV, canvasDepth)
	
	# Because the canvas-space has units of pixels, added nodes must
	# be greatly scaled up to appear acceptably-sized
	canvasUpscale = [100, 100, 100]
	
	# Add a grid to provide a bit of frame of reference
	grid = vizshape.addGrid(size=(2, 2), step=0.5, scale=canvasUpscale, parent=canvas)
	
	# Add axes which will serve as the indicator
	# The axes are parented to the canvas, placing them at the origin in
	# canvas-space, which is the lower-left corner of the canvas.
	axes = vizshape.addAxes(scale=canvasUpscale, enable=viz.DEPTH_TEST, parent=canvas)


# Setup a link in orientation between the dojo (world coordinates)
# and the axes (the indicator of world orientation)
link = viz.link(dojo, axes, mask=viz.LINK_ORI)

# Because the axes are linked to the main view, any orientation of the
# main view needs to be negated in order that the axes align properly
# with the world coordinates. Setup a link operator wherein the link
# is post-multiplied with the inverse matrix of the main view.
link.postMultInverseLinkable(viz.MainView)
Please let me know if this is helpful, or if I could provide further assistance. Thank you for using Vizard.

Best,

Jeremy Sarchet
Reply With Quote