使用C#和ActiViz .NET进行多重纹理

时间:2015-11-30 20:53:37

标签: c# vtk multitexturing

我想使用ActiViz .NET和C#来多重纹理从.obj文件创建的对象。

只要我知道以某种方式使用单一纹理的纹理,我就遇到了多个纹理的问题。基于我在VTK GitHub上发现的内容,我开始编写代码,但是 SetBlendingMode MapDataArrayToMultiTextureAttribute 方法存在问题。 我像这样使用SetBlendingMode:

texture.SetBlendingMode(vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);

结果与:

The best overloaded method match for 'Kitware.VTK.vtkTexture.SetBlendingMode(int)' has some invalid arguments

使用MapDataArrayToMultiTextureAttribute,它看起来像这样:

mapper.MapDataArrayToMultiTextureAttribute(vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", vtkDataObject.FIELD_ASSOCIATION, 0);

以类似的消息结束:

The best overloaded method match for 'Kitware.VTK.vtkPolyDataMapper.MapDataArrayToMultiTextureAttribute(int, string, int, int)' has some invalid arguments

整个代码:

private void ReadOBJ(string path)
        {

            vtkTesting test = vtkTesting.New();

        // Varibles for obj file and texture
        string filePath = path;
        string texturePath0 = @"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_0.jpg";
        string texturePath1 = @"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_1.jpg";
        string texturePath2 = @"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_2.jpg";
        string texturePath3 = @"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_3.jpg";

        // Open jpeg file including texture
        vtkJPEGReader jpegReader = new vtkJPEGReader();
        jpegReader.SetFileName(texturePath0);
        jpegReader.Update();

        vtkJPEGReader jpegReader1 = new vtkJPEGReader();
        jpegReader1.SetFileName(texturePath1);
        jpegReader1.Update();
        vtkJPEGReader jpegReader2 = new vtkJPEGReader();
        jpegReader2.SetFileName(texturePath2);
        jpegReader2.Update();
        vtkJPEGReader jpegReader3 = new vtkJPEGReader();
        jpegReader3.SetFileName(texturePath3);
        jpegReader3.Update();

        // Open obj file
        vtkOBJReader reader = new vtkOBJReader();
        if (!File.Exists(filePath))
        {
            MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
            return;
        }
        reader.SetFileName(filePath);
        reader.Update();

        vtkTriangleFilter triangleFilter = vtkTriangleFilter.New();
        triangleFilter.SetInputConnection(reader.GetOutputPort());

        vtkStripper stripper = vtkStripper.New();
        stripper.SetInputConnection(triangleFilter.GetOutputPort());
        stripper.Update();

        vtkPolyData polydata = stripper.GetOutput();
        polydata.Register(null);
        polydata.GetPointData().SetNormals(null);

        vtkFloatArray TCoords = vtkFloatArray.New();
        TCoords.SetNumberOfComponents(2);
        TCoords.Allocate(8, 0);

        TCoords.InsertNextTuple2(0.0, 0.0);
        TCoords.InsertNextTuple2(1.0, 0.0);
        TCoords.InsertNextTuple2(0.0, 1.0);
        TCoords.InsertNextTuple2(1.0, 1.0);
        TCoords.SetName("TCoords");

        polydata.GetPointData().AddArray(TCoords);


        // Create texture
        vtkTexture texture = new vtkTexture();
        vtkTexture texture1 = new vtkTexture();
        vtkTexture texture2 = new vtkTexture();
        vtkTexture texture3 = new vtkTexture();
        texture.SetInputConnection(jpegReader.GetOutputPort());
        texture1.SetInputConnection(jpegReader1.GetOutputPort());
        texture2.SetInputConnection(jpegReader2.GetOutputPort());
        texture3.SetInputConnection(jpegReader3.GetOutputPort());

        texture.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);
        texture1.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);
        texture2.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);
        texture3.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);

        // Mapping textures


        vtkTextureMapToCylinder mapSphere = new vtkTextureMapToCylinder();
        mapSphere.SetInputConnection(reader.GetOutputPort());


        // Visualize           
        vtkPolyDataMapper mapper = vtkPolyDataMapper.New();           
        mapper.SetInput(polydata);

        // Get a reference to the renderwindow of our renderWindowControl1
        vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
        // Renderer
        vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();  

        vtkRenderWindowInteractor iren = vtkRenderWindowInteractor.New();
        iren.SetRenderWindow(renderWindow);

        // Create actor and add mapper with texture
        vtkActor actor = vtkActor.New();

        vtkOpenGLHardwareSupport hardware = vtkOpenGLRenderWindow.SafeDownCast(renderWindow).GetHardwareSupport();
        bool supported = hardware.GetSupportsMultiTexturing();
        int tu = 0;

        if (supported)
        {
            tu = hardware.GetNumberOfFixedTextureUnits();
        }
        if (supported && tu > 2)
        {
            mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
            mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_1, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
            mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_2, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
            mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_3, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);              

            actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, texture);
            actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_1, texture1);
            actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_2, texture2);
            actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_3, texture3);

        }
        else
        {
            if (supported)
            {
                mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", (int)vtkDataObject.AttributeTypes.POINT, 0);
            }
            actor.SetTexture(texture);
        }

        actor.SetMapper(mapper);
        renderWindow.AddRenderer(renderer);



        // Hide current actor
        renderer.RemoveAllViewProps();
        // Set background color
        renderer.SetBackground(0.3, 0.6, 0.3);
        // Add new actor to the renderer
        renderer.AddActor(actor);

        // Render
        renderWindow.Render();

        }

有没有人对C#和VTK的多重纹理有任何经验,可以帮助我吗?你知道其他任何可以帮助我的解决方案吗?

编辑01.12.2015

感谢JohnnyQ回答,我可能会让上面提到的功能正常工作。我说"可能"因为现在我正在运行代码并选择.obj文件程序停止使用以下两个错误之一: An unhandled exception of type 'System.AccessViolationException' occurred in Kitware.VTK.dll包含有关读取或写入受保护内存或程序尝试的信息,只有vshost32.exe stop working消息才会停止。

我已将上述代码更新为实际版本。任何建议仍然受到赞赏。

1 个答案:

答案 0 :(得分:0)

您必须从枚举类型显式转换为整数类型。

e.g。

var dropdown = $("#dropdown").kendoDropDownList({
    dataSource: [{
        text: "",
        value: ""
    }],
    dataTextField: "text",
    dataValueField: "value",
    open: function (e) {
        // If the treeview is not visible, then make it visible.
        if (!$treeviewRootElem.hasClass("k-custom-visible")) {
            $treeviewRootElem.slideToggle('fast', function () {
                dropdown.close();
                $treeviewRootElem.addClass("k-custom-visible");
            });
        }
    }
}).data("kendoDropDownList");
var $dropdownRootElem = $(dropdown.element).closest("span.k-dropdown");

var treeview = $("#treeview").kendoTreeView({
    select: function (e) {
        // When a node is selected, display the text for the node in the dropdown and hide the treeview.
        $dropdownRootElem.find("span.k-input").text($(e.node).children("div").text());
        $treeviewRootElem.slideToggle('fast', function () {
            $treeviewRootElem.removeClass("k-custom-visible");
        });
    }
}).data("kendoTreeView");
var $treeviewRootElem = $(treeview.element).closest("div.k-treeview");

// Hide the treeview.
$treeviewRootElem.width($dropdownRootElem.width())
    .css({
    "border": "1px solid grey",
        "display": "none",
        "position": "absolute"
});

// Position the treeview so that it is below the dropdown.
$treeviewRootElem.css({
    "top": $dropdownRootElem.position().top + $dropdownRootElem.height(),
        "left": $dropdownRootElem.position().left
});

$(document).click(function (e) {
    // Ignore clicks on the treetriew.
    if ($(e.target).closest("div.k-treeview").length == 0) {
        // If visible, then close the treeview.
        if ($treeviewRootElem.hasClass("k-custom-visible")) {
            $treeviewRootElem.slideToggle('fast', function () {
                $treeviewRootElem.removeClass("k-custom-visible");
            });
        }
    }
});