Re: MP3D Engine Alpha 32
Posted: Wed Feb 10, 2016 11:41 am
Psychophanta , i depends on one of the mesh creations example in PB-Ogre. it is good that Michael have made his Graphics engine in parallel with PB ogre, except of course the physics engine
only the texturing needs corrections, may be every shape have its specifics, but for now i consider the u, v to go from 1 to 4*pi. regarding the physics we can insert a physical ball inside the torus only if its physics body have a property as static

Psychophanta example ported to PB-Ogre
only the texturing needs corrections, may be every shape have its specifics, but for now i consider the u, v to go from 1 to 4*pi. regarding the physics we can insert a physical ball inside the torus only if its physics body have a property as static

Psychophanta example ported to PB-Ogre
Code: Select all
Enumeration
#camera
#sphere
EndEnumeration
#CameraSpeed = 0.5
Global torus
Define.f MouseX, MouseY, keyX, keyY
InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
OpenWindow(0, 0, 0, DesktopW, DesktopH, "Torus arc Mesh .....'W' for wire/solid frame ..... mouse /arrow keys to move/rotate the camera ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
CreateCamera(#camera, 0, 0, 100, 100)
MoveCamera(#camera, 0, 5, 10, #PB_Absolute)
CameraLookAt(#camera,0,0,0)
CameraBackColor(#camera, RGB(0,0,0))
CreateLight(0,RGB(255,255,255),10,20,0)
AmbientColor(RGB(200,200,200))
CreateMaterial(1, LoadTexture(1, "ground_diffuse.png")) ; Geebee2.bmp ; MRAMOR6X6.jpg; ground_diffuse.png
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialShadingMode(1, #PB_Material_Wireframe)
CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
MaterialCullingMode(2, #PB_Material_NoCulling)
Structure Point3D
x.f:y.f:z.f
EndStructure
Structure Vector3D Extends Point3D
m.f;<-length(modulo)
EndStructure
Macro Wrap(number,margin1,margin2)
(margin1#+(number#)%((margin2#)-(margin1#)))
EndMacro
Macro ProductoEscalar(a,b)
(a#\x*b#\x+a#\y*b#\y+a#\z*b#\z)
EndMacro
Macro getmodulo(v)
(Sqr#ProductoEscalar(v#,v#))
EndMacro
Procedure Rotate_3DVector_by_Angle_adding(*fi.Vector3D,*R0.Vector3D)
;Esta funcion halla un vector resultado de una rotacion en el espacio de otro vector inicial.
;Esta funcion admite entonces, como parametros de entrada, 2 vectores:
; - 'Vector radio' que se desea rotar. Este vector tiene direccion no colineal con el eje de rotaci?n.
; - 'Vector angulo' ('velocidad angular' * 'tiempo'), es el angulo en el que se rota el 'Vector radio' dado.
; Su modulo indica el numero de radianes a rotar, su direccion indica el angulo en el espacio en el que se rota (eje de rotaci?n)
; su sentido indica el sentido de la rotacion
;NOTA: la funcion devuelve el vector velocidad rectilinea (3er parametro) y el nuevo vector radio (4? parametro)
; pero ambos parametros se pueden omitir haciendo que la velocidad rectilinea no se devuelva y que el nuevo radio se devuelva en el 2? parametro
Protected Rt.Vector3D,u.Vector3D,P0.Vector3D
*fi\m=ProductoEscalar(*fi,*fi)
If *fi\m
u\m=ProductoEscalar(*R0,*fi)/*fi\m
*fi\m=Sqr(*fi\m)
;Proyeccion de *R0 sobre *fi:
Rt\x=u\m**fi\x
Rt\y=u\m**fi\y
Rt\z=u\m**fi\z
;Calcular P0-> (proyeccion ortogonal de *R0 sobre *fi):
P0\x=*R0\x-Rt\x
P0\y=*R0\y-Rt\y
P0\z=*R0\z-Rt\z
P0\m=getmodulo(P0)
If P0\m=0.0:ProcedureReturn:EndIf
;Calcular el producto vectorial: u-> = *fi-> X P0->
u\x=*fi\y*P0\z-*fi\z*P0\y
u\y=*fi\z*P0\x-*fi\x*P0\z
u\z=*fi\x*P0\y-*fi\y*P0\x
;ahora obtener *R0-> = (Proyeccion de *R0 sobre *fi)-> + (cos(|*fi->|)·P0-> + |P0->|/|u->|·sin(|*fi->|)·u->)->:
u\m=getmodulo(u)
*R0\x=Rt\x
*R0\y=Rt\y
*R0\z=Rt\z
!mov edi,dword[p.p_fi]
!fld dword[edi+12]; <- get *fi\m
!fsincos
!fstp dword[p.v_Rt]; <- Rt\x=Cos(*fi\m)
!fstp dword[p.v_Rt+4]; <- Rt\y=Sin(*fi\m)
; o bien si no se quiere usar ensamblador sustituirlo por
; Rt\x=Cos(*fi\m):Rt\y=Sin(*fi\m)
*R0\x+Rt\x*P0\x+P0\m/u\m*Rt\y*u\x
*R0\y+Rt\x*P0\y+P0\m/u\m*Rt\y*u\y
*R0\z+Rt\x*P0\z+P0\m/u\m*Rt\y*u\z
*R0\m=P0\m
EndIf
EndProcedure
Procedure.i CreateTorusArc(l.l,lt.l,r0.f,r1.f,rt.f,at.f,t.f=0.0)
txu.f : txv.f
If l<2 Or lt<2 Or r0<0 Or r1<0 Or rt<0.0001:ProcedureReturn 0:EndIf
Protected vert.Vector3D,ang.Vector3D,a.l,b.l,p.f,torsion.f,v0.l,v1.l
;Mesh.i=MP_CreateMesh()
CreateMesh(3 , #PB_Mesh_TriangleList, #PB_Mesh_Dynamic )
;l = numero de lados en la seccion (number of sides in cut area)
;lt = lados toro (number of sides of torus)
;r0 = radio grosor inicial (radius of cut area at initial extreme)
;r1 = radio grosor final (radius of cut area at end extreme)
;rt = radio del toro (radius of torus)
;at = longitud del arco del toro (arc lenght of the torus)
;t = torsion (torsion)
For b=0 To lt
For a=0 To l-1
p=r0+(r1-r0)*b/lt
vert\x=rt+p*Cos(2*#PI*a/l+torsion) +(r1-r0)*b/lt/2
vert\y=p*Sin(2*#PI*a/l+torsion)
vert\z=0
If b<>0
ang\x=0:ang\y=-at*b/lt:ang\z=0
Rotate_3DVector_by_Angle_adding(@ang,@vert)
EndIf
;MP_AddVertex(Mesh.i,vert\x,vert\y,vert\z,0,Int(a/l)/(lt+1),Mod(a,l)/l)
MeshVertexPosition(vert\x,vert\y,vert\z)
MeshVertexTextureCoordinate(txu, txv)
;MeshVertexTextureCoordinate(Int(a/l)/(lt+1),Mod(a,l)/l)
MeshVertexNormal(vert\x,vert\y,vert\z)
;MeshVertexColor(RGB(255,255,0))
txv = txv + 1/(4*#PI)
Next
torsion+t
txv = 0
txu = txu + 1/(4*#PI) ; texture coordinates
Next
For b=0 To lt-1
For a=0 To l-1
v0=Wrap(b*l+a,b*l,b*l+l):v1=Wrap(b*l+a+l+1,b*l+l,b*l+2*l); <- uso estas variables para no recalcularlas (para ganar tiempo)
;MP_AddTriangle(Mesh.i,v0,Wrap(b*l+a+l,b*l+l,b*l+2*l),v1)
MeshFace(v0,Wrap(b*l+a+l,b*l+l,b*l+2*l),v1)
;MP_AddTriangle(Mesh.i,v0,Wrap(b*l+a+1,b*l,b*l+l),v1)
MeshFace(v0,Wrap(b*l+a+1,b*l,b*l+l),v1)
Next
Next
NormalizeMesh(1)
FinishMesh(#True)
CreateEntity(3,MeshID(3),MaterialID(1))
;MP_EntitySetNormals(Mesh.i)
;ProcedureReturn Mesh.i
EndProcedure
;Tor.i=CreateTorusArc(25,3,0.4,0,2,#PI,0)
;Tor.i=CreateTorusArc(25,5,0.4,0.2,2,#PI,0)
; Tor.i=CreateTorusArc(3,30,1,0,2,#PI/3,0)
; Tor.i=CreateTorusArc(16,30,0.4,0.4,2,#PI,0)
;Tor.i=CreateTorusArc(4,10,1,0.5,2,#PI/2,0.1)
;Tor.i=CreateTorusArc(3,2,1,1,2,#PI/8,0)
Tor.i=CreateTorusArc(16,30,1,0,2,2.2*#PI,0)
;Tor.i=CreateTorusArc(16,30,1, 0.2, 2,1.8*#PI,0)
CreateEntityBody(3, #PB_Entity_StaticBody, 1, 1, 2)
CreateSphere(#sphere, 0.3)
CreateEntity(#sphere, MeshID(#sphere), MaterialID(2), 2, 0, 0)
CreateEntityBody(#sphere,#PB_Entity_ConvexHullBody , 1, 1, 1) ;#PB_Entity_SphereBody
wireFrame = 0
Repeat
Event = WindowEvent()
If ExamineMouse()
MouseX = -MouseDeltaX()/15
MouseY = -MouseDeltaY()/15
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_W)
If wireFrame
MaterialShadingMode(1, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(1, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
EndIf
RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
MoveCamera(#Camera, KeyX, 0, KeyY)
RotateEntity(3, 0,1,0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
