diff --git a/data/Tupfile.lua b/data/Tupfile.lua index 5484d6c73b..b5d17a3047 100644 --- a/data/Tupfile.lua +++ b/data/Tupfile.lua @@ -340,6 +340,7 @@ tup.append_table(img_files, { {"3D/3DWAV", PROGS .. "/demos/3dwav/trunk/3dwav"}, {"3D/CROWNSCR", PROGS .. "/demos/crownscr/trunk/crownscr"}, {"3D/FREE3D04", PROGS .. "/demos/free3d04/trunk/free3d04"}, + {"3D/RAY", PROGS .. "/demos/ray/ray"}, {"3D/VIEW3DS", PROGS .. "/demos/3DS/VIEW3DS"}, {"DEMOS/BCDCLK", PROGS .. "/demos/bcdclk/trunk/bcdclk"}, {"DEMOS/CIRCLE", PROGS .. "/develop/examples/circle/trunk/circle"}, diff --git a/programs/demos/ray/DATARAY.INC b/programs/demos/ray/DATARAY.INC new file mode 100644 index 0000000000..c40c9ff5bf --- /dev/null +++ b/programs/demos/ray/DATARAY.INC @@ -0,0 +1,80 @@ +non_rot_light: +;light: +dd 0.9, 0.9, 1.0 +;light2: +dd 0.1, 0.05, 0.5 +;light3: +dd 0.9, 0.1, 0.3 + + +;sph_max dd 2 +non_rot_sphere: + dd 0.7 + dd 0.3 + dd 0.4 ;, 0.0 + + dd 0.2 + dd 0.7 + dd 0.3 ;, 0.0 + + dd 0.1 + dd 0.1 + dd 0.1 ;, 0.0 + + dd 0.7 + dd 0.7 + dd 0.1 ;, 0.0 + + dd 0.5 + dd 0.2 + dd 0.1 ;, 0.0 + + +sph_radius: +R dd 0.25 +R2 dd 0.22 +r3 dd 0.1 +r4 dd 0.25 +r5 dd 0.1 + +align 16 +camera: +x0 dd 0.5 +y0 dd 0.5 +z0 dd - 12.0 , 0.0 + +;x1 dd +;y1 dd +;z1 dd +float2: +dd 2.0, 2.0, 2.0, 2.0 +float4: +dd 4.0, 4.0, 4.0, 4.0 +float255: +dd 255.0, 255.0, 255.0, 255.0 +;correct_tex: +;dd 64.0, 64.0, 64.0, 64.0 +;light: +;dd 0.9, 0.9, 1.0, 0.0 +;light2: +;dd 0.1, 0.05, 0.5, 0.0 +;light3: +;dd 0.9, 0.1, 0.3, 0.0 +lights_color: +dd 250.0, 255.0, 255.0, 0.0 +dd 232.0, 254.0, 220.0, 0.0 +dd 225.0,37.0, 247.0, 0.0 + +light_factor dd 0.33 +tex_x dd 128 +align 16 +nearest_sphere: +rd 4 +nearest_radius dd ? +smalest_t dd ? +align 16 +light: + rd 4 * MAX_LIGHTS +sphere: + rd 4 * MAX_SPHERES +;bitmap file '128x128.raw' \ No newline at end of file diff --git a/programs/demos/ray/RAY.ASM b/programs/demos/ray/RAY.ASM new file mode 100644 index 0000000000..c13bcd1a06 --- /dev/null +++ b/programs/demos/ray/RAY.ASM @@ -0,0 +1,216 @@ +; +; application : Tiny ray tracer +; compilator : Fasm +; system : MenuetOS64/KolibriOS +; author : Maciej Guba aka macgub +; email : macgub3@wp.pl +; web : http://macgub.hekko.pl + +include "../../macros.inc" + +timeout equ 3 +XRES equ 500 ; window size +YRES equ 500 +maxx = XRES +maxy = YRES +use32 + + org 0x0 + + db 'MENUET01' ; 8 byte id + dd 0x01 ; header version + dd START ; start of code + dd I_END ; size of image + dd I_END ; memory for app + dd I_END ; esp + dd 0x0 , 0x0 ; I_Param , I_Icon + +START: ; start of execution + + call draw_window + +still: + + mov eax,23 ; wait here for event + mov ebx,timeout + int 0x40 +; mov eax,11 ; check for event no wait +; int 0x40 + + cmp eax,1 ; redraw request ? + je red + cmp eax,2 ; key in buffer ? + je key + cmp eax,3 ; button in buffer ? + je button + + jmp noclose + + red: ; redraw + call draw_window + jmp still + + key: ; key + mov eax,2 ; just read it and ignore + int 0x40 + jmp still + + button: ; button + mov eax,17 ; get id + int 0x40 + + cmp ah,1 ; button id=1 ? + jne noclose + + mov eax,-1 ; close this program + int 0x40 + noclose: + +; mov eax,13 +; mov ebx,20*65536+maxx-25 +; mov ecx,20*65536+maxy-25 +; xor edx,edx +; int 0x40 + + mov edi,screen + mov ecx,maxx*maxy*3/4 + xor eax,eax + cld + rep stosd + + add [deg_counter],1 + cmp [deg_counter],360 + jne @f + mov [deg_counter],0 +@@: + fninit + fld [one_deg] + fimul [deg_counter] + fsincos + fstp [sin] + fstp [cos] + + mov ecx,MAX_SPHERES ;MAX_LIGHTS + mov esi,non_rot_sphere ;light + mov edi,sphere + +.rotary: + + + fld dword[esi] + fsub [xo] + fmul [cos] + fld dword[esi+8] + fsub [zo] + fmul [sin] + fchs + faddp + fadd [xo] ; top of stack - new 'x' + fstp dword[edi] + + fld dword[esi+8] + fsub [zo] + fmul [cos] + fld dword[esi] + fsub dword[xo] + fmul [sin] + faddp + fadd [zo] + fstp dword[edi+8] +; fld dword[esi+4] +; fstp dword[edi+4] + push dword[esi+4] + pop dword[edi+4] + mov dword[edi+12],0.0 + + add esi,12 + add edi,16 + sub ecx,1 + jnz .rotary + + mov ecx,MAX_LIGHTS + mov esi,non_rot_light + mov edi,light + +.rotary_lights: + + + fld dword[esi] + fsub [xo] + fmul [cos] + fld dword[esi+4] + fsub [yo] + fmul [sin] + fchs + faddp + fadd [xo] ; top of stack - new 'x' + fstp dword[edi] + + fld dword[esi] + fsub [xo] + fmul [sin] + fchs + fld dword[esi+4] + fsub dword[yo] + fmul [cos] + faddp + fadd [yo] + fstp dword[edi+4] +; fld dword[esi+8] +; fstp dword[edi+8] + push dword[esi+8] + pop dword[edi+8] + mov dword[edi+12],0.0 + + add esi,12 + add edi,16 + sub ecx,1 + jnz .rotary_lights + + + + call main_loop + + mcall 7,screen,,<0,0> + + + +jmp still + + + +include 'RAY.INC' + +; ********************************************* +; ******* WINDOW DEFINITIONS AND DRAW ******** +; ********************************************* +draw_window: + + mcall 12,1 + + mcall 48,4 ; get skin height + lea ecx,[eax + (100 shl 16) + maxy+4] + mov edi,title + mcall 0,<100,maxx+9>,,0x74000000 + + mcall 12,2 + + ret + +title db 'Ray tracing',0 +xo dd 0.5 +yo dd 0.5 +zo dd 0.1 +deg_counter dw 0 +one_deg dd 0.017453 +include 'dataray.inc' +sin dd ? +cos dd ? +screen rb XRES * YRES * 3 +mem_stack: + rb 65536 +I_END: + + + + diff --git a/programs/demos/ray/RAY.INC b/programs/demos/ray/RAY.INC new file mode 100644 index 0000000000..da93741a9e --- /dev/null +++ b/programs/demos/ray/RAY.INC @@ -0,0 +1,288 @@ +MAX_SPHERES = 5 +MAX_LIGHTS = 3 +;ray + +main_loop: + xor eax,eax ; y +.next_line: + xor ebx,ebx ; x + +@@: + push eax + push ebx + call find_intersection + pop ebx + pop eax + add ebx,1 + cmp ebx,XRES + jnz @b + + add eax,1 + cmp eax,YRES + jnz .next_line + +ret +init_calc: ; do some intial calculations + +ret + +find_intersection: ;with for now single sphere +; eax - y +; ebx - x + push ebp + mov ebp,esp + sub esp,128 + and ebp,0xfffffff0 + .dz equ dword[ebp-8] + .dy equ dword[ebp-12] + .dx equ [ebp-16] + .a equ dword[ebp-20] + .b equ dword[ebp-24] + .c equ dword[ebp-28] + .delta equ dword[ebp-32] + .iy equ dword[ebp-36] + .ix equ [ebp-40] + .t1 equ [ebp-44] + .t2 equ [ebp-48] + .n equ [ebp-64] + .sph_xyz equ [ebp-80] + .one_write equ byte [ebp-81] ;tells if sth written in 'nearest' data + .sph_counter equ dword [ebp-85] + + + mov .iy,eax + mov .ix,ebx + + mov .one_write,0 + xorps xmm0,xmm0 + cvtpi2ps xmm0,.ix + mov ecx,XRES + cvtsi2ss xmm2,ecx + shufps xmm2,xmm2,0 + divps xmm0,xmm2 + subps xmm0,[camera] + movaps .dx,xmm0 + movaps xmm1,xmm0 + mulps xmm1,xmm0 + haddps xmm1,xmm1 + haddps xmm1,xmm1 + movss .a,xmm1 + mov .sph_counter,0 + .next_sph: + xorps xmm5,xmm5 + movaps xmm5,[camera] + mov edx,.sph_counter + shl edx,4 + add edx,sphere + subps xmm5,[edx] ;[sphere] ;;[edx] + mulps xmm5,[float2] + movaps xmm0,.dx + mulps xmm5,xmm0 + haddps xmm5,xmm5 + haddps xmm5,xmm5 + movss .b,xmm5 + + + movaps xmm4,[edx] ;[sphere] ; [edx] + mulps xmm4,xmm4 + ; haddps xmm4,xmm4 + ; haddps xmm4,xmm4 + movaps xmm5,[camera] + mulps xmm5,xmm5 + addps xmm4,xmm5 + haddps xmm4,xmm4 + haddps xmm4,xmm4 + ; addss xmm4,xmm5 + movaps xmm5,[edx] ;;[sphere] ;; [edx] + mulps xmm5,[camera] + haddps xmm5,xmm5 + haddps xmm5,xmm5 + mulss xmm5,[float2] + subss xmm4,xmm5 + mov ebx,.sph_counter + shl ebx,2 + add ebx,sph_radius + movss xmm5,[ebx] ;[R] ; [ebx] + mulss xmm5,xmm5 + subss xmm4,xmm5 + movss .c,xmm4 + + movss xmm5,.b + mulss xmm5,xmm5 + mulss xmm4,.a + mulss xmm4,[float4] + subss xmm5,xmm4 + movss .delta,xmm5 + xorps xmm6,xmm6 + cmpnltss xmm5,xmm6 + movd ecx,xmm5 ; ecx = -1 greater than 0.0 + cmp ecx,0 + jnz @f + jmp .next_s ; no intersection + ; add .sph_counter,1 + ; cmp .sph_counter,MAX_SPHERES + ; jnz .next_sph + ; jmp .put_pixel + + + @@: + movss xmm5,.delta + sqrtss xmm5,xmm5 + movss xmm4,xmm5 + subss xmm6,.b + movss xmm7,xmm6 + subss xmm6,xmm5 + divss xmm6,[float2] + divss xmm6,.a + movss .t1,xmm6 + addss xmm4,xmm7 + divss xmm4,[float2] + divss xmm4,.a + movss .t2,xmm4 + movss xmm5,xmm4 + + cmpnltss xmm4,.t1 + movd ecx,xmm4 + or ecx,ecx + jne @f + movss xmm5,.t2 + @@: + cmp .one_write,0 ; test if sth in 'nearest' data is written + jz @f + movss xmm4,xmm5 + cmpnltss xmm4,[smalest_t] + movd ecx,xmm4 + or ecx,ecx + jz .next_s + @@: + movss [smalest_t],xmm5 +; push .a +; pop [smalest_a] +; push .b +; pop [smalest_b] +; push .c +; pop [smalest_c] +; push .delta +; pop [smalest_delta] + movaps xmm0,[edx] + movaps [nearest_sphere],xmm0 + push dword[ebx] + pop dword[nearest_radius] + mov .one_write,1 + + .next_s: + add .sph_counter,1 + cmp .sph_counter,MAX_SPHERES + jnz .next_sph + + .put_pixel: + cmp .one_write,0 + je .end + + movss xmm5,[smalest_t] + shufps xmm5,xmm5,0 ; calc and put pixel + movaps xmm6,.dx + mulps xmm6,xmm5 + movaps xmm4,[camera] + addps xmm4,xmm6 ; xmm4 - x,y,z on the sphere + movaps xmm7,xmm4 + subps xmm4,[nearest_sphere] + ; movaps xmm6,xmm7 + ; addps xmm6,[nearest_sphere] + ; movaps .sph_xyz,xmm6 + + movss xmm0,[nearest_radius] + shufps xmm0,xmm0,0 + divps xmm4,xmm0 ; xmm4 - normal to surface vector + movaps xmm1,xmm4 ; copy of normal in xmm1 + xor eax,eax + xorps xmm3,xmm3 + movss xmm2,[light_factor] + shufps xmm2,xmm2,0 + + .next_light: + ; push eax + mov ebx,eax + shl ebx,4 + ;add eax,light + movaps xmm5,[light+ebx];[light] ;; [eax] + subps xmm5,xmm7 ; calc light unit vector + movaps xmm6,xmm5 + mulps xmm5,xmm5 + haddps xmm5,xmm5 + haddps xmm5,xmm5 + sqrtss xmm5,xmm5 + divps xmm6,xmm5 ; xmm6 - normalized light vector + ; dot_product + movaps xmm4,xmm1 ; xmm4 - normal to surface + mulps xmm4,xmm6 + haddps xmm4,xmm4 + haddps xmm4,xmm4 + shufps xmm4,xmm4,0 +; movaps xmm5,xmm4 +; mulps xmm5,xmm5 +; mulps xmm5,xmm5 +; mulps xmm5,[color] + mulps xmm4,[lights_color+ebx] ; xmm4 - computed col. light vector dep. +; addps xmm4,xmm5 + mulps xmm4,xmm2 + addps xmm3,xmm4 + ; pop eax + add eax,1 + cmp eax,MAX_LIGHTS + jnz .next_light +if 0 + ; mix with texture + movaps xmm0,.sph_xyz + movss xmm1,[nearest_radius] + shufps xmm1,xmm1,0 + divps xmm0,xmm1 + mulps xmm0,[correct_tex] ; f64 + ; addps xmm0,[correct_tex] + cvtss2si eax,xmm0 + psrldq xmm0,4 + cvtss2si ebx,xmm0 + imul ebx,[tex_x] + add ebx,eax + lea ebx,[ebx*3] + add ebx,bitmap + ;mov eax,[ebx] + movd xmm1,[ebx] + xorps xmm7,xmm7 + punpcklbw xmm1,xmm7 + punpcklwd xmm1,xmm7 + cvtdq2ps xmm1,xmm1 + mulps xmm3,xmm1 + divps xmm3,[float255] + ; divps xmm1,[float2] + ; divps xmm3,[float2] + ; addps xmm3,xmm1 +end if + + + minps xmm3,[float255] + cvtps2dq xmm3,xmm3 + packssdw xmm3,xmm3 + packuswb xmm3,xmm3 + + + + + mov edi,screen + mov ecx,XRES + imul ecx,.iy + add ecx,.ix + lea ecx,[ecx*3] + add edi,ecx + movd [edi],xmm3 + +.end: + add esp,128 + pop ebp + +ret + + + + + diff --git a/programs/demos/ray/Tupfile.lua b/programs/demos/ray/Tupfile.lua new file mode 100644 index 0000000000..b37b7abb54 --- /dev/null +++ b/programs/demos/ray/Tupfile.lua @@ -0,0 +1,3 @@ +if tup.getconfig("NO_FASM") ~= "" then return end +tup.rule("echo lang fix " .. ((tup.getconfig("LANG") == "") and "en" or tup.getconfig("LANG")) .. " > lang.inc", {"lang.inc"}) +tup.rule({"ray.asm", extra_inputs = {"lang.inc"}}, "fasm %f %o " .. tup.getconfig("KPACK_CMD"), "ray") diff --git a/programs/demos/ray/build.bat b/programs/demos/ray/build.bat new file mode 100644 index 0000000000..fdbb40c06e --- /dev/null +++ b/programs/demos/ray/build.bat @@ -0,0 +1,2 @@ +@fasm ray.asm ray +@pause \ No newline at end of file