TransformMesh example (flapping bird)

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

TransformMesh example (flapping bird)

Post by applePi »

TransformMesh can be considered an "All In One" tool , it works for the normal meshes like the robot model in models folder, move, scale, rotate, as a whole.
but to work with specific parts of the mesh, we need to make the mesh manually and using AddSubMesh function to mark a new submesh which we can refer to.
the following demo is a study of the function, a kind of a flapping robotic bird. the wings are 2 rectangles centered on the X axes then rotated 90 degree on either sides and stretched.
some issues here:
1- the example displayed okay in the default directX but not displayed correctly in opengl subsys. my card is geforce GT 520 . may be the way the mesh are constructed !!.
2- when we press space to toggle up/down move i have used moveEntity and it is okay here, but if we comment the moveEntity and uncomment the lines 56, 59 TransformMesh(3, 0, 0.01, 0, 1, 1, 1, 0, 0, 0) then we will see the wings detached slightly, and this should not happened since the transformation here are for the whole mesh.
3-the rotating eye radar are cenetered around Y, but move it to the tail and it will not rotate around itself exactly , i know it is a centering issue, but the eye are part of the whole mesh by design and not separated!!.

i will look how to deform planes and balls with this function , but after designing planes and balls which have many AddSubMesh in their construction so we can refer to every little square separately
if you find difficulty with TransformMesh then there are always the many mesh functions which can be used for deforming . but i find it a handy and useful tool.
Image

Code: Select all

Declare CreateRobot()
Global wingAngle.f = 30
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "Space to toggle Up and Down -- arrows Up/Down to increase/decrease frequency", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
InitEngine3D()
    
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive("/", #PB_3DArchive_FileSystem)
    
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-2, 0, 0, 0)

cam=CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(cam, 0, -1, 20)
CameraLookAt(cam, 0, 0, 0)

CreateLight(0,RGB(255,255,255),-10,10,30)
AmbientColor(RGB(100,100,100))

CreateMaterial(0, LoadTexture(0, "nskingr.jpg"))
MaterialCullingMode(0, #PB_Material_NoCulling)
;MaterialBlendingMode(0, #PB_Material_AlphaBlend)
;enable the above line (AlphaBlend) will give you wrong visual impression
DisableMaterialLighting(0, #True)

CreateRobot() ; the construction of the robotic bird
CreateEntity(3,MeshID(3), MaterialID(0))
;ScaleEntity(3, 3,1,1)
MoveEntity(3, 0,0,0)

;RotateEntity(3,0,86,0)

;transform the left and right wings so it will be rotated 90 and then elongated by 5 
TransformMesh(3, 0, 0, 0, 1, 1, 1, 90, 0, 0, 2) 
TransformMesh(3, 0, 0, 0, 1.5, 1, 5, 0, 0, 0, 2)
TransformMesh(3, 0, 0, 0, 1, 1, 1, -90, 0, 0, 3)
TransformMesh(3, 0, 0, 0, 1.5, 1, 5, 0, 0, 0, 3)  
;TransformMesh(3, 3, 2, 0, 1, 1, 1, 0, 0, 0, 4) ; move the Radar right and up
TransformMesh(3, 0, 1.3, 0, 1, 1, 1, 0, 0, 0, 4) ; move the Radar up

neg= -1 : roX.f = 2 :sz.f = 2
Repeat
    
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up) 
    wingAngle - 1
    ElseIf KeyboardPushed(#PB_Key_Down) 
      wingAngle + 1
    ElseIf KeyboardReleased(#PB_Key_Space) ; to toggle Up and Down move
      g !1
    EndIf  
    If g
        ;TransformMesh(3, 0, 0.01, 0, 1, 1, 1, 0, 0, 0) 
        MoveEntity(3, 0,0.01,0)
      Else
        ;TransformMesh(3, 0, -0.01, 0, 1, 1, 1, 0, 0, 0) 
        MoveEntity(3, 0,-0.01,0)
      EndIf  
    
  ; to make the wings flapping, ie change the wing rotation direction
  angle + 1
  If angle > wingAngle
    angle = 1
    roX = roX * neg
  EndIf   
;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
TransformMesh(3, 0, 0, 0, 1, 1, 1, roX, 0, 0, 2) ; roX will make the wing to continualy rotate until a specific angle (wingAngle) reached
TransformMesh(3, 0, 0, 0, 1, 1, 1, -roX, 0, 0, 3) ; rotation (ie flapping) for the second wing
TransformMesh(3, 0, 0, 0, 1, 1, 1, 0, 1, 0, 4) ; rotate the Radar (spying eye) continuasly around Y axis

RotateEntity(3,0,0.2,0, #PB_Relative)

   RenderWorld()
   FlipBuffers()
   ExamineKeyboard()
   If KeyboardReleased(#PB_Key_Escape)
      Quit = #True
    EndIf
    
  Until Quit = #True Or WaitWindowEvent(1)=#PB_Event_CloseWindow
  
  Procedure CreateRobot()
  Protected.l i,r
  Protected.f x, y, z,u, v
  Protected.w t1, t2, t3
  CreateMesh(3, #PB_Mesh_TriangleList ,#PB_Mesh_Dynamic)
  r=0
  
  Restore Vertices ; plot vertices for the head and tail triangles
  For i=0 To 5
      
    Read.f x : Read.f y : Read.f z
    Read.f u: Read.f v
        
    MeshVertexPosition(x , y , z )
    MeshVertexTextureCoordinate(u, v)
   
  Next i
  Restore Faces
  For i=0 To 1
    Read.w t1 : Read.w t2 : Read.w t3
    MeshFace(t1, t2, t3)
  Next i
  ;;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
  Restore Vertices2 ; plot again the same above points but for the sake to make it another submesh for the purpose of texturing it separately
  AddSubMesh(#PB_Mesh_TriangleList)
  For i=0 To 5
      
    Read.f x : Read.f y : Read.f z
    Read.f u: Read.f v
        
    MeshVertexPosition(x , y , z )
    MeshVertexTextureCoordinate(u, v)
   
  Next i
  Restore Faces2
  For i=0 To 5
    Read.w t1 : Read.w t2 : Read.w t3
    MeshFace(t1, t2, t3)
  Next i

;;====================================================  
;the Left and right wings coordinates 
For i=1 To 2 ; every Loop generate one wing composed from 2 triangles (ie faces)
AddSubMesh(#PB_Mesh_TriangleList)
MeshVertexPosition(1, 0, 0)
MeshVertexTextureCoordinate(0, 0)

MeshVertexPosition(2, 0, 0)
MeshVertexTextureCoordinate(1, 0)

MeshVertexPosition(2, 1, 0)
MeshVertexTextureCoordinate(1, 1)

MeshVertexPosition(1, 1, 0)
MeshVertexTextureCoordinate(0, 1)

MeshFace(0, 1, 2)
MeshFace(2, 3, 0)

Next 

;Radar coordinates  (the rotating triangle with an eye)
AddSubMesh(#PB_Mesh_TriangleList)
Restore Radar
  For i=0 To 2
      
    Read.f x : Read.f y : Read.f z
    Read.f u: Read.f v
        
    MeshVertexPosition(x , y , z )
    MeshVertexTextureCoordinate(u, v)
   
  Next i
  MeshFace(0, 1, 2)
;;ooooooooooooooooooooooooooooooooooooooooooooooooooooo

FinishMesh(#True)
NormalizeMesh(3)

EndProcedure
  
  DataSection ; the data of the elongated Prism (the body of the bird) and the Radar)
  
  Vertices:  ; for the body head triangle and back triangle
  Data.f 0, 0 ,1 , 0.742, 0.131  ; head   
  Data.f 0, 1, 0 ,0.621, 0.033
  Data.f 0, 0, -1  , 0.523,0.131
  
  Data.f 6, 0 ,1 , 0.742, 0.131   ; back
  Data.f 6, 1, 0 , 0.621, 0.033
  Data.f 6, 0, -1  , 0.523,0.131
  
  Vertices2: ; for the body sides
  Data.f 0, 0 ,1 , 0.762, 0.469  ; head   
  Data.f 0, 1, 0 ,0.898, 0.471
  Data.f 0, 0, -1  , 0.762,0.57
  
  Data.f 6, 0 ,1 , 0.762, 0.469   ; back
  Data.f 6, 1, 0 , 0.898, 0.471
  Data.f 6, 0, -1  , 0.762,0.57
  
  Radar:
  Data.f 0, 0 ,1 , 0.695,  0.11  ; the Radar (rotating triangle with eye)
  Data.f 0, 1, 0 , 0.617, 0.033
  Data.f 0, 0, -1, 0.551, 0.11
  
  Faces:
  Data.w 0, 1 ,2   
  Data.w 3, 4 ,5
    
  Faces2:
  Data.w 0, 3 ,4   
  Data.w 4, 1 ,0
  Data.w 0, 3 ,5
  Data.w 5, 2 ,0
  Data.w 2, 5, 4
  Data.w 4, 1, 2
          
EndDataSection

    
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: TransformMesh example (flapping bird)

Post by IdeasVacuum »

Hi ApplPi
Your origami is not as good as your code :)
There does seem to be something odd with the meshing, though the wings in OpenGL actually look more bird-wing like.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: TransformMesh example (flapping bird)

Post by applePi »

Hi IdeasVacuum
you said Origami , yes this reminds me of this topic, i will test some simple folding and unfolding cubes at first. i begins to think that directX are more forgiving than opengl since any way i design the mesh it will be displayed okay. the main prism body of the (bird) like this
Image
at first the 2 blue triangles, then addsubmesh, and then the 3 sides constructed with a fresh vertices other than the blue triangles. it is okay in DX.
but your idea about the Origami are a good subject.
Post Reply