我想使用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:UsersadminDesktopFox-Skull-objFox Skull_0.jpg";
string texturePath1 = @"C:UsersadminDesktopFox-Skull-objFox Skull_1.jpg";
string texturePath2 = @"C:UsersadminDesktopFox-Skull-objFox Skull_2.jpg";
string texturePath3 = @"C:UsersadminDesktopFox-Skull-objFox 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的多纹理有任何经验吗?可以帮助我吗?你知道其他可以帮助我的解决方案吗?
编辑2015年12月1日
感谢JohnnyQ的回答,我可能会使上面提到的功能发挥作用。我说"可能"是因为现在当我运行代码并选择.obj文件时,程序会因以下两个错误之一而停止工作:具有关于读取或写入受保护的存储器或程序的尝试的信息的CCD_ 1仅以CCD_。
我已经将上面的代码更新为实际版本。任何建议都将不胜感激。
您必须从枚举类型显式转换为整型。
例如
texture.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);