Page 1 of 1

Cube mesh spiral effect

Posted: Mon Mar 23, 2015 3:08 pm
by Didaktik
In the old demos for the Amiga computer was popular twisting effect of the cube. :)

As if the verge of his rubber.

How can I reproduce this effect in Ogre?

The first thing that comes to mind, split edges of the cube into segments. And rotate the upper and lower bounds. But how to calculate the lengths of the edges? They get like rubber and their length is changed, it is not just a line.

Image

Re: Cube mesh spiral effect

Posted: Tue Mar 24, 2015 9:33 am
by applePi
a simulation of that twisted cube:
* scale down a cube verticaly by 0.01
* make 100 such objects and align it over each other
* rotate every object from bottom to top an increasing amount of degrees
but i don't know how to texture this twisted compound cube from the sides . a possible solution not tested yet is to use the approach described here http://purebasic.fr/english/viewtopic.php?f=3&t=61884 to add the meshes each to the other and after all the meshes united together try to apply the texture to the Grand mesh

Image

Code: Select all

#CameraSpeed = 0.5
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()



OpenWindow(0,0,0, 800, 600, "use mouse and arrow keys to move camera", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600,0, 0, 0)

;Light & Shadow
AmbientColor(RGB(200, 200, 200))
CreateLight(#PB_Any,RGB(151, 251, 151), -5, 10, 5)
;WorldShadows(#PB_Shadow_Additive)

;Camera
CreateCamera(0, 0, 0,100,100)
CameraBackColor(0, RGB(145, 182, 201))
MoveCamera(0, 0, 7, 15, #PB_Absolute)  
CameraLookAt(0, 0,0,0)   

;Textures & material
CreateMaterial(0, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))      
CreateMaterial(1, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg"))) 
CreateMaterial(2, TextureID(LoadTexture(#PB_Any, "Geebee2.bmp")))  

CreateCube(0, 3)
CreateEntity(0, MeshID(0), MaterialID(2))
ScaleEntity(0, 1,0.01,1)

For i =1 To 100
  CopyEntity(0,i)
  MoveEntity(i,0,i*3/100,0, #PB_Absolute)
Next i

For i=1 To 100
  rot.f + 1
  RotateEntity(i, 0, -rot, 0, #PB_Absolute)
  
Next


Repeat

      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Space)

        EndIf
        
        
        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
        
      EndIf
      For i=0 To 100
        RotateEntity(i, 0,1,0, #PB_Relative)
        Next 
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    

  
the usual cube are made from 2 triangles on every side and are not sufficient to twist.
a possible algorithm
1- make a cube from hundreds of triangles
2- we apply increasingly small circular motion to every vertex, once we know its x position, and the circle equation. needs more work than the above solution but surely it is interesting

Re: Cube mesh spiral effect

Posted: Tue Mar 24, 2015 3:32 pm
by applePi
here is the add mesh approach as in the link above, gives exactly the same shape above. but we need to attach the u/v in a different way for the sides. or we can use a modeling software to paint the sides after saving the mesh and exporting it to another format

Code: Select all

Enumeration
   
   #LIGHT
   #CAMERA
   #mainwin
   #Plane
   #sphere
  
EndEnumeration
#cameraSpeed = 0.2

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Define.f KeyX, KeyY

Declare CreateMatrix()


InitEngine3D()
   InitMouse()
   InitKeyboard()
   InitSprite()
   ExamineDesktops()
   
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "arrow keys+mouse: move/rotate camera.... X/Z rotate object", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
         Add3DArchive(".",#PB_3DArchive_FileSystem)
         Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\Textures\",#PB_3DArchive_FileSystem)
         Add3DArchive(#PB_Compiler_Home+"Examples\3D\Data\GUI",#PB_3DArchive_FileSystem)
         Parse3DScripts()
         
                  
         CreateLight(0,RGB(255, 255, 95),-50,50,100)
         ;;============================================
         OpenWindow3D(0,0,0,300,120,"Engine Stats")
         TextGadget3D(0,2,0,300,30,"")
         TextGadget3D(1,2,0,300,60,"")
         ShowGUI(255,0)
    ;=============================================         
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
            
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 0, 0, 7, #PB_Absolute)
    CameraFOV(#camera, 70)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,-5,0)
        
    CreateLight(0, RGB(255,255,255), 40, 20, 0)
    AmbientColor(RGB(200, 200, 200))
    
    
    CreateMaterial(3, LoadTexture(3, "snow_1024.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "ValetCoeur.jpg")) 
    MaterialCullingMode(1, #PB_Material_NoCulling)
    DisableMaterialLighting(1, #False)
    
    CreateMaterial(2, LoadTexture(2, "RustySteel.jpg"))
    MaterialCullingMode(2, #PB_Material_NoCulling)
    
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
    
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    MaterialCullingMode(5, #PB_Material_NoCulling)
    
    CreateMatrix()
    
    WorldGravity(-9)
    CreateSphere(#sphere, 0.2)
    CreateEntity(#sphere, MeshID(#sphere), MaterialID(1))
    
    EntityPhysicBody(#sphere, #PB_Entity_SphereBody, 1,3,0.2)
    WorldShadows(#PB_Shadow_Additive)
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      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 KeyboardPushed(#PB_Key_Z)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          
          RotateEntity(0,0,0,0.5, #PB_Relative) 
        ElseIf KeyboardPushed(#PB_Key_X)
          DisableEntityBody(#sphere, #False)
          ApplyEntityImpulse(#sphere, 0, 0, 0 )
          
          RotateEntity(0,0,0,-0.5, #PB_Relative) 
        EndIf
       If KeyboardPushed(#PB_Key_S)
          SaveMesh(0, "Compound.mesh")
        EndIf         
        EndIf
       
        rot.f+0.6
        RotateEntity(0, 0.0,0.5,0.0,#PB_Relative)
        RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      ;;==============================================
            SetGadgetText3D(0,"FPS: "+StrF(Engine3DStatus(#PB_Engine3D_CurrentFPS )))
            SetGadgetText3D(1,"Tris: "+StrF(Engine3DStatus(#PB_Engine3D_NbRenderedTriangles))) 
      ;;================================================
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
 
End


Procedure CreateMatrix()
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  CreateCube(30,3)
   
  count = 1
  While count <=100
   
  AddSubMesh(#PB_Mesh_TriangleList)
  
  CopyMesh(30,31)
  rot.f + 1
  TransformMesh(31,0,count*3/100,0, 1,0.01,1,0,rot,0)
  GetMeshData(31,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate | #PB_Mesh_Normal, 0, MeshVertexCount(31)-1)
  GetMeshData(31,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(31, 0)-1)
  ArrSize = ArraySize(MeshData())
  
  For c=0 To ArrSize
      
      x.f = MeshData(c)\x 
      y.f = MeshData(c)\y
      z.f = MeshData(c)\z
      MeshVertexPosition(x,y,z)
      MeshVertexNormal(1,1,1)
      MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v) 
      
   Next   
   
   ArrSizeInd = ArraySize(MeshDataInd()) 
   For i=0 To ArrSizeInd Step 3
     MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
   Next
   
 count+1  
 Wend
    
 ;NormalizeMesh(0)
 FinishMesh(#True)
 NormalizeMesh(0)
  SetMeshMaterial(0, MaterialID(5))
    
  CreateEntity(0, MeshID(0), MaterialID(5), 0,-5,0)
  ;EntityPhysicBody(0, #PB_Entity_BoxBody , 1, 0.2, 1)  
  EntityPhysicBody(0, #PB_Entity_StaticBody , 1, 0.2, 1)  
EndProcedure


Re: Cube mesh spiral effect

Posted: Tue Mar 24, 2015 8:13 pm
by falsam
Nice twist cube ApplePi. I love :)

Re: Cube mesh spiral effect

Posted: Tue Mar 24, 2015 8:16 pm
by Comtois
Look at example Examples\3D\demos\MeshManualFlag.pb

You can build each side of your cube on this principle, and then rotate vertices

[EDIT]
Nices demos applePi :)