Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
661 ataualpa 1
;
2
; application : Flag - Polonia in Tertio Millenium - wavy shading rotary area
3
; compiler    : FASM
4
; system      : MenuetOS
5
; author      : macgub
6
; email       : macgub3@wp.pl
7
; web         : www.menuet.xt.pl
8
; Fell free to use this intro in your own distribution of MenuetOS.
9
SIZE_X equ 220
10
SIZE_Y equ 260
11
TIMEOUT equ 1
12
ROUND equ 12
13
points_count equ 50
14
triangles_count equ 54
15
 
16
use32
17
 
18
        org    0x0
19
 
20
        db     'MENUET01'       ; 8 byte id
21
        dd     0x01        ; header version
22
        dd     START        ; start of code
23
        dd     I_END        ; size of image
24
        dd     I_END        ; memory for app
25
        dd     I_END        ; esp
26
        dd     0x0 , 0x0        ; I_Param , I_Icon
27
 
28
START:    ; start of execution
29
 
30
     call draw_window
31
 ;    call generate_map
32
 
33
still:
34
 
35
    mov  eax,23   ; wait here for event with timeout
36
    mov  ebx,TIMEOUT
37
    cmp [speed_flag],0xff
38
    jne speed_skip
39
    mov eax,11
40
   speed_skip:
41
    int  0x40
42
 
43
    cmp  eax,1   ; redraw request ?
44
    je  red
45
    cmp  eax,2   ; key in buffer ?
46
    je  key
47
    cmp  eax,3   ; button in buffer ?
48
    je  button
49
 
50
    jmp  noclose
51
 
52
  red:    ; redraw
53
    call draw_window
54
    jmp  noclose
55
 
56
  key:    ; key
57
    mov  eax,2   ; just read it and ignore
58
    int  0x40
59
    jmp  noclose
60
 
61
  button:   ; button
62
    mov  eax,17   ; get id
63
    int  0x40
64
 
65
    cmp  ah,1   ; button id=1 ?
66
    jne shad_button
67
    mov  eax,-1   ; close this program
68
    int  0x40
69
    shad_button:
70
    cmp ah,2
71
    jne speed_button
72
    not [shad_flag]         ; set shadow / flag mode
73
    speed_button:
74
    cmp ah,3
75
    jne noclose
76
    not [speed_flag]
77
  noclose:
78
 
79
  call calculate_angle   ; calculates sinus and cosinus
80
  call generate_map
81
  call copy_points
82
  call rotate_points
83
  call translate_points  ; translate from 3d to 2d
84
  call clrscr            ; clear the screen
85
  call sort_triangles
86
  call draw_triangles    ; draw all triangles from the list
87
 
88
  mov eax,7          ; put image
89
  mov ebx,screen
90
  mov ecx,SIZE_X shl 16 + SIZE_Y
91
  mov edx,5 shl 16 + 20
92
  int 0x40
93
 
94
  jmp  still
95
generate_map:
96
  finit
97
  mov edi,points
98
  xor ebx,ebx      ;z
99
 again_gen1:
100
  mov eax,70           ;x
101
 again_gen:
102
  mov word[edi],ax
103
  mov word[edi+4],bx
104
  fild word[edi]
105
  fidiv [i20]
106
  fadd [current_angle]
107
  fsin
108
  fimul [i20]
109
  fiadd [i75]
110
  fistp word [edi+2]
111
;  fild word[edi]       ;another map generation
112
;  fisub [i100]
113
;  fidiv [i75]
114
;  fmul st,st0
115
;  fild word[edi+4]
116
;  fisub [i50]
117
;  fidiv [i20]
118
;  fmul st,st0
119
;  faddp
120
;  fsqrt
121
;  fadd [current_angle]
122
;  fsin
123
;  fimul [i20]
124
;  fiadd [i75]
125
;  fistp word[edi+2]
126
 
127
  add ax,10
128
  add edi,6
129
  cmp ax,170
130
  jne again_gen
131
  add bx,20
132
  cmp bx,100
133
  jne again_gen1
134
  mov dword[edi],0xffffffff
135
ret
136
i20 dw 20
137
i50 dw 50
138
i75 dw 75
139
i100 dw 100
140
 
141
sort_triangles:
142
  mov esi,triangles
143
  mov edi,triangles_with_z
144
  mov ebp,points_rotated
145
 
146
 make_triangle_with_z:            ;makes list with triangles and z position
147
  xor eax,eax
148
  mov ax,word[esi]
149
  shl eax,1
150
  mov ebx,eax
151
  shl eax,1
152
  add eax,ebx
153
  push ebp
154
  add ebp,eax
155
  xor ecx,ecx
156
  mov cx,word[ebp+4]
157
  pop ebp
158
 
159
  xor eax,eax
160
  mov ax,word[esi+2]
161
  shl eax,1
162
  mov ebx,eax
163
  shl eax,1
164
  add eax,ebx
165
  push ebp
166
  add ebp,eax
167
  add cx,word[ebp+4]
168
  pop ebp
169
 
170
  xor eax,eax
171
  mov ax,word[esi+4]
172
  shl eax,1
173
  mov ebx,eax
174
  shl eax,1
175
  add eax,ebx
176
  push ebp
177
  add ebp,eax
178
  add cx,word[ebp+4]
179
  pop ebp
180
 
181
  mov ax,cx
182
  cwd
183
  idiv [i3]
184
  cld
185
  movsd      ; store vertex coordinates
186
  movsw
187
  stosw      ; middle vertex coordinate  'z' in triangles_with_z list
188
  cmp dword[esi],0xffffffff
189
  jne make_triangle_with_z
190
  movsd   ; copy end mark
191
 
192
;macro sort
193
 
194
  mov [sort_flag],1
195
 next_booble:
196
  mov esi,triangles_with_z   ;sort list triangles_with_z booble metod
197
  cmp [sort_flag],0
198
  je end_sort
199
  mov [sort_flag],0
200
 check_and_check:
201
 ; cmp dword[esi],0xffffffff
202
 ; je next_booble
203
  cmp dword[esi+8],0xffffffff
204
  je next_booble
205
  mov ax,word[esi+6]
206
  cmp ax,word[esi+14]
207
  jge no_chg_pos
208
  mov eax,dword[esi]
209
  mov ebx,dword[esi+4]
210
  xchg eax,dword[esi+8]
211
  xchg ebx,dword[esi+12]
212
  mov dword[esi],eax
213
  mov dword[esi+4],ebx               ; sort_flag=1 if change occured
214
  mov [sort_flag],1
215
 no_chg_pos:
216
  add esi,8
217
  jmp check_and_check     ;check end mark end if greater
218
 end_sort:
219
 
220
 ; translate triangles_with_z to sorted_triangles
221
  mov esi,triangles_with_z
222
  mov edi,sorted_triangles
223
 again_copy:
224
  movsd
225
  movsw
226
  add esi,2
227
  cmp dword[esi],0xffffffff
228
  jne again_copy
229
  movsd  ; copy end mark too
230
ret
231
sort_flag db 0
232
clrscr:
233
  mov edi,screen
234
  mov ecx,SIZE_X*SIZE_Y*3/4
235
  xor eax,eax
236
  cld
237
  rep stosd
238
ret
239
calculate_angle:
240
  finit
241
  fldpi
242
  fidiv [i180]
243
  fimul [angle_counter]
244
  fst [current_angle]
245
  fld st
246
  fidiv [i2]
247
  fsincos
248
  fstp [singamma]
249
  fstp [cosgamma]
250
  fsincos
251
  fstp [sinbeta]
252
  fstp [cosbeta]
253
  inc [angle_counter]
254
  cmp [angle_counter],360
255
  jne end_calc_angle
256
  mov [angle_counter],0
257
 end_calc_angle:
258
ret
259
i180 dw 90
260
i2 dw 2
261
rotate_points:
262
  finit                           ; y axle rotate
263
  mov ebx,points_rotated
264
 again_r:
265
  mov ax,word[ebx] ;x
266
  sub ax,[xo]
267
  mov [xsub],ax
268
  mov ax,word[ebx+4] ;z
269
  sub ax,[zo]
270
  mov [zsub],ax
271
  fld [sinbeta]
272
  fimul [zsub]
273
  fchs
274
  fld [cosbeta]
275
  fimul [xsub]
276
  faddp
277
  fiadd [xo]
278
  fistp word[ebx]  ;x
279
  fld [sinbeta]
280
  fimul [xsub]
281
  ;fchs
282
  fld [cosbeta]
283
  fimul [zsub]
284
  faddp
285
  fiadd [zo]
286
  fistp word[ebx+4] ;z
287
 
288
  mov ax,word[ebx+2] ;y                  ; z axle rotate
289
  sub ax,[yo]
290
  mov [ysub],ax
291
  mov ax,word[ebx]  ;x
292
  sub ax,[xo]
293
  mov [xsub],ax
294
  fld [singamma]
295
  fimul[ysub]
296
  fld [cosgamma]
297
  fimul [xsub]
298
  faddp
299
  fiadd [xo]
300
  fistp word[ebx] ;x
301
  fld [cosgamma]
302
  fimul [ysub]
303
  fld [singamma]
304
  fimul [xsub]
305
  fchs
306
  faddp
307
  fiadd [yo]
308
  fistp word[ebx+2] ;y
309
 
310
  add ebx,6
311
  cmp dword[ebx],0xffffffff
312
  jne again_r
313
ret
314
xsub dw ?
315
ysub dw ?
316
zsub dw ?
317
draw_triangles:
318
  mov [tr_counter],1
319
  mov ebp,points_rotated
320
;  mov esi,triangles
321
  mov esi,sorted_triangles
322
 again_dts:
323
  xor eax,eax
324
  mov ax,word[esi]
325
  shl eax,1
326
  mov [dtpom],eax
327
  shl eax,1
328
  add eax,[dtpom]
329
  push ebp
330
  add ebp,eax
331
  mov ax,word[ebp]
332
  mov [xx1],ax
333
  mov ax,word[ebp+2]
334
  mov [yy1],ax
335
  mov ax,word[ebp+4]
336
  mov [zz1],ax
337
  pop ebp
338
 
339
  xor eax,eax
340
  mov ax,word[esi+2]
341
  shl eax,1
342
  mov [dtpom],eax
343
  shl eax,1
344
  add eax,[dtpom]
345
  push ebp
346
  add ebp,eax
347
  mov ax,word[ebp]
348
  mov [xx2],ax
349
  mov ax,word[ebp+2]
350
  mov [yy2],ax
351
  mov ax,word[ebp+4]
352
  mov [zz2],ax
353
  pop ebp
354
 
355
  xor eax,eax
356
  mov ax,word[esi+4]
357
  shl eax,1
358
  mov [dtpom],eax
359
  shl eax,1
360
  add eax,[dtpom]
361
  push ebp
362
  add ebp,eax
363
  mov ax,word[ebp]
364
  mov [xx3],ax
365
  mov ax,word[ebp+2]
366
  mov [yy3],ax
367
  mov ax,word[ebp+4]
368
  mov [zz3],ax
369
  pop ebp
370
  push ebp
371
  push esi
372
 
373
macro set_flag
374
{
375
  mov edx,0x00ffffff
376
  inc [tr_counter]
377
  cmp [tr_counter],triangles_count/2
378
  jl skip_red
379
 set_red:
380
  mov edx,0x00ff0000
381
 skip_red:
382
}
383
 
384
  mov ax,[zz1]
385
  add ax,[zz2]
386
  add ax,[zz3]
387
  cwd
388
  idiv [i3]
389
  sub ax,100 ;77
390
;  shl ax,1
391
  neg al
392
  xor edx,edx
393
  mov dh,al                  ;set color according to z position
394
  mov dl,al
395
;  push dx
396
;  shl edx,8
397
;  pop dx
398
 
399
  cmp [shad_flag],0
400
  je skip_col
401
  set_flag
402
 skip_col:
403
  mov ax,[xx1]
404
  shl eax,16
405
  mov ax,[yy1]
406
  mov bx,[xx2]
407
  shl ebx,16
408
  mov bx,[yy2]
409
  mov cx,[xx3]
410
  shl ecx,16
411
  mov cx,[yy3]
412
  mov edi,screen
413
  call draw_triangle
414
  pop esi
415
  pop ebp
416
 
417
  add esi,6
418
  cmp dword[esi],0xffffffff
419
  jne again_dts
420
ret
421
i3 dw 3
422
tr_counter dw 0
423
dtpom dd ?
424
xx1 dw ?
425
yy1 dw ?
426
zz1 dw ?
427
xx2 dw ?
428
yy2 dw ?
429
zz2 dw ?
430
xx3 dw ?
431
yy3 dw ?
432
zz3 dw ?
433
translate_points:
434
  finit
435
  mov ebx,points_rotated
436
 again_trans:
437
  fild word[ebx+4] ;z1
438
  fmul [sq]
439
  fld st
440
  fiadd word[ebx]  ;x1
441
  fistp word[ebx]
442
  fchs
443
  fiadd word[ebx+2] ;y1
444
  fistp word[ebx+2] ;y1
445
 
446
  add ebx,6
447
  cmp dword[ebx],0xffffffff
448
  jne again_trans
449
ret
450
copy_points:
451
  mov esi,points
452
  mov edi,points_rotated
453
  mov ecx,points_count*3+2
454
  cld
455
  rep movsw
456
ret
457
 
458
draw_triangle:
459
;----------in - eax - x1 shl 16 + y1
460
;------------- -ebx - x2 shl 16 + y2
461
;---------------ecx - x3 shl 16 + y3
462
;---------------edx - color 0x00rrggbb
463
;---------------edi - pointer to screen buffer
464
 @ch3:
465
  cmp ax,bx
466
  jg @ch1
467
 @ch4:    ; sort parameters
468
  cmp bx,cx
469
  jg @ch2
470
  jle @chEnd
471
 @ch1:
472
  xchg eax,ebx
473
  jmp @ch4
474
 @ch2:
475
  xchg ebx,ecx
476
  jmp @ch3
477
 @chEnd:
478
  mov [@y1],ax  ; ....and store to user friendly  variables
479
  mov [@y2],bx
480
  mov [@y3],cx
481
  shr eax,16
482
  shr ebx,16
483
  shr ecx,16
484
  mov [@x1],ax
485
  mov [@x2],bx
486
  mov [@x3],cx
487
  mov [@col],edx
488
 
489
  cmp [@y1],0
490
  jl @end_triangle
491
  cmp [@y2],0
492
  jl @end_triangle
493
  cmp [@y3],0
494
  jl @end_triangle
495
  cmp [@x1],0
496
  jl @end_triangle
497
  cmp [@x2],0
498
  jl @end_triangle
499
  cmp [@x3],0
500
  jl @end_triangle
501
  cmp [@y1],SIZE_Y
502
  jg @end_triangle
503
  cmp [@y2],SIZE_Y
504
  jg @end_triangle
505
  cmp [@y3],SIZE_Y
506
  jg @end_triangle
507
  cmp [@x1],SIZE_X
508
  jg @end_triangle
509
  cmp [@x2],SIZE_X
510
  jg @end_triangle
511
  cmp [@x3],SIZE_X
512
  jg @end_triangle
513
 
514
  neg ax       ; calculate delta 12
515
  add ax,bx
516
  cwde
517
  shl eax,ROUND
518
  cdq
519
  mov bx,[@y2]
520
  mov cx,[@y1]
521
  sub ebx,ecx
522
  cmp ebx,0
523
  jne @noZero1
524
  mov [@dx12],0
525
  jmp @yesZero1
526
 @noZero1:
527
  idiv ebx
528
  mov  [@dx12],eax
529
 @yesZero1:
530
 
531
  mov ax,[@x3]        ; calculate delta 13
532
  sub ax,[@x1]
533
  cwde
534
  shl eax,ROUND
535
  cdq
536
  xor ebx,ebx
537
  xor ecx,ecx
538
  or bx,[@y3]
539
  or cx,[@y1]
540
  sub ebx,ecx
541
  cmp ebx,0
542
  jne @noZero2
543
  mov [@dx13],0
544
  jmp @yesZero2
545
 @noZero2:
546
  idiv ebx
547
  mov [@dx13],eax
548
 @yesZero2:
549
 
550
  mov ax,[@x3]     ; calculate delta 23 [dx23]
551
  sub ax,[@x2]
552
  cwde
553
  shl eax,ROUND
554
  cdq
555
  xor ebx,ebx
556
  xor ecx,ecx
557
  or bx,[@y3]
558
  or cx,[@y2]
559
  sub ebx,ecx
560
  cmp ebx,0
561
  jne @noZero3
562
  mov [@dx23],0
563
  jmp @yesZero3
564
 @noZero3:
565
  idiv ebx
566
  mov [@dx23],eax
567
 @yesZero3:
568
 
569
 
570
  xor eax,eax    ;eax - xk1
571
  or ax,[@x1]
572
  shl eax,ROUND
573
  mov ebx,eax     ; ebx - xk2
574
  xor esi,esi       ; esi - y
575
  or si,[@y1]
576
 @next_line1:
577
  mov ecx,eax     ; ecx - x11
578
  sar ecx,ROUND
579
  mov edx,ebx      ;edx - x12
580
  sar edx,ROUND
581
  cmp ecx,edx
582
  jle @nochg
583
  xchg ecx,edx
584
 @nochg:
585
  pusha
586
  mov ebx,ecx
587
  sub edx,ecx
588
  mov ecx,edx
589
  mov edx,esi
590
  mov eax,[@col]
591
  call @horizontal_line
592
  popa
593
  add eax,[@dx13]
594
  add ebx,[@dx12]
595
  inc esi
596
  cmp si,[@y2]
597
  jl @next_line1
598
 
599
  xor esi,esi
600
  or si,[@y2]
601
  xor ebx,ebx
602
  mov bx,[@x2]
603
  shl ebx,ROUND
604
 @next_line2:
605
  mov ecx,eax
606
  sar ecx,ROUND
607
  mov edx,ebx
608
  sar edx,ROUND
609
  cmp ecx,edx
610
  jle @nochg1
611
  xchg ecx,edx
612
 @nochg1:
613
  pusha
614
  mov eax,[@col]
615
  mov ebx,ecx
616
  sub edx,ecx
617
  mov ecx,edx
618
  mov edx,esi
619
  call @horizontal_line
620
  popa
621
  add eax,[@dx13]
622
  add ebx,[@dx23]
623
  inc esi
624
  cmp si,[@y3]
625
  jl @next_line2
626
  @end_triangle:
627
ret
628
@col dd ?
629
@y1 dw ?
630
@x1 dw ?
631
@y2 dw ?
632
@x2 dw ?
633
@y3 dw ?
634
@x3 dw ?
635
@dx12 dd ?
636
@dx13 dd ?
637
@dx23 dd ?
638
 
639
@horizontal_line:
640
;---------in
641
;---------eax - color of line,  0x00RRGGBB
642
;---------ebx - x1 - x position of line begin
643
;---------ecx - lenght of line
644
;---------edx - y position of line
645
;---------edi - pointer to buffer
646
  jcxz @end_hor_l
647
  push eax
648
  mov eax,SIZE_X*3
649
  mul edx
650
  add edi,eax       ; calculate line begin adress
651
  add edi,ebx
652
  shl ebx,1
653
  add edi,ebx
654
  pop eax
655
  cld
656
 @ddraw:
657
  push eax
658
  stosw
659
  shr eax,16
660
  stosb
661
  pop eax
662
  loop @ddraw
663
  @end_hor_l:
664
ret
665
 
666
 
667
 
668
 
669
;   *********************************************
670
;   *******  WINDOW DEFINITIONS AND DRAW ********
671
;   *********************************************
672
 
673
 
674
draw_window:
675
 
676
 
677
    mov  eax,12      ; function 12:tell os about windowdraw
678
    mov  ebx,1      ; 1, start of draw
679
    int  0x40
680
 
681
       ; DRAW WINDOW
682
    mov  eax,0      ; function 0 : define and draw window
683
    mov  ebx,100*65536+SIZE_X+20    ; [x start] *65536 + [x size]
684
    mov  ecx,100*65536+SIZE_Y+30    ; [y start] *65536 + [y size]
685
    mov  edx,0x04000000     ; color of work area RRGGBB,8->color gl
686
    mov  esi,0x805080d0     ; color of grab bar  RRGGBB,8->color gl
687
    mov  edi,0x005080d0     ; color of frames    RRGGBB
688
    int  0x40
689
 
690
       ; WINDOW LABEL
691
    mov  eax,4      ; function 4 : write text to window
692
    mov  ebx,8*65536+8     ; [x start] *65536 + [y start]
693
    mov  ecx,0x20ddeeff     ; font 1 & color ( 0xF0RRGGBB )
694
    mov  edx,labelt     ; pointer to text beginning
695
    mov  esi,labellen-labelt    ; text length
696
    int  0x40
697
 
698
      ; flag color button
699
    mov  eax,8      ; function 8 : define and draw button
700
    mov  ebx,(SIZE_X-35)*65536+12    ; [x start] *65536 + [x size]
701
    mov  ecx,5*65536+12     ; [y start] *65536 + [y size]
702
    mov  edx,2      ; button id
703
    mov  esi,0x64504A     ; button color RRGGBB
704
    int  0x40
705
 
706
       ; speed button
707
    mov  eax,8      ; function 8 : define and draw button
708
    mov  ebx,(SIZE_X-53)*65536+12    ; [x start] *65536 + [x size]
709
    mov  ecx,5*65536+12     ; [y start] *65536 + [y size]
710
    mov  edx,3      ; button id
711
    mov  esi,0x64504A     ; button color RRGGBB
712
    int  0x40
713
 
714
    mov  eax,12      ; function 12:tell os about windowdraw
715
    mov  ebx,2      ; 2, end of draw
716
    int  0x40
717
 
718
    ret
719
 
720
 
721
; DATA AREA
722
angle_counter dw 0
723
sq dd 0.707
724
xo dw 110 ;87
725
zo dw 0
726
yo dw 125
727
shad_flag db 0
728
speed_flag db 0
729
 
730
triangles:
731
dw 0,1,10, 10,11,1, 1,2,11, 11,12,2, 2,3,12, 12,13,3, 3,4,13, 13,14,4, 4,5,14
732
dw 14,15,5, 5,6,15, 15,16,6, 6,7,16, 16,17,7, 7,8,17, 17,18,8, 8,9,18, 18,19,9
733
dw 10,11,20, 20,21,11, 11,12,21, 21,22,12, 12,13,22, 22,23,13, 13,14,23
734
dw 23,24,14, 14,15,24, 24,25,15, 15,16,25, 25,26,16, 16,17,26, 26,27,17
735
dw 17,18,27, 27,28,18, 18,19,28, 28,29,19, 20,21,30, 30,31,21, 21,22,31
736
dw 31,32,22, 22,23,32, 32,33,23, 23,24,33, 33,34,24, 24,25,34, 34,35,25
737
dw 25,26,35, 35,36,26, 26,27,36, 36,37,27, 27,28,37, 37,38,28, 28,29,38
738
dw 38,39,29
739
 dd 0xffffffff ;<- end marker
740
 
741
 
742
 
743
labelt:
744
     db   '3d wavy rotaring area'
745
labellen:
746
sinbeta rd 1
747
cosbeta rd 1
748
singamma rd 1
749
cosgamma rd 1
750
current_angle rd 1
751
 
752
points rw points_count*3 + 2
753
points_rotated rw points_count*3 + 2
754
triangles_with_z rw triangles_count*4 + 2 ; triangles triple dw + z position
755
sorted_triangles rw triangles_count*3 + 2
756
screen rb SIZE_X * SIZE_Y * 3   ; screen buffer
757
memStack rb 1000 ;memory area for stack
758
I_END:
759