Page 1 of 1

Addon toys for MeshManualFlag example

Posted: Tue Jan 01, 2013 2:02 pm
by applePi
the 2 examples in Examples\3D\Demos folder: MeshManualFlag and MeshManualParametrics are great for showing how to dynamicaly change the positions of the vertices, and deforming a mesh. thanks for those examples.
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

Re: Addon toys for MeshManualFlag example

Posted: Tue Jan 01, 2013 2:15 pm
by Fred
Really nice !

Re: Addon toys for MeshManualFlag example

Posted: Tue Jan 01, 2013 4:44 pm
by Comtois
applePi wrote: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.
Here is the fix, does it work ?

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Mesh Manual - SuperQuadratics 
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;

;http://lrv.fri.uni-lj.si/~franc/SRSbook/geometry.pdf

; Use [F2]/[F3] - Change Render Material
; Use cursor to change SuperQuadratics 

#Color = #True ; #False white mesh / #True color mesh

#TwoPi = 2.0 * #PI
#CameraSpeed = 1

Global Epsilon2.f = 1
Global Epsilon1.f = 1

#Scale = 20
#Delta = 1.0 / 10.0 

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Define.f x, y, z, nx, ny, nz, u, v, sx, sy, sz, Depth = 200
Define.l Co
Define.w t1, t2, t3

Declare CreateSuperQuadratics()
Declare UpdateSuperQuadratics()
Declare SuperQuadratics()

Macro Text3D(No, Texte, Color, Alignment, x, y, z, Sx, Sy)
  CreateText3D(No, Texte)
  Text3DColor(No, Color)
  Text3DAlignment(No, Alignment)
  
  CreateNode(No, x, y, z)
  AttachNodeObject(No, Text3DID(No))
  ScaleNode(No, Sx, Sy, 1)
EndMacro

;Convert 2D to 3D 
Macro Convert2DTo3D(Camera, x, y, d)
  If PointPick(Camera, x, y)
    sx = CameraX(Camera) + PickX() * d
    sy = CameraY(Camera) + PickY() * d
    sz = CameraZ(Camera) + PickZ() * d 
  EndIf  
EndMacro 

If InitEngine3D()
  
  Add3DArchive("../Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("../Data/fonts", #PB_3DArchive_FileSystem)
  Add3DArchive("../Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    CreateSuperQuadratics()
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe)
    
    SetMeshMaterial(0, MaterialID(0))
    
    Centre = CreateNode(#PB_Any)
    AttachNodeObject(Centre, MeshID(0))
    ScaleNode(Centre, #scale, #scale, #scale)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 40, 150, #PB_Absolute)
    CameraFOV(0, 40)
    CameraLookAt(0, NodeX(Centre),  NodeY(Centre),  NodeZ(Centre))
    CameraBackColor(0, $330000)
    
    CreateLight(0, RGB(55,55,255), -10, 60, 10)
    CreateLight(0, RGB(255,55,55), 10, 60, -10)
    AmbientColor(RGB(90, 90, 90))
    
    Convert2DTo3D(0, CameraViewWidth(0)-140, 60, Depth)
    Text3D(0, "               ", RGBA(255, 255, 0, 255), 
          #PB_Text3D_Left | #PB_Text3D_Bottom, sx, sy, sz, 10, 10)
    
    Convert2DTo3D(0, CameraViewWidth(0)/2, CameraViewHeight(0)-60, Depth)    
    Text3D(1, "SuperQuadratics", RGBA(  0, 255, 0, 255), 
          #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered, sx, sy, sz, 10, 10)
    
    Convert2DTo3D(0, 40, 50, Depth) 
    Text3D(3, "Use cursor to change SuperQuadratics  ", RGBA(255, 255, 255, 255), 
          #PB_Text3D_Left | #PB_Text3D_VerticallyCentered, sx, sy, sz, 6, 6) 
    
    Convert2DTo3D(0, 40, 100, Depth)     
    Text3D(2, "Use [F2]/[F3] - Change Render Material", RGBA(255, 255, 255, 255), 
          #PB_Text3D_Left | #PB_Text3D_VerticallyCentered, sx, sy, sz, 6, 6)  
        
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_F2)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_F3)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
        
        If KeyboardPushed(#PB_Key_Right)
          Epsilon2 + 0.01
        ElseIf KeyboardPushed(#PB_Key_Left) And Epsilon2 > 0.01
          Epsilon2 - 0.01 
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          Epsilon1 + 0.01
        ElseIf KeyboardPushed(#PB_Key_Down) And Epsilon1 > 0.01
          Epsilon1 - 0.01 
        EndIf
        
      EndIf
      
      UpdateSuperQuadratics()
      
      RotateNode(Centre, 0.3, 0.3, 0.3, #PB_Relative)
      
      Text3DCaption(0, StrF(Engine3DFrameRate(#PB_Engine3D_Current), 2))
      
      RenderWorld()
      
      Screen3DStats()      
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Macro Vertex(T, P, C)
  x = CalculateX(T, P)
  y = CalculateY(T, P)
  z = CalculateZ(P)
  
  ;nx = CalculateNX(T, P)
  ;ny = CalculateNY(T, P)
  ;nz = CalculateNZ(P)
  
  MeshVertexPosition(x, y, z)
  ;MeshVertexNormal(nx, ny, nz)
  If #Color
    MeshVertexColor(RGB(Abs(x*C),Abs(y*C), Abs(z*C)))
  EndIf  
EndMacro

Macro SuperCos(Angle, Epsilon)
  (Pow(Abs(Cos(Angle)), Epsilon) * Sign(Cos(Angle)))
EndMacro

Macro SuperSin(Angle, Epsilon)
  (Pow(Abs(Sin(Angle)), Epsilon) * Sign(Sin(Angle)))
EndMacro 

Procedure.d CalculateX(Theta.d, Phi.d)
  ProcedureReturn SuperCos(Phi, Epsilon1) * SuperCos(Theta, Epsilon2)  
EndProcedure

Procedure.d CalculateY(Theta.d, Phi.d)
  ProcedureReturn SuperCos(Phi, Epsilon1) * SuperSin(Theta, Epsilon2) 
EndProcedure

Procedure.d CalculateZ(Phi.d)
  ProcedureReturn SuperSin(Phi, Epsilon1) 
EndProcedure

Procedure.d CalculateNX(Theta.d, Phi.d)
  ProcedureReturn SuperCos(Phi, 2.0-Epsilon1) * SuperCos(Theta, 2.0-Epsilon2)  
EndProcedure

Procedure.d CalculateNY(Theta.d, Phi.d)
  ProcedureReturn SuperCos(Phi, 2.0-Epsilon1) * SuperSin(Theta, 2.0-Epsilon2) 
EndProcedure

Procedure.d CalculateNZ(Phi.d)
  ProcedureReturn SuperSin(Phi, 2.0-Epsilon1) 
EndProcedure

Procedure CreateSuperQuadratics()
  CreateMesh(0)
  SuperQuadratics()
  FinishMesh(#False)
EndProcedure

Procedure UpdateSuperQuadratics()
  UpdateMesh(0, 0)
  SuperQuadratics()
  FinishMesh(#False)
EndProcedure

Procedure SuperQuadratics()
  
  Protected.d Theta, Phi, Delta = #Delta
  Protected.d x, y, z, nx, ny, nz
  Co = #scale*8
  
  Theta = -#PI
  Phi = -0.5 * #PI
  While Theta <= #PI
    While Phi <= 0.5 * #PI
      
      Vertex(Theta, Phi, Co)
      Vertex(Theta + Delta, Phi, Co)
      Vertex(Theta + Delta, Phi + Delta, Co)
      Vertex(Theta, Phi + Delta, Co)
      
      MeshFace(VertexIndex, VertexIndex + 1, VertexIndex + 2)
      MeshFace(VertexIndex, VertexIndex + 2, VertexIndex + 3)
      
      VertexIndex + 4
      
      Phi + Delta
    Wend
    Phi = -0.5 * #PI
    Theta + Delta
  Wend
  
EndProcedure

Re: Addon toys for MeshManualFlag example

Posted: Tue Jan 01, 2013 7:02 pm
by applePi
Hi Comtois
thank you for the great example. it runs successfully on windows 7, and in windows xp/sp3 it gives an error as in the picture:
http://s7.postimage.org/r0vwmk34b/error_msg.png
this is using PB 5.10b2 on both systems using the same computer. my vga card is Geforce 210. i have tried it with debugger on and off

Re: Addon toys for MeshManualFlag example

Posted: Tue Jan 01, 2013 7:47 pm
by Comtois
applePi wrote:Hi Comtois
thank you for the great example. it runs successfully on windows 7, and in windows xp/sp3 it gives an error as in the picture:
http://s7.postimage.org/r0vwmk34b/error_msg.png
this is using PB 5.10b2 on both systems using the same computer. my vga card is Geforce 210. i have tried it with debugger on and off
should be fixed for the next release.