3

I am new to WPF and models, and I wanted to make an interface that could open various obj files and display the models.

At the momment all of them appear black in the viewport as below:

black model

I'm not sure why, but I think it is a lightning issue, because all the models have different positions, and I don't know how to change the position. I don't think it has to do with the obj it self, as in the viewer from windows, it appears colored:

colored model

This is my xaml part:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

    <ListBox x:Name="Groupfiles" SelectionChanged="Groupfiles_Selected" Grid.Column="0"></ListBox>
    <StackPanel Grid.Column="1">
        <Button x:Name="ClickThebutton" Click="ClickThebutton_Click" >Click Me</Button>
    </StackPanel>
    <helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" Grid.Column="2" >
    </helix:HelixViewport3D>
</Grid>

This is the c# part:

ModelVisual3D device3D = new ModelVisual3D();
string file_to_open = null;
file_to_open = Groupfiles.SelectedItem.ToString();
string obj_file = Path.ChangeExtension(file_to_open, ".obj");
if (!File.Exists(obj_file))
{
     /// converts original file to obj file
}

device3D.Content = Display3d(obj_file);

viewPort3d.Children.Clear();
viewPort3d.Children.Add(device3D);
viewPort3d.ZoomExtents();

The Display Method is as such (from the helix3d tutorial I saw):

public static Model3D Display3d(string model)
{
    Model3D device = null;
    try
    {
        //Adding a gesture here
        viewPort3d.RotateGesture =new MouseGesture(MouseAction.LeftClick));
            
        //Import 3D model file
        ModelImporter import = new ModelImporter();

        //Load the 3D model file
        device= import.Load(model);
    }
    catch (Exception e)
    {
        // Handle exception in case can not find the 3D model file
        MessageBox.Show("Exception Error : " + e.StackTrace);
    }
    return device;
}

I have tried to find an answer to this issue but to no avail. Could you please help me.

Peter O.
  • 32,158
  • 14
  • 82
  • 96

2 Answers2

0

I did not see you setup any lights in your code.

Lance H
  • 886
  • 1
  • 9
  • 14
  • I used to have the in xaml inside the viewerport, but still the same effect. Could please point to how to do it properly? – José Pintor Jan 23 '21 at 17:01
0

I solved my issue by adding these two lines after zoomextents();

ui.viewPort3d.Children.Remove(ui.defaultLights);
ui.viewPort3d.Children.Add(ui.defaultLights);

Thank you for everyone who took their time to help.