Re: MP3D Engine Alpha 32
Posted: Fri Dec 20, 2013 5:19 pm
				
				The nanosecond we can turn a 2D image into a complete 3D model, is the nanosecond I will cry myself to sleep of happiness =D Lets work together to make this dream a reality!
			http://www.purebasic.com
https://www.purebasic.fr/english/

Code: Select all
#Mode = 7 ; #Mode = 1 or #Mode = 7
    Structure PointVertex
      x.f
      y.f
      z.f
      CompilerIf #Mode = 7
        Size.f ;--- Only if Mode = 7
      CompilerEndIf  
      Color.l;d3dcolor
    EndStructure
    
    Global mAngle.f = Cos(-0.1)
    Global pAngle.f = Sin(0.1)
    Global move.f = 0
    
    ; code for MP3D
    Declare DrawMatrix()
    Declare isolate()
    
    Global angle.f
    
ExamineDesktops()
MP_Graphics3D (DesktopWidth(0),DesktopHeight(0),0,3) ; Erstelle ein WindowsFenster #Window = 0
MP_Viewport(0,0,DesktopWidth(0),DesktopHeight(0)-5)
SetWindowTitle(0, "press up/down to move camera, A/Z to move up.down  ")
    camera=MP_CreateCamera()
    light=MP_CreateLight(2)
    MP_LightSetColor (light, RGB(255,255,255))
        
    Global size = 20000
    Global Entity= MP_CreatePrimitives (size, #Mode)   
     
    Define.f red, green, blue
    Quit.b = #False
    ;==============================================================
    
    DrawMatrix()
    MP_PrimitivesBlendingMode(Entity, 5,2)
    MP_PositionCamera(camera, 0, 0, 700)
    MP_CameraLookAt(camera,0,0,0)
    MP_PositionEntity(light, 0 , 0, 10)
    MP_EntityLookAt(light,0,0,0)
    
    MP_VSync(0)
    
    xx.f=0 :zz.f=0 : camY=0
    While Not MP_KeyDown(#PB_Key_Escape) And Not WindowEvent() = #PB_Event_CloseWindow
     
      If MP_KeyDown(#PB_Key_Up)
       
        zz.f + 20
      ElseIf MP_KeyDown(#PB_Key_Down)
       
        zz.f - 20
     
      EndIf
      If MP_KeyDown(#PB_Key_A)
        camY+10
        MP_PositionCamera(camera, 0, camY, 700)
          ElseIf MP_KeyDown(#PB_Key_Z)
            camY-10
            MP_PositionCamera(camera, 0, camY, 700)
            ElseIf MP_KeyHit(#PB_Key_R)
            MP_ScaleEntity(Entity, 1, 0.1, 1)
            
        EndIf
         
        MP_PositionEntity(Entity, xx, 0, zz)
        MP_TurnEntity(Entity,0,1,0)
       
        isolate(); call the routine for rotating only the green points
        
        MP_DrawText (1,1,"FPS = "+Str(MP_FPS()))
        
      MP_RenderWorld()
       
      MP_Flip ()
    Wend
    ;Debug move
    Procedure DrawMatrix()
      
      *Memory = MP_GetMemPrimitives(Entity)
      
      For i=0 To size
        x = Random(200)-100:y = Random(200)-100:z = Random(200)-100
        *myvertex.PointVertex = *Memory + i * SizeOf(PointVertex)
     
    ;check if point inside a specific sphere: 
      q.f = (Pow(x,2) + Pow(y,2) + Pow(z,2))
      ;If q <= (size / 40)-100
      If q <= 4900
        r=0:g=255:b=0
        ;r=255:g=119:b=119
       
        *myvertex\x = x
        *myvertex\y = y
        *myvertex\z = z
        
        *myvertex\color = MP_ARGB(200,r,g,b)
       
        ;ElseIf q> size / 20
        ;ElseIf q> 10000
          ;r=0:g=0:b=0
          ;r=119:g=255:b=119
          
        ;*myvertex\x = x
        ;*myvertex\y = y
        ;*myvertex\z = z
        
        ;*myvertex\color = MP_ARGB(200,r,g,b)
          
      EndIf
      Next
      
      MP_CloseMemPrimitives(Entity)
      
    EndProcedure 
    Procedure isolate() ; rotate green core
      angle.f = 0.05
    
      *Memory = MP_GetMemPrimitives(Entity)
      *myvertex.PointVertex = *Memory
      
    For i=0 To size
         
    If *myvertex\Color & $FF00
      x.f = *myvertex\x
      z.f = *myvertex\z
      *myvertex\x = mAngle * x - pAngle * z
      *myvertex\z = mAngle * z + pAngle * x
      
      move + 0.01
      
      *myvertex\x + Sin(move)
      *myvertex\y + Cos(move/t)
      *myvertex\z + Cos(move)
      t+1
      If t=15000
        t=0
        move = 0
      EndIf
      
           
      CompilerIf #Mode = 7
        *myvertex\Size = 2.5 ;--- Only if Mode = 7
      CompilerEndIf
     
    EndIf 
    *myvertex + SizeOf(PointVertex)
      
       Next
       
       MP_CloseMemPrimitives(Entity)
     EndProcedure
     
    Code: Select all
#Mode=1 ; #Mode=1 or #Mode=7
Structure Vector
  x.f
  y.f
  z.f
  m.f
EndStructure
Structure PointVertex
  x.f
  y.f
  z.f
  CompilerIf #Mode=7
    Size.f ;--- Only if Mode=7
  CompilerEndIf 
  color.l;d3dcolor
EndStructure
Global xres.l=640,yres.l=480
MP_Graphics3D(xres,yres,0,3):MP_VSync(1)
SetWindowTitle(0,"press up/down to move camera")
camera=MP_CreateCamera()
light=MP_CreateLight(2)
MP_LightSetColor(light,RGB(245,245,245))
MP_PositionEntity(light,0,10,20)
MP_EntityLookAt(light,0,0,0)
Global size=100000,objeto.l=1
Global Entity=MP_CreatePrimitives(size,#Mode),*Memory.PointVertex,Dim *OriginVertex.PointVertex(size),Dim TargetVertex.Vector(size),Dim trans.Vector(size)
Global TransCount.l
#TransSteps=90:#ObjectStayTime=500
Procedure DrawMatrix()
  Protected r.w=20,g.w=200,b.w=0,i.l
  *Memory=MP_GetMemPrimitives(Entity)
  For i=0 To size
    *OriginVertex(i)=*Memory+i*SizeOf(PointVertex)
    *OriginVertex(i)\color=MP_ARGB(200,r,g,b)
    *OriginVertex(i)\x=(Random(1800)-900)/10*i/size
    *OriginVertex(i)\y=(Random(1800)-900)/10*i/size
    *OriginVertex(i)\z=(Random(1800)-900)/10*i/size
  Next
  MP_CloseMemPrimitives(Entity)
EndProcedure
Macro getmod(ve)
  v#\m=Sqr(v#\x*v#\x+v#\y*v#\y+v#\z*v#\z)
EndMacro
Macro GetTransStepVector()
  trans(i)\x=(TargetVertex(i)\x-*OriginVertex(i)\x)/#TransSteps
  trans(i)\y=(TargetVertex(i)\y-*OriginVertex(i)\y)/#TransSteps
  trans(i)\z=(TargetVertex(i)\z-*OriginVertex(i)\z)/#TransSteps
EndMacro
Procedure object(obj.l)
  Protected i.l,xd.f
  MP_GetMemPrimitives(Entity)
  Select obj
    Case 2
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(xd)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(xd)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 3
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(xd)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 4
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Sin(i*360/size)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 5
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(xd)*100
        TargetVertex(i)\y=Cos(xd)*Sin(xd)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 6
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(xd)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(xd)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 7
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(i*360/size)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(xd)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 8
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(xd)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(xd)*Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 9
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Sin(i*360/size)*Cos(i*360/size)*Sin(xd)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(xd)*Cos(xd)*100
        TargetVertex(i)\z=Sin(xd)*Cos(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 10
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(xd)*100
        TargetVertex(i)\z=Cos(xd)*Sin(xd)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 11
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Sin(xd)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(xd)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 12
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(i*360/size)*Cos(xd)*100
        TargetVertex(i)\y=Sin(i*360/size)*Sin(xd)*100
        TargetVertex(i)\z=Sin(i*360/size)*Sin(xd)*Cos(i*360/size)*Cos(xd)*200 
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 13
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(xd)*100
        TargetVertex(i)\y=Sin(i*360/size)*Sin(xd)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 14
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Sin(xd)*Cos(xd)*100
        TargetVertex(i)\y=Sin(xd)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 15
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(xd)*Cos(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 16
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(i*360/size)*Sin(i*360/size)*100
        TargetVertex(i)\y=Sin(i*360/size)*Sin(xd)*Cos(i*360/size)*100
        TargetVertex(i)\z=Sin(xd)*Cos(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 17
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Cos(xd)*100
        TargetVertex(i)\y=Cos(i*360/size)*Sin(i*360/size)*100
        TargetVertex(i)\z=Sin(i*360/size)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 18
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Cos(xd)*Sin(i*360/size)*100
        TargetVertex(i)\y=Cos(xd)*Cos(i*360/size)*100
        TargetVertex(i)\z=Cos(xd)*100
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 19
      For i=0 To size
        xd=Random(1800)
        TargetVertex(i)\x=Sin(xd)*Cos(xd)*Sin(i*360/size)*200
        TargetVertex(i)\y=Sin(i*360/size)*Cos(i*360/size)*Sin(xd)*200
        TargetVertex(i)\z=Sin(i*360/size)*Cos(xd)*Cos(i*360/size)*200
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Case 20
      For i=0 To size
        TargetVertex(i)\x=Random(150)-75
        TargetVertex(i)\y=Random(150)-75
        TargetVertex(i)\z=Random(150)-75
        If Random(1)
          PokeF(TargetVertex(i)+SizeOf(float)*(Random(2)),75)
        Else
          PokeF(TargetVertex(i)+SizeOf(float)*(Random(2)),-75)
        EndIf
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
    Default
      For i=0 To size
        TargetVertex(i)\x=(Random(1800)-900)/10*i/size
        TargetVertex(i)\y=(Random(1800)-900)/10*i/size
        TargetVertex(i)\z=(Random(1800)-900)/10*i/size
        ;Get "trans" step-vector:
        GetTransStepVector()
      Next
  EndSelect
  Transcount=#TransSteps
  MP_CloseMemPrimitives(Entity)
EndProcedure
;==============================================================
MP_ScaleEntity(Entity,1.5,1.5,1.5)
;MP_MeshSetBlendColor(Entity,$FFFFFFFF)
;MP_MeshSetAlpha(Entity,3)
;MP_MeshAlphaSort(Entity);
MP_PrimitivesBlendingMode(Entity,5,2)
MP_PositionCamera(camera,0,0,500)
MP_CameraLookAt(camera,0,0,0)
DrawMatrix()
zz.f=0
While MP_KeyUp(#PB_Key_Escape) And WindowEvent()<>#PB_Event_CloseWindow
  If MP_KeyDown(#PB_Key_Up)
    zz.f+20
  ElseIf MP_KeyDown(#PB_Key_Down)
    zz.f-20
  EndIf
  contador.l+1
  If contador>=#ObjectStayTime
    contador=0
    objeto+1:If objeto>20:objeto=1:EndIf
    object(objeto.l)
  EndIf
  If TransCount>0
    TransCount-1
    If TransCount>0
      MP_GetMemPrimitives(Entity)
      For i.l=0 To size
        *OriginVertex(i)\x+trans(i)\x
        *OriginVertex(i)\y+trans(i)\y
        *OriginVertex(i)\z+trans(i)\z
      Next
      MP_CloseMemPrimitives(Entity)
    Else
      MP_GetMemPrimitives(Entity)
      For i.l=0 To size
        *OriginVertex(i)\x=TargetVertex(i)\x
        *OriginVertex(i)\y=TargetVertex(i)\y
        *OriginVertex(i)\z=TargetVertex(i)\z
      Next
      MP_CloseMemPrimitives(Entity)
    EndIf
  EndIf
  MP_PositionEntity(Entity,0,0,zz)
  MP_TurnEntity(Entity,1.333,0.5,0.2)
  MP_DrawText(1,1,"FPS="+Str(MP_FPS()))
  MP_RenderWorld()
  MP_Flip()
Wend
