Author Topic: 3D Lib[BPLUS]  (Read 5066 times)

0 Members and 1 Guest are viewing this topic.

Offline Blitz Amateur

  • Atari ST
  • ***
  • Posts: 243
  • Karma: 13
    • View Profile
3D Lib[BPLUS]
« on: September 05, 2006 »
I decided I'd release one of my older Blitz software 3D libs. It contains code for cameras, parented objects(Still needs work though), gouraud shading, lights, and more. As of right now, it's messy, and mostly uncommented. I'll include a test program though, so if you feel like it, you can try and look thorugh it =)

It works in B2D, B3D, and B+

Screenshot:


test.bb
Code: [Select]
;Here's a super simple reference
;
;CreateObject(x,y,z,parent)
;CreateCamera(x,y,z)
;CreateSurface(object)
;CreateLight(x,y,z,maxdist)
;CreateTorus(surface,radius,ringwidth,segments1,segments2)
;CreateCube(surface,xpos,ypos,zpos,xsize,ysize,zsize)
;UpdateNormals(object)
;Update3D()
;LockIt
;Render3D
;UnlockIt
;TurnEntity(entity,rollspeed,pitchspeed,yawspeed)
;RotateEntity(entity,roll,pitch,yaw)
;TranslateEntity(entity,xspeed,yspeed,zspeed)
;
;
;Also, for those interested, multiple cameras can be used,
;just create an extra camera and assign it to Current_Camera
;Example:
;
;cam1=CreateCamera(10,0,0) ; A camera to the right
;cam2=CreateCamera(-10,0,0) ; A camera to teh left
;
;Current_camera = cam2
;
;(Current_camera is auto-assigned to the first created camera)

SeedRnd MilliSecs()

Include "Engine.bb"

;Delta timing code
Global cmillis
Global millispf=33
Global tmillis

;Colors for toruses
Global current_r = 173
Global current_g = 140
Global current_b = 158

;Initialize graphics
init 640,480,32,2

;Setup the backbuffer
SetBuffer BackBuffer()

;Define z clipping plane
Clip_Point = 0.1

;Define our objects
o=CreateObject(0,0,0) ; Parent object of the toruses and cubes
o2=CreateObject(0,0,0,o) ; Stationary torus
o3=CreateObject(0,0,30,o) ; Moving torus
cube=CreateObject(0,0,0) ; Cube used for the background.
o4=CreateObject(0,0,0,o) ; Object for smaller cubes


;Create a camera
cam=CreateCamera(0,0,-60)

;Surface for the background cube
surfcube=CreateSurface(cube)

;Surface for teh smaller cubes
surfcubes=CreateSurface(o4)

;Surface for the stationary torus
surf=CreateSurface(o2)

;Surface for the moving torus
surf2=CreateSurface(o3)

;Create lights
l=CreateLight(100,0,-200,4000)
l2=CreateLight(-100,0,-200,4000)




;Create our toruses
CreateTorus surf, 17,4,30,20
CreateTorus surf2, 17,4,30,20

;Create large background cube
Createcube surfcube, 0,0,0,130,130,130

;The scale of the cube we make out of cubes. This controls the separation
cs# = 1.4

;Create cube of cubes

;Front side
Createcube surfcubes, -20*cs,-20*cs,-20*cs,5,5,5
Createcube surfcubes, 20*cs,-20*cs,-20*cs,5,5,5
Createcube surfcubes, -20*cs,20*cs,-20*cs,5,5,5
Createcube surfcubes, 20*cs,20*cs,-20*cs,5,5,5
Createcube surfcubes, 0*cs,-20*cs,-20*cs,5,5,5
Createcube surfcubes, 20*cs,0*cs,-20*cs,5,5,5
Createcube surfcubes, 0*cs,20*cs,-20*cs,5,5,5
Createcube surfcubes, -20*cs,0*cs,-20*cs,5,5,5

;Center
Createcube surfcubes, -20*cs,-20*cs,0*cs,5,5,5
Createcube surfcubes, 20*cs,-20*cs,0*cs,5,5,5
Createcube surfcubes, -20*cs,20*cs,0*cs,5,5,5
Createcube surfcubes, 20*cs,20*cs,0*cs,5,5,5

;Back side
Createcube surfcubes, -20*cs,-20*cs,20*cs,5,5,5
Createcube surfcubes, 20*cs,-20*cs,20*cs,5,5,5
Createcube surfcubes, -20*cs,20*cs,20*cs,5,5,5
Createcube surfcubes, 20*cs,20*cs,20*cs,5,5,5
Createcube surfcubes, 0*cs,-20*cs,20*cs,5,5,5
Createcube surfcubes, -20*cs,0*cs,20*cs,5,5,5
Createcube surfcubes, 0*cs,20*cs,20*cs,5,5,5
Createcube surfcubes, 20*cs,0*cs,20*cs,5,5,5


;Update normas on our objects for lighitng
UpdateNormals cube
UpdateNormals o4
UpdateNormals o2
UpdateNormals o3

;Invert the large cube
scaleobject cube,-1


;Scaling controls for the moving torus
scaleval=30
scalevaldir=1


;Check for first frame (Delta timing)
fframe=0

;FPS counter variables
frames=0
fps=0

;More delta timing
ms=MilliSecs()
tmillis=MilliSecs()

;Main loop
Repeat
;Sacling control for our moving torus
scaleobject(o3,Abs(scaleval/60.0)+0.5)

;Take a screenshot on keypress of Enter
If KeyHit(28) SaveBuffer(BackBuffer(),"pic.bmp")

;Coloring of objects
entitycolor o2,current_r,current_g,current_b
entitycolor cube,0,0,255
entitycolor o3,255-current_r,255-current_g,255-current_b


;Update objects
update3D

;Check to see if we have time left to render
If MilliSecs()-tmillis <= millispf Or fframe = 0
Flip
Cls
;Lock buffer and render
LockIt
render3D
UnlockIt
;If we still have extra time, get rid of it
If MilliSecs()-tmillis < millispf
WaitEvent(MilliSecs()-tmillis) ; B+
;TimeWaste(MilliSecs()-tmillis) ; B2D/B3D
EndIf
; Advance counter
tmillis=tmillis+millispf
Else ; If not extra time, advance our counter
tmillis=tmillis+millispf
EndIf

; Rotate parent entity
TurnEntity o,2,7,4

; Rotate moving torus
TurnEntity o3,3,3,0
; Move the moving torus ;-)
moveentity o3,0,0,scaleval
;Rotate the BG cube
TurnEntity cube,0,0,1


;Camera controls
If MouseHit(1)
MouseXSpeed()
MouseYSpeed()
MoveMouse MouseX(),MouseY()
EndIf
If MouseDown(1)
pitch=pitch+MouseYSpeed()
yaw=yaw+MouseXSpeed()
EndIf

;More camera control
If KeyDown(200) TranslateEntity cam,0,0,1
If KeyDown(208) TranslateEntity cam,0,0,-1
If KeyDown(203) TranslateEntity cam,-1,0,0
If KeyDown(205) TranslateEntity cam,1,0,0
RotateEntity cam,0,pitch,yaw

;FPS counter
frames=frames+1
If MilliSecs()-ms >= 1000
ms=MilliSecs()
fps=frames
frames=0
EndIf

;Scaling controls for the moving torus
If scaleval = 30
scalevaldir=-1
EndIf
If scaleval = -30
scalevaldir=1
EndIf
scaleval = scaleval + scalevaldir

fframe=1 ; This tells us we are past our first frame
Until KeyDown(1)
If dbg = 1 CloseFile dbgfile ; File created for debugging
End

;Function for burning a specific amount of MilliSecs.
;This is so you can do things in your downtime if you want to.
Function TimeWaste(time)
Local temp=MilliSecs()
Repeat
If KeyDown(1) Exit
Until temp+time < MilliSecs()
End Function

Engine.bb
Code: [Select]
;Constants

Const hidden=%1
Const disablebackfacecull=%10
Const nolighting=%100
Const cellighting=%1000

Const oneover255#=1.0/255.0
Const dbg=0


Type TObject
Field X#,Y#,Z#
Field OFFX#,OFFY#,OFFZ#
Field roll#,pitch#,yaw#
Field State
Field Surfaces
Field NSurfaces
Field Matrix
Field Parent
Field Tnd#
Field scale#
End Type

Type TMatrix
Field m00#
Field m01#
Field m02#
Field m10#
Field m11#
Field m12#
Field m20#
Field m21#
Field m22#
End Type

Type TVertex
Field X#,Y#,Z#
Field TRX#,TRY#,TRZ#
Field RX#,RY#,RZ#
Field NX#,NY#,NZ#
Field RNX#,RNY#,RNZ#
Field ANX#,ANY#,ANZ#
Field SX#,SY#
Field NormalNums#
Field ARGB
Field LARGB
Field u#,v#
End Type

Type TTriangle
Field NX#,NY#,NZ#
Field U1#,V1#
Field U2#,V2#
Field U3#,V3#
Field Vertex1
Field Vertex2
Field Vertex3
End Type

Type TCamera
Field X#,Y#,Z#
Field roll#,pitch#,yaw#
Field Matrix
End Type

Type TSurface
Field Triangles
Field NTriangles
Field Vertices
Field NVertices
Field texture
End Type

Type TTexture
Field OriginalPixels
Field MipMappedPixels
Field OriginalW
Field OriginalH
Field MipMappedW
Field MipMappedH
Field MipMapped
End Type

Type TLight
Field X#,Y#,Z#
Field TX#,TY#,TZ#
Field NX#,NY#,NZ#
Field MAXDIST#
End Type

Type Point
Field sx#,sy#,r,g,b
Field x#,y#,z#,nx#,ny#,nz#
Field u#,v#
End Type

Type Vector
Field X#,Y#,Z#
End Type

Global XRes=640,YRes=480
Global HXRes=640,HYRes=480
Global ZBuffer
Global ZBufferClear
Global distance = 20
Global Current_Camera
m.TMatrix = New TMatrix
m2.TMatrix = New TMatrix
Global Multiplied_Matrix = Handle(m)
Global Multiplied_Matrix2 = Handle(m2)
Global focus#=256,bfc=1
Global pnt0.point = New point
Global pnt1.point = New point
Global pnt2.point = New point
Global tpnt0.point = New point
Global tpnt1.point = New point
Global Clip_Point# = 100
Global current_buffer
Global current_buffer_bank
Global current_buffer_pitch
Global current_buffer_format
Global polycount
;Global lightx#,lighty#,lightz#
Global lightvect.vector = New vector
Global vertvect.vector = New vector
Global floatval#,pointlen#
Global ARGB1,ARGB2,ARGB3
Global dbgfile
Global GFXBuffer,GFXBufferp,GFXBufferf,GFXBufferc

Function GFX_Functions_____________()
End Function

Function Init(w,h,depth,win)
If dbg = 1 dbgfile=WriteFile("debug.txt")
; If GfxModeExists(w,h,depth,win)
XRes=w
YRes=h
hxres=w Shr 1
hyres=h Shr 1
Graphics w,h,depth,win
LockBuffer
; GFXBuffer = CreateBank(LockedPitch()*(XRes*LockedFormat()))
; GFXBufferc = CreateBank(LockedPitch()*(XRes*LockedFormat()))
; GFXBufferp = LockedPitch()
; GFXBufferf = LockedFormat()
UnlockBuffer
ZBuffer=CreateBank(w*h*4)
ZBufferClear=CreateBank(w*h*4)
For i=0 To w*h-1
PokeFloat ZBuffer,i*4,10000
PokeFloat ZBufferClear,i*4,10000
Next
Return 1
; Else
; Return 0
; EndIf

End Function

Function Creation_Functions_________()
End Function

Function CreateLight(X#,Y#,Z#,MAXDIST#)

l.TLight = New TLight
l\x=x
l\y=y
l\z=z
l\maxdist=maxdist*maxdist
Return Handle(l)

End Function

Function AddVertex(surf,X#,Y#,Z#,ARGB,u#=0.0,v#=0.0)

s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
size=BankSize(s\Vertices)
index=size/4

ver.TVertex = New TVertex
ver\X=X
ver\Y=Y
ver\Z=Z
ver\ARGB=ARGB

ResizeBank s\Vertices, size+4
PokeInt s\Vertices, size, Handle(ver)
s\NVertices=s\NVertices+1
Return index

End Function

Function CreateSurface(mesh)
m.TObject=Object.TObject(mesh)
size=BankSize(m\Surfaces)
s.TSurface = New TSurface
s\Triangles = CreateBank(0)
s\Vertices = CreateBank(0)
ResizeBank m\Surfaces, size+4
PokeInt m\surfaces, size, Handle(s)
m\NSurfaces = m\NSurfaces + 1
Return Handle(s)
End Function

Function CreateCamera(x,y,z)

m.Tmatrix = New Tmatrix
c.Tcamera = New TCamera
c\x=x
c\y=y
c\z=z
c\matrix=Handle(m)
If current_camera = 0 current_camera=Handle(c)
Return Handle(c)

End Function

Function AddTriangle(surf,v1,v2,v3)

s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
size=BankSize(s\Triangles)
index=size/4
t.TTriangle = New TTriangle
t\vertex1=v1
t\vertex2=v2
t\vertex3=v3
v.TVertex = Object.TVertex(getvertex(surf,t\vertex1))
t\u1 = v\u
t\v1 = v\v
v.TVertex = Object.TVertex(getvertex(surf,t\vertex2))
t\u2 = v\u
t\v2 = v\v
v.TVertex = Object.TVertex(getvertex(surf,t\vertex3))
t\u3 = v\u
t\v3 = v\v
ResizeBank s\triangles, size+4
PokeInt s\Triangles, size, Handle(t)
s\NTriangles=s\NTriangles+1
Return index

End Function

Function CreateMatrix()
t.Tmatrix = New TMatrix
Return Handle(t)
End Function

Function CreateObject(x,y,z,parent=0)

m.Tmatrix = New Tmatrix
o.TObject = New TObject
o\surfaces=CreateBank(0)
o\matrix = Handle(m)
o\x=x
o\y=y
o\z=z
o\offx=x
o\offy=y
o\offz=z
o\parent=parent
o\scale=1
Return Handle(o)

End Function


Function LoadTexture(fn$)

img=LoadImage(fn)
w=ImageWidth(img)
h=ImageHeight(img)

t.TTexture = New TTexture

t\originalpixels = CreateBank((w*h)*4)
t\originalw = w
t\originalh = h
tw=w-1
th=h-1

LockBuffer ImageBuffer(img)

For y=0 To th

offset=y*tw

For x=0 To tw

PokeInt t\originalpixels,(offset+x) Shl 2,ReadPixelFast(x,y,ImageBuffer(img))

Next

Next

UnlockBuffer ImageBuffer(img)

FreeImage img

Return Handle(t)

End Function

Function MipMap(tex,amount=1)

t.TTexture = Object.TTexture(tex)

t\mipmappedw = t\originalw Shr amount
t\mipmappedh = t\originalh Shr amount

t\mipmapped = 1

tw = t\mipmappedw-1
th = t\mipmappedh-1
ta = amount Shl 1

For y=0 To th

offset=y*tw
offset2=(y*ta)*(t\originalw-1)

For x = 0 To tw

PokeInt t\mipmappedpixels,(offset+x) Shl 2,PeekInt(t\originalpixels,(offset2+x*ta) Shl 2)

Next

Next

End Function



Function Retrieval_Functions_________()
End Function

Function GetSurface(mesh, index)
o.TObject = Object.TObject(mesh)
If o = Null RuntimeError "No Such Object"
Return PeekInt(o\surfaces, index*4)
End Function

Function GetVertex(surf, index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
Return PeekInt(s\Vertices, index*4)
End Function

Function TriangleVertex(tri,num)

t.ttriangle = Object.ttriangle(tri)
If t = Null RuntimeError("triangle not found")

Select num
Case 0
Return t\vertex1
Case 1
Return t\vertex2
Case 2
Return t\vertex3
End Select

End Function

Function Value_Setting_Functions____()
End Function

Function ScaleObject(e_h,scale#)

m.TObject = Object.TObject(e_h)
If m = Null RuntimeError "No such Entity"
m\scale=scale

End Function

Function EntityColor(e_h,r,g,b)

m.TObject = Object.TObject(e_h)
If m = Null RuntimeError "No such Entity"
For i=0 To m\NSurfaces-1

surf=PeekInt(m\Surfaces,i*4)
s.TSurface = Object.TSurface(surf)

For i2=0 To s\NVertices-1

v_h=PeekInt(s\Vertices,i2*4)
v.TVertex = Object.TVertex(v_h)
v\ARGB = ( r Shl 16 ) + ( g Shl 8 ) + b

Next

Next

End Function

Function MoveEntity(e_h,x#,y#,z#)

o.TObject = Object.TObject(e_h)
If o = Null
c.Tcamera = Object.Tcamera(e_h)
If c = Null
l.TLight = Object.TLight(e_h)
If l = Null RuntimeError "No Such Entity"
l\x=x
l\y=y
l\z=z
Return
EndIf
c\x=x
c\y=y
c\z=z
Return
EndIf
o\x=x
o\y=y
o\z=z

End Function

Function TranslateEntity(e_h,x#,y#,z#)

o.TObject = Object.TObject(e_h)
If o = Null
c.Tcamera = Object.Tcamera(e_h)
If c = Null RuntimeError "No Such Entity"
m.TMatrix = Object.TMatrix(c\matrix)
c\x=c\x+m\m00*x
c\y=c\y+m\m01*x
c\z=c\z+m\m02*x
c\x=c\x+m\m10*y
c\y=c\y+m\m11*y
c\z=c\z+m\m12*y
c\x=c\x+m\m20*z
c\y=c\y+m\m21*z
c\z=c\z+m\m22*z
Return
EndIf
m.TMatrix = Object.TMatrix(o\matrix)
o\x=o\x+m\m00*x
o\y=o\y+m\m01*x
o\z=o\z+m\m02*x
o\x=o\x+m\m10*y
o\y=o\y+m\m11*y
o\z=o\z+m\m12*y
o\x=o\x+m\m20*z
o\y=o\y+m\m21*z
o\z=o\z+m\m22*z

End Function

Function RotateEntity(e_h,roll#,pitch#,yaw#)

o.TObject = Object.TObject(e_h)
If o = Null
c.Tcamera = Object.Tcamera(e_h)
If c = Null RuntimeError "No Such Entity"
c\roll=roll
c\pitch=pitch
c\yaw=yaw
Return
EndIf
o\roll=roll
o\pitch=pitch
o\yaw=yaw

End Function

Function TurnEntity(e_h,rolls#,pitchs#,yaws#)

o.TObject = Object.TObject(e_h)
If o = Null
c.Tcamera = Object.Tcamera(e_h)
If c = Null RuntimeError "No Such Entity"
c\roll=c\roll+rolls
c\pitch=c\pitch+pitchs
c\yaw=c\yaw+yaws
Return
EndIf
o\roll=o\roll+rolls
o\pitch=o\pitch+pitchs
o\yaw=o\yaw+yaws

End Function

Function SetupMatrix(m_h,roll#,pitch#,yaw#)

m.TMatrix = Object.TMatrix(m_h)
If m = Null RuntimeError "No Such Matrix"

;------------------------------------------------
; Calculate a rotation matrix
;------------------------------------------------
A#=Cos(pitch):B#=Sin(pitch)
C#=Cos(yaw):D#=Sin(yaw)
E#=Cos(roll):F#=Sin(roll)
AD#=A*D
BD#=B*D
m\m00=C*E
m\m01=-C*F
m\m02=D
m\m10=BD*E+A*F
m\m11=-BD*F+A*E
m\m12=-B*C
m\m20=-AD*E+B*F
m\m21=AD*F+B*E
m\m22=A*C

End Function

Function MultiplyMatrices(m1_h,m2_h,d_h)
m1.TMatrix = Object.TMatrix(m1_h)
m2.TMatrix = Object.TMatrix(m2_h)
m.TMatrix = Object.TMatrix(d_h)
If m1 = Null RuntimeError "No Such Matrix"
If m2 = Null RuntimeError "No Such Matrix"
If m = Null RuntimeError "No Such Matrix"
m\m00 = m1\m00*m2\m00 + m1\m01*m2\m01 + m1\m02*m2\m02
m\m10 = m1\m00*m2\m10 + m1\m01*m2\m11 + m1\m02*m2\m12
m\m20 = m1\m00*m2\m20 + m1\m01*m2\m21 + m1\m02*m2\m22
m\m01 = m1\m10*m2\m00 + m1\m11*m2\m01 + m1\m12*m2\m02
m\m11 = m1\m10*m2\m10 + m1\m11*m2\m11 + m1\m12*m2\m12
m\m21 = m1\m10*m2\m20 + m1\m11*m2\m21 + m1\m12*m2\m22
m\m02 = m1\m20*m2\m00 + m1\m21*m2\m01 + m1\m22*m2\m02
m\m12 = m1\m20*m2\m10 + m1\m21*m2\m11 + m1\m22*m2\m12
m\m22 = m1\m20*m2\m20 + m1\m21*m2\m21 + m1\m22*m2\m22
End Function

Function MultiplyMatricesInv(m1_h,m2_h,d_h)
m1.TMatrix = Object.TMatrix(m1_h)
m2.TMatrix = Object.TMatrix(m2_h)
m.TMatrix = Object.TMatrix(d_h)
If m1 = Null RuntimeError "No Such Matrix"
If m2 = Null RuntimeError "No Such Matrix"
If m = Null RuntimeError "No Such Matrix"
m\m00 = (m1\m00*-m2\m00 + m1\m01*-m2\m01 + m1\m02*-m2\m02)
m\m10 = (m1\m00*-m2\m10 + m1\m01*-m2\m11 + m1\m02*-m2\m12)
m\m20 = (m1\m00*-m2\m20 + m1\m01*-m2\m21 + m1\m02*-m2\m22)
m\m01 = (m1\m10*-m2\m00 + m1\m11*-m2\m01 + m1\m12*-m2\m02)
m\m11 = (m1\m10*-m2\m10 + m1\m11*-m2\m11 + m1\m12*-m2\m12)
m\m21 = (m1\m10*-m2\m20 + m1\m11*-m2\m21 + m1\m12*-m2\m22)
m\m02 = (m1\m20*-m2\m00 + m1\m21*-m2\m01 + m1\m22*-m2\m02)
m\m12 = (m1\m20*-m2\m10 + m1\m21*-m2\m11 + m1\m22*-m2\m12)
m\m22 = (m1\m20*-m2\m20 + m1\m21*-m2\m21 + m1\m22*-m2\m22)
End Function




Function Vertex_Values______________()
End Function

Function VertexX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\x
End Function

Function VertexY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\y
End Function

Function VertexZ#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\z
End Function

Function VertexNN%(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\NormalNums
End Function

Function VertexANX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\anx
End Function

Function VertexANY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\any
End Function

Function VertexANZ#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\anz
End Function

Function VertexNX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\nx
End Function

Function VertexNY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\ny
End Function

Function VertexNZ#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\nz
End Function

Function VertexRNX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\rnx
End Function

Function VertexRNY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\rny
End Function

Function VertexRNZ#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\rnz
End Function

Function VertexRX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\rx
End Function

Function VertexRY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\ry
End Function

Function VertexRZ#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\rz
End Function

Function VertexSX#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\sx
End Function

Function VertexSY#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\sy
End Function

Function VertexARGB#(surf,index)
s.TSurface = Object.TSurface(surf)
If s = Null RuntimeError "No Such Surface"
vert_h=PeekInt(s\Vertices, index*4)
v.TVertex = Object.TVertex(vert_h)
If v = Null RuntimeError "No Such Vertex"
Return v\ARGB
End Function

Function Control_Stuff______________()
End Function

Function triangletexcoords(surf,ti,u1$,v1#,u2#,v2#,u3#,v3#)

s.TSurface = Object.TSurface(surf)
t.TTriangle = Object.TTriangle(PeekInt(s\triangles,ti*4))
t\u1=u1
t\v1=v1
t\u2=u2
t\v2=v2
t\u3=u3
t\v3=v3

End Function

Function SurfaceTexture(surf,tex)

s.TSurface = Object.TSurface(surf)
s\texture=tex

End Function
Function ObjectState(obj,flags%)

o.TObject = Object.TObject(obj)
o\state = flags

End Function


Function Update3D()

c.TCamera = Object.TCamera(Current_Camera)
tm.TMatrix = Object.TMatrix(C\matrix)
SetupMatrix(c\matrix, c\roll, c\pitch, c\yaw)

For m.TObject = Each TObject
dparent=0
SetupMatrix(m\matrix, m\roll, m\pitch, m\yaw)

If m\parent <> 0
p.TObject=Object.TObject(m\parent)
SetupMatrix(p\matrix, p\roll, p\pitch, p\yaw)
ttm.TMatrix=Object.TMatrix(p\matrix)
xp#=m\x-p\offx
yp#=m\y-p\offy
zp#=m\z-p\offz
TX# = (ttm\m00 * XP + ttm\m01 * YP + ttm\m02 * ZP)
TY# = (ttm\m10 * XP + ttm\m11 * YP + ttm\m12 * ZP)
TZ# = (ttm\m20 * XP + ttm\m21 * YP + ttm\m22 * ZP)
m\offx=tx
m\offy=ty
m\offz=tz
multiplymatrices(p\matrix,m\matrix,multiplied_matrix2)
multiplymatrices(multiplied_matrix2,c\matrix,multiplied_matrix)
dparent=1
Else
m\offx=m\x
m\offy=m\y
m\offz=m\z
multiplymatrices(m\matrix,c\matrix,multiplied_matrix)
EndIf


XP#=m\offX - c\X
YP#=m\offY - c\Y
ZP#=m\offZ - c\Z

TX# = (tm\m00 * XP + tm\m01 * YP + tm\m02 * ZP)
TY# = (tm\m10 * XP + tm\m11 * YP + tm\m12 * ZP)
TZ# = (tm\m20 * XP + tm\m21 * YP + tm\m22 * ZP)

;multiplymatrices(m\matrix,c\matrix,multiplied_matrix)

For i=0 To m\NSurfaces-1

surf=PeekInt(m\Surfaces,i*4)
s.TSurface = Object.TSurface(surf)

For i2=0 To s\NVertices-1

v_h=PeekInt(s\Vertices,i2*4)
If dparent=1
UpdateVertex(v_h, multiplied_matrix, tx, ty, tz, multiplied_matrix2, m);m\matrix, m)
Else
UpdateVertex(v_h, multiplied_matrix, tx, ty, tz, m\matrix, m);m\matrix, m)
EndIf

Next

Next

Next

For l.TLight = Each TLight

XP#=l\X - c\X
YP#=l\Y - c\Y
ZP#=l\Z - c\Z

TX# = (tm\m00 * XP + tm\m01 * YP + tm\m02 * ZP)
TY# = (tm\m10 * XP + tm\m11 * YP + tm\m12 * ZP)
TZ# = (tm\m20 * XP + tm\m21 * YP + tm\m22 * ZP)

l\TX=TX
l\TY=TY
l\TZ=TZ

Next

End Function

Function Render3D()

polycount = 0

CopyBank ZBufferClear,0,ZBuffer,0,BankSize(ZBuffer)

For m.TObject = Each TObject

For i=0 To m\NSurfaces-1

surf=PeekInt(m\Surfaces,i*4)
s.TSurface = Object.TSurface(surf)

For i2=0 To s\NTriangles-1

t_h=PeekInt(s\triangles,i2*4)
t.TTriangle = Object.TTriangle(t_h)
vrt1=TriangleVertex(t_h,0)
vrt2=TriangleVertex(t_h,1)
vrt3=TriangleVertex(t_h,2)
; x1#=VertexSX(surf,vrt1)
; y1#=VertexSY(surf,vrt1)
; z1#=VertexRZ(surf,vrt1)

; ARGB1=VertexARGB(surf,vrt1)
; x2#=VertexSX(surf,vrt2)
; y2#=VertexSY(surf,vrt2)
; z2#=VertexRZ(surf,vrt2)
; ARGB2=VertexARGB(surf,vrt2)
; x3#=VertexSX(surf,vrt3)
; y3#=VertexSY(surf,vrt3)
; z3#=VertexRZ(surf,vrt3)
; ARGB3=VertexARGB(surf,vrt3)
If (m\state And disablebackfacecull)
tbfc = 0
Else
tbfc = 1
EndIf
triz t_h,surf,vrt1,vrt2,vrt3,tbfc;x1,y1,z1,ARGB1,x2,y2,z2,ARGB2,x3,y3,z3,ARGB3,1

Next

Next

Next

End Function

Function UpdateVertex(v_h,m_h,offx#,offy#,offz#,m2_h, mesh.TObject)
m.TMatrix = Object.TMatrix(m_h)
m2.TMatrix = Object.TMatrix(m2_h)
v.TVertex = Object.TVertex(v_h)
If v = Null RuntimeError "No Such Vertex"
If m = Null RuntimeError "No Such Matrix"
If m2 = Null RuntimeError "No Such Matrix"
TX#=v\x*mesh\scale
TY#=v\y*mesh\scale
tz#=v\z*mesh\scale
v\RX# = (m\m00 * tx + m\m01 * tY + m\m02 * tZ)+offX
v\RY# = (m\m10 * tX + m\m11 * tY + m\m12 * tZ)+offY
v\RZ# = (m\m20 * tX + m\m21 * tY + m\m22 * tZ)+offZ
; v\TRX# = (m2\m00 * v\X + m2\m01 * v\Y + m2\m02 * v\Z)+mesh\x;offX
; v\TRY# = (m2\m10 * v\X + m2\m11 * v\Y + m2\m12 * v\Z)+mesh\y;offY
; v\TRZ# = (m2\m20 * v\X + m2\m21 * v\Y + m2\m22 * v\Z)+mesh\z;offZ
v\TRX# = (m2\m00 * v\X + m2\m10 * v\Y + m2\m20 * v\Z)+mesh\x;offX
v\TRY# = (m2\m01 * v\X + m2\m11 * v\Y + m2\m21 * v\Z)+mesh\y;offY
v\TRZ# = (m2\m02 * v\X + m2\m12 * v\Y + m2\m22 * v\Z)+mesh\z;offZ
; v\RNX# = (m2\m00 * v\NX + m2\m01 * v\NY + m2\m02 * v\NZ)
; v\RNY# = (m2\m10 * v\NX + m2\m11 * v\NY + m2\m12 * v\NZ)
; v\RnZ# = (m2\m20 * v\NX + m2\m21 * v\NY + m2\m22 * v\NZ)
v\RNX# = (m2\m00 * v\NX + m2\m10 * v\NY + m2\m20 * v\NZ)
v\RNY# = (m2\m01 * v\NX + m2\m11 * v\NY + m2\m21 * v\NZ)
v\RNZ# = (m2\m02 * v\NX + m2\m12 * v\NY + m2\m22 * v\NZ)
; ttz#=focus#/(v\rz+distance)
; v\sx=(XRes Shr 1)+v\RX*ttz
; v\sy=(YRes Shr 1)+v\RY*ttz
randomval=(mesh\state And nolighting)
cel=0
cel=(mesh\state And cellighting)

If randomval
v\largb = v\argb
Else
lightpoint(v,mesh\tnd,cel)
EndIf
;v\largb=v\argb
End Function

Function UpdateNormals(mesh)
m.TObject=Object.TObject(mesh)
tnd#=m\tnd
tnx#=0
tny#=0
tnz#=0
num#=0

For i=0 To m\NSurfaces-1

surf=PeekInt(m\Surfaces,i*4)
s.TSurface = Object.TSurface(surf)

For i2=0 To s\NTriangles-1

t.TTriangle = Object.TTriangle(PeekInt(s\Triangles,i2*4))
vrt1=t\vertex1
vrt2=t\vertex2
vrt3=t\vertex3

wx1# = VertexX#(surf,vrt1)
wy1# = VertexY#(surf,vrt1)
wz1# = VertexZ#(surf,vrt1)
wx2# = VertexX#(surf,vrt2)
wy2# = VertexY#(surf,vrt2)
wz2# = VertexZ#(surf,vrt2)
wx3# = VertexX#(surf,vrt3)
wy3# = VertexY#(surf,vrt3)
wz3# = VertexZ#(surf,vrt3)

Ax# = wx1-wx3
Ay# = wy1-wy3
Az# = wz1-wz3
Bx# = wx2-wx3
By# = wy2-wy3
Bz# = wz2-wz3

t\Nx# = (Ay * Bz - Az * By)
t\Ny# = (Az * Bx - Ax * Bz)
t\Nz# = (Ax * By - Ay * Bx)

v1.TVertex = Object.TVertex(GetVertex(surf,vrt1))
v2.TVertex = Object.TVertex(GetVertex(surf,vrt2))
v3.TVertex = Object.TVertex(GetVertex(surf,vrt3))

v1\ANX = v1\ANX + t\nx
v1\ANY = v1\ANY + t\ny
v1\ANZ = v1\ANZ + t\nz
v1\NormalNums = v1\NormalNums + 1

v2\ANX = v2\ANX + t\nx
v2\ANY = v2\ANY + t\ny
v2\ANZ = v2\ANZ + t\nz
v2\NormalNums = v2\NormalNums + 1

v3\ANX = v3\ANX + t\nx
v3\ANY = v3\ANY + t\ny
v3\ANZ = v3\ANZ + t\nz
v3\NormalNums = v3\NormalNums + 1

If absf(t\nx) > tnx
tnx=absf(t\nx)
EndIf
If absf(t\ny) > tny
tny=absf(t\ny)
EndIf
If absf(t\nz) > tnz
tnz=absf(t\nz)
EndIf

Next

For i2=0 To s\NVertices-1

v.TVertex = Object.TVertex(PeekInt(s\Vertices,i2*4))
v\NX = v\ANX / v\NormalNums
v\NY = v\ANY / v\NormalNums
v\NZ = v\ANZ / v\NormalNums

Next

Next
tnd = 1.0/Sqr(tnx*tnx+tny*tny+tnz*tnz)
m\tnd = tnd
For i=0 To m\NSurfaces-1

surf=PeekInt(m\Surfaces,i*4)
s.TSurface = Object.TSurface(surf)

For i2=0 To s\NVertices-1

v.TVertex = Object.TVertex(PeekInt(s\Vertices,i2*4))
v\NX = v\NX / tnx
v\NY = v\NY / tny
v\NZ = v\NZ / tnz

Next

Next
End Function


Function Rendering_Stuff___________()
End Function


Function PCTri(p0.point,p1.point,p2.point,bnk,tw,th)



Local tp.point

If p1\sy < p0\sy
tp=p0
p0=p1
p1=tp
EndIf

If p2\sy < p0\sy
tp=p0
p0=p2
p2=tp
EndIf

If p2\sy < p1\sy
tp=p1
p1=p2
p2=tp
EndIf

tz1# = 1.0/p0\z
tz2# = 1.0/p1\z
tz3# = 1.0/p2\z

x1#=p0\sx
y1#=p0\sy
r1#=p0\r
g1#=p0\g
b1#=p0\b
z1#=tz1
u1#=p0\u*tz1
v1#=p0\v*tz1
x2#=p1\sx
y2#=p1\sy
r2#=p1\r
g2#=p1\g
b2#=p1\b
z2#=tz2
u2#=p1\u*tz2
v2#=p1\v*tz2
x3#=p2\sx
y3#=p2\sy
r3#=p2\r
g3#=p2\g
b3#=p2\b
z3#=tz3
u3#=p2\u*tz3
v3#=p2\v*tz3
DebugLog u1
DebugLog v1
yd1#=1.0/Float(y2-y1)
yd2#=1.0/Float(y3-y1)
yd3#=1.0/Float(y3-y2)

xpos1#=x1
rpos1#=r1
gpos1#=g1
bpos1#=b1
zpos1#=z1
upos1#=u1
vpos1#=v1
xspeed1#=(Float(x2-x1)*yd1)
rspeed1#=(Float(r2-r1)*yd1)
gspeed1#=(Float(g2-g1)*yd1)
bspeed1#=(Float(b2-b1)*yd1)
zspeed1#=(Float(z2-z1)*yd1)
uspeed1#=(Float(u2-u1)*yd1)
vspeed1#=(Float(v2-v1)*yd1)

xpos2#=x1
rpos2#=r1
gpos2#=g1
bpos2#=b1
zpos2#=z1
upos2#=u1
vpos2#=v1
xspeed2#=(Float(x3-x1)*yd2)
rspeed1#=(Float(r3-r1)*yd2)
gspeed1#=(Float(g3-g1)*yd2)
bspeed1#=(Float(b3-b1)*yd2)
zspeed2#=(Float(z3-z1)*yd2)
uspeed2#=(Float(u3-u1)*yd2)
vspeed2#=(Float(v3-v1)*yd2)

For y=y1 To y2-1

If y >= 0 And y < yres

offset = (y*(xres Shl 2))
bankoff = y*current_buffer_pitch

If xpos1 < xpos2
scz#=zpos1#
scr#=rpos1#
scg#=gpos1#
scb#=bpos1#
scu#=upos1#
scv#=vpos1#
sx%=Int(xpos1-0.49)
fx%=Int(xpos2+0.49)-1
xd#=1.0/(Float(fx)-Float(sx))
sczs#=(zpos2-zpos1)*xd
scrs#=(rpos2-rpos1)*xd
scgs#=(gpos2-gpos1)*xd
scbs#=(bpos2-bpos1)*xd
scus#=(upos2-upos1)*xd
scvs#=(vpos2-vpos1)*xd
Else
scz#=zpos2#
scr#=rpos2#
scg#=gpos2#
scb#=bpos2#
scu#=upos2#
scv#=vpos2#
sx%=Int(xpos2-0.49)
fx%=Int(xpos1+0.49)-1
xd#=1.0/(Float(fx)-Float(sx))
sczs#=(zpos1-zpos2)*xd
scrs#=(rpos1-rpos2)*xd
scgs#=(gpos1-gpos2)*xd
scbs#=(bpos1-bpos2)*xd
scus#=(upos1-upos2)*xd
scvs#=(vpos1-vpos2)*xd
EndIf

For x=sx To fx

toffset = offset+(x Shl 2)
tz#=1.0/scz
If tz < PeekFloat(zbuffer,toffset)
PokeFloat zbuffer, toffset, tz
u%=(scu*tz) And tw
v%=(scv*tz) And th
rgb1=PeekInt(bnk,(v*(tw-1)+(u)) Shl 2)
r = ( ( rgb1 Shr 16 ) And $FF ) * (scr/255.0)
g = ( ( rgb1 Shr 8 ) And $FF ) * (scg/255.0)
b = ( rgb1 And $FF ) * (scb/255.0)
PokeInt current_buffer_bank,bankoff+(x*current_buffer_format), ( r Shl 16 ) + ( g Shl 8 ) + b

EndIf
scz=scz+sczs
scr=scr+scrs
scg=scg+scgs
scb=scb+scbs
scu=scu+scus
scv=scv+scvs

Next
EndIf
xpos1=xpos1+xspeed1
zpos1=zpos1+zspeed1
rpos1=rpos1+rspeed1
gpos1=gpos1+gspeed1
bpos1=bpos1+bspeed1
upos1=upos1+uspeed1
vpos1=vpos1+vspeed1
xpos2=xpos2+xspeed2
zpos2=zpos2+zspeed2
rpos2=rpos2+rspeed2
gpos2=gpos2+gspeed2
bpos2=bpos2+bspeed2
upos2=upos2+uspeed2
vpos2=vpos2+vspeed2

Next

xpos1#=x2
zpos1#=z2
rpos1#=r2
gpos1#=g2
bpos1#=b2
upos1#=u2
vpos1#=v2
xspeed1#=(Float(x3-x2)*yd3)
zspeed1#=(Float(z3-z2)*yd3)
rspeed1#=(Float(r3-r2)*yd3)
gspeed1#=(Float(g3-g2)*yd3)
bspeed1#=(Float(b3-b2)*yd3)
uspeed1#=(Float(u3-u2)*yd3)
vspeed1#=(Float(v3-v2)*yd3)

For y=y2 To y3-1

If y >= 0 And y < yres

offset = (y*(xres Shl 2))
bankoff = y*current_buffer_pitch

If xpos1 < xpos2
scz#=zpos1#
scr#=rpos1#
scg#=gpos1#
scb#=bpos1#
scu#=upos1#
scv#=vpos1#
xd#=1.0/(xpos2-xpos1)
sczs#=(zpos2-zpos1)*xd
scrs#=(rpos2-rpos1)*xd
scgs#=(gpos2-gpos1)*xd
scbs#=(bpos2-bpos1)*xd
scus#=(upos2-upos1)*xd
scvs#=(vpos2-vpos1)*xd
sx%=Int(xpos1-0.49)
fx%=Int(xpos2+0.49)-1
Else
scz#=zpos2#
scr#=rpos2#
scg#=gpos2#
scb#=bpos2#
scu#=upos2#
scv#=vpos2#
xd#=1.0/(xpos1-xpos2)
sczs#=(zpos1-zpos2)*xd
scrs#=(rpos1-rpos2)*xd
scgs#=(gpos1-gpos2)*xd
scbs#=(bpos1-bpos2)*xd
scus#=(upos1-upos2)*xd
scvs#=(vpos1-vpos2)*xd
sx%=Int(xpos2-0.49)
fx%=Int(xpos1+0.49)-1
EndIf

For x=sx To fx

toffset = offset+(x Shl 2)
If scz < PeekFloat(zbuffer,toffset)
PokeFloat zbuffer, toffset, scz
tz#=1.0/scz
rgb1=PeekInt(bnk,((((scv*tz) And H)*(w-1))+((scu*tz) And W)) Shl 2)
r = ( ( rgb1 Shr 16 ) And $FF ) * (scr*oneover255)
g = ( ( rgb1 Shr 8 ) And $FF ) * (scg*oneover255)
b = ( rgb1 And $FF ) * (scb*oneover255)
PokeInt current_buffer_bank,bankoff+(x*current_buffer_format), ( r Shl 16 ) + ( g Shl 8 ) + b
EndIf
scz=scz+sczs
scr=scr+scrs
scg=scg+scgs
scb=scb+scbs
scu=scu+scus
scv=scv+scvs

Next
EndIf
xpos1=xpos1+xspeed1
zpos1=zpos1+zspeed1
rpos1=rpos1+rspeed1
gpos1=gpos1+gspeed1
bpos1=bpos1+bspeed1
upos1=upos1+uspeed1
vpos1=vpos1+vspeed1
xpos2=xpos2+xspeed2
zpos2=zpos2+zspeed2
rpos2=rpos2+rspeed2
gpos2=gpos2+gspeed2
bpos2=bpos2+bspeed2
upos2=upos2+uspeed2
vpos2=vpos2+vspeed2

Next

End Function

Function DrawTriRGB(p0.point,p1.point,p2.point)
polycount = polycount + 1
; Local p0.point = New point
; Local p1.point = New point
; Local p2.point = New point

; p0\sx=x1
; p0\sy=y1
; p0\z=z1
; p0\r=( ARGB1 Shr 16 ) And $FF
; p0\g=( ARGB1 Shr 8 ) And $FF
; p0\b=( ARGB1 ) And $FF
; p1\sx=x2
; p1\sy=y2
; p1\z=z2
; p1\r=( ARGB2 Shr 16 ) And $FF
; p1\g=( ARGB2 Shr 8 ) And $FF
; p1\b=( ARGB2 ) And $FF
; p2\sx=x3
; p2\sy=y3
; p2\z=z3
; p2\r=( ARGB3 Shr 16 ) And $FF
; p2\g=( ARGB3 Shr 8 ) And $FF
; p2\b=( ARGB3 ) And $FF

Local tp.point

If p1\sy < p0\sy
tp=p0
p0=p1
p1=tp
EndIf

If p2\sy < p0\sy
tp=p0
p0=p2
p2=tp
EndIf

If p2\sy < p1\sy
tp=p1
p1=p2
p2=tp
EndIf

x1#=p0\sx
y1=p0\sy
r1=p0\r
g1=p0\g
b1=p0\b
z1#=p0\z
x2#=p1\sx
y2=p1\sy
r2=p1\r
g2=p1\g
b2=p1\b
z2#=p1\z
x3#=p2\sx
y3=p2\sy
r3=p2\r
g3=p2\g
b3=p2\b
z3#=p2\z

xd1# = x2-x1
xd2# = x3-x1
xd3# = x3-x2

rd1# = r2-r1
rd2# = r3-r1
rd3# = r3-r2

gd1# = g2-g1
gd2# = g3-g1
gd3# = g3-g2

bd1# = b2-b1
bd2# = b3-b1
bd3# = b3-b2

zd1# = z2-z1
zd2# = z3-z1
zd3# = z3-z2

ratio1# = 1.0/(y2-y1)
ratio2# = 1.0/(y3-y1)
ratio3# = 1.0/(y3-y2)

xi1# = (xd1)*ratio1
xi2# = (xd2)*ratio2
xi3# = (xd3)*ratio3

ri1# = (rd1)*ratio1
ri2# = (rd2)*ratio2
ri3# = (rd3)*ratio3

gi1# = (gd1)*ratio1
gi2# = (gd2)*ratio2
gi3# = (gd3)*ratio3

bi1# = (bd1)*ratio1
bi2# = (bd2)*ratio2
bi3# = (bd3)*ratio3

zi1# = (zd1)*ratio1
zi2# = (zd2)*ratio2
zi3# = (zd3)*ratio3

lx#=x1
rx#=x1
lr#=r1
rr#=r1
lg#=g1
rg#=g1
lb#=b1
rb#=b1
lz#=z1
rz#=z1

For y=y1 To y2-1

DrawScanLinergb(lx,rx,lr,rr,lg,rg,lb,rb,lz,rz,y)

lx=lx+xi2
rx=rx+xi1
lr=lr+ri2
rr=rr+ri1
lg=lg+gi2
rg=rg+gi1
lb=lb+bi2
rb=rb+bi1
lz=lz+zi2
rz=rz+zi1

Next

rx=x2
rr=r2
rg=g2
rb=b2
rz=z2

For y=y2 To y3-1

DrawScanLinergb(lx,rx,lr,rr,lg,rg,lb,rb,lz,rz,y)

lx=lx+xi2
rx=rx+xi3
lr=lr+ri2
rr=rr+ri3
lg=lg+gi2
rg=rg+gi3
lb=lb+bi2
rb=rb+bi3
lz=lz+zi2
rz=rz+zi3

Next
; Delete p0
; Delete p1
; Delete p2

End Function

Function drawscanlinergb(x1,x2,r1,r2,g1,g2,b1,b2,z1#,z2#,y)

Local tempy#
Local temp

If y >= 0 And y < yres

offset = (y*(xres Shl 2))
bankoff = y*GFXBufferp
If x2 < x1
temp=x1
x1=x2
x2=temp
temp=r1
r1=r2
r2=temp
temp=g1
g1=g2
g2=temp
temp=b1
b1=b2
b2=temp
tempy=z1
z1=z2
z2=tempy
EndIf

r#=r1
g#=g1
b#=b1
z#=z1

xd#=1.0/(x2-x1)

ri#=(r2-r1)*xd
gi#=(g2-g1)*xd
bi#=(b2-b1)*xd
zi#=(z2-z1)*xd

If x1 < 0
x1=-x1
r=r+ri*x1
g=g+gi*x1
b=b+bi*x1
z=z+zi*x1
x1 = 0
EndIf
If x2 > xres-1 x2=xres-1
x2=x2-1
For x=x1 To x2
toffset = offset+(x Shl 2)
If z < PeekFloat(zbuffer,toffset)
myrgb=( r Shl 16 ) + ( g Shl 8 ) + b
PokeFloat zbuffer, toffset, z
WritePixelFast x,y,myrgb
; PokeInt GFXBuffer,bankoff+(x*GFXBufferf),$FF;myrgb
EndIf

r=r+ri
g=g+gi
b=b+bi
z=z+zi
Next

EndIf

End Function

Function backfacecull(p0.point,p1.point,p2.point)

vx1#= p0\sx-p1\sx
vy1#= p0\sy-p1\sy
vx2#= p2\sx-p1\sx
vy2#= p2\sy-p1\sy
normal=vx1*vy2-vx2*vy1
If normal < 0 Return 1

End Function

Function Trionscreen(p0.point,p1.point,p2.point)

; If (p0\sx>=1 And p0\sy>=1) Or (p1\sx>=1 And p1\sy>=1) Or (p2\sx>=1 And p2\sy>=1) Return 1
; If p0\sy>=1 Or p1\sy>=1 Or p2\sy>=1 Return 1
; If (p0\sx>=0 And p0\sx<=xres-1 And p0\sy>=0 And p0\sy<=yres-1) Return 1
; If (p1\sx>=0 And p1\sx<=xres-1 And p1\sy>=0 And p1\sy<=yres-1) Return 1
; If (p2\sx>=0 And p2\sx<=xres-1 And p2\sy>=0 And p2\sy<=yres-1) Return 1

;
; If p0\sx<=xres-2 Or p1\sx<=xres-2 Or p2\sx<=xres-2 Return 1
; If p0\sy<=yres-2 Or p1\sy<=yres-2 Or p2\sy<=yres-2 Return 1
Return 1

End Function


Function ZClipping_Stuff____________()
End Function

Function triz(t_h,tsurf,vrt1,vrt2,vrt3,entbfc)

t.TTriangle = Object.TTriangle(t_h)
s.Tsurface = Object.TSurface(tsurf)
tex_h=s\texture
;TriangleVertex(t_h,0)
; vrt2=t\vertex2;TriangleVertex(t_h,1)
; vrt3=t\vertex3;TriangleVertex(t_h,2)
v1.TVertex = Object.TVertex(GetVertex(tsurf,vrt1))
v2.TVertex = Object.TVertex(GetVertex(tsurf,vrt2))
v3.TVertex = Object.TVertex(GetVertex(tsurf,vrt3))
pnt0\x = v1\rx;VertexRX(tsurf,vrt1)
pnt0\y = v1\ry;VertexRY(tsurf,vrt1)
pnt0\z = v1\rz;VertexRZ(tsurf,vrt1)
pnt0\nx = v1\rnx;VertexRNX(tsurf,vrt1)
pnt0\ny = v1\rny;VertexRNY(tsurf,vrt1)
pnt0\nz = v1\rnz;VertexRNZ(tsurf,vrt1)
pnt0\u = t\u1
pnt0\v = t\v1
ARGB1 = v1\largb;VertexARGB(tsurf,vrt1)
pnt0\r = ( ARGB1 Shr 16 ) And $FF
pnt0\g = ( ARGB1 Shr 8 ) And $FF
pnt0\b = ( ARGB1 ) And $FF


pnt1\x = v2\rx;VertexRX(tsurf,vrt1)
pnt1\y = v2\ry;VertexRY(tsurf,vrt1)
pnt1\z = v2\rz;VertexRZ(tsurf,vrt1)
pnt1\nx = v2\rnx;VertexRNX(tsurf,vrt1)
pnt1\ny = v2\rny;VertexRNY(tsurf,vrt1)
pnt1\nz = v2\rnz;VertexRNZ(tsurf,vrt1)
ARGB2 = v2\largb;VertexARGB(tsurf,vrt1)
pnt1\u = t\u2
pnt1\v = t\v2
pnt1\r = ( ARGB2 Shr 16 ) And $FF
pnt1\g = ( ARGB2 Shr 8 ) And $FF
pnt1\b = ( ARGB2 ) And $FF

pnt2\x = v3\rx;VertexRX(tsurf,vrt1)
pnt2\y = v3\ry;VertexRY(tsurf,vrt1)
pnt2\z = v3\rz;VertexRZ(tsurf,vrt1)
pnt2\nx = v3\rnx;VertexRNX(tsurf,vrt1)
pnt2\ny = v3\rny;VertexRNY(tsurf,vrt1)
pnt2\nz = v3\rnz;VertexRNZ(tsurf,vrt1)
ARGB3 = v3\largb;VertexARGB(tsurf,vrt1)
pnt2\u = t\u3
pnt2\v = t\v3
pnt2\r = ( ARGB3 Shr 16 ) And $FF
pnt2\g = ( ARGB3 Shr 8 ) And $FF
pnt2\b = ( ARGB3 ) And $FF

clipmode = clipz(pnt0,pnt1,pnt2)

Select clipmode

Case 0 ; 000

project pnt0
project pnt1
project pnt2

drawtri pnt0,pnt1,pnt2,tex_h,entbfc

Case 1 ; 001

clip_z pnt0,pnt2,tpnt0
clip_z pnt1,pnt2,tpnt1
project pnt0
project pnt1
drawtri pnt0,pnt1,tpnt1,tex_h,entbfc
drawtri tpnt1,tpnt0,pnt0,tex_h,entbfc

Case 2 ; 010

clip_z pnt0,pnt1,tpnt0
clip_z pnt2,pnt1,tpnt1
project pnt0
project pnt2
drawtri pnt0,tpnt0,tpnt1,tex_h,entbfc
drawtri tpnt1,pnt2,pnt0,tex_h,entbfc

Case 3 ; 011

clip_z pnt0,pnt1,tpnt0
clip_z pnt0,pnt2,tpnt1
project pnt0
drawtri pnt0,tpnt0,tpnt1,tex_h,entbfc

Case 4 ; 100

clip_z pnt1,pnt0,tpnt0
clip_z pnt2,pnt0,tpnt1
project pnt1
project pnt2
drawtri tpnt0,pnt1,pnt2,tex_h,entbfc
drawtri pnt2,tpnt1,tpnt0,tex_h,entbfc

Case 5 ; 101

clip_z pnt1,pnt0,tpnt0
clip_z pnt1,pnt2,tpnt1
project pnt1
drawtri tpnt0,pnt1,tpnt1,tex_h,entbfc

Case 6 ; 110

clip_z pnt2,pnt0,tpnt0
clip_z pnt2,pnt1,tpnt1
project pnt2
drawtri tpnt0,tpnt1,pnt2,tex_h,entbfc

Case 7

Return

End Select

End Function

Function clip_z(p0.POINT, p1.POINT, c.POINT)
Local ratio#

ratio# = (Clip_Point - p1\z) / (p0\z - p1\z)
c\sx = 0
c\sy = 0
c\nx = p1\nx + ratio * (p0\nx - p1\nx)
c\ny = p1\ny + ratio * (p0\ny - p1\ny)
c\nz = p1\ny + ratio * (p0\nz - p1\nz)
c\x = p1\x + ratio * (p0\x - p1\x)
c\y = p1\y + ratio * (p0\y - p1\y)
c\z = Clip_Point
c\r = p1\r + ratio * (p0\r - p1\r)
c\g = p1\g + ratio * (p0\g - p1\g)
c\b = p1\b + ratio * (p0\b - p1\b)
; sc\a = p1\a + ratio * (p0\a - p1\a)
c\u = p1\u + ratio * (p0\u - p1\u)
c\v = p1\v + ratio * (p0\v - p1\v)
;etc

project(c)

End Function

Function project(p.POINT)
Local oz#
; If p\z >= Clip_Point Then
oz# = focus / (p\z#+distance)
p\sx = hxres+((p\x#) * oz#)
p\sy = hyres+((p\y#) * oz#)
; End If
End Function

Function DrawTri(p0.point,p1.point,p2.point,tex_h,entbfc)

If Not Trionscreen(p0,p1,p2) Return
If entbfc
If backfacecull(p0,p1,p2) Return
EndIf
If tex_h
tex.Ttexture = Object.TTexture(tex_h)
bnk = tex\originalpixels
tw#=tex\originalw
th#=tex\originalh
DebugLog p0\u
DebugLog p0\v
p0\u=p0\u*(tw-1)
p0\v=p0\v*(th-1)

p1\u=p1\u*(tw-1)
p1\v=p1\v*(th-1)

p2\u=p2\u*(tw-1)
p2\v=p2\v*(th-1)
PCTri p0,p1,p2,bnk,tw,th
Else
drawtrirgb p0,p1,p2
EndIf
End Function

Function clipz(p0.POINT,p1.POINT,p2.POINT)

p0b=0
p1b=0
p2b=0

If p0\z < Clip_Point p0b = 1
If p1\z < Clip_Point p1b = 1
If p2\z < Clip_Point p2b = 1

Return ( p0b Shl 2 ) + ( p1b Shl 1 ) + p2b

End Function

Function Normalize(dest.vector, v.vector)
   Scale# = 1.0 / VectorLength(v)
  dest\x# = v\x# * Scale#
  dest\y# = v\y# * Scale#
  dest\z# = v\z# * Scale#
End Function

Function Lighting_Stuff______________()
End Function

Function lightpoint(v.TVertex,tnd#,cel=0)

nx#=v\rnx
ny#=v\rny
nz#=v\rnz

ttr#=0
ttg#=0
ttb#=0
px#=v\trx
py#=v\try
pz#=v\trz
r#=(v\ARGB Shr 16) And $FF
g#=(v\ARGB Shr 8) And $FF
b#=(v\ARGB) And $FF
pointlen#=(px*px+py*py+pz*pz)
For l.TLight = Each TLight
dx#=l\x-px
dy#=l\y-py
dz#=l\z-pz
dist#=l\maxdist-Abs(dx*dx+dy*dy+dz*dz)

lvx# = Float(px# - l\x#)
lvy# = Float(py# - l\y#)
lvz# = Float(pz# - l\z#)
lmag# = Float(Sqr#(lvx*lvx+lvy*lvy+lvz*lvz))

ls# = Float(lvx# * nx#) + Float(lvy# * ny#) + Float(lvz# * nz#)
ls# = ls# / lmag#
If cel > 0
If ls# > -0.1
ls# = 0.0
Else
ls# = -1
EndIf
EndIf
ls# = ls# * (dist#/(l\maxdist))

If dist < 0
ls = 0
EndIf
If ls# < 0
ls# = -ls#
Else
ls# = 0
EndIf
ttr = ttr + Float(ls# * r);* 255
ttg = ttg + Float(ls# * g);* 255
ttb = ttb + Float(ls# * b);* 255
.nextlight
Next
If ttr < 0 ttr = 0
If ttg < 0 ttg = 0
If ttb < 0 ttb = 0
If ttr > 255 ttr = 255
If ttg > 255 ttg = 255
If ttb > 255 ttb = 255
.endlight
v\LARGB = ( ttr Shl 16 ) + ( ttg Shl 8 ) + ttb
; r=ttr
; g=ttg
; b=ttb

End Function

Function DotProduct#(v1.vector, v2.vector)
  Return ((v1\x# * v2\x#) + (v1\y# * v2\y#) + (v1\z# * v2\z#))
End Function

Function GetAngle#(NormalVector.vector, LightVector.vector)
  floatval#=DotProduct#(NormalVector, LightVector)
  Return ACos#( (floatval#*floatval#) / (VectorLength#(NormalVector) * VectorLength#(LightVector)) )
End Function

Function VectorLength#(v.vector)
  absvx#=v\x
  absvy#=v\y
  absvz#=v\z
;  absvx#=Absf#(v\x)
;  absvy#=Absf#(v\y)
;  absvz#=Absf#(v\z)
  Return ((absvx# * absvx#) + (absvy# * absvy#) + (absvz# * absvz#))
End Function

Function Misc_Stuff_________________()
End Function

Function LockIt()
LockBuffer
current_buffer = GraphicsBuffer()
; current_buffer_bank = LockedPixels()
; current_buffer_pitch = LockedPitch()
; current_buffer_format = LockedFormat()
End Function

Function UnlockIt()
UnlockBuffer current_buffer
End Function

Function CreateCube(surf,xpos#,ypos#,zpos#, xsize#,ysize#,zsize#,tex=0,texcoordscale#=0)

xsize = xsize * .5
ysize = ysize * .5
zsize = zsize * .5


vert1 = AddVertex(surf,-xsize#+xpos,ysize#+ypos,zsize#+zpos,$FFFFFF)
vert2 = AddVertex(surf,xsize#+xpos,ysize#+ypos,zsize#+zpos,$FF0000)
vert3 = AddVertex(surf,xsize#+xpos,-ysize#+ypos,zsize#+zpos,$00FF00)
vert4 = AddVertex(surf,-xsize#+xpos,-ysize#+ypos,zsize#+zpos,$0000FF)

vert5 = AddVertex(surf,-xsize#+xpos,ysize#+ypos,-zsize#+zpos,$FFFF00)
vert6 = AddVertex(surf,xsize#+xpos,ysize#+ypos,-zsize#+zpos,$FF00FF)
vert7 = AddVertex(surf,xsize#+xpos,-ysize#+ypos,-zsize#+zpos,$00FFFF)
vert8 = AddVertex(surf,-xsize#+xpos,-ysize#+ypos,-zsize#+zpos,$000000)



Tri = AddTriangle(surf,vert3,vert2,vert1)
Tri2 = AddTriangle(surf,vert1,vert4,vert3)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

Tri = AddTriangle(surf,vert5,vert6,vert7)
Tri2 = AddTriangle(surf,vert7,vert8,vert5)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

Tri = AddTriangle(surf,vert5,vert1,vert2)
Tri2 = AddTriangle(surf,vert2,vert6,vert5)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

Tri = AddTriangle(surf,vert4,vert8,vert7)
Tri2 = AddTriangle(surf,vert7,vert3,vert4)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

Tri = AddTriangle(surf,vert1,vert5,vert8)
Tri2 = AddTriangle(surf,vert8,vert4,vert1)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

Tri = AddTriangle(surf,vert7,vert6,vert2)
Tri2 = AddTriangle(surf,vert2,vert3,vert7)
;TriangleTexture Tri, tex
;TriangleTexture Tri2, tex
;TriangleTexCoords Tri, 0,0,texcoordscale#,0,texcoordscale#,texcoordscale#
;TriangleTexCoords Tri2, texcoordscale#,texcoordscale#,0,texcoordscale#,0,0

End Function

Function CreateTorus(surf,torrad#=50,torwidth#=10,segments=20,sides=20)

FATSTEP#=360.0/sides
DEGSTEP#=360.0/segments

radius#=0
tx#=0
ty#=0
tz#=0

fat#=0
Repeat
radius# = torrad# + (torwidth#)*Sin#(fat)
deg#=0
tz#=torwidth*Cos#(fat)
Repeat
tx#=radius*Cos#(deg)
ty#=radius*Sin#(deg)
AddVertex surf,tx,ty,tz,$ffffff
deg=deg+DEGSTEP
Until deg>=360
fat=fat+FATSTEP
Until fat>=360

For vert=0 To sides-1
For vert2=0 To segments-1
;(sides)*(segments-1)-1
v0=(vert*segments)+vert2
If vert = sides-1
v1=(0)+vert2;+segments
Else
v1=((vert+1)*segments)+vert2;+segments
EndIf
If vert2 = segments-1
v2=(vert*segments);vert+1+segments
Else
v2=(vert*segments)+vert2+1;vert+1+segments
EndIf
If vert = sides-1
If vert2 = segments-1
v3=(0);vert+1+segments
Else
v3=(0)+vert2+1;vert+1+segments
EndIf
Else
If vert2 = segments-1
v3=((vert+1)*segments);vert+1+segments
Else
v3=((vert+1)*segments)+vert2+1;vert+1+segments
EndIf
EndIf

If v1>=(segments*sides) Then v1=v1-(segments*sides)
If v2>=(segments*sides) Then v2=v2-(segments*sides)
If v3>=(segments*sides) Then v3=v3-(segments*sides)

cu1#=(Float(vert)/Float(sides))/Float(segments)
cv1#=(Float(vert) Mod Float(sides))/Float(sides)
cu2#=(Float(vert+segments)/Float(sides))/Float(segments)
cv2#=(Float(vert+segments) Mod Float(sides))/Float(sides)
cu3#=(Float(vert+1)/Float(sides))/Float(segments)
cv3#=(Float(vert+1) Mod Float(sides))/Float(sides)
cu4#=(Float(vert+1+segments)/Float(sides))/Float(segments)
cv4#=(Float(vert+1+segments) Mod Float(sides))/Float(sides)

t1=AddTriangle(surf,v3,v2,v0)
t2=AddTriangle(surf,v0,v1,v3)
; triangletexcoords surf,t1,cu1,cv1,cu2,cv2,cu3,cv3
; triangletexcoords surf,t1,cu2,cv2,cu4,cv4,cu3,cv3
Next
Next

If blah = 1
For vert=sides*(segments-1)-1 To sides*segments-1
v0=vert
v1=vert+segments
If v1 > (sides*segments-1) v1 = v1 - sides*segments
v2=vert+1
If v2 > (sides*segments-1) v2 = v2 - sides*segments
v3=vert+1+segments
If v3 > (sides*segments-1) v3 = v3 - sides*segments

If v1>=(segments*sides) Then v1=v1-(segments*sides)
If v2>=(segments*sides) Then v2=v2-(segments*sides)
If v3>=(segments*sides) Then v3=v3-(segments*sides)

cu1#=(Float(vert)/Float(sides))/Float(segments)
cv1#=(Float(vert) Mod Float(sides))/Float(sides)
cu2#=(Float(vert+segments)/Float(sides))/Float(segments)
cv2#=(Float(vert+segments) Mod Float(sides))/Float(sides)
cu3#=(Float(vert+1)/Float(sides))/Float(segments)
cv3#=(Float(vert+1) Mod Float(sides))/Float(sides)
cu4#=(Float(vert+1+segments)/Float(sides))/Float(segments)
cv4#=(Float(vert+1+segments) Mod Float(sides))/Float(sides)

t1=AddTriangle(surf,v3,v2,v0)
t2=AddTriangle(surf,v0,v1,v3)
; triangletexcoords surf,t1,cu1,cv1,cu2,cv2,cu3,cv3
; triangletexcoords surf,t1,cu2,cv2,cu4,cv4,cu3,cv3

Next
EndIf

End Function

Function absf#(fval#)

If fval < 0.0 Return 0-fval
Return fval

End Function

Function loadascii(fname$)
;checkinit3d()
Local p1.point;3d
Local p2.point;3d
Local p3.point;3d
Local file
Local fline$
Local tline$
Local char$
Local crap$
Local snum
Local fnum
Local pnum
Local v1
Local v2
Local v3
Local v4
Local v5
;e3d.entity3d = New entity3d
;e3d\sx# = 1
;e3d\sy# = 1
;e3d\sz# = 1
;e3d\r = 255
;e3d\g = 255
;e3d\b = 255
;e3d\a# = 1
mesh=createobject(0,0,0)
m.TObject = Object.TObject(mesh)

file = ReadFile(fname$)
If file = 0 Then RuntimeError "Cannot Load Model " + Chr$(34) + fname$ + Chr$(34) + " !"
crap$ = ReadLine$(file)
crap$ = ReadLine$(file)
fline$ = ReadLine$(file)
snum = Int(Right$(fline$,Len(fline$) - 8))
;e3d\s = CreateBank(snum Shl 2)
For i = 0 To snum - 1
surf=CreateSurface(mesh)
s3d.Tsurface = Object.Tsurface(surf)
crap$ = ReadLine$(file)
pnum = Int(ReadLine$(file))
; s3d\p = CreateBank(pnum Shl 2)
For j = 0 To pnum - 1
; p3d.point3d = New point3d
; p3d\eid = Handle(e3d)
fline$ = ReadLine$(file)
tline$ = ""
v1 = False
v2 = False
v3 = False
v4 = False
v5 = False
For k = 3 To Len(fline$) - 2
char$ = Mid$(fline$,k,1)
If char$ = " "
If Not v5
If v4
v# = Float(tline$)
v5 = True
ElseIf v3
u# = Float(tline$)
v4 = True
ElseIf v2
z# = Float(tline$)
v3 = True
ElseIf v1
y# = Float(tline$)
v2 = True
Else
x# = Float(tline$)
v1 = True
EndIf
tline$ = ""
EndIf
Else
tline$ = tline$ + char$
EndIf
Next
; PokeInt s3d\p,j Shl 2,Handle(p3d)
AddVertex surf,x,y,z,$FFFFFF,u,v
Next
crap$ = ReadLine$(file)
For j = 0 To pnum - 1
p3d.TVertex = Object.TVertex(getvertex(surf,j))
fline$ = ReadLine$(file) + " "
tline$ = ""
v1 = False
v2 = False
v3 = False
For k = 1 To Len(fline$)
char$ = Mid$(fline$,k,1)
If char$ = " "
If Not v3
If v2
p3d\nz# = Float(tline$)
v3 = True
ElseIf v1
p3d\ny# = Float(tline$)
v2 = True
Else
p3d\nx# = Float(tline$)
v1 = True
EndIf
tline$ = ""
EndIf
Else
tline$ = tline$ + char$
EndIf
Next
Next
fnum = Int(ReadLine$(file))
; s3d\f = CreateBank(fnum Shl 2)
For j = 0 To fnum - 1
; f3d.face3d = New face3d
; f3d\eid = Handle(e3d)
fline$ = ReadLine$(file)
tline$ = ""
v1 = False
v2 = False
v3 = False
For k = 3 To Len(fline$) - 3
char$ = Mid$(fline$,k,1)
If char$ = " "
If Not v3
If v2
tv3% = Int(tline$)
v3 = True
ElseIf v1
tv2% = Int(tline$)
v2 = True
Else
tv1% = Int(tline$)
v1 = True
EndIf
EndIf
tline$ = ""
Else
tline$ = tline$ + char$
EndIf
AddTriangle surf,tv1,tv2,tv3
Next
; p1 = Object.point3d(f3d\v1)
; p2 = Object.point3d(f3d\v2)
; p3 = Object.point3d(f3d\v3)
; f3d\nx# = Float(p1\nx# + p2\nx# + p3\nx#) / 3.0
; f3d\ny# = Float(p1\ny# + p2\ny# + p3\ny#) / 3.0
; f3d\nz# = Float(p1\nz# + p2\nz# + p3\nz#) / 3.0
; PokeInt s3d\f,j Shl 2,Handle(f3d)
Next
; PokeInt e3d\s,i Shl 2,Handle(s3d)
Next
CloseFile file
Return mesh
End Function

Have fun =)
« Last Edit: July 21, 2007 by Shockwave »

Offline DruggedBunny

  • ZX 81
  • *
  • Posts: 8
  • Karma: 2
    • View Profile
Re: 3D Lib
« Reply #1 on: September 05, 2006 »
Nice, thanks for posting.

Quote
It works in B2D, B3D, and B+

(Just to save anyone confusion, the demo is BlitzPlus only!)

Very cool, though, much appreciated.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: 3D Lib
« Reply #2 on: September 05, 2006 »
This code is cool work.
:) Relaseing it obvoiusly means that you have something new up your sleeve? :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline RageSnax

  • C= 64
  • **
  • Posts: 25
  • Karma: 4
    • View Profile
Re: 3D Lib
« Reply #3 on: September 05, 2006 »
V cool n thanks

to change for bliz3d look in test.bb for

Code: [Select]
WaitEvent(MilliSecs()-tmillis) ; B+
;TimeWaste(MilliSecs()-tmillis) ; B2D/B3D

remark waitevent
unremark timewaste

Niceone B A :D

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: 3D Lib
« Reply #4 on: September 05, 2006 »
Very cool, ran nice and smooth on both my PCs  ;D
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: 3D Lib
« Reply #5 on: September 05, 2006 »
Quality stuff BA! Welldone and thanks for sharing dude :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Blitz Amateur

  • Atari ST
  • ***
  • Posts: 243
  • Karma: 13
    • View Profile
Re: 3D Lib
« Reply #6 on: September 05, 2006 »
RageSnax, thanks for pointing that out. I forgot to mention that in the first post :)




@Shockwave, Yeah. I'm going to start working on writing an FB 3D engine just for the fun of it =) There were parts of this system that weren't fast enough for my liking, and I didn't feel like trying to mod it that much. So I'm going to just start over. Although, progress will be slow since school just started.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: 3D Lib
« Reply #7 on: September 05, 2006 »

@Shockwave, Yeah. I'm going to start working on writing an FB 3D engine just for the fun of it =) There were parts of this system that weren't fast enough for my liking, and I didn't feel like trying to mod it that much. So I'm going to just start over. Although, progress will be slow since school just started.

A DBF intro or demo would be a cool thing :) Looking forward to seeing how your engine converts to FB.
I have experienced something like a 300% speed increase so I am sure that you will find the transition fascinating and thrilling just like I have.
Shockwave ^ Codigos
Challenge Trophies Won: