![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| 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 | |||