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

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

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

Posted feedbacks - PostScript

PostScript で。
2次元なら簡単なんですが、3次元は手頃なライブラリが見当らないので力技で。
動画というわけにはいかないので、パラパラ漫画を出力します。
シェーダーを実装する元気はないので、立体感は強引な陰線処理と
ステレオ図の出力で勘弁しといて下さい。
# どっかにレイトレースする PS のコードがあったような....
前半分はひたすら行列計算用のコードです。

このままプリンタ出力すると 36枚で立方体がぐるっと一周まわります。
ghostscriptかAcrobat でページをすばやく切り替えて眺めてやると
回っているように見えるかなぁ.....
  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
%!PS

% Tiny Vector Library
/DotProduct { % [Vector1] [Vector2]  DotProduct scaler
    [ 3 1 roll
    dup length 1 sub 0 1 3 -1 roll {
        3 copy get 3 1 roll exch get mul 3 1 roll
    } for
    pop pop
    ]

    0 exch
    { add } forall
} bind def

/VectorAdd { % [Vector1] [Vector2]   VectorMul  [NewVector]
    [ 3 1 roll
    dup length 1 sub 0 1 3 -1 roll {
    3 copy get 3 1 roll exch get add 3 1 roll
    } for
    pop pop
    ]
} bind def

/VectorScale { % [Vector] scale  VectorScale  [Vector']
    0 1 3 index length 1 sub {
        2 index 1 index get
        2 index mul
        3 index 3 1 roll put
    } for
    pop
} bind def

/VectorCopy { % [Vector]  VectorScale  [NewVector]
    aload length array astore
} bind def


% Tiny Matrix Library
/MatrixTranspose { % [Matrix1] MatrixTranspose [NewMatrix]
    [ exch
    dup 0 get length 1 sub 0 1 3 -1 roll
    {
        [ exch 2 index
        {
            1 index get
            exch
        } forall
        pop ]
        exch
    } for
    pop
    ]
} bind def

/MatrixMul { % [[Matrix1]] [[Matrix2]]  MatrixMul  [[NewMatrix]]
    MatrixTranspose exch
    [ 3 1 roll
    {
        [ exch 2 index
        {
            1 index DotProduct exch
        } forall
        pop
    ] exch
    } forall
    pop
    ]
} bind def

/MatrixCopy { % [[Matrix]] MatrixCopy [[NewMatrix]]
    [ exch
    {
        VectorCopy
    } forall
    ]
} bind def

/Matrix3Copy { % [[[Matrix]]] Matrix3Copy [[[NewMatrix]]]
    [ exch
    {
        MatrixCopy
    } forall
    ]
} bind def

/VectorZero { % [Vector] VectorZero [Vector']
    0 1 2 index length 1 sub {
        1 index exch 0 put
    } for
} bind def
/VectorRoll { % [Vector] roll_count VectorRoll [Vector']
    mark 2 index aload length counttomark 1 add index roll
    counttomark 2 add index astore pop pop pop
} bind def

/MatrixIdentity { % size MatrixIdentity [[NewMatrix]]
    [ exch dup array VectorZero dup 0 1 put
    % [ size [1 0 ... 0]
    exch {
        dup VectorCopy 1 VectorRoll
    } repeat
    pop
    ]
} bind def

% End of Matrix Library
% --------------------------------------
/VY { [ exch ] MatrixTranspose } bind def
/VX { MatrixTranspose 0 get } bind def

/RotateMatrixX { % theta(degree)   RotateMatrixX   [Matrix]
    dup cos exch sin 2 copy neg % cos sin cos -sin
    [
        [ 1 0 0]
        [ 0 6 -2 roll ]
        [ 0 7 -2 roll exch ]
    ]
} bind def

/RotateMatrixY { % theta(degree)   RotateMatrixY   [Matrix]
    dup cos exch sin 2 copy neg % cos sin cos -sin
    [
        [ 6 -1 roll 0 7 -1 roll ]
        [ 0 1 0 ]
        [ 5 -1 roll 0 7 -1 roll ]
    ]
} bind def

/RotateMatrixZ { % theta(degree)   RotateMatrixZ   [Matrix]
    dup cos exch sin 2 copy neg % cos sin cos -sin
    [
        [ 4 -2 roll 0 ]
        [ 5 -2 roll exch 0 ]
        [ 0 0 1 ]
    ]
} bind def

/CompareDistance { % [x1 y1 z1] [x2 y2 z2] CompareDistance [] [] z1-z2
    dup 0 get 2 get 3 -1 roll dup 0 get 2 get
    % [V2] z2 [V1] z1
    exch 4 1 roll sub
} bind def

/Sort { % [[x y] [x1 y1] Array Data ] {CompareFunction}  Sort [ArrayData]
    cvx [ 3 -1 roll
    aload length
    % func -mark[- [] [] [] [] [] len
    -1 2 { % func -mark[- [] [] [] [] [] len2
        -1 2 {
            3 1 roll
            counttomark 1 add index exec  %% Compare
            0 lt { exch } if
            3 -1 roll
            1 roll
        } for
        counttomark 1 roll
    } for
    counttomark 1 roll
    ] exch pop
} bind def

/CalcVertex { % [Vect] distance scale  CalcVertex  x y
        3 -1 roll aload pop
        % distance scale x y z
        5 -2 roll exch
        % x y z scale  distance
        dup 0 eq {
            pop  exch pop
        } {
            3 -1 roll add div
        } ifelse
        % x y scale
        dup 3 -1 roll mul
        % x scale scale*y
        3 1 roll mul exch
} bind def


/DrawPolygon { % [[GC] [Vertex1] [Vertex2] ...] distance scale
    3 -1 roll mark exch aload pop
    counttomark 2 add index counttomark 1 add index CalcVertex
    moveto

    counttomark 1 sub {
        counttomark 2 add index counttomark 1 add index CalcVertex
        lineto
    } repeat
    closepath
    gsave 0.5 setgray fill  grestore
    0.1 setlinewidth stroke
    pop pop pop pop
} bind def

/Projection {
    % distance scale [[Object]] view_matrix Projection
    %                 distance scale [[Object]] view_matrix 
    4 1 roll dup Matrix3Copy dup {
        % vmt dist scale [[Obj]] [[NewObj]] [[Polygon-n]]
        {
            % vmt dist scale [[Obj]] [[NewObj]] [[Vertex-n]]
            dup 6 index exch VY MatrixMul VX
            0 exch putinterval
        } forall
    } forall
    /CompareDistance Sort
    % vmt dist scale [[Obj]] [[NewObj']] 
    {
        % vmt dist scale [[Obj]] [[NewPolygon-n]]
        3 index 3 index DrawPolygon
    } forall
    4 -1 roll
} bind def


/Translate { % [[[Object]]] [dx dy dz]  Translate [[[Object']]]
    1 index {
        % [[[Object]]] [dx dy dz] [[Polygon]]
        {
            % [[[Object]]] [dx dy dz] [x y z]
            dup 2 index VectorAdd
            % [[[Object]]] [dx dy dz] [x y z] [nx ny nz]
            0 exch putinterval
        } forall
    } forall
    pop
} bind def

% ---------------- Test Code -------------------

/Cube [
    % Gravity Center, Vertex1, 2, .... 
    [[0.5 0.5 0] [0 0 0] [1 0 0] [1 1 0] [0 1 0]]
    [[0.5 0.5 1] [0 0 1] [1 0 1] [1 1 1] [0 1 1]]
    [[0 0.5 0.5] [0 0 0] [0 1 0] [0 1 1] [0 0 1]]
    [[1 0.5 0.5] [1 0 0] [1 1 0] [1 1 1] [1 0 1]]
    [[0.5 0 0.5] [0 0 0] [0 0 1] [1 0 1] [1 0 0]]
    [[0.5 1 0.5] [0 1 0] [0 1 1] [1 1 1] [1 1 0]]
] def


/Demo {
    % Projection Distance  & Scale
    5   400
    % Object
    Cube [-0.5 -0.5 -0.5] Translate
    % Initial View Matrix
    3 MatrixIdentity
    -20 RotateMatrixX MatrixMul
    20 RotateMatrixY MatrixMul
    5 RotateMatrixZ MatrixMul

    % Rotation
    36 {
        gsave
        200 200 translate
        1 1 scale
        0.1 setlinewidth
        Projection
%---- Stereo Projection ----
        200 0 translate
        5 RotateMatrixY MatrixMul
        Projection
        -5 RotateMatrixY MatrixMul
%---- End Stereo Projection ----
        grestore
        showpage
        10 RotateMatrixY MatrixMul
    } repeat
    4 { pop } repeat
} bind def

Demo

Index

Feed

Other

Link

Pathtraq

loading...