[Windows Phone 7] Creating a Mesh programmatically

Feb 19, 2011 at 8:29 PM
Edited Feb 19, 2011 at 8:30 PM

Hello!

Firstly I want to say you how deeply I'm impressed by your work and how many opportunities it gives me to develop Silverlight applications for WP7 using 3D graphics. Thank you for great job :)

 

Unfortunately I have a small problem and can't understand what's wrong here.

I've written a small class, which represents an object consisting of 4 vertices. This object is then supposed to become larger/smaller by changing its 'Scale' property:

 

 

public class MyMesh : Balder.Objects.Geometries.Geometry
    {
        internal Vertex[] BaseVertices = { new Vertex(-2,0,0), new Vertex(0,0,0),
                              new Vertex(0,2,0), new Vertex(0,0,2)};
        internal Vertex[] ModifiedVertices = new Vertex[4];

        float scale = 1;

        public float Scale
        {
            set
            {
                scale = value;
                InvalidatePrepare();
            }
            get
            {
                return scale;
            }
        }

        public override void Prepare(Viewport viewport)
        {
            GenerateVertices();
            GenerateFaces();

            GeometryHelper.CalculateNormals(FullDetailLevel);

            base.Prepare(viewport);
        }

        public void GenerateVertices()
        {
            FullDetailLevel.AllocateVertices(BaseVertices.Length);

            for (int i = 0; i < BaseVertices.Length; i++)
            {
                ModifiedVertices[i] = new Vertex(BaseVertices[i].X * Scale, BaseVertices[i].Y * Scale, BaseVertices[i].Z * Scale);
                FullDetailLevel.SetVertex(i, ModifiedVertices[i]);
            }
        }

        void GenerateFaces()
        {
            FullDetailLevel.AllocateFaces(2);

            FullDetailLevel.SetFace(0, new Face(0, 2, 1));
            FullDetailLevel.SetFace(1, new Face(1, 2, 3));
        }
}

 

When it's rendered for the first time to the screen, everything is OK, by when I try to change the 'scale' property from the code and the 'InvalidatePrepare()' method is called, there is a NullReferenceException somewhere in 'GenerateFaces'.

So, what's wrong here???

 

With best regards,

Mikhail

Coordinator
Feb 20, 2011 at 5:17 AM

Hi and thanks for your kind words, glad you like it.

I'm not quite sure why it would fail during the second generating of the mesh, but for what you're trying to achieve you could use the built in scaling. If you just drop the Scale property you've created and use the BaseVertices directly. There is already a Scale property on the base class Node that Geometry comes from. That Scale property is a Coordinate, so you can control X,Y,Z individually or set all 3 components to the same.

Hope this helps.

Feb 20, 2011 at 7:27 AM
Edited Feb 20, 2011 at 7:28 AM

Thank you, but actually this is VERY strange. I've just discovered that I can't even run this SIMPLIEST example:

 

public class MyMesh : Balder.Objects.Geometries.Geometry
    {
        public override void Prepare(Viewport viewport)
        {
            GenerateVertices();
            GenerateLines();

            base.Prepare(viewport);
        }

        void GenerateVertices()
        {
            FullDetailLevel.AllocateVertices(4);

            FullDetailLevel.SetVertex(0, new Vertex(-2, 0, 0));
            FullDetailLevel.SetVertex(1, new Vertex(0, 0, 0));
            FullDetailLevel.SetVertex(2, new Vertex(0, 2, 0));
            FullDetailLevel.SetVertex(3, new Vertex(0, 0, 2));
        }

        void GenerateLines()
        {
            FullDetailLevel.AllocateLines(5);
            FullDetailLevel.SetLine(0, new Balder.Objects.Geometries.Line(1, 0));
            FullDetailLevel.SetLine(1, new Balder.Objects.Geometries.Line(1, 2));
            FullDetailLevel.SetLine(2, new Balder.Objects.Geometries.Line(1, 3));
            FullDetailLevel.SetLine(3, new Balder.Objects.Geometries.Line(0, 2));
            FullDetailLevel.SetLine(4, new Balder.Objects.Geometries.Line(2, 3));
        }
    }

 

- I get the same NullReferenceException. (Call stack location: Balder.dll!Balder.Rendering.Xna.GeometryDetailLevel.PrepareVertexBuffer(Balder.Objects.Geometries.Geometry geometry) + 0x8 bytes)

I just declare the instance of MyMesh in the XAML file and do nothing more, but it doesn't work!..

 

<Execution:Game Width="400" Height="500" VerticalAlignment="Top">
                <Execution:Game.Camera>
                    <View:Camera Position="-3,1,-5"/>
                </Execution:Game.Camera>
                <Lighting:OmniLight 
                    Diffuse="#808080"
                    Specular="White" 
                    Position="-1,1,-5" 
                    Strength="0.5"/>
                <my:MyMesh/>
            </Execution:Game>

I wonder if this functionality was tested on the phone? Should it work correctly?

Coordinator
Feb 21, 2011 at 9:42 PM

Hi,

well.   Lines aren't supported yet, but if you'd switch that to faces you should get a result. 

Let me know if it doesn't work with Faces.