Declare.l CreateShape(x.l, y.l, Shape.l, Texture.l)
#Rad = #PI / 180
Global MapWidth.l
Global Inside.f
Global ASteps.f
MapWidth.l = 100
Inside.f = (32 * MapWidth.l) / (2 * #PI)
ASteps.f = 360 / (32 * MapWidth.l)
Structure s_Vertex
;vertice
px.f
py.f
pz.f
;Normal
nx.f
ny.f
nz.f
;UV
u.f
v.f
EndStructure
Global Dim Vertices.s_Vertex(7)
Structure s_Face
face.w[3]
EndStructure
Dim Triangles.s_Face(4)
Procedure CalcNormals(P1, P2, P3, Negative.b)
If Negative
Vertices(P1)\nx = ((Vertices(P3)\py - Vertices(P2)\py) * (Vertices(P2)\pz - Vertices(P1)\pz)) - ((Vertices(P3)\pz - Vertices(P2)\pz) * (Vertices(P2)\py - Vertices(P1)\py))
Vertices(P1)\ny = ((Vertices(P3)\pz - Vertices(P2)\pz) * (Vertices(P2)\px - Vertices(P1)\px)) - ((Vertices(P3)\px - Vertices(P2)\px) * (Vertices(P2)\pz - Vertices(P1)\pz))
Vertices(P1)\nz = ((Vertices(P3)\px - Vertices(P2)\px) * (Vertices(P2)\py - Vertices(P1)\py)) - ((Vertices(P3)\py - Vertices(P2)\py) * (Vertices(P2)\px - Vertices(P1)\px))
Else
Vertices(P1)\nx = ((Vertices(P1)\py - Vertices(P2)\py) * (Vertices(P2)\pz - Vertices(P3)\pz)) - ((Vertices(P1)\pz - Vertices(P2)\pz) * (Vertices(P2)\py - Vertices(P3)\py))
Vertices(P1)\ny = ((Vertices(P1)\pz - Vertices(P2)\pz) * (Vertices(P2)\px - Vertices(P3)\px)) - ((Vertices(P1)\px - Vertices(P2)\px) * (Vertices(P2)\pz - Vertices(P3)\pz))
Vertices(P1)\nz = ((Vertices(P1)\px - Vertices(P2)\px) * (Vertices(P2)\py - Vertices(P3)\py)) - ((Vertices(P1)\py - Vertices(P2)\py) * (Vertices(P2)\px - Vertices(P3)\px))
EndIf
Length.f = Sqr( Vertices(P1)\px * Vertices(P1)\px + Vertices(P1)\ny * Vertices(P1)\ny + Vertices(P1)\nz * Vertices(P1)\nz)
If Length = 0 : Length = 1 : EndIf
; Vertices(P1)\nx / Length
; Vertices(P1)\ny / Length
; Vertices(P1)\nz / Length
Vertices(P2)\nx = Vertices(P1)\nx : Vertices(P3)\nx = Vertices(P1)\nx
Vertices(P2)\ny = Vertices(P1)\ny : Vertices(P3)\ny = Vertices(P1)\ny
Vertices(P2)\nz = Vertices(P1)\nz : Vertices(P3)\nz = Vertices(P1)\nz
EndProcedure
Procedure CalcNormals2(*P1.s_Vertex, *P2.s_Vertex, *P3.s_Vertex, Negative.b)
If Negative
*P1\nx = ((*P3\py - *P2\py) * (*P2\pz - *P1\pz)) - ((*P3\pz - *P2\pz) * (*P2\py - *P1\py))
*P1\ny = ((*P3\pz - *P2\pz) * (*P2\px - *P1\px)) - ((*P3\px - *P2\px) * (*P2\pz - *P1\pz))
*P1\nz = ((*P3\px - *P2\px) * (*P2\py - *P1\py)) - ((*P3\py - *P2\py) * (*P2\px - *P1\px))
Else
*P1\nx = ((*P1\py - *P2\py) * (*P2\pz - *P3\pz)) - ((*P1\pz - *P2\pz) * (*P2\py - *P3\py))
*P1\ny = ((*P1\pz - *P2\pz) * (*P2\px - *P3\px)) - ((*P1\px - *P2\px) * (*P2\pz - *P3\pz))
*P1\nz = ((*P1\px - *P2\px) * (*P2\py - *P3\py)) - ((*P1\py - *P2\py) * (*P2\px - *P3\px))
EndIf
Length.f = Sqr( *P1\px * *P1\px + *P1\ny * *P1\ny + *P1\nz * *P1\nz)
If Length = 0 : Length = 1 : EndIf
; Vertices(P1)\nx / Length
; Vertices(P1)\ny / Length
; Vertices(P1)\nz / Length
*P2\nx = *P1\nx : *P3\nx = *P1\nx
*P2\ny = *P1\ny : *P3\ny = *P1\ny
*P2\nz = *P1\nz : *P3\nz = *P1\nz
EndProcedure
InitEngine3D()
InitSprite()
InitSprite3D()
InitKeyboard()
InitMouse()
ScreenW = 640 ;800
ScreenH = 480;600
BitDepth = 16 ;This should be changed in settings
ProgramName.s = "tD Racers"
SpriteMode = 0 ; #PB_Sprite_Memory ;This should be changed in settings
;Allows us to use PNG files!
;UsePNGImageDecoder()
OpenScreen(ScreenW, ScreenH, BitDepth, ProgramName.s)
;Add3DArchive("Data\", #PB_3DArchive_FileSystem)
Add3DArchive("Data3\", #PB_3DArchive_FileSystem)
Tid = LoadTexture(#PB_Any, "ship.png")
Mid = CreateMaterial(#PB_Any, TextureID(Tid))
MaterialBlendingMode (Mid, #PB_Material_AlphaBlend | #PB_Material_Add)
;MaterialAmbientColor(Mid, RGB(0, 0, 0))
MaterialFilteringMode(Mid, #PB_Material_None)
;MaterialSpecularColor(Mid, RGB(128, 128, 128))
;MaterialAmbientColor(Mid, RGB(128,128,128))
;DisableMaterialLighting(Mid, 1)
MaterialShadingMode(Mid, #PB_Material_Phong)
Ret_Mesh = CreateMesh(#PB_Any, 6)
Vertices(0)\px = 0 -32
Vertices(0)\py = 0 -32
Vertices(0)\pz = 100
Vertices(0)\u = 0.5
Vertices(0)\v = 0.125
Vertices(1)\px = 48 -32
Vertices(1)\py = 0 -32
Vertices(1)\pz = 100
Vertices(1)\u = 0.5 ;1
Vertices(1)\v = 0.5
Vertices(2)\px = 48 -32
Vertices(2)\py = 64 -32
Vertices(2)\pz = 100
Vertices(2)\u = 0; 1
Vertices(2)\v = 0.5
Vertices(3)\px = 0 -32
Vertices(3)\py = 64 -32
Vertices(3)\pz = 100
Vertices(3)\u = 0
Vertices(3)\v = 0.125
CalcNormals(3, 0, 1, 0)
CalcNormals(1, 2, 3, 0)
Triangles(0)\face[0] = 3
Triangles(0)\face[1] = 0
Triangles(0)\face[2] = 1
Triangles(1)\face[0] = 1
Triangles(1)\face[1] = 2
Triangles(1)\face[2] = 3
Vertices(4)\px = 48 -32
Vertices(4)\py = 0 -32
Vertices(4)\pz = 100
Vertices(4)\u = 0.5
Vertices(4)\v = 0.5
Vertices(5)\px = 64 -32
Vertices(5)\py = 0 -32
Vertices(5)\pz = 76
Vertices(5)\u = 0.5
Vertices(5)\v = 0.796875
Vertices(6)\px = 64 -32
Vertices(6)\py = 64 -32
Vertices(6)\pz = 76
Vertices(6)\u = 0
Vertices(6)\v = 0.796875
Vertices(7)\px = 48 -32
Vertices(7)\py = 64 -32
Vertices(7)\pz = 100
Vertices(7)\u = 0
Vertices(7)\v = 0.5
CalcNormals2(@Vertices(7), @Vertices(4), @Vertices(5), 0)
CalcNormals2(@Vertices(5), @Vertices(6), @Vertices(7), 0)
Triangles(2)\face[0] = 7
Triangles(2)\face[1] = 4
Triangles(2)\face[2] = 5
Triangles(3)\face[0] = 5
Triangles(3)\face[1] = 6
Triangles(3)\face[2] = 7
SetMeshData(Ret_Mesh, #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, Vertices(),

SetMeshData(Ret_Mesh, #PB_Mesh_Face, Triangles(), 4)
Ret_Ent = CreateEntity(#PB_Any, MeshID(Ret_Mesh), MaterialID(Mid))
EntityRenderMode(Ret_Ent, #PB_Entity_CastShadow)
;EntityPhysicBody(Ret_Ent, #PB_Entity_BoxBody)
;SetEntityMass(Ret_Ent, 1)
;SetEntityFriction(Ret_Ent, 20)
For a = 0 To 100 Step 2
For b = -5 To 5 Step 2
CreateShape(a, b, 1, MaterialID(Mid))
Next
Next
;Add a caméra, indispensable if something is to be seen
#Camera = 0
CreateCamera(#Camera, 0, 0, 100, 100) ; Créate caméra
CameraBackColor(#Camera, RGB(0,0,0)) ; Back color is blue
CameraLocate(#Camera,0,0,500) ; Position the caméra
WorldShadows(#PB_Shadow_Additive)
CreateLight(1, RGB(255,255,255), 0, 0, 200)
;OpenLibrary(54,"Engine3D.dll")
; CallCFunction(54,"LightAttenuation",0,1)
AmbientColor(RGB(155, 155, 155))
;CameraRenderMode(#Camera, #PB_Camera_Wireframe)
;make fog
;Fog(RGB(127, 127, 127), 0.1, 450, 500)
Repeat
ClearScreen(0)
;{ Input
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_1)
Ent_Rot + 90
EndIf
If KeyboardPushed(#PB_Key_Left)
KeyX = -1
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = 1
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyZ = 1
ElseIf KeyboardPushed(#PB_Key_Down)
KeyZ = -1
Else
KeyZ = 0
EndIf
If KeyboardPushed(#PB_Key_Home)
KeyY = 1
ElseIf KeyboardPushed(#PB_Key_End)
KeyY = -1
Else
KeyY = 0
EndIf
If KeyboardPushed(#PB_Key_PageUp)
RollZ = 3
Else
If KeyboardPushed(#PB_Key_PageDown)
RollZ = -3
Else
;RollZ = 0
EndIf
EndIf
If KeyboardPushed(#PB_Key_Z)
EnableWorldPhysics(1)
;WorldGravity(9.

EntityPhysicBody(Ret_Ent, #PB_Entity_BoxBody)
SetEntityMass(Ret_Ent, 1)
SetEntityFriction(Ret_Ent, 20)
EndIf
If KeyboardPushed(#PB_Key_Add)
Frame.f+0.005
EndIf
EndIf
If ExamineMouse()
MouseX = -MouseDeltaX()/2
MouseY = -MouseDeltaY()/2
EndIf
;}
;RotateCamera(#Camera, MouseX, MouseY, RollZ*2)
MoveCamera (#Camera, KeyX*2, KeyZ*2, KeyY*2)
;RotateCamera(#Camera, 0, 0, EntityAngleX(Ret_Ent))
;RotateEntity(Ret_Ent, 5, 0, 10.5)
; CameraLookAt(#Camera, EntityX(ret_ent), EntityY(ret_ent), EntityZ(ret_ent)) ; Point/orient the caméra towards the entity
; CameraLookAt(#Camera, 0, 0, 0) ; Point/orient the caméra towards the entity
RotateCamera(#Camera, MouseX, MouseY, RollZ*2)
;RotateEntity(ret_ent, 0, 0, Ent_Rot)
LightLocate(1, CameraX(#Camera), CameraY(#Camera), CameraZ(#Camera))
RenderWorld() ; Display the 3D world
FlipBuffers()
;Delay(10)
OnErrorResume()
Until KeyboardPushed(#PB_Key_Escape)
;Debug ret_ent
End
Procedure.l CreateShape(x.l, y.l, Shape.l, Texture.l)
;s_Vertex px py px
; ShipNfo\Gx = 0 + (Cos((ShipNfo\GAngle) * #Rad) * (MaxSpeed-1))
; ShipNfo\Gy = 0 + (Sin((ShipNfo\GAngle) * #Rad) * (MaxSpeed-1))
If Shape.l = 1
Dim Vert.s_Vertex(24)
Dim Tris.s_Face(2)
Vert(0)\px = (Cos( (ASteps * (x.l * 32)) * #Rad) * Inside)
Vert(0)\pz = (Sin( (Asteps * (x.l * 32)) * #Rad) * Inside)
Vert(0)\py = y.l * 32
Vert(0)\u = 0
Vert(0)\v = 0
Vert(1)\px = (Cos( (ASteps * ((x.l * 32) + 32)) * #Rad) * Inside)
Vert(1)\pz = (Sin( (ASteps * ((x.l * 32) + 32)) * #Rad) * Inside)
Vert(1)\py = y.l * 32
Vert(1)\u = 1 ;0.5
Vert(1)\v = 0
Vert(2)\px = (Cos( (ASteps * ((x.l * 32) + 32)) * #Rad) * (Inside + 32))
Vert(2)\pz = (Sin( (ASteps * ((x.l * 32) + 32)) * #Rad) * (Inside + 32))
Vert(2)\py = y.l * 32
Vert(2)\u = 1 ;0.5
Vert(2)\v = 1 ;0.5
Vert(3)\px = (Cos( (ASteps * (x.l * 32)) * #Rad) * (Inside + 32))
Vert(3)\pz = (Sin( (Asteps * (x.l * 32)) * #Rad) * (Inside + 32))
Vert(3)\py = y.l * 32
Vert(3)\u = 0
Vert(3)\v = 1 ;0.5
CalcNormals2(@Vert(3), @Vert(0), @Vert(1), 0)
CalcNormals2(@Vert(1), @Vert(2), @Vert(3), 0)
Tris(0)\face[0] = 3
Tris(0)\face[1] = 0
Tris(0)\face[2] = 1
Tris(1)\face[0] = 1
Tris(1)\face[1] = 2
Tris(1)\face[2] = 3
; Vert(4)\px =
R_Mesh = CreateMesh(#PB_Any, 6)
SetMeshData(R_Mesh, #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, Vert(), 4)
SetMeshData(R_Mesh, #PB_Mesh_Face, Tris(), 2)
R_Ent = CreateEntity(#PB_Any, MeshID(R_Mesh), Texture.l)
EntityRenderMode(R_Ent, #PB_Entity_CastShadow)
;ProcedureReturn R_Ent
EndIf
EndProcedure