2011-04-12 50 views
2

我想以標題的形式說明,將相機放置在一個球體中,這是我在Autodesk中創建的一個2星形物體,並將球體放置在世界中視覺工作室,並在其中放置一個相機。這樣做的自然方法是將球體放入vector.zero,然後將相機放在向量中。零點以及任何方向的觀點。但一旦相機進入我的領域,我什麼也看不到..只是背景..這裏是代碼任何幫助表示讚賞。將相機放置在球體中以產生空間效果

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace _3dGame 
{ 

    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     GraphicsDeviceManager graphics; 
     GameObject starBubble; // represents the outter star texture 
     Camera gameCamera;//represents the camera. 
     SpriteBatch spriteBatch; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 


     protected override void Initialize() 
     { 

      starBubble = new GameObject(); 
      gameCamera = new Camera(); 
      base.Initialize(); 
     } 


     Model myModel; 

     // The aspect ratio determines how to scale 3d to 2d projection. 
     float aspectRatio; 

     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      myModel = Content.Load<Model>("Models\\star_bubble2"); 
      aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio; 
     } 


     protected override void UnloadContent() 
     { 

     } 


     protected override void Update(GameTime gameTime) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
       ButtonState.Pressed) 
       this.Exit(); 

      modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * 
       MathHelper.ToRadians(0.1f); 

      base.Update(gameTime); 
     } 


     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     // Set the position of the model in world space, and set the rotation. 
     Vector3 modelPosition = Vector3.Zero; 
     float modelRotation = 0.0f; 

     // Set the position of the camera in world space, for our view matrix. 
     Vector3 cameraPosition = new Vector3(0,0,0); 

     protected override void Draw(GameTime gameTime) 
     { 
      graphics.GraphicsDevice.Clear(Color.White); 

      // Copy any parent transforms. 
      Matrix[] transforms = new Matrix[myModel.Bones.Count]; 
      myModel.CopyAbsoluteBoneTransformsTo(transforms); 

      // Draw the model. A model can have multiple meshes, so loop. 
      foreach (ModelMesh mesh in myModel.Meshes) 
      { 
       // This is where the mesh orientation is set, as well 
       // as our camera and projection. 
       foreach (BasicEffect effect in mesh.Effects) 
       { 
        effect.EnableDefaultLighting(); 
        effect.World = transforms[mesh.ParentBone.Index] * 
         Matrix.CreateRotationY(modelRotation) 
         * Matrix.CreateTranslation(modelPosition); 
        effect.View = Matrix.CreateLookAt(cameraPosition, 
         Vector3.Zero, Vector3.Up); 
        effect.Projection = Matrix.CreatePerspectiveFieldOfView(
         MathHelper.ToRadians(45.0f), aspectRatio, 
         1.0f, 10000.0f); 
       } 
       // Draw the mesh, using the effects set above. 
       mesh.Draw(); 
      } 
      base.Draw(gameTime); 
     } 
    } 
} 
+1

我說這個不知道你使用的框架,但你可能會[背面剔除(HTTPS被咬傷:// secure.wikimedia.org/wikipedia/en/wiki/Back_face_culling)。如果您可以從外部看到球體,但不能從內部看到球體,那幾乎可以肯定會發生什麼,並且在繪製球體時您會想要關閉球體(但是爲其他所有項目保留,因爲它會提高性能) 。希望這可以幫助! – 2011-04-12 23:36:31

+0

經過大量搜索之後,謝謝你,我發現了做到這一點的方式,你是對的!:)非常感謝你,你可以添加這個作爲你的答案。 – Martinos 2011-04-13 00:00:49

回答

2

(事實上:固定法線很重要)

額外提示:

不要用相機旋轉球。

z緩衝區關閉時先繪製球體。

See this article(它使用一個天空盒,但不在這裏重要)

相關問題