Joshy
gdb wrote:Reading symbols from .\cloth.exe...done.
(gdb) r
Starting program: D:\done\cloth.exe
[New Thread 10612.0xdb0]
[New Thread 10612.0x1fd8]
[New Thread 10612.0x14ec]
[New Thread 10612.0x219c]
[New Thread 10612.0x2290]
[Thread 10612.0x2290 exited with code 0]
[Thread 10612.0x219c exited with code 1]
[New Thread 10612.0x25c8]
[New Thread 10612.0x2760]
[New Thread 10612.0x784]
[Thread 10612.0x784 exited with code 0]
[Thread 10612.0x2760 exited with code 1]
[New Thread 10612.0x28ec]
[Thread 10612.0x28ec exited with code 1]
Program received signal SIGSEGV, Segmentation fault.
0x00007ffb094f0e57 in ntdll!RtlFreeHeap () from C:\WINDOWS\SYSTEM32\ntdll.dll
Code: Select all
#include "fbgfx.bi"
#ifndef __FB_64BIT__
Type REAL As single
#else
Type REAL As double
#endif
'Randomize Timer
#define scr_w 1024
#define scr_h 756
#define scr_b 32 ' 8,15,16,24 or 32
#if (scr_b=8)
Type pixel As Ubyte
#undef RGB
#define RGB(r,g,b) ((r And &HE0) Or ((g And &HE0) Shr 3) Or ((b And &HC0) Shr 6))
Sub Palette332()
Dim As Integer i,r,g,b
For i = 0 To 255
r=(((i Shr 5) And &H07) * 255) / 7
g=(((i Shr 2) And &H07) * 255) / 7
b=(((i Shr 0) And &H03) * 255) / 3
Palette i,r,g,b
Next
End Sub
#elseif (scr_b=15) Or (scr_b=16)
Type pixel As Ushort
#undef RGB
#define RGB(r,g,b) (((r Shr 3) Shl 11) Or ((g Shr 2) Shl 5) Or (b Shr 3))
#elseif (scr_b=24) Or (scr_b=32)
Type pixel As Ulong
#else
#error 666: bits per pixel (scr_b) must be 8,15,16,24 Or 32
#endif
#define SHIFTS 8 ' 24:8 fixed point format
Type vector2d
As Integer x,y
End Type
Sub triangle(d As pixel Ptr, _
p() As vector2d , _
c As pixel ) ' color
dim as integer t =Any,b=Any,l=Any,r=Any
dim as integer d1 =Any,d2=Any,s1=Any,s2=Any
dim as pixel ptr row=Any,cstart=Any,cend=Any
dim as vector2d v0 =Any,v1=Any,v2=Any
v0=p(0):v1=p(1):v2=p(2)
If (v1.y>v2.y) then swap v1,v2
If (v0.y>v2.y) then swap v0,v2
If (v0.y>v1.y) then swap v0,v1
If (v2.y=v0.y) then return
s1=((v2.x-v0.x) Shl SHIFTS)/(v2.y-v0.y)
d1=v0.x Shl SHIFTS
For i As Integer=0 To 1
s2=((v1.x-v0.x) Shl SHIFTS)/(v1.y-v0.y)
d2=v0.x Shl SHIFTS
t=v0.y ' top
' begin in first row (top=0)
If t<0 Then d1-=s1*t : d2-=s2*t : t=0
b=v1.y ' bottom
' end in last row (b=scr_h-1)
If b>=scr_h Then b=scr_h-1
If b<=t Then Goto next_triangle
row=d+t*scr_w ' first row
b-=t ' how many scanlines
While b ' from top to bottom
l=d1 Shr SHIFTS:r=d2 Shr SHIFTS
If l>r Then Swap l,r
If l>=scr_w Then Goto next_scanline
If r<1 Then Goto next_scanline
If r>=scr_w Then r=scr_w-1
cstart=row+l ' first pixel
cend =row+r ' last pixel
While cstart<cend:*cstart=c:cstart+=1:Wend
next_scanline:
d1+=s1:d2+=s2:row+=scr_w:b-=1
Wend
next_triangle:
d1= (v0.x Shl SHIFTS)+((v1.y-v0.y)*s1)
v0=v1:v1=v2
Next
End Sub
const cells as Integer = 100
const nTriangles as Integer = cells*cells*2
const stiffnes as REAL = 200
const gravity as REAL = -9.81
const DT as REAL = 1/30
const w as REAL = scr_w/(cells-2)
const h as REAL = scr_h/(cells*2)
const wm as REAL = scr_w/2
const hm as REAL = scr_h/2
Type vector3d
As REAL x,y,z
End Type
Type POINT3D
As VECTOR3D p,v,f
As BOOLEAN fixed
As REAL mass,e
End Type
Dim Shared As POINT3D points (cells-1,cells-1)
Sub CreateIt()
For x As Integer=0 To cells-1 step (cells-1)\5
points(x,0).fixed=true
next
For y As Integer=0 To cells-1
For x As Integer=0 To cells-1
With points(x,y)
.p.x=-wm+x*w
.p.y=hm
.mass=1+rnd
End With
Next
Next
End Sub
Sub CalcForces()
Dim As VECTOR3D Fd,Vd,F
Dim As REAL force,delta,direction,l2,air
Static As REAL sw
air=Sin(sw) ' *(5+Rnd*5)
sw+=0.1*dt
' forces x,y
For y As Integer=0 To cells-1
For x As Integer=0 To cells-1
points(x,y).f.x = air'*Rnd
points(x,y).f.y = gravity * points(x,y).mass
Next
Next
For y As Integer=0 To cells-1
For x As Integer=0 To cells-2
With points(x+1,y)
Fd.x=points(x,y).p.x-.p.x
Fd.y=points(x,y).p.y-.p.y
l2=fd.x*fd.x + fd.y*fd.y
If l2 Then
l2=Sqr(l2)
delta=l2-w
force=delta*stiffnes
vd.x = points(x,y).v.x-.v.x
vd.y = points(x,y).v.y-.v.y
force+=(vd.x*fd.x + vd.y*fd.y)/l2
fd.x/=l2:fd.y/=l2
f.x=fd.x*force
f.y=fd.y*force
points(x,y).f.x-=f.x
points(x,y).f.y-=f.y
.f.x+=f.x
.f.y+=f.y
End If
End With
Next
Next
For y As Integer=0 To cells-2
For x As Integer=0 To cells-1
With points(x,y+1)
Fd.x=points(x,y).p.x-.p.x
Fd.y=points(x,y).p.y-.p.y
l2=fd.x*fd.x + fd.y*fd.y
If l2 Then
l2=Sqr(l2)
delta=l2-h
force=delta*stiffnes
vd.x = points(x,y).v.x-.v.x
vd.y = points(x,y).v.y-.v.y
force+=(vd.x*fd.x + vd.y*fd.y)/l2
fd.x/=l2:fd.y/=l2
f.x=fd.x*force
f.y=fd.y*force
points(x,y).f.x-=f.x
points(x,y).f.y-=f.y
.f.x+=f.x
.f.y+=f.y
End If
End With
Next
Next
For y As Integer=0 To cells-1
For x As Integer=0 To cells-1
With points(x,y)
If .fixed=0 Then
.v.x+= .f.x * DT ' velocity + force
.v.y+= .f.y * DT
.p.x+= .v.x * DT ' position + velocity
.p.y+= .v.y * DT
If .p.y<1-(scr_h\2) Then .v.y*=-0.999
.v.x*=0.999
End If
End With
Next
Next
End Sub
Sub DrawFilledTriangles()
static As vector2d t(2)
dim As pixel c
dim As Integer i,j
dim As pixel Ptr pixels=screenptr
For y As Integer=0 To cells-2
i=(y and 1)
For x As Integer=0 To cells-2
t(0).x=wm+points(x ,y ).p.x
t(0).y=hm-points(x ,y ).p.y
t(1).x=wm+points(x+1,y ).p.x
t(1).y=hm-points(x+1,y ).p.y
t(2).x=wm+points(x+1,y+1).p.x
t(2).y=hm-points(x+1,y+1).p.y
dim as REAL rr=points(x,y).f.x + points(x+1,y).f.x + points(x+1,y).f.x + points(x+1,y+1).f.x + points(x,y+1).f.x
rr=sqr(rr)
dim as REAL gg=points(x,y).f.y + points(x+1,y).f.y + points(x+1,y).f.y + points(x+1,y+1).f.y + points(x,y+1).f.y
gg=sqr(gg)
dim as REAL bb=points(x,y).v.x*points(x,y).v.y
bb+=points(x+1,y ).v.x*points(x+1,y ).v.y
bb+=points(x+1,y+1).v.x*points(x+1,y+1).v.y
bb+=points(x ,y+1).v.x*points(x ,y+1).v.y
bb=sqr(bb)
dim as integer r=rr*8:if r>255 then r=255
dim as integer g=gg*8:if g>255 then g=255
dim as integer b=bb*8:if b>255 then b=255
j=(x and 1)
If i xor j then
r\=2:g\=2:b\=2
End If
c=rgb(r,g,b)
Triangle pixels,t(),c
t(1).x=wm+points(x,y+1).p.x
t(1).y=hm-points(x,y+1).p.y
Triangle pixels,t(),c
Next
Next
End Sub
'
'main
'
dim as integer frames,fps
dim as double t1,t2
ScreenRes scr_w,scr_h,scr_b
#if (scr_b=8)
Palette332
#endif
CreateIt
t1=Timer
While inkey()=""
CalcForces
ScreenLock
cls : DrawFilledTriangles
ScreenUnlock
frames+=1
If frames mod 50=0 Then
t2=Timer: fps=50/(t2-t1) : t1=t2
WindowTitle nTriangles & " triangles FPS=" & Str(fps) & " [ESC] = quit"
End If
Wend