McNeel Wiki
Info: Rhino 4.0 SDK: Textures and Mappings
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
DeveloperC++, .NET
VersionRhino 4

Prerequisites:

  • You know what mesh texture coordinates are and how they are used to render images stored in a bitmap on a mesh.
    • If you do not satisfy this prequisite, then you can learn about texture coordinates in a computer graphics textbook like Computer Graphics: Principles and Practice in C, by Foley, van Dam, et al, or the OpenGL Programming Guide (The Red Book).
  • You know how to write C++ Rhino 4.0 plug-ins. You know how to use CRhinoGetObject to select objects. You can add and modify CRhinoDoc objects and table entries.
  • You are familiar with the CRhinoMeshObject and ON_Mesh classes.

Vocabulary:

  • Texture Bitmap: A bitmap image, usually saved in a file.
  • Texture Coordinates: In Rhino these are 2d and 3d points that are saved in an ON_Mesh in the m_T[] or m_TC[] arrays.
  • Texture Mapping: A function that sets texture coordinates. Persistent texture mappings are stored in CRhinoDoc::m_texture_mapping_table[].
  • Render Material: A collection of rendering color and shading information, including the names of texture bitmaps. Rendering materials are stored in CRhinoDoc::m_material_table[].
  • Object Attributes: Attributes of a Rhino object, including the rendering materials and texture mappings the object uses, are stored in the CRhinoObjectAttributes class returned by CRhinoObject::Attributes().

Example 1: Create a material with bitmap texture.

This example

  • Creates a material with a bitmap texture.
  • Modifies a mesh object's attributes and texture coordinates so the bitmap is projected onto the mesh along the world Z axis.
  // Create a material with a texture bitmap
  ON_Texture tex;
  tex.m_filename = L"full path to your texture.jpg/bmp/...";
  tex.m_bOn = true;
  tex.m_type = ON_Texture::bitmap_texture;
  tex.m_mode = ON_Texture::modulate_texture;


  ON_Material mat;
  mat.m_diffuse.SetRGB(150,0,0);
  mat.m_specular.SetRGB(200,200,200);
  mat.m_shine = 0.5*ON_Material::GetMaxShine()
  mat.AddTexture(tex);


  int mat_index = context.m_doc.m_material_table.AddMaterial(mat);
  if ( mat_index < 0 )
    return CRhinoCommand::failure;  


  // Select a mesh to modify
  CRhinoGetObject go;
  go.SetGeometryFilter(ON::mesh_object);
  go.SetCommandPrompt(L"Select a mesh");
  go.GetObjects(1,1);
  if ( CRhinoCommand::success != go.CommandResult() )
    return go.CommandResult();
  const CRhinoMeshObject* mesh0_object =
     CRhinoMeshObject::Cast(go.Object(0).Object());
  if ( 0 == mesh0_object )
    return CRhinoCommand::failure;
  const ON_Mesh* mesh0 = mesh0_object->Mesh();
  if ( 0 == mesh0 )
    return CRhinoCommand::failure;


  // Copy the mesh and set its texture coordinates
  ON_Mesh* mesh1 = new OnMesh(mesh0);
  ON_BoundingBox bbox = mesh1->BoundingBox();
  ON_Interval x_extents(bbox.m_min.x,bbox.m_max.x);
  ON_Interval y_extents(bbox.m_min.y,bbox.m_max.y);


  const int vertex_count = mesh1->m_V.Count();
  mesh1->m_T.Reserve(vertex_count);
  mesh1->m_T.SetCount(0);
  for ( int vi = 0; vi < vertex_count; vi++ )
  {
    const ON_3dPoint& V = mesh->m_V[vi];
    ON_2fPoint& tc = mesh1->m_T.AppendNew();
    tc.x = (float)x_extents.NormalizedParameterAt(V.x);
    tc.y = (float)y_extents.NormalizedParameterAt(V.y);
  }


  // Update the mesh
  CRhinoMeshObject* mesh1_object = new CRhinoMeshObject();
  mesh1_object.SetMesh(mesh1);
  context.m_doc.ReplaceObject(CRhinoObjRef(mesh0_object),mesh1_object);
  ON_3dmObjectAttributes att = mesh1_object->Attributes();
  att.m_material_index;
  att.SetMaterialSource( ON::material_from_object );
  context.m_doc.Redraw();

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result
    ' Create a material with a texture bitmap
    Dim tex As New OnTexture()
    tex.m_filename = "full path to your texture.jpg/bmp/..."
    tex.m_bOn = True
    tex.m_type = IOnTexture.TYPE.bitmap_texture
    tex.m_mode = IOnTexture.MODE.modulate_texture


    Dim mat As New OnMaterial()
    mat.m_diffuse.SetRGB(150, 0, 0)
    mat.m_specular.SetRGB(200, 200, 200)
    mat.m_shine = 0.5 * OnMaterial.MaxShine()
    mat.AddTexture(tex)


    Dim mat_index As Integer = context.m_doc.m_material_table.AddMaterial(mat)
    If (mat_index < 0) Then Return IRhinoCommand.result.failure


    ' Select a mesh to modify
    Dim go As New MRhinoGetObject()
    go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object)
    go.SetCommandPrompt("Select a mesh")
    go.GetObjects(1, 1)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then
      Return go.CommandResult()
    End If
    Dim mesh0_object As IRhinoMeshObject = _
        MRhinoMeshObject.ConstCast(go.Object(0).Object())
    If (mesh0_object Is Nothing) Then Return IRhinoCommand.result.failure
    Dim mesh0 As IOnMesh = mesh0_object.Mesh()
    If (mesh0 Is Nothing) Then Return IRhinoCommand.result.failure


    ' Copy the mesh and set its texture coordinates
    Dim mesh1 As New OnMesh(mesh0)
    Dim bbox As OnBoundingBox = mesh1.BoundingBox()
    Dim x_extents As New OnInterval(bbox.m_min.x, bbox.m_max.x)
    Dim y_extents As New OnInterval(bbox.m_min.y, bbox.m_max.y)


    Dim vertex_count As Integer = mesh1.m_V.Count()
    mesh1.m_T.Reserve(vertex_count)
    mesh1.m_T.SetCount(0)
    For vi As Integer = 0 To vertex_count - 1
      Dim V As IOn3fPoint = mesh1.m_V(vi)
      Dim tc As New On2fPoint()
      tc.x = CType(x_extents.NormalizedParameterAt(V.x), Single)
      tc.y = CType(y_extents.NormalizedParameterAt(V.y), Single)
      mesh1.m_T.Append(tc)
    Next
    ' Update the mesh
    Dim mesh1_object As New MRhinoMeshObject()
    mesh1_object.SetMesh(mesh1)
    context.m_doc.ReplaceObject(New MRhinoObjRef(mesh0_object), mesh1_object)
    Dim att As New On3dmObjectAttributes(mesh1_object.Attributes())
    att.m_material_index = mat_index
    att.SetMaterialSource(IOn.object_material_source.material_from_object)
    mesh1_object.ModifyAttributes(att)
    context.m_doc.Redraw()


    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    // Create a material with a texture bitmap
    OnTexture tex = new OnTexture();
    tex.m_filename = "full path to your texture.jpg/bmp/...";
    tex.m_bOn = true;
    tex.m_type = IOnTexture.TYPE.bitmap_texture;
    tex.m_mode = IOnTexture.MODE.modulate_texture;


    OnMaterial mat = new OnMaterial();
    mat.m_diffuse.SetRGB(150,0,0);
    mat.m_specular.SetRGB(200,200,200);
    mat.m_shine = 0.5*OnMaterial.MaxShine();
    mat.AddTexture(tex);


    int mat_index = context.m_doc.m_material_table.AddMaterial(mat);
    if ( mat_index < 0 )
      return IRhinoCommand.result.failure;


    // Select a mesh to modify
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetGeometryFilter( IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object );
    go.SetCommandPrompt("Select a mesh");
    go.GetObjects(1,1);
    if( go.CommandResult() != IRhinoCommand.result.success )
      return go.CommandResult();
    IRhinoMeshObject mesh0_object = MRhinoMeshObject.ConstCast( go.Object(0).Object() );
    if( mesh0_object==null )
      return IRhinoCommand.result.failure;
    IOnMesh mesh0 = mesh0_object.Mesh();
    if ( mesh0==null )
      return IRhinoCommand.result.failure;


    // Copy the mesh and set its texture coordinates
    OnMesh mesh1 = new OnMesh(mesh0);
    OnBoundingBox bbox = mesh1.BoundingBox();
    OnInterval x_extents = new OnInterval(bbox.m_min.x,bbox.m_max.x);
    OnInterval y_extents = new OnInterval(bbox.m_min.y,bbox.m_max.y);


    int vertex_count = mesh1.m_V.Count();
    mesh1.m_T.Reserve(vertex_count);
    mesh1.m_T.SetCount(0);
    for( int vi=0; vi<vertex_count; vi++ )
    {
      IOn3fPoint V = mesh1.m_V[vi];
      On2fPoint tc = new On2fPoint();
      tc.x = (float)x_extents.NormalizedParameterAt(V.x);
      tc.y = (float)y_extents.NormalizedParameterAt(V.y);
      mesh1.m_T.Append(tc);
    }


    // Update the mesh
    MRhinoMeshObject mesh1_object = new MRhinoMeshObject();
    mesh1_object.SetMesh(mesh1);
    context.m_doc.ReplaceObject(new MRhinoObjRef(mesh0_object),mesh1_object);
    On3dmObjectAttributes att = new On3dmObjectAttributes(mesh1_object.Attributes());
    att.m_material_index = mat_index;
    att.SetMaterialSource(IOn.object_material_source.material_from_object);
    mesh1_object.ModifyAttributes(att);
    context.m_doc.Redraw();


    return IRhinoCommand.result.success;
  }
rename · changes · history · subscriptions · lost and found · references · file upload