Subversion Repositories Kolibri OS

Rev

Rev 2491 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 halyavin 1
;
2
; 3D POLYGONAL CUBE - ASCL
3
;
4
; Pavlushin Evgeni
5
; mail: waptap@mail.ru       site: www.deck4.narod.ru
6
;
7
; Create on base 3D test sample
8
; Mikolaj Felix mfelix@polbox.com
9
;
10
 
11
use32
12
               org     0x0
13
               db     'MENUET01'              ; 8 byte id
14
               dd     0x01                    ; header version
15
               dd     START                   ; start of code
16
               dd     I_END                   ; size of image
17
               dd     0x30000                  ; memory for app
18
               dd     0x30000                  ; esp
19
               dd     0x0 , 0x0               ; I_Param , I_Icon
20
 
21
MAX_POINTS equ 8
22
MAX_TRIANGLES equ 12
23
SCREEN_X equ 320
24
SCREEN_Y equ 200
25
 
26
include 'lang.inc'
485 heavyiron 27
include '..\..\..\macros.inc'
625 diamond 28
;purge mov
31 halyavin 29
include 'ascl.inc'
30
include 'ascgl.inc'
485 heavyiron 31
 
31 halyavin 32
START:
625 diamond 33
init_sin_cos:
34
	finit
35
	fldz
36
	xor edi,edi
37
	mov ecx,512
38
isc_make:
39
	fld st0
40
	fsincos
41
	fmul [fixed_point_const]
42
	fistp word [cos_table+edi]
43
	fmul [fixed_point_const]
44
	fistp word [sin_table+edi]
45
 
46
	fadd [inc_angle]
47
 
48
	add edi,2
49
	loop isc_make
50
	fstp st0
51
red:
31 halyavin 52
    call draw_window
53
 
54
still:
55
    mov eax,11
485 heavyiron 56
    mcall
31 halyavin 57
 
58
    dec eax
59
;    cmp  eax,1                  ; window redraw request ?
60
    jz   red
61
    dec eax
62
;    cmp  eax,2                  ; key in buffer ?
63
    jz   key
64
    dec eax
65
;    cmp  eax,3                  ; button in buffer ?
66
    jz   button
67
 
2491 leency 68
    fps  220,8,cl_White,cl_Black
31 halyavin 69
 
70
main_loop:
71
 
72
	mov esi,object
73
	mov edi,object_rotated
74
	mov ecx,MAX_POINTS*3
75
	cld
76
	rep movsw
77
 
78
	mov esi,angle_x
79
	mov edi,object_rotated
80
	mov ecx,MAX_POINTS
81
	call rotate_points
82
 
83
	mov esi,object_rotated
84
	mov edi,object_translated
85
	mov ecx,MAX_POINTS
86
	call translate_points
87
 
88
	call draw_faces
89
 
90
	call clear_screen_buffer
91
 
92
	add [angle_x],2
93
	add [angle_y],3
94
	add [angle_z],1
95
 
96
     jmp still
97
 
98
key:
99
     mov eax,2
485 heavyiron 100
     mcall
31 halyavin 101
     jmp still
102
button:
103
     mov eax,17
485 heavyiron 104
     mcall
31 halyavin 105
     cmp ah,1
106
     jne still
107
exit:
108
     mov eax,-1
485 heavyiron 109
     mcall
31 halyavin 110
 
111
;Draw window
112
draw_window:
2205 leency 113
    mcall 12, 1 ;Start window redraw
31 halyavin 114
 
2491 leency 115
	mcall 48, 4
116
 
117
	lea	ecx, [100*65536+SCREEN_Y+4+eax]; [y start] *65536 + [y size] + [skin_height]
118
	xor	eax, eax   ;Draw window
2205 leency 119
    mov ebx,100*65536+(SCREEN_X+9)  ;x start*65536+x size
2016 leency 120
    mov edx,0x54000000         ;0x03 use skinned window
485 heavyiron 121
    mov edi,title
122
    mcall
2491 leency 123
 
2205 leency 124
    mcall 12, 2 ;End window redraw
125
 
31 halyavin 126
    ret
127
 
2205 leency 128
title db '3D Cube Sample',0
31 halyavin 129
 
130
 
131
; Draw faces procedure
132
 
133
draw_faces:
134
 
135
	mov esi,link
136
	mov ecx,MAX_TRIANGLES
137
df_draw:
138
	push ecx
139
	mov ecx,3
140
	mov edi,@@tx1 ;bp
141
df_get_point:
625 diamond 142
	movzx	ebx, byte [esi]
143
	movzx	eax, word [object_translated + ebx*4]
144
	stosd
145
	movzx	eax, word [object_translated + ebx*4 + 2]
146
	stosd
31 halyavin 147
	inc esi
148
	dec ecx
149
	jnz df_get_point
150
 
625 diamond 151
	mov eax,[@@ty1]
152
	sub eax,[@@ty3]
153
	mov ebx,[@@tx2]
154
	sub ebx,[@@tx1]
155
	imul ebx
156
	push eax
157
	mov eax,[@@tx1]
158
	sub eax,[@@tx3]
159
	mov ebx,[@@ty2]
160
	sub ebx,[@@ty1]
161
	imul ebx
31 halyavin 162
	pop ebx
625 diamond 163
	sub ebx,eax
31 halyavin 164
	jge df_next
165
 
625 diamond 166
	movzx	eax, byte [esi]
31 halyavin 167
 
625 diamond 168
	mov  	[@@xcol], eax
31 halyavin 169
 
170
	call filled_triangle
171
df_next:
625 diamond 172
	inc esi
31 halyavin 173
	pop ecx
174
	dec ecx
175
	jnz df_draw
176
	ret
177
 
178
;modify
179
;include graphlib.asm
180
 
181
clear_screen_buffer:
182
 
183
;outscrbuf
2491 leency 184
 mcall 48, 4
31 halyavin 185
 mov ebx,scrbuf
186
 mov ecx,SCREEN_X*65536+SCREEN_Y
2491 leency 187
 lea edx,[5*65536+eax]
625 diamond 188
 mov eax,7
485 heavyiron 189
 mcall
31 halyavin 190
 
191
;White background
192
 mov edi,scrbuf
193
 mov ecx,(SCREEN_X*SCREEN_Y*3)/4
2565 mario79 194
 mov eax,0xffffffff
31 halyavin 195
 cld
196
 rep stosd
197
 
198
	ret
199
 
200
;include triangle.asm
201
; Mikolaj Felix 14/5/2001
202
; mfelix@polbox.com
203
 
204
;filled trangle procedure
205
 
625 diamond 206
align 4
207
@@tx1  dd 0
208
@@ty1  dd 0
209
@@tx2  dd 0
210
@@ty2  dd 0
211
@@tx3  dd 0
212
@@ty3  dd 0
213
@@xcol dd 0
31 halyavin 214
 
625 diamond 215
@@dx12 dd 0
216
@@dx13 dd 0
217
@@dx23 dd 0
31 halyavin 218
 
219
filled_triangle:
220
 
625 diamond 221
 mov eax,[@@xcol]  ;trnsforming color
31 halyavin 222
 mov bl,al        ;byte bbbggrrx
223
 mov dl,al        ;to 3 byte
224
 mov dh,al        ;bbbxxxxx ggxxxxxx rrxxxxxx
225
 and dh,00000001b
226
 
227
 and al,11100000b
228
 and bl,00011000b
229
 and dl,00000110b
230
 shl bl,3
231
 shl dl,5
232
 
233
 cmp dh,1
234
 jne no_bitup
235
 or  al,00011111b
236
 or  bl,00111111b
237
 or  dl,00111111b
238
no_bitup:
239
 
240
 shl eax,8 ;puck colors
241
 mov al,bl
242
 shl eax,8
243
 mov al,dl
244
 mov dword [@@rgb],eax
245
 
625 diamond 246
	mov eax,[@@ty1]
247
	cmp eax,[@@ty3]
31 halyavin 248
	jb ft_check1
249
 
625 diamond 250
	xchg eax,[@@ty3]
251
	mov [@@ty1],eax
31 halyavin 252
 
625 diamond 253
	mov eax,[@@tx1]
254
	xchg eax,[@@tx3]
255
	mov [@@tx1],eax
31 halyavin 256
ft_check1:
625 diamond 257
	mov eax,[@@ty2]
258
	cmp eax,[@@ty3]
31 halyavin 259
	jb ft_check2
260
 
625 diamond 261
	xchg eax,[@@ty3]
262
	mov [@@ty2],eax
31 halyavin 263
 
625 diamond 264
	mov eax,[@@tx2]
265
	xchg eax,[@@tx3]
266
	mov [@@tx2],eax
31 halyavin 267
ft_check2:
625 diamond 268
	mov eax,[@@ty1]
269
	cmp eax,[@@ty2]
31 halyavin 270
	jb ft_check3
271
 
625 diamond 272
	xchg eax,[@@ty2]
273
	mov [@@ty1],eax
31 halyavin 274
 
625 diamond 275
	mov eax,[@@tx1]
276
	xchg eax,[@@tx2]
277
	mov [@@tx1],eax
31 halyavin 278
ft_check3:
279
 
625 diamond 280
	mov ebx,[@@ty2]
281
	sub ebx,[@@ty1]
31 halyavin 282
	jnz ft_dx12_make
283
 
625 diamond 284
	mov [@@dx12],dword 0
31 halyavin 285
	jmp ft_dx12_done
286
ft_dx12_make:
625 diamond 287
	mov eax,[@@tx2]
288
	sub eax,[@@tx1]
289
	shl eax,7
290
	cdq
291
	idiv ebx
292
	mov [@@dx12],eax			; dx12 = (x2-x1)/(y2-y1)
31 halyavin 293
ft_dx12_done:
294
 
625 diamond 295
	mov ebx,[@@ty3]
296
	sub ebx,[@@ty1]
31 halyavin 297
	jnz ft_dx13_make
298
 
625 diamond 299
	mov [@@dx13],dword 0
31 halyavin 300
	jmp ft_dx13_done
301
ft_dx13_make:
625 diamond 302
	mov eax,[@@tx3]
303
	sub eax,[@@tx1]
304
	shl eax,7
305
	cdq
306
	idiv ebx
307
	mov [@@dx13],eax			; dx13 = (x3-x1)/(y3-y1)
31 halyavin 308
ft_dx13_done:
309
 
625 diamond 310
	mov ebx,[@@ty3]
311
	sub ebx,[@@ty2]
31 halyavin 312
	jnz ft_dx23_make
313
 
625 diamond 314
	mov [@@dx23],dword 0
31 halyavin 315
	jmp ft_dx23_done
316
ft_dx23_make:
625 diamond 317
	mov eax,[@@tx3]
318
	sub eax,[@@tx2]
319
	shl eax,7
320
	cdq
321
	idiv ebx
322
	mov [@@dx23],eax			; dx23 = (x3-x2)/(y3-y2)
31 halyavin 323
ft_dx23_done:
324
 
625 diamond 325
	mov eax,[@@tx1]
326
	shl eax,7
327
	mov ebx,eax
31 halyavin 328
 
625 diamond 329
	mov ecx,[@@ty1]
31 halyavin 330
ft_loop1:
331
 
332
 pushad
333
 
625 diamond 334
	mov [@@ly],ecx
335
	mov edx,ebx
336
	shr edx,7
337
	mov [@@lx2],edx
338
	mov edx,eax
339
	shr edx,7
340
	mov [@@lx1],edx
341
	mov eax,[@@xcol]
342
 mov [@@lcol],eax
31 halyavin 343
 call horizontal_line
344
 
345
 popad
346
 
625 diamond 347
	add eax,[@@dx13]
348
	add ebx,[@@dx12]
349
	inc ecx
350
	cmp ecx,[@@ty2]
31 halyavin 351
	jb ft_loop1
352
 
353
 
625 diamond 354
	mov ebx,[@@tx2]
355
	shl ebx,7
356
	mov ecx,[@@ty2]
31 halyavin 357
ft_loop2:
358
 
359
 pushad
360
 
625 diamond 361
	mov [@@ly],ecx
362
	mov edx,ebx
363
	shr edx,7
364
	mov [@@lx2],edx
365
	mov edx,eax
366
	shr edx,7
367
	mov [@@lx1],edx
368
 mov eax,[@@xcol]
369
 mov [@@lcol],eax
31 halyavin 370
	call horizontal_line
371
 
372
 popad
373
 
625 diamond 374
	add eax,[@@dx13]
375
	add ebx,[@@dx23]
31 halyavin 376
	inc ecx
625 diamond 377
	cmp ecx,[@@ty3]
31 halyavin 378
	jb ft_loop2
379
 
380
	ret
381
 
382
;horizontal line subproc
383
 
625 diamond 384
align 4
385
@@lx1  dd 0
386
@@lx2  dd 0
387
@@ly   dd 0
388
@@lcol dd 0
31 halyavin 389
 
390
@@rgb  dd 0
391
 
392
horizontal_line:
393
 
625 diamond 394
 mov ecx,[@@lx1]
395
 sub ecx,[@@lx2]
31 halyavin 396
 ja  x12
397
 je  ext
398
; ret
625 diamond 399
 neg ecx
31 halyavin 400
 mov edi,3
401
 jmp xx
402
x12:
403
 mov edi,-3
404
 jmp xx
405
ext:
406
 mov ecx,-1 ;1
407
; sub ebp,3
408
xx:
625 diamond 409
 mov eax,[@@ly]
31 halyavin 410
 mov ebx,SCREEN_X ;320
411
 mul ebx
625 diamond 412
 add eax,[@@lx1]
413
 lea ebp,[eax*3-3]	; for delete white dots
31 halyavin 414
 add ecx,2
415
loo:
416
 
417
 mov eax,dword [@@rgb]
418
 mov bl,al
419
 shr eax,8 ;puck colors
420
 
421
 mov byte [scrbuf+ebp],ah
422
 mov byte [scrbuf+ebp+1],al
423
 mov byte [scrbuf+ebp+2],bl
424
 add ebp,edi
425
 dec ecx
426
 jnz loo
427
 
428
	ret
429
 
430
;include fixed3d.asm
431
; Mikolaj Felix 25/5/2001
432
; mfelix@polbox.com
433
 
434
;------------------------------------------------------------
435
;	ds:si	- offset to angles
436
;	ds:di	- offset to 3d points
437
;	cx	- number of points
438
;------------------------------------------------------------
439
 
440
@@sin_x dw 0
441
@@cos_x dw 0
442
@@sin_y dw 0
443
@@cos_y dw 0
444
@@sin_z dw 0
445
@@cos_z dw 0
446
 
447
@@px equ word [edi]
448
@@py equ word [edi+2]
449
@@pz equ word [edi+4]
450
 
451
rotate_points:
452
 
453
 push edi
454
	mov edi,@@sin_x
455
	mov edx,3
456
rp_sin_cos:
625 diamond 457
	mov ebx, [esi]
458
	and ebx,511
459
	mov ax,word [sin_table+ebx*2]
31 halyavin 460
	mov word [edi],ax
625 diamond 461
	mov ax,word [cos_table+ebx*2]
31 halyavin 462
	mov word [edi+2],ax
463
 
464
	add esi,2
465
	add edi,4
466
	dec edx
467
	jnz rp_sin_cos
468
	pop edi
469
 
470
rp_rotate:
471
 
472
	; rotate around x-axis
473
 
474
	mov ax,@@py
475
	imul [@@cos_x]
476
	mov bx,ax
477
	mov si,dx
478
 
479
	mov ax,@@pz
480
	imul [@@sin_x]
481
	sub bx,ax
482
	sbb si,dx
483
	shrd bx,si,14
484
	push bx
485
 
486
	mov ax,@@py
487
	imul [@@sin_x]
488
	mov bx,ax
489
	mov si,dx
490
 
491
	mov ax,@@pz
492
	imul [@@cos_x]
493
	add bx,ax
494
	adc si,dx
495
	shrd bx,si,14
496
 
497
	pop @@py
498
	mov @@pz,bx
499
 
500
	; rotate around y-axis
501
 
502
	mov ax,@@px
503
	imul [@@cos_y]
504
	mov bx,ax
505
	mov si,dx
506
 
507
	mov ax,@@pz
508
	imul [@@sin_y]
509
	sub bx,ax
510
	sbb si,dx
511
	shrd bx,si,14
512
	push bx
513
 
514
	mov ax,@@px
515
	imul [@@sin_y]
516
	mov bx,ax
517
	mov si,dx
518
 
519
	mov ax,@@pz
520
	imul [@@cos_y]
521
	add bx,ax
522
	adc si,dx
523
	shrd bx,si,14
524
 
525
	pop @@px
526
	mov @@pz,bx
527
 
528
	; rotate around z-axis
529
 
530
	mov ax,@@px
531
	imul [@@cos_z]
532
	mov bx,ax
533
	mov si,dx
534
 
535
	mov ax,@@py
536
	imul [@@sin_z]
537
	sub bx,ax
538
	sbb si,dx
539
	shrd bx,si,14
540
	push bx
541
 
542
	mov ax,@@px
543
	imul [@@sin_z]
544
	mov bx,ax
545
	mov si,dx
546
 
547
	mov ax,@@py
548
	imul [@@cos_z]
549
	add bx,ax
550
	adc si,dx
551
	shrd bx,si,14
552
 
553
	pop @@px
554
	mov @@py,bx
555
 
556
	add edi,6
557
	dec ecx
558
	jnz rp_rotate
559
 
560
	ret
561
 
562
;------------------------------------------------------------
563
;	ds:si	- offset to 3d points
564
;	es:di	- offset to 2d points
565
;	cx	- number of points
566
;------------------------------------------------------------
567
 
568
 mx dw 0
569
 my dw 0
570
 
571
translate_points:
572
 pushad
573
 mov eax,37
574
 mov ebx,1
485 heavyiron 575
 mcall
31 halyavin 576
 mov ebx,eax
577
 shr eax,16
578
 and ebx,0xffff
579
 cmp ax,SCREEN_X
580
 jna x_n
581
 mov ax,0 ;SCREEN_X
582
x_n:
583
 cmp bx,SCREEN_Y
584
 jna y_n
585
 mov bx,0 ;SCREEN_Y
586
y_n:
587
 mov [mx],ax
588
 mov [my],bx
589
 popad
590
 
625 diamond 591
	movzx ebx,word [esi+4]
31 halyavin 592
 mov ax,[my]
593
 cmp ax,0
594
 jng no_m
595
 shl ax,3
596
 add bx,ax
597
no_m:
598
 add bx,256  ; Z factor (zoom)
599
 
625 diamond 600
 	movsx eax,word [esi]
601
	shl eax,8
602
	cdq
603
	idiv ebx
604
	add eax,(SCREEN_X/2) ;160         ;X factor (center X)
31 halyavin 605
	stosw
606
 
625 diamond 607
	movsx eax,word [esi+2]
608
	shl eax,8
609
	cdq
610
	idiv ebx
611
	add eax,(SCREEN_Y/2) ;100         ;Y factor (center Y)
31 halyavin 612
	stosw
613
 
614
	add esi,6
615
	dec ecx
616
	jnz translate_points
617
	ret
618
 
619
fixed_point_const dd 16384.0
620
inc_angle dd 0.01227184630309			; pi/256
621
 
622
angle_x dw 0
623
angle_y dw 0
624
angle_z dw 0
625
 
626
object	dw -50,-50,-50, 50,-50,-50, 50,50,-50, -50,50,-50
627
      	dw -50,-50, 50, 50,-50, 50, 50,50, 50, -50,50, 50
628
 
629
link:
630
 db  0,1,2,10000011b, 0,2,3,10000011b ;purpure   side
631
 db  5,4,7,00000111b, 5,7,6,00000111b ;soft-red  side
632
 db  1,5,6,00011000b, 1,6,2,00011000b ;soft-lime side
633
 db  4,0,3,11100001b, 4,3,7,11100001b ;soft-blue side
634
 db  4,5,1,00011111b, 1,0,4,00011111b ;yellow    side
635
 db  3,2,6,00000000b, 3,6,7,00000000b ;black     side
636
 
637
sin_table:
638
rw 512
639
cos_table:
640
rw 512
641
 
642
object_rotated:
643
rw MAX_POINTS*3
644
object_translated:
645
rw MAX_POINTS*2
646
 
647
scrbuf:
648
I_END: