the first example MeshManualFlag animating some equation so the result resembles a flying flag, and while i am studying the example i have added a sphere over the flag and you can use the arrow keys to move the sphere following the dynamicaly deformed surface of the flag.
after that i have made a hole at the middle of the flag by making the face(the triangle) made from 3 connections of a vertex to itself instead of p1,p2,p3 as an example p1,p1,p1, also you can use p1,p2,p1 (a line instead of a triangle)
to run the second example in PB Examples\3D\Demos MeshManualParametrics comment the Text3D (until it is fixed) in lines 93/96/99/102 and you will see a great graphics which can be deformed using the arrow keys.
for the following example use the arrow keys to move the ball, and a/s/d/w to move the flag, move mouse to rotate the camera . what i need is a more reading and understanding of the slope of surface at different points so the ball or a toy vertical axes can be exactly perpendicular to the slope.
Code: Select all
; PureBasic - Mesh Manual - Flag
;
; (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;
;
IncludeFile #PB_Compiler_Home + "Examples\3D\Screen3DRequester.pb"
#CameraSpeed = 1
#NbX=30
#NbZ=30
#TEX = 7
#MAT = 8
Global deltaX.f, deltaY.f
Global.f WaveAngle, WaveFrequency, WavePeriodX, WavePeriodZ, WaveAmplitude
WaveFrequency=3 ;=waves/second
WavePeriodX =9 ;=1/Wave lenght
WavePeriodZ =11 ;=1/Wave lenght
WaveAmplitude=3
Define.f KeyX, KeyY, MouseX, MouseY
Declare UpdateMatrix()
Declare CreateMatrix()
If InitEngine3D()
Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data/Packs/skybox.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "Examples\3D\Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
;-Material
GetScriptMaterial(1, "Scene/GroundBlend")
MaterialCullingMode(1, 1)
LoadTexture(#TEX, "Caisse.png")
CreateMaterial(#MAT, TextureID(#TEX))
CreateSphere(103, 2)
CreateEntity(103, MeshID(103), MaterialID(#MAT))
;CreateEntity(103, MeshID(103), #PB_Material_None)
;-Mesh
CreateMatrix()
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,0,50,80, #PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(90, 0, 0))
;-Light
CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
AmbientColor(RGB(90, 90, 90))
;- Skybox
SkyBox("stevecube.jpg")
Repeat
Screen3DEvents()
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
deltaX=deltaX-0.08
ElseIf KeyboardPushed(#PB_Key_Right)
deltaX=deltaX+0.08
EndIf
If KeyboardPushed(#PB_Key_Up)
deltaY=deltaY-0.08
ElseIf KeyboardPushed(#PB_Key_Down)
deltaY=deltaY+0.08
EndIf
If KeyboardPushed(#PB_Key_A)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_D)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_W)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_S)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
EndIf
If ExamineMouse()
MouseX = -(MouseDeltaX()/10)
MouseY = -(MouseDeltaY()/10)
EndIf
MoveCamera (0, KeyX, 0, KeyY)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
; Waves
UpdateMatrix()
WaveAngle = WaveAngle+WaveFrequency
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
;-Procédures
Procedure DrawMatrix()
Protected.l x, z, Nb
Protected.w P1, P2, P3, P4
For z=0 To #Nbz
For x=0 To #NbX
;les coordonnées de vertex
y.f=Sin(Radian((WaveAngle+ x *WavePeriodX+ z *WavePeriodZ)))*WaveAmplitude
MeshVertexPosition(x , y, z )
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(x/#NbX, z/#Nbz)
Next x
Next z
y.f=Sin(Radian((WaveAngle+(28+deltaX)*WavePeriodX+(28+deltaY)*WavePeriodZ)))*WaveAmplitude
MoveEntity(103, 28+deltaX , y, 28+deltaY ,#PB_Absolute)
;y coordinates of the 4 neighbor points
yyE.f= Sin(Radian((WaveAngle+(28+deltaX+1)*WavePeriodX+(28+deltaY)*WavePeriodZ)))*WaveAmplitude
yyW.f= Sin(Radian((WaveAngle+(28+deltaX-1)*WavePeriodX+(28+deltaY)*WavePeriodZ)))*WaveAmplitude
yyN.f= Sin(Radian((WaveAngle+(28+deltaX)*WavePeriodX+(28+deltaY+1)*WavePeriodZ)))*WaveAmplitude
yyS.f= Sin(Radian((WaveAngle+(28+deltaX)*WavePeriodX+(28+deltaY-1)*WavePeriodZ)))*WaveAmplitude
; aproximate of the slope at point x,z
slopeX.f = (yyN - yyS) * 10
slopeZ.f = (yyE - yyW) * 10
RotateEntity(103, -slopeX,0,slopeZ,#PB_Absolute)
Nb=#NbX+1
For z=0 To #NbZ-1
For x=0 To #NbX-1
P1=x+(z*Nb)
P2=P1+1
P3=x+(z+1)*Nb
P4=P3+1
;make a hole in the middle at point 15,15
If x=15 And z=15
MeshFace(P1, P1, P1)
MeshFace(P2, P2, P2)
Else
MeshFace(P3, P2, P1)
MeshFace(P3, P4, P2)
EndIf
Next
Next
EndProcedure
Procedure CreateMatrix()
CreateMesh(0, 4, #True)
DrawMatrix()
FinishMesh(#False)
SetMeshMaterial(0, MaterialID(1))
CreateNode(0)
AttachNodeObject(0, MeshID(0))
AttachNodeObject(0, EntityID(103))
ScaleNode(0, 2, 2, 2)
MoveNode(0, -30, 0, -30)
EndProcedure
Procedure UpdateMatrix()
UpdateMesh(0, 0)
DrawMatrix()
FinishMesh(#False)
EndProcedure
