コード例 #1
0
		protected override void Initialize()
		{
			base.Initialize();
			_graphicsDeviceManager = new WpfGraphicsDeviceService(this);

			_texture = Content.Load<Texture2D>("hello");

			_spriteBatch = new SpriteBatch(GraphicsDevice);

			_keyboard = new WpfKeyboard(this);
			_mouse = new WpfMouse(this);
		}
コード例 #2
0
		protected override void Initialize()
		{
			float tilt = MathHelper.ToRadians(0);  // 0 degree angle
												   // Use the world matrix to tilt the cube along x and y axes.
			_worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
			_viewMatrix = Matrix.CreateLookAt(new Vector3(5, 5, 5), Vector3.Zero, Vector3.Up);

			_projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
				MathHelper.ToRadians(45),  // 45 degree angle
				(float)GraphicsDevice.Viewport.Width /
				(float)GraphicsDevice.Viewport.Height,
				1.0f, 100.0f);

			_basicEffect = new BasicEffect(GraphicsDevice);

			_basicEffect.World = _worldMatrix;
			_basicEffect.View = _viewMatrix;
			_basicEffect.Projection = _projectionMatrix;

			// primitive color
			_basicEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f);
			_basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
			_basicEffect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
			_basicEffect.SpecularPower = 5.0f;
			_basicEffect.Alpha = 1.0f;

			_basicEffect.LightingEnabled = true;
			if (_basicEffect.LightingEnabled)
			{
				_basicEffect.DirectionalLight0.Enabled = true; // enable each light individually
				if (_basicEffect.DirectionalLight0.Enabled)
				{
					// x direction
					_basicEffect.DirectionalLight0.DiffuseColor = new Vector3(1, 0, 0); // range is 0 to 1
					_basicEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1, 0, 0));
					// points from the light to the origin of the scene
					_basicEffect.DirectionalLight0.SpecularColor = Vector3.One;
				}

				_basicEffect.DirectionalLight1.Enabled = true;
				if (_basicEffect.DirectionalLight1.Enabled)
				{
					// y direction
					_basicEffect.DirectionalLight1.DiffuseColor = new Vector3(0, 0.75f, 0);
					_basicEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(0, -1, 0));
					_basicEffect.DirectionalLight1.SpecularColor = Vector3.One;
				}

				_basicEffect.DirectionalLight2.Enabled = true;
				if (_basicEffect.DirectionalLight2.Enabled)
				{
					// z direction
					_basicEffect.DirectionalLight2.DiffuseColor = new Vector3(0, 0, 0.5f);
					_basicEffect.DirectionalLight2.Direction = Vector3.Normalize(new Vector3(0, 0, -1));
					_basicEffect.DirectionalLight2.SpecularColor = Vector3.One;
				}
			}

			_vertexDeclaration = new VertexDeclaration(new[]
			{
				new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
				new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
				new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
			});

			Vector3 topLeftFront = new Vector3(-1.0f, 1.0f, 1.0f);
			Vector3 bottomLeftFront = new Vector3(-1.0f, -1.0f, 1.0f);
			Vector3 topRightFront = new Vector3(1.0f, 1.0f, 1.0f);
			Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f);
			Vector3 topLeftBack = new Vector3(-1.0f, 1.0f, -1.0f);
			Vector3 topRightBack = new Vector3(1.0f, 1.0f, -1.0f);
			Vector3 bottomLeftBack = new Vector3(-1.0f, -1.0f, -1.0f);
			Vector3 bottomRightBack = new Vector3(1.0f, -1.0f, -1.0f);

			Vector2 textureTopLeft = new Vector2(0.0f, 0.0f);
			Vector2 textureTopRight = new Vector2(1.0f, 0.0f);
			Vector2 textureBottomLeft = new Vector2(0.0f, 1.0f);
			Vector2 textureBottomRight = new Vector2(1.0f, 1.0f);

			Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f);
			Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f);
			Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f);
			Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f);
			Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f);
			Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f);

			var cubeVertices = new VertexPositionNormalTexture[36];

			// Front face.
			cubeVertices[0] = new VertexPositionNormalTexture(topLeftFront, frontNormal, textureTopLeft);
			cubeVertices[1] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
			cubeVertices[2] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);
			cubeVertices[3] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
			cubeVertices[4] = new VertexPositionNormalTexture(bottomRightFront, frontNormal, textureBottomRight);
			cubeVertices[5] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);

			// Back face.
			cubeVertices[6] = new VertexPositionNormalTexture(topLeftBack, backNormal, textureTopRight);
			cubeVertices[7] = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
			cubeVertices[8] = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
			cubeVertices[9] = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
			cubeVertices[10] = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
			cubeVertices[11] = new VertexPositionNormalTexture(bottomRightBack, backNormal, textureBottomLeft);

			// Top face.
			cubeVertices[12] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
			cubeVertices[13] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);
			cubeVertices[14] = new VertexPositionNormalTexture(topLeftBack, topNormal, textureTopLeft);
			cubeVertices[15] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
			cubeVertices[16] = new VertexPositionNormalTexture(topRightFront, topNormal, textureBottomRight);
			cubeVertices[17] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);

			// Bottom face.
			cubeVertices[18] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
			cubeVertices[19] = new VertexPositionNormalTexture(bottomLeftBack, bottomNormal, textureBottomLeft);
			cubeVertices[20] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
			cubeVertices[21] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
			cubeVertices[22] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
			cubeVertices[23] = new VertexPositionNormalTexture(bottomRightFront, bottomNormal, textureTopRight);

			// Left face.
			cubeVertices[24] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);
			cubeVertices[25] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
			cubeVertices[26] = new VertexPositionNormalTexture(bottomLeftFront, leftNormal, textureBottomRight);
			cubeVertices[27] = new VertexPositionNormalTexture(topLeftBack, leftNormal, textureTopLeft);
			cubeVertices[28] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
			cubeVertices[29] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);

			// Right face.
			cubeVertices[30] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
			cubeVertices[31] = new VertexPositionNormalTexture(bottomRightFront, rightNormal, textureBottomLeft);
			cubeVertices[32] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);
			cubeVertices[33] = new VertexPositionNormalTexture(topRightBack, rightNormal, textureTopRight);
			cubeVertices[34] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
			cubeVertices[35] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);

			_vertexBuffer = new VertexBuffer(GraphicsDevice, _vertexDeclaration, cubeVertices.Length, BufferUsage.None);
			_vertexBuffer.SetData(cubeVertices);

			_keyboard = new WpfKeyboard(this);
			_mouse = new WpfMouse(this);
		}