Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Adding models to a scene programmatically

 
 
Old 03-03-2006   #1 (permalink)
Kevin Hoffman


 
 

Adding models to a scene programmatically

You know, if the samples that claim they are for the Feb CTP (The MSDN
SDK site) would actually work properly, I think my blood pressure might
be a little lower.

Anyway, following their example for how to add models to a scene
dynamically, I have the following code in my "Loaded" event handler:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Storyboard s;

s = (Storyboard)this.FindResource("sbAngleRotater");
this.BeginStoryboard(s);

// create a couple extra cubes
for (int i = 0; i < 3; i++)
{
ModelVisual3D extraCube = new ModelVisual3D();
Model3DGroup modelGroup = new Model3DGroup();
GeometryModel3D model3d = new GeometryModel3D();


model3d.Geometry =
(MeshGeometry3D)Application.Current.Resources["UnitCube"];
DiffuseMaterial graySide = new DiffuseMaterial(new
SolidColorBrush(Colors.Gray));
model3d.Material = graySide;
modelGroup.Children.Add(model3d);

extraCube.Content = modelGroup;
extraCube.Transform = new TranslateTransform3D(-1 * i, -1 * i,
-1 * i);
mainViewport.Children.Add(extraCube);
}
}

Here's the rub : None of the programmatically generated cubes show up.
I know that the "UnitCube" resource is working just fine because I have
one of those cubes displaying properly using pure XAML.

Is there something the MSDN sample completely left out? I've tried
about 20 different variations of this, including putting ambient lights
in the new group to no avail.

The ultimate goal here is to take the geometry supplied for the
origin-centered cube (StaticResource UnitCube) and create a couple
clones in the scene, using a TranslateTransform3d to "move" them to
different places in the scene.

Unfortunately, my scene still only has the items from my XAML and not
from the programmatic creation.


My System SpecsSystem Spec
Old 03-04-2006   #2 (permalink)
Bob Brown


 
 

Re: Adding models to a scene programmatically

Have you checked to make sure that the Application.Current.Resources is not
returning null to the Geometry property of your GeometryModel3D? It looks
like you're loading resources from two separate places in this code. I just
wanted to make sure that your mesh is defined in "Application.xaml" because
it looks like that's where you're searching for it.

-Bob


"Kevin Hoffman" <alothien@gmail.com> wrote in message
news:1141357279.334348.236020@u72g2000cwu.googlegroups.com...
> You know, if the samples that claim they are for the Feb CTP (The MSDN
> SDK site) would actually work properly, I think my blood pressure might
> be a little lower.
>
> Anyway, following their example for how to add models to a scene
> dynamically, I have the following code in my "Loaded" event handler:
>
> private void Window_Loaded(object sender, RoutedEventArgs e)
> {
> Storyboard s;
>
> s = (Storyboard)this.FindResource("sbAngleRotater");
> this.BeginStoryboard(s);
>
> // create a couple extra cubes
> for (int i = 0; i < 3; i++)
> {
> ModelVisual3D extraCube = new ModelVisual3D();
> Model3DGroup modelGroup = new Model3DGroup();
> GeometryModel3D model3d = new GeometryModel3D();
>
>
> model3d.Geometry =
> (MeshGeometry3D)Application.Current.Resources["UnitCube"];
> DiffuseMaterial graySide = new DiffuseMaterial(new
> SolidColorBrush(Colors.Gray));
> model3d.Material = graySide;
> modelGroup.Children.Add(model3d);
>
> extraCube.Content = modelGroup;
> extraCube.Transform = new TranslateTransform3D(-1 * i, -1 * i,
> -1 * i);
> mainViewport.Children.Add(extraCube);
> }
> }
>
> Here's the rub : None of the programmatically generated cubes show up.
> I know that the "UnitCube" resource is working just fine because I have
> one of those cubes displaying properly using pure XAML.
>
> Is there something the MSDN sample completely left out? I've tried
> about 20 different variations of this, including putting ambient lights
> in the new group to no avail.
>
> The ultimate goal here is to take the geometry supplied for the
> origin-centered cube (StaticResource UnitCube) and create a couple
> clones in the scene, using a TranslateTransform3d to "move" them to
> different places in the scene.
>
> Unfortunately, my scene still only has the items from my XAML and not
> from the programmatic creation.
>



My System SpecsSystem Spec
Old 03-04-2006   #3 (permalink)
Kevin Hoffman


 
 

Re: Adding models to a scene programmatically

That's just plain bizarre. I assumed, given that the rest of the .NET
Framework works this way, that if you do:

(MeshGeometry3D)expression

then if 'expression' is null, you will get a type cast exception ..
e.g. when working with Session State in ASP.NET:

MyCustomer customer = (MyCustomer)Session["customer"];

if Session["customer" is null, you get a typecast failure attempting to
cast 'null' to 'MyCustomer'.

The only way I can see this working is if there is an explicit type
converter for 'null' for the MeshGeometry3D class.

btw, I changed Application.Current.Resources["UnitCube"] to
this.Resources["UnitCube"] and all my stuff magically appeared.

I really thought that typecasting would've given me an exception if the
resource didn't exist - thats why I never persued that avenue. I
figured if I wasn't getting a typecast failure on that line of code
then all was good since 'null' should've thrown an exception...
*boggle*

Thanks,
Kevin

My System SpecsSystem Spec
Old 03-06-2006   #4 (permalink)
viliescu


 
 

Re: Adding models to a scene programmatically

I just wanted to clarify casting to null issue:
Null doesn't provoke an exception in a cast. This example is valid:
MyCustomer customer = (MyCustomer)null;
--
Valentin Iliescu [MVP C#]


"Kevin Hoffman" wrote:

> That's just plain bizarre. I assumed, given that the rest of the .NET
> Framework works this way, that if you do:
>
> (MeshGeometry3D)expression
>
> then if 'expression' is null, you will get a type cast exception ..
> e.g. when working with Session State in ASP.NET:
>
> MyCustomer customer = (MyCustomer)Session["customer"];
>
> if Session["customer" is null, you get a typecast failure attempting to
> cast 'null' to 'MyCustomer'.
>
> The only way I can see this working is if there is an explicit type
> converter for 'null' for the MeshGeometry3D class.
>
> btw, I changed Application.Current.Resources["UnitCube"] to
> this.Resources["UnitCube"] and all my stuff magically appeared.
>
> I really thought that typecasting would've given me an exception if the
> resource didn't exist - thats why I never persued that avenue. I
> figured if I wasn't getting a typecast failure on that line of code
> then all was good since 'null' should've thrown an exception...
> *boggle*
>
> Thanks,
> Kevin
>
>

My System SpecsSystem Spec
Old 03-07-2006   #5 (permalink)
Stefano \WildHeart\ Lanzavecchia


 
 

Re: Adding models to a scene programmatically


"Kevin Hoffman" <alothien@gmail.com> wrote in message
news:1141491927.100280.56320@z34g2000cwc.googlegroups.com...
> That's just plain bizarre. I assumed, given that the rest of the .NET
> Framework works this way, that if you do:
>
> (MeshGeometry3D)expression
>
> then if 'expression' is null, you will get a type cast exception ..
> e.g. when working with Session State in ASP.NET:
>
> MyCustomer customer = (MyCustomer)Session["customer"];
>
> if Session["customer" is null, you get a typecast failure attempting to
> cast 'null' to 'MyCustomer'.


Strange C# compiler you have... The following compiles and runs on the .NET
framework 2.0:

using System;
using System.Collections.Generic;
using System.Text;
namespace NullTest
{
class A
{
int b;
string c;
}

class Program
{
static void Main(string[] args)
{
A a = new A();
A b = null;
A c = (A)null;
Console.WriteLine("{0}, {1}, {2}", a, b, c);
}
}
}

--
WildHeart'2k6



My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
32/64-bit Raw Image support for Windows (~300 camera models supported) Vista General
Adding meta:resourcekey to database programmatically looping througwebcontrols .NET General
HTK models in Vista speech recognition Vista General
Microsoft Adds 3-D City Models to Live Search Vista News


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46