Some Curves

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

Some Curves

Post by applePi »

some Curves

v5.46 to 5.70

Code: Select all

Declare DrawWaves()
Declare SineWave()
Declare CreateMatrix()
Declare toon_triangle()
Declare traffic_triangle()

#CameraSpeed = 0.1
Define.f KeyX, KeyY, MouseX, MouseY
Global.f FrameTime

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

OpenWindow(0, 0, 0, 800, 600, "Shapes with MeshVertexPosition and Meshes attached to Nodes .... .. use mouse and keys to move the camera", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

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()

CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 2, 8)
CameraLookAt(0, 0,0,0)

CreateLight(1, RGB(220,220,220), 0,100,20)

CreateMaterial(1, LoadTexture(1, "White.jpg"))
DisableMaterialLighting(1, #True)

CreateMaterial(2, LoadTexture(2, "snow_1024.jpg"))
MaterialBlendingMode(2, #PB_Material_AlphaBlend)
SetMaterialColor(2, #PB_Material_DiffuseColor, RGBA(0, 200, 100, 100))
CreateMaterial(3, LoadTexture(3, "RustyBarrel.png"))

CreateCube(500, 1)

;create ground entity using the Cube mesh number 500 created above 
CreateEntity(500, MeshID(500), #PB_Material_None, 0, -0.9, 0)
; and then we scale it to resemble a ground
ScaleEntity(500, 10,0.5,10)

;the Museum
CreateEntity(510, MeshID(500),MaterialID(2), 1.2,1,-1)
ScaleEntity(510, 0.2,3,4)
CreateEntity(520, MeshID(500),MaterialID(2), -1.2,1,-1)
ScaleEntity(520, 0.2,3,4)
CreateEntity(530, MeshID(500),MaterialID(2), 0,1,-3)
ScaleEntity(530, 2.6,3,0.2)
CreateEntity(540, MeshID(500),MaterialID(2), 0,2.6,-1)
ScaleEntity(540, 2.6,0.2,4)

antiques = LoadMesh(#PB_Any , "facial.mesh")
antiques = CreateEntity(#PB_Any, MeshID(antiques),MaterialID(3), 0,-0.3,-0.8)
ScaleEntity(antiques, 0.04,0.04,0.04)

;-Mesh
CreateMatrix()
glLineWidth_(2) ; to get thick lines compile with opengl subsystem

Repeat
    Repeat
    event = WindowEvent()
  Until event = 0
  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
        
        
        
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf

      UpdateMesh(0, 0)
      DrawWaves()
      FinishMesh(#False)
        
      UpdateMesh(3, 0)
      SineWave()
      FinishMesh(#False)
   
      RotateNode(2,0,-1,0,#PB_Relative)
      
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative) 
            
      RenderWorld()
      
      FlipBuffers()


  
    Until KeyboardPushed(#PB_Key_Escape)
    
    ;-Procedures
Procedure DrawWaves()
  FrameTime.f = ElapsedMilliseconds()/800
  Protected.f x,y,x1,x2,y1,y2,nextX,nextY, inc
  y = 0.0: x = -1: n=0
  inc = 0.25
  While y<=2
     x.f = -1
     nextX = -1
     
     While x<=0.75
       curX.f = nextX
       nextX = x+inc
       
        x1 = x
        y1 = y+Sin(FrameTime+y*y+curX*curX+curX)/5
        x2 = nextX
        y2 = nextY+Sin(FrameTime+nextY*nextY+nextX*nextX+nextX)/5
     
        MeshVertexPosition(x1, y1,  1)
        MeshVertexColor(RGB(0,255,0))
        MeshVertexPosition(x2, y2,  1)
        MeshVertexColor(RGB(0,255,0))
                  
          x+inc
          n+1
      Wend
      
      nextY = y+inc
            
      y+inc
            
    Wend
    ;Debug n 
  EndProcedure 
  
    

Procedure toon_triangle()
  FrameTime.f = ElapsedMilliseconds()/800
  Protected.f x,y, inc, s , s2, t 
  Protected.l k
  s =  1/19: s2 = 1/6 ; toon triangle
  k = 3
  ;s = 0: s2 = 1/19 ; hexagon
  ;k = 6
  
  inc = 2*#PI/200 
  

     While t <= 2.000001*#PI  
       
       x = s*Sin(t)* Sin(k* t) - s* Cos(k* t)* Cos(t) + Cos(t) + s2* Sin(k* t)* Cos(t) - s2* Sin(t)* Cos(k* t)
       y = Sin(t) + s2* Sin(t)* Sin(k* t) + s2* Cos(t)* Cos(k* t) - s* Sin(t)* Cos(k* t) - s* Sin(k* t)* Cos(t)
       
       MeshVertexPosition(x, y,  0)
       MeshVertexColor(RGB(255,0,0))
  
       t + inc 
     Wend

  
   EndProcedure
Procedure traffic_triangle()
  FrameTime.f = ElapsedMilliseconds()/800
  Protected.f x,y,inc, s , s2, t 
  Protected k,l
  s =  0:  s2 = 1/3 ; triangle
  k = 3
  
  ;s = 0: s2 = 1/19 ; hexagon
  ;k = 6
  
  inc = 2*#PI/200 

     While t <= 2.000001*#PI  
       
       x = s*Sin(t)* Sin(k* t) - s* Cos(k* t)* Cos(t) + Cos(t) + s2* Sin(k* t)* Cos(t) - s2* Sin(t)* Cos(k* t)
       y = Sin(t) + s2* Sin(t)* Sin(k* t) + s2* Cos(t)* Cos(k* t) - s* Sin(t)* Cos(k* t) - s* Sin(k* t)* Cos(t)
       
       MeshVertexPosition(x, y,  0)
       MeshVertexColor(RGB(255,0,0))
  
       t + inc 
     Wend
      
 
    EndProcedure    

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_LineList, #PB_Mesh_Dynamic)
  DrawWaves()
  FinishMesh(#False)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  AttachNodeObject(0, MeshID(0))
  
  CreateMesh(1, #PB_Mesh_LineStrip, #PB_Mesh_Dynamic)
  toon_triangle()
  FinishMesh(#False)
  SetMeshMaterial(1, MaterialID(1))
  
  CreateNode(1, -2.4,1,0)
  AttachNodeObject(1, MeshID(1))
  
  CreateMesh(2, #PB_Mesh_LineStrip, #PB_Mesh_Dynamic)
  traffic_triangle()
  FinishMesh(#False)
  SetMeshMaterial(2, MaterialID(1))
  
  CreateNode(2, 2.4,1,0)
  AttachNodeObject(2, MeshID(2))
  
  CreateMesh(3, #PB_Mesh_LineStrip, #PB_Mesh_Dynamic)
  SineWave()
  FinishMesh(#False)
  SetMeshMaterial(3, MaterialID(1))
  
  CreateNode(3, -2.4,1,-0.6)
  AttachNodeObject(3, MeshID(3))
  
  ScaleNode(3, 0.2, 0.2, 0.2)
EndProcedure

Procedure SineWave()
  FrameTime.f = ElapsedMilliseconds()/500
  Protected.f x,y,x1,x2,y1,y2,nextX,nextY, inc
  y = 0.0: x = -1: n=100
  inc = 0.05
  
     x.f = -#PI
     While x<=#PI

        y = Sin(FrameTime+x)+0.5
        MeshVertexPosition(x, y,  3)
        MeshVertexColor(RGB(0,255,0))
        x+inc
        
     Wend
    
  EndProcedure 
 
the non finalized graphics( or say mesh) attached to a Node (to make it visualized) is suitable for plotting live new and updated data, exactly like cinema frames. and also can be used to grow meshes (suitable to to show a growing trees )
the example is the PB MeshManualFlag.pb adapted slightly to show how to grow the mesh
if you want to capture the mesh (as obj model) even it is not finalized use GLXtractor described here viewtopic.php?f=36&t=53022#p520998
but the examples should be compiled with opengl subsystem
press X/Z to increase/decrease the mesh

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Mesh Manual - Flag 
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
; 
;


#CameraSpeed = 2
Global NbX=5
Global NbZ=5

Global.f AngleVague, WaveFrequency, WavePeriodX, WavePeriodZ, WaveAmplitude
WaveFrequency=3  ;=waves/second
WavePeriodX  =9  ;=1/Wave length
WavePeriodZ  =11 ;=1/Wave length
WaveAmplitude=3

Define.f KeyX, KeyY, MouseX, MouseY

Declare UpdateMatrix()
Declare CreateMatrix()


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

OpenWindow(0, 0, 0, 800, 600, "MeshManualFlag", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  ;Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
    
    ;-Material  
    GetScriptMaterial(1, "Scene/GroundBlend")
    MaterialCullingMode(1, 1)
    
    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,50,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
    
    ;- Skybox
    ;SkyBox("stevecube.jpg")
    
    CameraRenderMode(0, #PB_Camera_Wireframe)
    SetWindowTitle(0, "Press X or Z to increase/decrease mesh size")
    
    Repeat
      
    Repeat
     event = WindowEvent()
    Until event = 0
       
      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_X)
          NbX+1: NbZ+1
        ElseIf KeyboardPushed(#PB_Key_Z)
          NbX-1: NbZ-1
          If NbX < 1: NbX=1:EndIf
          If NbZ < 1: NbZ=1:EndIf
        EndIf
        
        
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)  
      
      ; Waves
      UpdateMatrix()
      AngleVague = AngleVague+WaveFrequency
            
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  


End

;-Procedures
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  
  For b=0 To NbZ
    For a=0 To NbX
      ;les coordonnées de vertex
      y.f=Sin(Radian((AngleVague+a*WavePeriodX+b*WavePeriodZ)))*WaveAmplitude 
      MeshVertexPosition(a - NbX/2, y, b - NbZ/2) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/NbX, b/NbZ)
    Next a
  Next b
    
  Nb=NbX+1
  For b=0 To NbZ-1
    For a=0 To NbX-1
      P1=a+(b*Nb)
      P2=P1+1
      P3=a+(b+1)*Nb
      P4=P3+1
  
      MeshFace(P3, P2, P1)
      MeshFace(P2, P3, P4)
    Next
  Next
EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  DrawMatrix()
  FinishMesh(#False)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  AttachNodeObject(0, MeshID(0))
  ScaleNode(0, 2, 2, 2)
EndProcedure

Procedure UpdateMatrix()
  UpdateMesh(0, 0)
  DrawMatrix()
  FinishMesh(#False)
EndProcedure
Edit:
to finalize the mesh attached to node and then create an entity: do this:
FreeNode(0) ; free the node in which the mesh attached to
UpdateMesh(0, 0)
DrawMatrix() ; draw the mesh the final time
FinishMesh(#True)
CreateEntity(0, MeshID(0), MaterialID(1))

to animate a finalized mesh then look PB example SetMeshData.pb

press 'F' any time you want to finalize the mesh

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Mesh Manual - Flag 
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
; 
;


#CameraSpeed = 2
Global NbX=5
Global NbZ=5
Global ss

Global.f AngleVague, WaveFrequency, WavePeriodX, WavePeriodZ, WaveAmplitude
WaveFrequency=3  ;=waves/second
WavePeriodX  =9  ;=1/Wave length
WavePeriodZ  =11 ;=1/Wave length
WaveAmplitude=3

Define.f KeyX, KeyY, MouseX, MouseY

Declare DrawMatrix()
Declare UpdateMatrix()
Declare CreateMatrix()


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

OpenWindow(0, 0, 0, 800, 600, "MeshManualFlag, ... press 'F' to finalize the mesh", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  ;Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
    
    ;-Material  
    GetScriptMaterial(1, "Scene/GroundBlend")
    MaterialCullingMode(1, 1)
    
    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,50,80, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 20, 150, 120)
    AmbientColor(RGB(90, 90, 90))
    
    ;- Skybox
    ;SkyBox("stevecube.jpg")
    
    CameraRenderMode(0, #PB_Camera_Wireframe)
    SetWindowTitle(0, "Press X or Z to increase/decrease mesh size . ... Press 'F' to finalize the Mesh and create entity any time")
    
    Repeat
      
    Repeat
     event = WindowEvent()
    Until event = 0
       
      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_X)
          NbX+1: NbZ+1
        ElseIf KeyboardPushed(#PB_Key_Z)
          NbX-1: NbZ-1
          If NbX < 1: NbX=1:EndIf
          If NbZ < 1: NbZ=1:EndIf
        EndIf
        
        If KeyboardReleased(#PB_Key_F)
          If ss = 0
          FreeNode(0)
          UpdateMesh(0, 0)
          DrawMatrix()
          FinishMesh(#True)
          CreateEntity(0, MeshID(0), MaterialID(1))
          ScaleEntity(0, 2, 2, 2)
          ss = 1
          EndIf
        EndIf
     
        
      EndIf
      
      If ss = 1
        RotateEntity(0, 0,1,0, #PB_Relative)
      Else
        ; Waves
        UpdateMatrix()
        AngleVague = AngleVague+WaveFrequency 
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)
      EndIf
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)  
      
                        
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  


End

;-Procedures
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  
  For b=0 To NbZ
    For a=0 To NbX
      ;les coordonnées de vertex
      y.f=Sin(Radian((AngleVague+a*WavePeriodX+b*WavePeriodZ)))*WaveAmplitude 
      MeshVertexPosition(a - NbX/2, y, b - NbZ/2) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/NbX, b/NbZ)
    Next a
  Next b
    
  Nb=NbX+1
  For b=0 To NbZ-1
    For a=0 To NbX-1
      P1=a+(b*Nb)
      P2=P1+1
      P3=a+(b+1)*Nb
      P4=P3+1
  
      MeshFace(P3, P2, P1)
      MeshFace(P2, P3, P4)
    Next
  Next
EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  DrawMatrix()
  FinishMesh(#False)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  AttachNodeObject(0, MeshID(0))
  ScaleNode(0, 2, 2, 2)
EndProcedure

Procedure UpdateMatrix()
  UpdateMesh(0, 0)
  DrawMatrix()
  FinishMesh(#False)
EndProcedure
User avatar
Psychophanta
Addict
Addict
Posts: 4968
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Some Curves

Post by Psychophanta »

It is useful to display real time samples from any source in a 3D screen in a cartesian coords way.
Just attaching the lines streams meshes to a node at which the camera is also attached.
Really is what i had looking for, so thanks :)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply