helix-toolkit / helix-toolkit

Helix Toolkit is a collection of 3D components for .NET.

Home Page:http://helix-toolkit.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem loading model?

logiclrd opened this issue · comments

I want to display a model in a 3D viewport as part of my WPF application. When I add the model to my project and double-click it in Visual Studio, it displays as expected:

image

I've never done this before, so I found a tutorial and followed it step by step, and when I finished all the steps and ran it, my window looked like this:

image

I found myself wondering if this were a "white on a white background" issue, so I made the window blue, and sure enough, as I drag the camera around, something is visible.

image

This is some sort of reflection off of the surface; as the camera moves, the weird little swatches of white move smoothly too. But, the surface of the mug itself doesn't seem to be rendered. I assumed I must be doing something wrong, since I'm brand new to this, so I found a sample online someone else had made of a simple OBJ file viewer, and when I fire that up, I get:

image

Hmm, white-on-white again? I made this one also have a blue background, and:

image

Well, this looks familiar! So, given that the sample is ostensibly known to work, it seems I'm not doing anything wrong on the code side. And, Visual Studio loads and displays the model just fine, so it seems the model itself isn't broken. What's going wrong? Is there something I can do differently to make this work??

I'm placing the viewport like this:

        <Viewport3D Name="v3dViewport" Grid.ColumnSpan="2" Grid.RowSpan="2" MouseDown="v3dViewport_MouseDown" MouseMove="v3dViewport_MouseMove" MouseUp="v3dViewport_MouseUp" />

I'm loading the model like this:

			var reader = new ObjReader(Dispatcher);

			var model = reader.Read(@"B:\code\SubDesigner\Mug.obj");

			model.Children.Add(new AmbientLight(Color.FromRgb(190, 190, 190)));
			model.Children.Add(new DirectionalLight() { Direction = new Vector3D(3, -4, 5), Color = Colors.White });

			_visual = new ModelVisual3D();
			_visual.Content = model;
			_visual.Transform = new Transform3DGroup();

			v3dViewport.Camera =
				new PerspectiveCamera()
				{
					Position = new Point3D(0, 0, -20),
					LookDirection = new Vector3D(0, 0, 1),
					UpDirection = new Vector3D(0, 1, 0),
					NearPlaneDistance = 0,
					FarPlaneDistance = 50,
					FieldOfView = 45
				};

			v3dViewport.Children.Add(_visual);
			v3dViewport.Children.Add(new DefaultLights());

Am I doing something obviously wrong? Thanks!

Please upload a sample project so we can help you to debug more easily.

I found out the problem :-) Bear in mind that I'm super new to all this, so this is probably the first thing you'd have checked. But, the .mtl file referred to an external image file for its map_Kd (it came that way from the place I found it), and that reference was broken. When I fixed that, it started working properly. :-)

image

Visual Studio's loader must substitute a solid white texture when it can't find the image, so it "automagically" did the right thing, by accident.

I've since figured out how to dynamically draw to the texture at runtime (which I need for this project). It looks like I'm off to the races. :-)