challenge ポリゴンを表示するプログラム

適当なポリゴンを表示させて、描画するプログラムを書いてください。
ポリゴンは回転させてください。

2D処理だけなら、標準ライブラリで大体いけますが、
3D処理は追加でライブラリを利用すると思うので、
何のライブラリを利用したのか書いてください。

Posted feedbacks - C#

DirectXを使いました~。
平面ポリゴンを回転させてるだけです。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

class ViewPoly : Form
{
    private Device dev;
    private VertexBuffer vertex;
    private float polyTheta = 0.0f;

    public bool Init()
    {
        this.Text = "ポリゴン回ります";
        this.MaximizeBox = false;
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.Size = new Size(480, 480);

        PresentParameters pp = new PresentParameters();
        pp.Windowed = true;
        pp.SwapEffect = SwapEffect.Discard;
        pp.EnableAutoDepthStencil = true;
        pp.AutoDepthStencilFormat = DepthFormat.D16;

        try
        {
            this.dev = new Device(
                0, 
                DeviceType.Reference,
                this.Handle,
                CreateFlags.SoftwareVertexProcessing,
                pp
            );
        }
        catch
        {
            return false;
        }

        this.vertex = new VertexBuffer(
            typeof(CustomVertex.PositionColored),
            4,
            this.dev,
            Usage.None,
            CustomVertex.PositionColored.Format,
            Pool.Managed
        );

        this.dev.Transform.View = Matrix.LookAtLH(
            new Vector3(0.0f, 0.0f, -15.0f),
            new Vector3(0.0f, 0.0f, 0.0f),
            new Vector3(0.0f, 1.0f, 0.0f)
        );

        this.dev.Transform.Projection = Matrix.PerspectiveFovLH(
            Geometry.DegreeToRadian(60.0f),
            (float)this.dev.Viewport.Width / (float)this.dev.Viewport.Height,
            1.0f,
            100.0f
        );

        this.dev.RenderState.Lighting = false;
        this.dev.RenderState.CullMode = Cull.None;

        return true;
    }

    public void Render()
    {
        this.dev.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkBlue, 1.0f, 0);
        this.dev.BeginScene();
        this.RollingPoly();
        this.dev.EndScene();
        this.dev.Present();

    }

    private void RollingPoly()
    {
        float theta = Geometry.DegreeToRadian(
            (float)((polyTheta -= 10.0f) % 360.0f)
        );

        CustomVertex.PositionColored[] vertexPc
            = new CustomVertex.PositionColored[4];

        vertexPc[0] = new CustomVertex.PositionColored(
            -4.0f * (float)(Math.Sin(theta)),
            4.0f,
            -4.0f * (float)Math.Cos(theta),
            Color.Red.ToArgb()
        );
        vertexPc[1] = new CustomVertex.PositionColored(
            4.0f * (float)(Math.Sin(theta)),
            4.0f,
            4.0f * (float)Math.Cos(theta),
            Color.Blue.ToArgb()
        );
        vertexPc[2] = new CustomVertex.PositionColored(
            -4.0f * (float)(Math.Sin(theta)),
            -4.0f,
            -4.0f * (float)Math.Cos(theta),
            Color.Red.ToArgb()
        );
        vertexPc[3] = new CustomVertex.PositionColored(
            4.0f * (float)(Math.Sin(theta)),
            -4.0f,
            4.0f * (float)Math.Cos(theta),
            Color.Blue.ToArgb()
        );

        using (GraphicsStream gs
            = this.vertex.Lock(0, 0, LockFlags.None))
        {
            gs.Write(vertexPc);
            this.vertex.Unlock();
        }

        this.dev.SetStreamSource(0, this.vertex, 0);
        this.dev.VertexFormat = CustomVertex.PositionColored.Format;
        this.dev.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
    }

    public void dispose()
    {
        if (this.vertex != null)
            this.vertex.Dispose();

        if (this.dev != null)
            this.dev.Dispose();
    }

    public static void Main(string[] args)
    { 
        using (ViewPoly vp = new ViewPoly())
        {
            if (vp.Init())
            {
                vp.Show();
                while (vp.Created)
                {
                    vp.Render();
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(50);
                }
                vp.dispose();
            }
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...