Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
Dim gdm As GraphicsDeviceManager = GraphicsDeviceManager.Current
If gdm.RenderMode = RenderMode.Unavailable Then
Select Case gdm.RenderModeReason
Case RenderModeReason.SecurityBlocked
MessageBox.Show( _
"3D support is currently disabled due to enforce" & _
" security. You can enable it by following these steps ...")
Case RenderModeReason.GPUAccelerationDisabled
MessageBox.Show( _
"Developer error! Use the enableGPUAcceleration " & _
"parameter in the test page to enable 3D rendering.")
Case RenderModeReason.Not3DCapable
MessageBox.Show( _
"Your computer doesn't appear to support 3D rendering.")
Case RenderModeReason.TemporarilyUnavailable
MessageBox.Show( _
"There was a problem accessing the video card driver.")
End Select
Else
' Run the application as normal.
Me.RootVisual = New MainPage()
End If
End Sub
http://space.silverlightchina.net/xpeter/demo/3D.htm
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Test8
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Model myModel;
float Angle = 0;
float Angle1 = 0;
float myScale = 1f;
Vector2 currentPosition;
float currentScaleWheelValue;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myModel =Content .Load<Model>("Models\\teapot");
}
protected override void UnloadContent()
{
}
private void CheckPutIn()
{
KeyboardState newState = Keyboard.GetState();
//左右键控制绕Y轴旋转
if (newState.IsKeyDown(Keys.Left))
Angle += 0.01f;
if (newState.IsKeyDown(Keys.Right))
Angle -= 0.01f;
//上下键控制绕Z轴旋转
if (newState.IsKeyDown(Keys.Up))
Angle1 += 0.01f;
if (newState.IsKeyDown(Keys.Down))
Angle1 -= 0.01f;
//A、S键控制缩放
if (newState.IsKeyDown(Keys.A))
myScale += 0.01f;
if (newState.IsKeyDown(Keys.S))
myScale -= 0.01f;
this.IsMouseVisible = true;
MouseState newState1 = Mouse.GetState();
//鼠标左键控制旋转
if (newState1.LeftButton == ButtonState.Pressed)
{
if (currentPosition.X != newState1.X||currentPosition .Y!=newState1.Y)
{
if (currentPosition.X < newState1.X)
Angle += 0.01f;
if (currentPosition.X > newState1.X)
Angle -= 0.01f;
if (currentPosition.Y > newState1.Y)
Angle1 += 0.01f;
if (currentPosition.Y < newState1.Y)
Angle1 -= 0.01f;
}
currentPosition.X = newState1.X;
currentPosition.Y = newState1.Y;
}
//鼠标滚轮控制缩放
if (newState1.ScrollWheelValue != currentScaleWheelValue)
{
if (currentScaleWheelValue > newState1.ScrollWheelValue)
myScale += 0.01F;
if (currentScaleWheelValue < newState1.ScrollWheelValue)
myScale -=0.01f;
}
currentScaleWheelValue = newState1.ScrollWheelValue;
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
CheckPutIn();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.CreateRotationY(Angle)*Matrix.CreateRotationZ(Angle1)*Matrix.CreateScale(myScale);
effect.View = Matrix.CreateLookAt(new Vector3(0,8,20),Vector3 .Zero,Vector3 .Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper .ToRadians(45),
(float)graphics .GraphicsDevice .Viewport.Width/
(float)graphics.GraphicsDevice.Viewport .Height,1.0f,100.0f);
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
}