技術をかじる猫

適当に気になった技術や言語、思ったこと考えた事など。

正方形テクスチャ表示

間が開いた。
まずは必要な変数定義と頂点。

BasicEffect effect;
Texture2D texture;

VertexPositionTexture[] vertexes = new[] {
    new VertexPositionTexture(new Vector3(0.7f, 1, 0), new Vector2(1, 0)),
    new VertexPositionTexture(new Vector3(-0.7f, -1, 0), new Vector2(0, 1)),
    new VertexPositionTexture(new Vector3(-0.7f, 1, 0), new Vector2(0, 0)),
    new VertexPositionTexture(new Vector3(0.7f, -1, 0), new Vector2(1, 1))
};

VertexBuffer vertexBuffer;
IndexBuffer indexBuffer;

int[] indices = new int[]
{ 
    0, 1, 2,
    0, 3, 1
};

次にLoadContentで画像の読み込みや頂点設定を行う。

this.effect = new BasicEffect(this.GraphicsDevice);
this.effect.TextureEnabled = true;

this.texture = this.Content.Load<Texture2D>("friends");

this.indexBuffer = new IndexBuffer(
    this.GraphicsDevice,
    IndexElementSize.ThirtyTwoBits,
    indices.Length,
    BufferUsage.None);

this.indexBuffer.SetData<int>(indices);

this.vertexBuffer = new VertexBuffer(
    this.GraphicsDevice,
    typeof(VertexPositionTexture),
    this.vertexes.Length,
    BufferUsage.None);

this.vertexBuffer.SetData<VertexPositionTexture>(this.vertexes);

もちろんUnloadContentでのDisposeは忘れない。

this.vertexBuffer.Dispose();
this.indexBuffer.Dispose();
this.effect.Dispose();

そしたら、作画を行う。作画の際に、テクスチャを設定する。

this.effect.Texture = this.texture;

foreach (var pass in this.effect.CurrentTechnique.Passes)
{
    pass.Apply();
    GraphicsDevice.SetVertexBuffer(vertexBuffer);
    GraphicsDevice.Indices = indexBuffer;

    GraphicsDevice.DrawIndexedPrimitives(
        PrimitiveType.TriangleList,
        0,  //baseIndex
        0,  //minVertexIndex
        this.vertexes.Length,
        0,  //startIndex
        indices.Length / 3
    );
}

というかこの定義眺めたら漠然とした不安。
もしかして1オブジェクトあたり1テクスチャしか貼れないのか、、、、。
1枚のテクスチャに複数の画像を入れるという昔ながらの手法には適しているが、1オブジェクト1テクスチャの原則が必要になるのは正直嬉しくないなぁ(==;