我想使用 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
消息停止。
我已将上面的代码更新为实际版本。任何建议仍然受到赞赏。