H3DAPI Examples repository

From H3D.org

Jump to: navigation, search

Contents

Using H3DAPI

Examples distributed with H3DAPI

The examples are referred to by the search path after navigating to H3DAPI/examples directory in an installation of H3DAPI.

X3D-only Examples

These examples only use an X3D-file. There might be H3D-specific nodes in use though.

  • CubeMap/CubeMap.x3d - Demonstrates the ComposedCubeMapTexture X3D node.
  • DepthMapSurface/depthMappedITS.x3d - Shows the feeling of the DepthMapSurface node on a simple plane. Does not work with OpenHapticsrenderer.
  • DepthMapSurface/depthMappedSphere.x3d - Shows the feeling of the DepthMapSurface node on a sphere. Does not work with OpenHapticsrenderer.
  • ForceEffects/forcefield.x3d - Demonstrates the ForceField node. A force tries to push the haptics device to the right.
  • ForceEffects/magneticGeometryEffect.x3d - Demonstrates the MagneticGeometryEffect node. A spring force tries to pull the haptics device towards the surface of a Geometry. The geometry is not displayed by default.
  • ForceEffects/positionFunctionEffect.x3d - Demonstrates the PositionFunctionEffect node. A force that depends on the position of the haptics device is calculated.
  • ForceEffects/timeFunctionEffect.x3d - Demonstrate the TimeFunctionEffect node. A force that depends on time is calculated.
  • HapticLayers/hapticLayers.x3d - Shows the usage of different surface layers. A box has another shape-layer inside it. The second shape is different from the box.
  • magneticlines/magneticlines.x3d - Displays lines that are magnetic. Only works with OpenHapticsRenderer.
  • Shader nodes/cg_glass.x3d - Example of how to use shaders in H3DAPI. Use the CG language to render an object which looks like it is made of glass.
  • Shader nodes/glsl_polkadot.x3d - Example of how to use shaders in H3DAPI. Use the GLSL language to render an object with white dots on a red colored surface.
  • Sound - The sound follows the haptics device. Demonstrates the Sound node.
  • SuperShape/SuperShape.x3d - Demonstrates the SuperShape node.
  • x3dmodels - In this directory there are subdirectories containing 3D-models defined in X3D with added haptics. They can be touched with the haptics device when loaded in a scene.
  • All - In this directory there is at least one example for every node in H3D. Some might use python. The text file h3d_examples_list (no file ending) contains a mapping from node to example x3d name.

X3D/python Examples

  • bumpmap/bumpmap.x3d - Shows how to create a bump mapped texture using a MultiTexture node and python. The haptics device is used to position the light source.
  • DeformableShape/deform.x3d - Demonstrates the usage of the DeformableShape node. This node is a simple deformable shape which works best for membrane-like objects. Uses python to generate the shape to deform.
  • DynamicTransform/dynamic.x3d - Demonstrates the functionality of the DynamicTransform node on a box that can be pushed but which tries to get back to an equilibrium.
  • flight/flight.x3d - A simple example showing how to steer a flight pod around in a virtual world. Does not use X3D navigation. The pod is controlled by the haptics device or in case of no haptics device the Keyboard.
  • ForceEffects/springs.x3d - Demonstrates the node SpringEffect. A spring is set in the center of a transparent non-haptic sphere. When the haptics device is placed inside the sphere the sphere radius increases and the haptics device is pulled towards the center as long as it is inside the sphere. A custom field defined in python is used to change the radius of the sphere.
  • manualExamples/Python/Sphere.x3d - Example from the manual. When the left mouse buttons is pressed the color of the sphere is changed.
  • manualExamples/Python/Spheres.x3d - Example from the manual. When the left mouse buttons is pressed a new colored sphere is pplaced at a (limited) random location in the scene.
  • Shader nodes/cg_earthshader.x3d - Example of how to use shaders in H3DAPI. Use the CG language to be able to use the haptics device as an imitation of the sun shining on earth.
  • Shader nodes/glsl_earthshader.x3d - Example of how to use shaders in H3DAPI. Use the GLSL language to be able to use the haptics device as an imitation of the sun shining on earth.
  • SpaceTennis/SpaceTennis.x3d - A simple tennis game in which the "racket" is controlled by the haptics device. Gives force feedback when the racket hits the ball.
  • All - In this directory there is at least one example for every node in H3D. Some might use python. The text file h3d_examples_list (no file ending) contains a mapping from node to example x3d name.

C++ Examples

  • manualExamples/C++/CMakeLists.txt - Examples from the manual. Use CMake to generate build files for Sphere and Spheres. The C++ equivalence of manualExamples/Python/Sphere.x3d and manualExamples/Python/Spheres.x3d.
  • ParticleDemo - Only when checking out source. An incomplete example that demonstrates the Particle Systems component of X3D.
  • ThreadExample - An example of setting up a thread in H3DAPI.
  • H3DLoad and H3DViewer are not located in the examples folder but can be seen as examples of programs that use H3DAPI to load X3D-scenes and set different properties.

VRML Examples

Examples not distributed with H3DAPI

X3D/python Examples

Paint texture using Haptics Device

This example shows how easy it is to use H3DAPI to let the user draw in a texture using the haptics device as pencil. There are limits to this approach though. A to big texture will not be updated at graphics frame-rate. For such cases (and for arbitrary textures) a C++ approach would have to be used. As an example of a C++ approach see PaintableTexture tutorial

paintTexture.x3d:

<Group>
    <Transform rotation="1 0 0 0">
      <Shape>
        <Appearance DEF="APPEARANCE" >
          <Material/>
          <SmoothSurface />
        </Appearance>
        <Rectangle2D DEF="GEOM" size="0.4 0.4" solid="FALSE" />
      </Shape>
    </Transform>
 
  <PythonScript DEF="PS" url="paintTexture.py" >
    <Appearance USE="APPEARANCE" containerField="references" />
  </PythonScript>
 
  <ROUTE fromNode="GEOM" fromField="contactTexCoord"
         toNode="PS"      toField="drawTexCoord"/>
 
</Group>

paintTexture.py

 
from H3DInterface import *
 
# make sure the proxy is drawn at the position of the haptics device
tex_channels = 1
tex_size_s = 32
tex_size_t = 32
tex_matrix = []
for i in range( tex_size_s * tex_size_t ):
  tex_matrix.append( "0xFF" )
 
appearance_node, = references.getValue()
 
string_of_values = ""
for i in tex_matrix:
  string_of_values= string_of_values + i + " "
pixelTexture =  createX3DNodeFromString( """<PixelTexture repeatS="false" repeatT="false" image=" %i %i %i %s"  />
""" % (tex_size_s, tex_size_t, tex_channels, string_of_values) )[0]
appearance_node.texture.setValue( pixelTexture )
 
class DrawTexCoord( AutoUpdate( MFVec3f ) ):
  def update( self, event ):
    event_value = event.getValue()
    for value in event_value:
      # added because 0,0,0 is sent as first event, maybe due to resize.
      if value.x == value.y == value.z == 0:
        return event_value
      s_index = int(value.x * tex_size_s)
      if s_index >= tex_size_s:
        s_index = tex_size_s - 1
      t_index = int((1 - value.y) * tex_size_t)
      if t_index >= tex_size_t:
        t_index = tex_size_t - 1
      if( tex_matrix[t_index * tex_size_t + s_index] != "0x50" ):
        tex_matrix[t_index * tex_size_t + s_index] = "0x50"
        string_of_values = ""
        for i in tex_matrix:
          string_of_values= string_of_values + i + " "
        pixelTexture =  createX3DNodeFromString( """<PixelTexture repeatS="false" repeatT="false" image=" %i %i %i %s"  />
        """ % (tex_size_s, tex_size_t, tex_channels, string_of_values) )[0]
        appearance_node.texture.setValue( pixelTexture )
    return event_value
 
drawTexCoord = DrawTexCoord()
 
 

C++ Examples

  • Extended C++ manual examples - A kind forum user extended the C++ examples in manualExamples/C++ and added some extra features. Follow the link to the forum thread.

Extending H3DAPI

Take a look at the source code. All classes inheriting from classes with the X3D-prefix or H3D-prefix are example implementation of the feature the base class represents.

VHTK and the UI nodes are also good examples of how to extend H3DAPI. Take a look at the source code for those libraries.

Also check the section on advanced H3DAPI programming.

Personal tools
go to