Mobius Strip example

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

Mobius Strip example

Post by applePi »

another object from topology is the Mobius Strip http://en.wikipedia.org/wiki/Mobius_strip , you can make one by twisting a paper strip 180 degree then paste its ends. one of its parametric equations:
x = Cos(u) * ( 1 + (v/2 * Cos(u/2)) )
y = Sin(u) * ( 1 + (v/2 * Cos(u/2)) )
z = v/2 * Sin(u/2)

0 <= u <= 2*Pi
-0.5 <= v <= 0.5

the code are too short and basic a few lines only and all the rest of the code are just for controlling the graphics, i prefer the textured one. the width of the strip are from -0.5 to 0.5 through 2 vertices .and we connect it to the second 2 vertices to form 2 triangles. and this goes through the whole circle ie 2*Pi .
i have tried the gnuplot version, and it takes too much time and too much hard disk flashing on/off while here in PureBasic it is instantaneous, suitable for science demonstrations. i'm thinking on how to attach a small ant model to the surface so it moves around the strip to show the strange case of this strip
PS: if you cut the paper mobius strip in half and through its longitude you will get a bigger circle, if you cut it again you will have 2 interwoven circles. this is always surprising kids try it yourself.

Press W for the wireFrame version

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA_ONE
   #BUTTON
   #mainwin
   #plane
 EndEnumeration

Quit.b = #False
rot.l=1 :stopFlag = 1 : wireFrame.b = 0
xs.f = 0.3:ys.f = 0.3:zs.f = 0.3
x.f: y.f :z.f: x0.f: y0.f=1 :z0.f
rotx.f:roty.f=0.5:rotz.f :rotx0.f: roty0.f: rotz0.f
up.f = 1.8: depth.f=0

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "PgUp PgD scale mesh..Arrows for rotation, space: stop/rotate,  QA far/near, key_pad R/L/U/D", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(#BUTTON, 0, DesktopHeight(0)-60, 60, 30, "rotate/stop") 

;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-70, 0, 0, 0)
;WorldShadows(#PB_Shadow_Additive)

InitKeyboard()
SetFrameRate(60)

Add3DArchive("/", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)

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

CreateCamera(#CAMERA_ONE, 0, 0, 400, 400)
MoveCamera(#CAMERA_ONE, 0, 4, 9)
CameraLookAt(#CAMERA_ONE, 0, 0, 0)

RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf
CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(1))

SetActiveGadget(#BUTTON)
SkyDome("clouds.jpg", 100) ;for blue color background
; material for the plot
CreateMaterial(0, LoadTexture(0, "terrain_texture.jpg"))
;MaterialShadingMode(0, #PB_Material_Wireframe)
MaterialCullingMode(0, #PB_Material_NoCulling)
DisableMaterialLighting(0, #True)
CreateMesh(1, #PB_Mesh_TriangleList , #PB_Mesh_Static )
SetMeshMaterial(1, MaterialID(0))

x.f: y.f :z.f : u.f: v.f: r.f
i.l: j.l
v.f = -0.5 :  u.f=0 :txu.f : txv.f
While u <= 2*#PI
  
   While v<= 0.5
       t+1 ; total number of vertices       
       x = Cos(u) * ( 1 + (v/2 * Cos(u/2)) )
			 y = Sin(u) * ( 1 + (v/2 * Cos(u/2)) )
			 z = v/2 * Sin(u/2)
          
          MeshVertexPosition(x, y, z);
          MeshVertexTextureCoordinate(txu, txv)
          MeshVertexNormal(x, y, z)
         txv = txv + 1 ; texture coordinates
         
         v + 1
       Wend
       txv = 0
        txu = txu + 1/(2*#PI/0.1) ;texture coordinates
        v = -0.5
        u + 0.1
      Wend
      ;Debug t ; here 126 vertices from 0 to 125
      For j=0 To t-3 Step 2
                    
          MeshFace(j,j+2,j+1)
          MeshFace(j+1,j+2,j+3 )
          
        Next 
      ;connecting the first 2 vertices index with the last 2 vertices (twisted!!!) 
      MeshFace(0,125,1)
      MeshFace(1,125,124)
      
      
    NormalizeMesh(1)
    FinishMesh(#True)
    CreateEntity(1, MeshID(1), MaterialID(0))  
    ScaleEntity(1,3,3,3)
    MoveEntity(1,0,up,depth,#PB_Absolute)
    
    ;Main loop
    x=90: y=0: z=0 : h.f
Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case #BUTTON
        If rot = 0
          rot = 1
          rotx= rotx0:roty=roty0:rotz=rotz0 ; restore rotation status
          stopFlag = 1
          
        Else
          rot = 0
          rotx0= rotx:roty0=roty:rotz0=rotz ;back up rotation status
          rotx=0:roty=0:rotz=0
          stopFlag = 0
          
        EndIf
                    
    EndSelect
  EndIf 
  If stopFlag=1
    x + rotx
    y + roty
    z + rotz
  EndIf
  
   RotateEntity(1, x, y, z)
   
   RenderWorld()
   FlipBuffers()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)  ; rotate left
    rotx=1:roty=0:rotz=0
    rotx0 = rotx: roty0 = roty :rotz0 = rotz
    x + rotx
    y + roty
    z + rotz
    stopFlag=0
    rot = 0
  ElseIf KeyboardPushed(#PB_Key_Down) ; rotate right
    rotx=-1:roty=0:rotz=0
    rotx0 = rotx: roty0 = roty :rotz0 = rotz
    x + rotx
    y + roty
    z + rotz
    stopFlag=0
    rot = 0
  ElseIf KeyboardPushed(#PB_Key_Right)   ; rotate up
    rotx=0:roty=1:rotz=0
    rotx0 = rotx: roty0 = roty :rotz0 = rotz
    x + rotx
    y + roty
    z + rotz
    stopFlag=0
    rot = 0
  ElseIf KeyboardPushed(#PB_Key_Left) ; rotate down
    rotx=0:roty=-1:rotz=0 
    rotx0 = rotx: roty0 = roty :rotz0 = rotz
    x + rotx
    y + roty
    z + rotz
    stopFlag=0
    rot = 0
  EndIf 
  
  If KeyboardPushed(#PB_Key_PageUp) ; scale up model
    xs.f = 1.1:ys.f = 1.1:zs.f = 1.1
    ScaleEntity(1,xs,ys,zs)
    
  ElseIf KeyboardPushed(#PB_Key_PageDown) ; scale down model
    xs = 0.9:ys = 0.9:zs= 0.9
    ScaleEntity(1,xs,ys,zs)
    
  EndIf
  If KeyboardPushed(#PB_Key_Pad8) ; up move
    up + 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
   ElseIf KeyboardPushed(#PB_Key_Pad2) ; down move
    up - 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
  ElseIf KeyboardPushed(#PB_Key_Pad6)
    h + 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
    ElseIf KeyboardPushed(#PB_Key_Pad4)
    h - 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
    
    ElseIf KeyboardPushed(#PB_Key_Q) ; forward move
    depth - 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
    ElseIf KeyboardPushed(#PB_Key_A) ; inward move
    depth + 0.1
    MoveEntity(1,h,up,depth,#PB_Absolute)
    ElseIf KeyboardReleased(#PB_Key_W) ; display wire frame for the material
      If wireFrame=0
      MaterialShadingMode(0, #PB_Material_Wireframe)
      wireFrame ! 1
         ElseIf wireFrame=1
           MaterialShadingMode(0, #PB_Material_Solid)
           wireFrame ! 1
      EndIf
      
      
  EndIf
   If KeyboardPushed(#PB_Key_Escape)
      Quit = #True
    EndIf
    
    
Until Quit = #True Or Event = #PB_Event_CloseWindow


MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Mobius Strip example

Post by MachineCode »

Doesn't run here (missing DLL). I have DirectX 11 installed.
Last edited by MachineCode on Wed Mar 20, 2013 11:57 am, edited 1 time in total.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Mobius Strip example

Post by rsts »

Most impressive.

Many thanks for sharing.

(runs fine here - PB 5.11 beta 3 x64 and 32 - on windows 8 x64)
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Mobius Strip example

Post by Fred »

Very good examples, as always !
Post Reply