Re: MP3D Engine Alpha 32
Posted: Thu Dec 26, 2013 3:05 pm
				
				
			http://www.purebasic.com
https://www.purebasic.fr/english/

Code: Select all
If i = 61     ; the end of the cup handle 
  Nozzle = 0  ; to close the cup handle
EndIf 

Code: Select all
#BUTTON = 6
Global stop = 0
Structure vector3d
  x.f
  y.f
  z.f
EndStructure
    
Declare cup(incr.f) 
Quit.b = #False
ExamineDesktops()
MP_Graphics3D (DesktopWidth(0),DesktopHeight(0),0,2) ; Erstelle ein WindowsFenster #Window = 0
SetWindowTitle(0, "Tubes .. press space to toggle rotation, press S  to save to knot.x file") 
ButtonGadget(#BUTTON, 0, DesktopHeight(0)-60, 60, 30, "rotate/stop") 
MP_Viewport(0,0,DesktopWidth(0),DesktopHeight(0)-60)
camera=MP_CreateCamera()
light=MP_CreateLight(2)
MP_LightSetColor (light, RGB(255,255,255))
MP_PositionEntity(light, 0, 10, 20)
MP_EntityLookAt(light,0,0,0)
Global Mesh = MP_CreateMesh()
SetActiveGadget(#BUTTON)
  
Quit.b = #False 
;==============================================================
tex$ = #PB_Compiler_Home + "Examples/3D/Data/Textures/" + "clouds.jpg"
Texture = MP_LoadTexture(tex$)
MP_EntitySetTexture (Mesh, Texture )
MP_MaterialEmissiveColor (Texture,0,255,255,255)
MP_PositionCamera(camera, 0, 2, 5)
MP_CameraLookAt(camera,0,0,0)
MP_PositionEntity(light, 0 , 0, 10)
MP_EntityLookAt(light,0,0,0)
cup(0.5) ;its coefficient determines the radius of the bigger nozzle
MP_ScaleEntity(Mesh, 0.1, 0.1, 0.1)
MP_RotateEntity(Mesh, -90, 0, 0)
pot = MP_CopyEntity(Mesh) ; copy the cup to pot
For i=0 To 2562
  MP_FreeTriangle(pot, 1) ; converting the glass shape to a pot
Next  
rot.l=1 :stopFlag = 1 : wireFrame.b = 0
xs.f = 0.1:ys.f = 0.1:zs.f = 0.1
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
h.f=0:up.f = -6 :depth.f=-10
x.f=90: y.f=0: z.f=0 :indx = 126
MP_PositionEntity(Mesh,h+3,up,depth)
MP_PositionEntity(pot,h-5,up-3,depth)
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
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
  If MP_KeyDown(#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 MP_KeyDown(#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 MP_KeyDown(#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 MP_KeyDown(#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 MP_KeyDown(#PB_Key_PageUp) ; scale up model
    xs.f + 0.01:ys.f + 0.01:zs.f + 0.01
    MP_ScaleEntity(Mesh,xs,ys,zs)
    
  ElseIf MP_KeyDown(#PB_Key_PageDown) ; scale down model
    xs -0.01:ys -0.01:zs- 0.01
    If xs<0 :xs=0:ys=0:zs=0:EndIf
    MP_ScaleEntity(Mesh,xs,ys,zs)
    
  EndIf
  If MP_KeyDown(#PB_Key_Pad8) ; up move
    up + 0.1
    MP_PositionEntity(Mesh,h,up,depth)
   ElseIf MP_KeyDown(#PB_Key_Pad2) ; down move
    up - 0.1
    MP_PositionEntity(Mesh,h,up,depth)
  ElseIf MP_KeyDown(#PB_Key_Pad6)
    h - 0.1
    MP_PositionEntity(Mesh,h,up,depth)
    ElseIf MP_KeyDown(#PB_Key_Pad4)
    h + 0.1
    MP_PositionEntity(Mesh,h,up,depth)
    
    ElseIf MP_KeyDown(#PB_Key_Q) ; forward move
    depth - 0.1
    MP_PositionEntity(Mesh,h,up,depth)
    ElseIf MP_KeyDown(#PB_Key_A) ; inward move
    depth + 0.1
    MP_PositionEntity(Mesh,h,up,depth)
    ElseIf MP_KeyHit(#PB_Key_W) ; display wire frame for the material
      If wireFrame=0
      MP_Wireframe (1) 
      wireFrame ! 1
         ElseIf wireFrame=1
           MP_Wireframe (0)
           wireFrame ! 1 
      EndIf
    ElseIf MP_KeyDown(#PB_Key_D) 
      MP_FreeTriangle(Mesh, 1)
      ;indx -1: MP_FreeVertex(Mesh ,indx)
    ElseIf MP_KeyDown(#PB_Key_S)  
      MP_SaveMesh("glass.x",Mesh)
        
  EndIf
   If MP_KeyDown(#PB_Key_Escape)
      Quit = #True
    EndIf
  MP_DrawText (1,1,"press W _wireFrame, D _ delete triangles")  
  MP_DrawText (1,15,"keyPad keys: up.down.left.right")
  ;MP_RotateEntity(Mesh, 90+x, y, z)
  MP_RotateEntity(Mesh, -180+x, y, z)
  MP_RotateEntity(pot, -180+x, y, z)
  MP_RenderWorld() ; Erstelle die Welt
  MP_Flip ()  
    
Until Quit = #True Or Event = #PB_Event_CloseWindow
; main program
 Procedure cup(incr.f)
  x.f: y.f :z.f : u.f: v.f: r.f
      majorOrbit.l = 100 : minorOrbit.l = 20
      majorStep.f   = 2 * #PI / majorOrbit
      minorStep.f   = 2 * #PI / minorOrbit
      i.l: j.l: txu.f : txv.f
      Zincr.f 
       
      Nozzle.f = -15  ; determine the radius of the smaller nozzle of the funnel
      Nozzle2.f = 4
      
      For i = 0 To majorOrbit
        
        If i <= 20
          Nozzle.f  + incr
          
          Zincr = 0.3 ; Zincr determine the rate of increase/decrease the circles radius over time
        EndIf
        
        If i <= 60 And i > 20
         
          Zincr = 0.45
          Nozzle = 4
        EndIf 
                
            If i = 61     ; the end of the cup handle and the begening of its big cone (which contains the Liquid)
           
              Nozzle = 0  ; to close the cup handle, it will close the big cone bottom
            EndIf 
       
        If i > 61  ; the begening of the big cone (which contains the Liquid)
          
         ; Zincr = 0.45
                     
          Nozzle2.f  + incr  ; incr determines the radius of the bigger nozzle
          Nozzle = Nozzle2
        EndIf 
        
        v = i * majorStep
        For j = 0 To minorOrbit
          u = j * minorStep
          
          x = Cos(u)*Nozzle 
          y = Sin(u)*Nozzle
         
          
          MP_AddVertex (Mesh, x, y,z,0,txu,txv)
          ; texture the whole mesh with one picture stretched
          txv = txv + 1/minorOrbit ; texture coordinates
          
        Next
        
        If i > 61
            y2.f = 0.25 * x * x - 1.5 * x + 0.25 ;parabola curve
            Zincr = y2/40
          EndIf
        z + Zincr
        
        
        txv = 0
        txu = txu + 1/majorOrbit 
      Next
      For i = 0 To majorOrbit-1
      For j = 0 To minorOrbit
         
          MP_AddTriangle (Mesh,t,t+1,t + minorOrbit+1)
          MP_AddTriangle (Mesh,t + minorOrbit+1,t + minorOrbit+2,t+1 )
          
          If i=majorOrbit-1 And j=minorOrbit-1 ;bypass the last triangle
            minorOrbit-1
          EndIf 
          t + 1   
          
     Next
     
   Next  
      
    
  EndProcedure
  
  Code: Select all
MP_Graphics3DWindow(0, 0, 800, 600, "MP3D", 0)
MP_VSync(2)
light0 = MP_CreateLight(1)
MP_PositionEntity(light0, 0, 0, 0)
cam = MP_CreateCamera()
MP_PositionCamera(cam, 0, 0, -35)
MP_AmbientSetLight (RGB( 0, 154, 205))
cube = MP_CreateCube()
MP_PositionEntity(cube, 0, 0, 0)
cube2= MP_CreateSphere(1)
MP_PositionEntity(cube2,0,0,-25)
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow
  
    
  If MP_KeyDown(#PB_Key_Down) = 1
    MP_MoveCamera(cam,0,0,-3)
  EndIf   
  If MP_KeyDown(#PB_Key_Up) = 1
    MP_MoveCamera(cam,0,0,3)
  EndIf 
  If MP_KeyDown(#PB_Key_Left) = 1
    MP_MoveCamera(cam,-5,0,0)
  EndIf 
  If MP_KeyDown(#PB_Key_Right) = 1
    MP_MoveCamera(cam,5,0,0)
  EndIf 
    If MP_KeyDown(#PB_Key_W) = 1 
    MP_TurnCamera(cam,0,0,1)      ; its the Z axis, should be X Axis
  EndIf 
    If MP_KeyDown(#PB_Key_S) = 1
    MP_TurnCamera(cam,0,0,-1)
  EndIf 
  MP_DrawText(1,10,"FPS = "+Str(MP_FPS()))
  MP_DrawText(1,25,"MP_EntityGetPitch(cam) = "+MP_EntityGetPitch(cam))
  MP_DrawText(1,45,"MP_EntityGetYaw(cam) = "+MP_EntityGetYaw(cam))
  MP_DrawText(1,65,"MP_EntityGetRoll(cam) = "+MP_EntityGetRoll(cam))
  MP_DrawText(1,85,"Press W to look up and S to look down")
  MP_RenderWorld()
  MP_Flip()  
WendConfirmed, the 3 axis are swapped!N_Gnom wrote:Have a problem.
MP_TurnCamera doesn´t work correctly.
Code: Select all
MP_Graphics3DWindow(0, 0, 800, 600, "MP3D", 0)
MP_VSync(2)
light0 = MP_CreateLight(1)
MP_PositionEntity(light0, 0, 0, 0)
cam = MP_CreateCamera()
MP_PositionCamera(cam, 0, 0, -35)
MP_AmbientSetLight (RGB( 0, 154, 205))
cube = MP_CreateCube()
MP_PositionEntity(cube, 0, 0, 0)
;cube2 = MP_CreateSphere(1)
cube2 = MP_CreateTeapot()
MP_PositionEntity(cube2,0,0,-25)
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow
 
  If MP_KeyDown(#PB_Key_Down) = 1 ; down
    MP_TurnCamera(cam,0,-1,0)
  EndIf   
  
  If MP_KeyDown(#PB_Key_Up) = 1 ; up
    MP_TurnCamera(cam,0,1,0)
  EndIf
  
  If MP_KeyDown(#PB_Key_Left) = 1 ; left
    MP_TurnCamera(cam,-1,0,0)
  EndIf
  
  If MP_KeyDown(#PB_Key_Right) = 1 ; right
    MP_TurnCamera(cam,1,0,0)
  EndIf
 
  If MP_KeyDown(#PB_Key_W) = 1 ; front
    MP_TurnCamera(cam,0,0,1)      ; its the Z axis, should be X Axis
  EndIf
  
  If MP_KeyDown(#PB_Key_S) = 1 ; back
    MP_TurnCamera(cam,0,0,-1)
  EndIf
  
  MP_DrawText(1,10,"FPS = "+Str(MP_FPS()))
  MP_DrawText(1,25,"MP_EntityGetPitch(cam) = "+MP_EntityGetPitch(cam))
  MP_DrawText(1,45,"MP_EntityGetYaw(cam) = "+MP_EntityGetYaw(cam))
  MP_DrawText(1,65,"MP_EntityGetRoll(cam) = "+MP_EntityGetRoll(cam))
  MP_DrawText(1,85,"Press W to turn left and S to tunr right")
  MP_RenderWorld()
  MP_Flip() 
WendCode: Select all
MP_Graphics3DWindow(0, 0, 800, 600, "MP3D", 0)
MP_VSync(2)
light0 = MP_CreateLight(1)
MP_PositionEntity(light0, 0, 0, 0)
cam = MP_CreateCamera()
MP_PositionCamera(cam, 0, 0, -35)
MP_AmbientSetLight (RGB( 0, 154, 205))
cube = MP_CreateCube()
MP_PositionEntity(cube, 0, 0, 0)
;cube2 = MP_CreateSphere(1)
cube2 = MP_CreateTeapot()
MP_PositionEntity(cube2,0,0,-25)
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow
 
   
  If MP_KeyDown(#PB_Key_Down) = 1
    MP_MoveCamera(cam,0,-1,0)
  EndIf   
 If MP_KeyDown(#PB_Key_Up) = 1
    MP_MoveCamera(cam,0,1,0)
  EndIf
  If MP_KeyDown(#PB_Key_Left) = 1
    MP_MoveCamera(cam,-1,0,0)
  EndIf
  If MP_KeyDown(#PB_Key_Right) = 1
    MP_MoveCamera(cam,1,0,0)
  EndIf
  
  
  If MP_KeyDown(#PB_Key_W) = 1
    MP_MoveCamera(cam,0,0,1)      ; its the Z axis, should be X Axis
  EndIf
    If MP_KeyDown(#PB_Key_S) = 1
    MP_MoveCamera(cam,0,0,-1)
  EndIf
  
  MP_DrawText(1,10,"FPS = "+Str(MP_FPS()))
  MP_DrawText(1,25,"MP_EntityGetPitch(cam) = "+MP_EntityGetPitch(cam))
  MP_DrawText(1,45,"MP_EntityGetYaw(cam) = "+MP_EntityGetYaw(cam))
  MP_DrawText(1,65,"MP_EntityGetRoll(cam) = "+MP_EntityGetRoll(cam))
  MP_DrawText(1,85,"Press W to go front and S to go back down")
  MP_RenderWorld()
  MP_Flip() 
WendCode: Select all
MP_Graphics3DWindow(0, 0, 800, 600, "MP3D", 0)
MP_VSync(2)
light0 = MP_CreateLight(1)
MP_PositionEntity(light0, 0, 0, 0)
cam = MP_CreateCamera()
MP_PositionCamera(cam, 0, 0, -35)
MP_AmbientSetLight (RGB( 0, 154, 205))
cube = MP_CreateCube()
MP_PositionEntity(cube, 0, 0, 0)
cube2= MP_CreateSphere(1)
MP_PositionEntity(cube2,0,0,-25)
While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow
 
   
  If MP_KeyDown(#PB_Key_Down) = 1
    MP_TurnCamera(cam,0,-1,0)
  EndIf   
  If MP_KeyDown(#PB_Key_Up) = 1
    MP_TurnCamera(cam,0,1,0)
  EndIf
  If MP_KeyDown(#PB_Key_Left) = 1
    MP_TurnCamera(cam,-1,0,0)
  EndIf
  If MP_KeyDown(#PB_Key_Right) = 1
    MP_TurnCamera(cam,1,0,0)
  EndIf
    If MP_KeyDown(#PB_Key_W) = 1
    MP_MoveCamera(cam,0,0,1)      ; its the Z axis, should be X Axis
  EndIf
    If MP_KeyDown(#PB_Key_S) = 1
    MP_MoveCamera(cam,0,0,-1)
  EndIf
  MP_DrawText(1,10,"FPS = "+Str(MP_FPS()))
  MP_DrawText(1,25,"MP_EntityGetPitch(cam) = "+MP_EntityGetPitch(cam))
  MP_DrawText(1,45,"MP_EntityGetYaw(cam) = "+MP_EntityGetYaw(cam))
  MP_DrawText(1,65,"MP_EntityGetRoll(cam) = "+MP_EntityGetRoll(cam))
  MP_DrawText(1,85,"Press W to go front and S to go back down")
  MP_RenderWorld()
  MP_Flip() 
WendCode: Select all
Define .f
ExamineDesktops()
Global bitplanes.b=DesktopDepth(0),RX.w=DesktopWidth(0),RY.w=DesktopHeight(0)
MP_Graphics3D(RX,RY,0,1):MP_VSync(1)
SetWindowTitle(0,"e")
Global.i camera,light,arrow
camera=MP_CreateCamera()
MP_PositionCamera(camera,0,5,-10)
MP_CameraLookAt(camera,0,0,0)
light=MP_CreateLight(2)
MP_LightSetColor(light,RGB(219,118,50))
MP_PositionEntity(light,-6,10,-5)
MP_AmbientSetLight(RGB(0,100,200))
arrow=MP_CreateCylinder(60,200)
MP_PositionMesh(arrow,0,0,0)
MP_RotateEntity(arrow,0,0,0)
Repeat
  MP_TurnCamera(camera,0,0,0); <- with this line you will see nothing  o_O (Why?) . Delete it to see all working nice.
  MP_RenderWorld()
  MP_Flip()
Until MP_KeyDown(#PB_Key_Escape) Or WindowEvent()=#PB_Event_CloseWindow