Main Menu

Program using types is crashing

Started by Kman1011, December 11, 2008, 01:36:17 PM

Previous topic - Next topic

Kman1011

I started to write a simple program using types but for some reason when the variable 'p' reaches 10000
the prgram will crash

Am I missing something here?

;; PROJECT : Trig1
; AUTHOR  : Kent Seburn
; CREATED : 17/03/2008
; EDITED  : 10/12/2008
; ---------------------------------------------------------------------

type Vertex_Attribute
x
y
z
Sx
Sy
ang as float
dist as float
offset as float
angdif as float
str_Dist as float
V_ang as float
V_dist as float
V_offset as float
V_angdif as float
V_str_dist as float
endtype

dim Vertex(50000) as Vertex_Attribute
;dim panel(1000)

openscreen 1024,768,32,2

X2#=getscreenwidth()/2
Y2#=getscreenheight()/2
Zpos=-150

spd=40
hl=Y2#
for y=1 to 30
for x=1 to 30
for z=1 to 30
Vertex(p).x=x
Vertex(p).y=y
Vertex(p).z=z
;panel(p)=rndrgb()
inc p
next z
next x
next y
end


if anybody can see what I don't see or don't understand yet. Please reply

Thank U
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin


Works in PB1.64, but I suspect you're referring to PBFX 1.74 where it does crash.   Best guess, would be there's issue with auto type allocation and the memory manger.





kevin



You can get around this issue for rolling your own buffer and using typed pointer access to store the data in it.



type Vertex_Attribute
x
y
z
Sx
Sy
ang as float
dist as float
offset as float
angdif as float
str_Dist as float
V_ang as float
V_dist as float
V_offset as float
V_angdif as float
V_str_dist as float
endtype


// Size of the Vertex Attribute structure in bytes
Constant SizeOf_Vertex_Attribute=  16 * 4  // 16 fields by 4 bytes per field

// Alloc a bank to store this stuff in
VertexBuffer=NewBank(50000*SizeOf_Vertex_Attribute)


X2#=getscreenwidth()/2
Y2#=getscreenheight()/2
Zpos=-150

// Define a Typed pointer for type like access
Dim Vertex as Vertex_Attribute pointer


spd=40
hl=Y2#
for y=1 to 30
for x=1 to 30
for z=1 to 30
Vertex=GetBankPtr(VertexBuffer)+pos
Vertex.x=x
Vertex.y=y
Vertex.z=z
Pos=(pos+SizeOf_Vertex_Attribute)
next z
next x
next y
print "done"
sync
waitkey

end






Kman1011

Yeah, that seems to work.

So we're allocating the space to store 'Vertex' into memory as opposed to an array.

I'll have to study this for awhile. Not used to programming this way but whatever works :)

Thanks
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin

QuoteSo we're allocating the space to store 'Vertex' into memory as opposed to an array.

  yep, we're basically simulating an array in this case. 


  What are you ultimately trying to do ?


Kman1011

Well I was just experimenting around in making some sort of 3D Engine using basic trigonometry

In this example you can pan with right/left arrow keys and forward and back with up down keys and 8 & 2 on numeric pad to look up and down.


; PROJECT : Trig2
; AUTHOR  : Kent Seburn
; CREATED : 17/03/2008
; EDITED  : 11/12/2008
; ---------------------------------------------------------------------

type Vertex_Attribute
x
y
z
Sx
Sy
ang as float
dist as float
offset as float
angdif as float
str_Dist as float
V_ang as float
V_dist as float
V_offset as float
V_angdif as float
V_str_dist as float
endtype

dim Vertex(50000) as Vertex_Attribute
dim panel(1000)

openscreen 1024,768,32,2

X2#=getscreenwidth()/2
Y2#=getscreenheight()/2
Zpos=-150

spd=40
hl=Y2#
for Py=1 to 3000 step 100;
for Px=1 to 3000 step 100
Vertex(p).x=Px
Vertex(p).y=Py
Vertex(p).z=0
inc p
;panel(p)=rndrgb()
next Px
next Py

;end
;for p=0 to 5;
; Vertex_X(p)=readdata();
; Vertex(p).y=readdata()
; Vertex(p).z=readdata()
;next p

do
cls 0;$0000DD

for p=0 to 500
Vertex(p).ang=getangle2d(Vertex(p).x,Vertex(p).y,X2#,Y2#)
Vertex(p).dist=getdistance2d(Vertex(p).x,Vertex(p).y,X2#,Y2#)
Vertex(p).V_dist=getdistance3d(Vertex(p).x,Vertex(p).y,Vertex(p).z,X2#,Y2#,Zpos)

Vertex(p).V_ang#=getangle2d(Vertex(p).dist*-1,Vertex(p).z*-1,0,zpos)

Vertex(p).angdif=wrapangle(ang,180)-wrapangle(Vertex(p).ang,180)
Vertex(p).V_angdif=wrapangle(V_ang,180)-wrapangle(Vertex(p).V_ang,180)

Vertex(p).offset=sinradius(Vertex(p).angdif,Vertex(p).dist)*-1

Vertex(p).str_dist=cosradius(Vertex(p).angdif,Vertex(p).dist)
Vertex(p).V_str_dist=cosradius(Vertex(p).V_angdif,Vertex(p).V_dist)

Vertex(p).V_offset=sinradius(Vertex(p).V_angdif,Vertex(p).V_dist)*-1
Vertex(p).sx=0
Vertex(p).sy=0
if Vertex(p).str_dist>5 and Vertex(p).V_str_dist>5 and Vertex(p).str_dist<10000
Vertex(p).Sx=getscreenwidth()/2+((getscreenwidth())*Vertex(p).offset)/Vertex(p).str_dist
Vertex(p).SY=getscreenheight()/2-((getscreenheight())*Vertex(p).V_offset)/Vertex(p).dist
inc pt
endif

next  p

for v=0 to 500
dotc Vertex(v).Sx,Vertex(v).Sy,$ffffff
next v

ink $FF0000
print "Angle: "+str$(ang)
print "V Angle"+str$(V_ang)
print "X pos:"+str$(X2#)
print "Y pos:"+str$(Y2#)

skip:
X1=mousex()
Y1=mousey()



ink $ffffff
;line X1,Y1,X2#,Y2#

ink $00ffff
;line X2#+cosradius(ang+45,dist#)*-1,Y2#+sinradius(ang+45,dist#)*-1,X2#,Y2#
;line X2#+cosradius(ang-45,dist#)*-1,Y2#+sinradius(ang-45,dist#)*-1,X2#,Y2#

if upkey()=true
X2#=X2#-cos(ang)*spd
Y2#=Y2#-sin(ang)*spd
;wait 50
endif

if downkey()=true
X2#=X2#+cos(ang)*spd
Y2#=Y2#+sin(ang)*spd
;wait 50
endif

if rightkey()=true
inc ang
;wait 50
endif

if leftkey()=true
dec ang
;wait 50
endif
if scancode()=72
inc V_ang
endif
if scancode()=80
dec V_ang
endif
sync
pt=0
loop


I was going to use it in a demo but I seem to recall that PB has 3D functions ie - Rotatevertexlist etc.

I looked at the example but couldn't understand where the data for the points came from. Do you have a tutorial for dummies on 3D? Trying to tackle new territory
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin


  The vertex are stored in buffer.   Here's trimmed version of the same demo.    it' uses the Vertex Object slib.  Which if you open it up and have a look at the functions inside.  Will show you that's simply  storing the information in some standard structures, so we can init this chunks of data and pass them to the rotation functions. 

  Although in PBFX you don't need any of this.. Why ? - because SPRITES are mesh based.   




  ; include the Vertex Objects expansion library
   #include "VertexObjects"

ProjectionX#=300
ProjectionY#=300


SizeX=1000
SizeY=1000
SizeZ=1000


NumberOfVertex=5000

SrcVertSize =12
DestVertSize =20

; create Vertex Obejct Structure
   VertexObject=CreateVertexObject(0,0,0)
   VertexObjectRotationMode(VertexObject,1) ; ZYX
   VertexObjectProjectionMode(VertexObject,1,ProjectionX#,ProjectionY#)

; Create Vertex Buffers    
SrcVertexBank=NewBank((NumberOfVertex+1)*SrcVertSize)
DestVertexBank=NewBank((NumberOfVertex+1)*DestVertSize)
SrcAddress =GetBankPtr(SrcVertexBank)

; Create Vertex list
For lp=0 To NumberOfVertex
PokeFloat SrcAddress,Float(RndRange(-sizex,sizex))
PokeFloat SrcAddress+4,Float(RndRange(-sizey,sizey))
PokeFloat SrcAddress+8,Float(RndRange(-sizez,sizez))
SrcAddress=SrcAddress+SrcVertSize
Next


; Vertex objects position in 3d space.
basex#=0
basey#=0
basez#=1000


Do

cls $112233

; Rotate vertex list (with XYZ rotation order)

; Get the address of the ORIGINAL (Source vertex buffer)
SrcAddress =GetBankPtr(SrcVertexBank)


; Get the address of the ROTATED  (Source vertex buffer)
DestAddress =GetBankPtr(DestVertexBank)
RotateVertexListXYZ SrcAddress,SrcVertSize,DestAddress,DestVertSize,baseX#,basey#,baseZ#,ax#,ay#,az#,NumberOfVertex


lockbuffer
counter=NumberOfVertex
Do
z#=PeekFloat(DestAddress+8)
If z#>10
screenX#=400+((PeekFloat(DestAddress)*projectionx#)/z#)
screenY#=300+((PeekFloat(DestAddress+4)*projectiony#)/z#)
DotC ScreenX#,Screeny#,$ffffff
EndIf
Destaddress=Destaddress+DestVertSize
DecLoop counter
unlockbuffer


SetCursor 0,0
speed#=10

If UpKey() Then basez#=basez#-speed#
If DownKey() Then  basez#=basez#+speed#

If basez#>3000 Then Basez#=3000
If basez#<-3000 Then Basez#=-3000


ax#=WrapAngle(ax#,1.01)
ay#=WrapAngle(ay#,0.42)
az#=WrapAngle(az#,0.9)

Print "Rotated Vertex Lists"
Print FPS()
Sync
Loop



Quote
I looked at the example but couldn't understand where the data for the points came from. Do you have a tutorial for dummies on 3D? Trying to tackle new territory

  erm google :)


Kman1011

So really..you could use TextureQuad to say... connect the dots?

Good example Btw
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin

#8
 Yep,

  3D in nut shell.

Init

  - define vertex
  - define faces (faces are groups of vertex that connect together)
  - define viewer  (the camera, from where this object is viewed from)


* render

   for each object
       - rotate vertex (locally)
       - Rotate/translate the object locally rotated vertex to the camera
       - draw visible faces (only drawing faces that point towards the camera, back faces (away facing polygons) are not drawn) 
   next

  That's a bit of crash course, but that's how 3D basics.    What's missing is things like clipping at object and at face level.
   

  I suggest looking the PB demo examples and in the source board, there are a number of examples that demonstrate  this.