none
Manipulation 3D dans xna pour wp7 RRS feed

  • Question

  • Salut tout le monde :

    Svp qui peut me donner ou est le problème dans ce code je veux manipuler le model manuellement dans xna for wp7 (voir  un  chat en 3 dimensions) : voila le code :

    C’est un projet mixte silverlight et xna version 4.0 :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input.Touch;
    namespace SlXnaProjet3D
    {
        public partial class GamePage : PhoneApplicationPage
        {
            ContentManager content;
            GameTimer timer;
            SpriteBatch spriteBatch;
            BasicEffect basicShader;
            
            Model chat;
            float cameraFOV = 45; // Initial camera FOV (serves as a zoom level)
            float rotationXAmount = 0.0f;
            float rotationYAmount = 0.0f;
            float? prevLength;        
            public GamePage()
            {
                InitializeComponent();
                // Get the content manager from the application
                content = (Application.Current as App).Content;
                
                // Create a timer for this page
                timer = new GameTimer();
                timer.UpdateInterval = TimeSpan.FromTicks(333333);
                timer.Update += OnUpdate;
                timer.Draw += Draw;            
            }
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                // Set the sharing mode of the graphics device to turn on XNA rendering
                SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
                
                TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.Pinch | GestureType.PinchComplete;
                // TODO: use this.content to load your game content here
                chat = content.Load<Model>("chat");
                // Initialize the basic shader           
                basicShader = new BasicEffect(SharedGraphicsDeviceManager.Current.GraphicsDevice);
                basicShader.DiffuseColor = Color.Aqua.ToVector3();
                basicShader.Alpha = 1.0f;
                basicShader.SpecularPower = 5.0f;
                basicShader.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);
                // Setup the camera vectors objects and view matrix
                Vector3 camPos = new Vector3(0.0f, 0.9f, 15.0f);
                Vector3 camView = new Vector3(0.0f, 0.9f, -5.0f);
                Vector3 camUp = new Vector3(0.0f, 1.0f, 0.0f);
                Matrix viewMatrix = Matrix.CreateLookAt(camPos, camView, camUp);
                Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(cameraFOV), SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.AspectRatio, 10, 20000);                     
               
                
                basicShader.View = viewMatrix;
                basicShader.Projection = projectionMatrix;                     
                // Start the timer
                timer.Start();
                base.OnNavigatedTo(e);
            }
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                // Stop the timer
                timer.Stop();
                // Set the sharing mode of the graphics device to turn off XNA rendering
                SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
                base.OnNavigatedFrom(e);
            }
            private void OnUpdate(object sender, GameTimerEventArgs e)
            {
                // TODO: Add your update logic here            
                HandleInput();
                chat.Root.Transform = GetRotationMatrix();            
            }
            
            private Matrix GetRotationMatrix()
            {
                Matrix matrix = Matrix.CreateWorld(new Vector3(0, 250, 0), Vector3.Forward, Vector3.Up) *
                   Matrix.CreateFromYawPitchRoll((float)Math.PI + MathHelper.PiOver2 + rotationXAmount / 100, rotationYAmount / 100, 0);
                return matrix;
            }
            private void HandleInput()
            {
                while (TouchPanel.IsGestureAvailable)
                {
                    GestureSample gestureSample = TouchPanel.ReadGesture();
                    switch (gestureSample.GestureType)
                    {
                        case GestureType.FreeDrag:
                            rotationXAmount += gestureSample.Delta.X;
                            rotationYAmount -= gestureSample.Delta.Y;
                            break;
                        case GestureType.Pinch:
                            float gestureValue = 0;
                            float minFOV = 60;
                            float maxFOV = 30;
                            float gestureLengthToZoomScale = 10;
                            Vector2 gestureDiff = gestureSample.Position - gestureSample.Position2;
                            gestureValue = gestureDiff.Length() / gestureLengthToZoomScale;
                            if (null != prevLength) // Skip the first pinch event
                                cameraFOV -= gestureValue - prevLength.Value;
                            cameraFOV = MathHelper.Clamp(cameraFOV, maxFOV, minFOV);
                            prevLength = gestureValue;
                            break;
                        case GestureType.PinchComplete:
                            prevLength = null;
                            break;
                        default:
                            break;
                    }
                }
            }      
       
            private void Draw(object sender, GameTimerEventArgs e)
            {              
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.RosyBrown);            
                spriteBatch.Begin();
                Matrix[] transforms = new Matrix[chat.Bones.Count];
                chat.CopyAbsoluteBoneTransformsTo(transforms);            
                ModelMesh mesh = chat.Meshes[0];
                ModelMeshPart meshPart = mesh.MeshParts[0];
                // set the vertex source to the mesh part's vertex buffer @ offset
                SharedGraphicsDeviceManager.Current.GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer, meshPart.VertexOffset);
                // set the index buffer to the mesh part's
                SharedGraphicsDeviceManager.Current.GraphicsDevice.Indices = meshPart.IndexBuffer;
                // Set a new world position
                Matrix scale = Matrix.CreateScale(4f, 4f, 4f);
                Matrix rot = Matrix.CreateRotationY((float)Math.PI / 8);
                basicShader.World = Matrix.Identity * scale * rot;
                foreach (EffectPass effectPass in basicShader.CurrentTechnique.Passes)
                {
                    effectPass.Apply();
                    SharedGraphicsDeviceManager.Current.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                                  0,
                                                                  0,
                                                                  meshPart.NumVertices,
                                                                  meshPart.StartIndex,
                                                                  meshPart.PrimitiveCount);
                spriteBatch.End();
            }
           
            }
           
        }
    }

    Ce code me tourne le model fixe et il ne me donne pas l’accès de lui  tourner.

    Svp qui peut m’aider j’ai quel que semaine à ce stade.



    abir

    • Déplacé Ciprian Duduiala dimanche 15 avril 2012 08:06 (Origine :Développement .NET (en général))
    mardi 10 avril 2012 17:38