[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System;
class MyGame : Game
{
GraphicsDeviceManager graphics;
BasicEffect basicEffect;
VertexPositionNormalTexture[] vertices;
ContentManager content;
Texture2D texture;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
int planeCount = 30;
vertices = new VertexPositionNormalTexture[planeCount * 6];
for (int i = 0; i < planeCount; i++)
{
VertexPositionNormalTexture[] plane
= new VertexPositionNormalTexture[4];
plane[0] = new VertexPositionNormalTexture(
getPosition(i, planeCount) + new Vector3(0, 1, 0),
getPosition(i, planeCount),
new Vector2((float)i / planeCount, 0)
);
plane[1] = new VertexPositionNormalTexture(
getPosition(i + 1, planeCount) + new Vector3(0, 1, 0),
getPosition(i + 1, planeCount),
new Vector2((float)(i + 1)/ planeCount, 0)
);
plane[2] = new VertexPositionNormalTexture(
getPosition(i, planeCount) + new Vector3(0, -1, 0),
getPosition(i, planeCount),
new Vector2((float)i / planeCount, 1)
);
plane[3] = new VertexPositionNormalTexture(
getPosition(i + 1, planeCount) + new Vector3(0, -1, 0),
getPosition(i + 1, planeCount),
new Vector2((float)(i + 1) / planeCount, 1)
);
vertices[i * 6] = plane[0];
vertices[i * 6 + 1] = plane[1];
vertices[i * 6 + 2] = plane[2];
vertices[i * 6 + 3] = plane[1];
vertices[i * 6 + 4] = plane[3];
vertices[i * 6 + 5] = plane[2];
}
}
Vector3 getPosition(int index, int count)
{
return new Vector3(
(float)Math.Sin(2 * Math.PI * index / count),
0,
(float)Math.Cos(2 * Math.PI * index / count)
);
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
basicEffect.View = Matrix.CreateLookAt(
new Vector3(0, 0, 8),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0)
);
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45),
Window.ClientBounds.Width / (float)Window.ClientBounds.Height,
1, 100
);
texture = content.Load<Texture2D>("FlyingSpaghettiMonster");
}
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent) { content.Unload(); }
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements
);
basicEffect.TextureEnabled = true;
basicEffect.Texture = texture;
basicEffect.LightingEnabled = true;
basicEffect.DirectionalLight0.Enabled = true;
basicEffect.DirectionalLight0.Direction = new Vector3(1, 0, -1);
basicEffect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
basicEffect.DirectionalLight0.SpecularColor = Color.White.ToVector3();
basicEffect.DiffuseColor = Color.Green.ToVector3();
basicEffect.SpecularColor = Color.Blue.ToVector3();
basicEffect.SpecularPower = 100;
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(
PrimitiveType.TriangleList,
vertices,
0,
vertices.Length / 3
);
pass.End();
}
basicEffect.End();
}
}