General purpose 3D /2D functions Plotting

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

General purpose 3D /2D functions Plotting

Post by applePi »

the old stuff remake in a usefull way, which can be used as a general purpose functions plotting
the curves here are plotted inside a room so its appears as a toys.
use arrow keys and mouse to fly around the scene, space to toggle rotation, H to hide /unhide the room
the room are borrowed from here http://www.purebasic.fr/english/viewtop ... 36&t=53816
and every thing are borrowed from the official purebasic examples.

the first demo is to plot this formula
x^4 + y^4 + z^4 - (x^2 + y^2 + z^2)^2 + 3 (x^2 + y^2 + z^2) = 3
the variable ranges are :{x, -2, 2}, {y, -2, 2}, {z, -2, 2}
this formula displayed in mathematica site http://reference.wolfram.com/mathematic ... phics.html as a ContourPlot3D[...]
http://reference.wolfram.com/mathematic ... en/O_5.gif

suppose we don't know how to solve this equation for Y, and it is provided as is.
there are some solutions in the web but it is too difficult and the life are too short. the way i do here is a brute force method. ie imagining a box subdivided by 200 * 200 * 200 cells, we check every cell on every contour and if it fulfills approximately the formula then we give it a color.
just wait >= 15 seconds since we want to scan 8000,000 cells. this resembles hunting equation which inhabits the box.
Image

Code: Select all

Declare Room()
Declare CreateMatrix()
Declare CreateMatrixAgain()

Global.s text

#CameraSpeed = 1

Enumeration
  
  #mesh
  #entity
  #tex
  #light
  #camera

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,ScreenX,ScreenY,"3D Contour detector",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

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

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

Room() ; call the room building

text="x^4 + y^4 + z^4 - (x^2 + y^2 + z^2)^2 + 3 (x^2 + y^2 + z^2) = 3"
CreateText3D(23, text)
    Text3DColor(23, RGBA(255, 0, 0, 255))
    Text3DAlignment(23, #PB_Text3D_HorizontallyCentered)
    ScaleText3D(23, 0.05, 0.05, 0)
    AttachEntityObject(102, "", Text3DID(23))
    MoveText3D(23, 0, 0.4, 10)


;Create Light
CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,11,53,#PB_Absolute)
CameraLookAt(0, 0, -2, 0)

AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "wood.jpg"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)

;-Mesh
CreateMatrix()

temp=1
Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX.f = #CameraSpeed
        Else
          KeyX.f = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY.f = #CameraSpeed
        Else
          KeyY.f = 0
        EndIf
        
      If KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
        temp ! 1
      EndIf
      
      If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  
      EndIf  
  roty - temp : ;roty-temp: roty-temp
  
  RotateEntity(#entity, rotx-0, roty/2, rotz,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      Text3DCaption(23, text)
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  Protected.f x, z
    
  NbX=200
  NbZ=200
  NbY=200
  
  ;xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
  xMin.f = -2 : yMin.f = -2: zMin.f = -2 : xMax.f = 2: yMax = 2 : zMax = 2
  ;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
  range = xMax - xMin
  step1.f = range / NbX
  x.f = xMin: z.f = zMin : y.f = yMin
  
  For c = 0 To NbY
  For b=0 To NbZ
    
    For a=0 To NbX
      
        
      k.f = Pow(x,4) + Pow(y,4) + Pow(z,4) - Pow(x*x + y*y + z*z,2) + 3 * (x*x + y*y + z*z)
      If k - 3.0 <= 0.1 And k - 3.0 >= -0.1 ; the range we want to check the formula value
      ;k.f = Sin(10*(x*x+z*z))/10 - y
      ;If k <= 0.1 And k >= -0.1
      
        MeshVertexPosition(x, y, z) 
        MeshVertexNormal(0,1,0) 
        MeshVertexTextureCoordinate(a/NbX, b/Nbz)
      EndIf
      x.f + step1
      
    Next a
    
    
    x = xMin
    z.f + step1
    
  Next b
  x = xMin
  z = zMin
  y + step1
Next c
  
EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,-4,0)
  
  ScaleEntity(#entity,4,4,4)
  
EndProcedure

Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


Tex  = LoadTexture(#PB_Any, "clouds.jpg")
Mat = CreateMaterial(#PB_Any, TextureID(Tex))
MaterialBlendingMode(Mat, #PB_Material_AlphaBlend)
SetMaterialColor(Mat, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 150))
;Create Ceiling
CreateTexture(4,32,32)
  StartDrawing(TextureOutput(4))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  ;CreateMaterial(4,TextureID(Mat))
  ;MaterialCullingMode(4, #PB_Material_NoCulling)
CreateCube(103,1)
CreateEntity(103,MeshID(103),MaterialID(Mat),0,16,0)
ScaleEntity(103,36.5,1,32)

;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure


Procedure CreateMatrixAgain()
  
  FreeEntity(#entity)
  CreateMatrix()
  
EndProcedure
      

the second demo is a general purpose equations plotter
there is 9 equations press 1 to 9 to change the display, if you press 9 you will plot a 2D curve (y = x ^ 2)
Image
there is an issue with equatoin 6 y = 0.7/Log(x*x+z*z)+0.6 , it has discontinuity when Denominator = 0 somewhere so we added a condition to let it continue to draw
all equations are from http://www.benjoffe.com/code/tools/functions3d/examples
Image

Code: Select all

Declare Room()
Declare CreateMatrix()
Declare CreateMatrixAgain()

Global choice = 8 ; to choose equation 8 for display
Global.s text

#PB_Shadow_TextureModulative = 5
#CameraSpeed = 1
#SQRT13 = 0.57735026


Enumeration
  #mainwin
  #mesh
  #entity
  #tex
  #light
  #camera

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,ScreenX,ScreenY,"3D curves Plot",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

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

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

Room() ; call the room building

text="Sin(10*(x*x+z*z))/10"
CreateText3D(23, text)
    Text3DColor(23, RGBA(255, 0, 0, 255))
    Text3DAlignment(23, #PB_Text3D_HorizontallyCentered)
    ScaleText3D(23, 0.05, 0.05, 0)
    AttachEntityObject(102, "", Text3DID(23))
    MoveText3D(23, 0, 0.4, 10)

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,11,53,#PB_Absolute)
CameraLookAt(0, 0, -2, 0)
    
;WorldShadows(#PB_Shadow_TextureModulative, 100, RGB(100, 200, 200), 2048)
;WorldShadows(#PB_Shadow_Modulative  , -1, RGB(100, 100, 100))

AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "wood.jpg"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)

;-Mesh
CreateMatrix()

temp=1
Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  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 KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
        temp ! 1
      EndIf
      
      If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  

          
      EndIf
      
      If KeyboardReleased(#PB_Key_1)
        choice = 1 : temp=1: CreateMatrixAgain()
        ElseIf KeyboardPushed(#PB_Key_2)
          choice = 2 : temp=1: CreateMatrixAgain()
          ElseIf KeyboardPushed(#PB_Key_3)
            choice = 3 : temp=1: CreateMatrixAgain()
            ElseIf KeyboardPushed(#PB_Key_4)
              choice = 4 : temp=1: CreateMatrixAgain()
              ElseIf KeyboardPushed(#PB_Key_5)
                choice = 5 : temp=1: CreateMatrixAgain()
                ElseIf KeyboardPushed(#PB_Key_6)
                  choice = 6 : temp=1: CreateMatrixAgain()
                  ElseIf KeyboardPushed(#PB_Key_7)
                    choice = 7 : temp=1: CreateMatrixAgain()
                    ElseIf KeyboardPushed(#PB_Key_8)
                      choice = 8: temp=1: CreateMatrixAgain()
                      ElseIf KeyboardPushed(#PB_Key_9)
                       choice = 9: temp=1: CreateMatrixAgain()

      EndIf  
  
  roty - temp : ;roty-temp: roty-temp
  RotateEntity(#entity, rotx-0, roty/2, rotz,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      Text3DCaption(23, text)
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  Protected.f x, z
  NbX=600
  NbZ=600
  NbY=600
  
  xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
  ;xMin.f = -3 : yMin.f = -3: zMin.f = -3 : xMax.f = 3: yMax = 3 : zMax = 3
  ;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
  range = xMax - xMin
  step1.f = range / NbX
  x.f = xMin: z.f = zMin : y.f = yMin
  ;choice = 8
  For b=0 To NbZ
    
    For a=0 To NbX
      Select choice
          Case 1
            y.f = ((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2 
            text = "((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2 "
          Case 2
            y.f = ATan ((z*z*z)/4 - (x*x))
            text = "ATan ((z*z*z)/4 - (x*x))"
          Case 3
            y.f = 1/(15*(x*x+z*z))
            text = "1/(15*(x*x+z*z))"
          Case 4
            y.f = Sin(5*x)*Cos(5*z)/5
            text = "Sin(5*x)*Cos(5*z)/5"
          Case 5
            y.f = 0.75/Exp((x*5)*(x*5)*(z*5)*(z*5))
            text = "0.75/Exp((x*5)*(x*5)*(z*5)*(z*5))"
          Case 6
            tmp.f = Log(x*x+z*z)+0.6
            y.f = 0.7/tmp 
             If tmp = 0 ; this is only for this equation, since it has a discontuity
               ;relax
               Else
                 y.f = 0.7/tmp
             EndIf
             text = "Log(x*x+z*z)+0.6" 
          Case 7
            y.f = 0.2
            text = "y = 0.2"
          Case 8
            y.f = Sin(10*(x*x+z*z))/10 
            text = "Sin(10*(x*x+z*z))/10 "
          Case 9
            y.f = x * x
            NbZ = 0 ; for 2D plot to exit the outer Loop
            text = "x ^ 2"  
      EndSelect
                      
      MeshVertexPosition(x, y, z) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/NbX, b/Nbz)
      x.f + step1
    Next a
    
    x = xMin
    z.f + step1
  Next b
  
  
EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,-8,0)
  ScaleEntity(#entity,10,13,10)
  
EndProcedure

Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


Tex  = LoadTexture(#PB_Any, "clouds.jpg")
Mat = CreateMaterial(#PB_Any, TextureID(Tex))
MaterialBlendingMode(Mat, #PB_Material_AlphaBlend)
SetMaterialColor(Mat, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 150))
;Create Ceiling
CreateTexture(4,32,32)
  StartDrawing(TextureOutput(4))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  ;CreateMaterial(4,TextureID(Mat))
  ;MaterialCullingMode(4, #PB_Material_NoCulling)
CreateCube(103,1)
CreateEntity(103,MeshID(103),MaterialID(Mat),0,16,0)
ScaleEntity(103,36.5,1,32)

;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure

Procedure CreateMatrixAgain()
  FreeEntity(#entity)
  CreateMatrix()
EndProcedure
      

the third demo is the same as the second demo but the points are connected with tiny triangles and textured so the curve have now a body, the surrounding box is from one of the official examples. i have added several equations from line 157- uncomment it to try, also try other plot ranges for the x,z line 146 .

if any demo does not run try changing compiler options -> Library subsystem to Opengl
Image

Code: Select all

Declare CreateMatrix()
Declare Room()
Define Color = RGB(255, 0, 0)
#PB_Shadow_TextureModulative = 5
#CameraSpeed = 1

Global.f x1,x2,z1,z2,y1,y2

Enumeration
  
  #mesh
  #entity
  #tex
  #light
  #camera

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,ScreenX,ScreenY,"3D curves textured",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

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

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

Room() ; build a room for the curve

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,8,53,#PB_Absolute)
CameraLookAt(0, 0, -2, 0)
    
WorldShadows(#PB_Shadow_Modulative  , -1, RGB(100, 100, 100))

AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "wood.jpg"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)
;MaterialShadingMode(#tex, #PB_Material_Wireframe     )
CreateMesh(#mesh, #PB_Mesh_TriangleList, #PB_Mesh_Static)

;-Mesh
CreateMatrix()
Global node = CreateNode(#PB_Any)
AttachNodeObject(node, EntityID(#entity))

temp=1

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  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 KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
    temp ! 1
  EndIf
  If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  

          
      EndIf
  
  roty - temp : ;roty-temp: roty-temp
  RotateNode(node, rotx-0, roty/2, rotz,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
     ;BoudingBox
      x1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxX)
      y1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxY)
      z1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxZ)
      x2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxX)
      y2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxY)
      z2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxZ)
      
 ;Bottom
      CreateLine3D(10, x1, y1, z1, Color, x2, y1, z1, Color)
      CreateLine3D(11, x2, y1, z1, Color, x2, y1, z2, Color)
      CreateLine3D(12, x2, y1, z2, Color, x1, y1, z2, Color)
      CreateLine3D(13, x1, y1, z2, Color, x1, y1, z1, Color)
      ;Top
      CreateLine3D(14, x1, y2, z1, Color, x2, y2, z1, Color)
      CreateLine3D(15, x2, y2, z1, Color, x2, y2, z2, Color)
      CreateLine3D(16, x2, y2, z2, Color, x1, y2, z2, Color)
      CreateLine3D(17, x1, y2, z2, Color, x1, y2, z1, Color)
      ;Edge
      CreateLine3D(18, x1, y1, z1, Color, x1, y2, z1, Color)
      CreateLine3D(19, x2, y1, z1, Color, x2, y2, z1, Color)    
      CreateLine3D(20, x2, y1, z2, Color, x2, y2, z2, Color)
      CreateLine3D(21, x1, y1, z2, Color, x1, y2, z2, Color)
        
      
     
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  Protected.f x, z
  NbX=180
  NbZ=180
    
  xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
  ;xMin.f = -3 : yMin.f = -3: zMin.f = -3 : xMax.f = 3: yMax = 3 : zMax = 3
  ;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
  range = xMax - xMin
  step1.f = range / NbX
  x.f = xMin: z.f = zMin : y.f = yMin
  For b=0 To NbZ
    
    For a=0 To NbX
      
      ;les coordonnées de vertex
      ;y.f = ((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2 
      ;y.f = 1
      ;y.f = ATan ((z*z*z)/4 - (x*x))
      ;y.f = 1/(15*(x*x+z*z))
      ;y.f = Sin(5*x)*Cos(5*z)/5
      ;y.f = 0.75/Exp((x*5)*(x*5)*(z*5)*(z*5))
      ;y.f = 0.2
      y.f = Sin(10*(x*x+z*z))/10
      MeshVertexPosition(x, y, z) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/NbX, b/Nbz)
      x.f + step1
    Next a
    
    x = xMin
    z.f + step1
  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(1)
  
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,-8,0)
  ScaleEntity(#entity,10,13,10)
  
      
EndProcedure


Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


Tex  = LoadTexture(#PB_Any, "clouds.jpg")
Mat = CreateMaterial(#PB_Any, TextureID(Tex))
MaterialBlendingMode(Mat, #PB_Material_AlphaBlend)
SetMaterialColor(Mat, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 150))
;Create Ceiling
CreateTexture(4,32,32)
  StartDrawing(TextureOutput(4))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  ;CreateMaterial(4,TextureID(Mat))
  ;MaterialCullingMode(4, #PB_Material_NoCulling)
CreateCube(103,1)
CreateEntity(103,MeshID(103),MaterialID(Mat),0,16,0)
ScaleEntity(103,36.5,1,32)

;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure
Last edited by applePi on Wed Jan 01, 2014 7:44 am, edited 2 times in total.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: General purpose 3D /2D functions Plotting

Post by VB6_to_PBx »

applePi ,
your last Code with Shadow
is most impressive !!!

Thanks for sharing
and have many prosperous New Years to come !!!
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: General purpose 3D /2D functions Plotting

Post by Samuel »

Very nice examples, applePi. :D
Have a Happy New Year!
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: General purpose 3D /2D functions Plotting

Post by applePi »

Thank you VB6_to_PBx and Samuel
happy new year and every year
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: General purpose 3D /2D functions Plotting

Post by Mythros »

@applepi: Is this a point cloud?! O_O

AMAZING DEMO! :D KUDOS!
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: General purpose 3D /2D functions Plotting

Post by applePi »

we can consider the example 1 and 2 above a points cloud, but we can't give it physics.
the curve points in the last example are connected with triangles, so it has entity, and the entity can have a physics. and this physics are convex if it is not static. so you can't put a small sphere in the cavities and zigzags of the 3D curve unless it is static
note that we haven't done delauny triangulation, it is just an ordered triangles over a plane, then we change its y's as a function of x, z to represent the math equation.
the physics are added to the curve and the room floor and ceiling, so the curve will fall to the floor, press key 'F' to give the curve some push and it will fly up to the ceiling, it can be considered a game. now i see this example can be used for the educational purposes so the learners can play with their creations, try different commented equations from line 169 and down.( some curves will stuck on the ceiling).
the keys used : arrows/mouse to move camera
H :hide the room,........... space : toggle rotation,...... F apply force to the 3D curve to make it fly to the ceiling

Code: Select all

Declare CreateMatrix()
Declare Room()
Define Color = RGB(255, 0, 0)
#PB_Shadow_TextureModulative = 5
#CameraSpeed = 1

Global.f x1,x2,z1,z2,y1,y2

Enumeration
  
  #mesh
  #entity
  #tex
  #light
  #camera

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,ScreenX,ScreenY,"3D curves textured",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

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

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

Room() ; build a room for the curve

EntityPhysicBody(100, #PB_Entity_StaticBody, 2, 0, 1)
EntityPhysicBody(101, #PB_Entity_StaticBody, 2, 0, 1)
EntityPhysicBody(102, #PB_Entity_StaticBody, 2, 0, 1)
EntityPhysicBody(103, #PB_Entity_StaticBody, 2, 0, 1) ; physics for the ceiling
EntityPhysicBody(104, #PB_Entity_StaticBody, 2, 0, 1) ;gives physics to the room floor

CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,8,53,#PB_Absolute)
CameraLookAt(0, 0, -2, 0)
    
WorldShadows(#PB_Shadow_Modulative  , -1, RGB(100, 100, 100))

AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "wood.jpg"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)
;MaterialShadingMode(#tex, #PB_Material_Wireframe     )
CreateMesh(#mesh, #PB_Mesh_TriangleList, #PB_Mesh_Static)

;-Mesh
CreateMatrix()
Global node = CreateNode(#PB_Any)
AttachNodeObject(node, EntityID(#entity))

EntityPhysicBody(#entity, #PB_Entity_ConvexHullBody , 2, 1, 1)
temp=1

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  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 KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
    temp ! 1
  EndIf
  If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  

          
   EndIf
      
   If KeyboardPushed(#PB_Key_F)
     ApplyEntityImpulse(#entity, 0, 1, 0 , 0, 0, 0)
   EndIf  

  
  roty - temp : ;roty-temp: roty-temp
  RotateNode(node, rotx-0, roty/2, rotz,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
     ;BoudingBox
      x1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxX)
      y1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxY)
      z1 = EntityBoundingBox(#entity, #PB_Entity_MinBoundingBoxZ)
      x2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxX)
      y2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxY)
      z2 = EntityBoundingBox(#entity, #PB_Entity_MaxBoundingBoxZ)
      
 ;Bottom
      CreateLine3D(10, x1, y1, z1, Color, x2, y1, z1, Color)
      CreateLine3D(11, x2, y1, z1, Color, x2, y1, z2, Color)
      CreateLine3D(12, x2, y1, z2, Color, x1, y1, z2, Color)
      CreateLine3D(13, x1, y1, z2, Color, x1, y1, z1, Color)
      ;Top
      CreateLine3D(14, x1, y2, z1, Color, x2, y2, z1, Color)
      CreateLine3D(15, x2, y2, z1, Color, x2, y2, z2, Color)
      CreateLine3D(16, x2, y2, z2, Color, x1, y2, z2, Color)
      CreateLine3D(17, x1, y2, z2, Color, x1, y2, z1, Color)
      ;Edge
      CreateLine3D(18, x1, y1, z1, Color, x1, y2, z1, Color)
      CreateLine3D(19, x2, y1, z1, Color, x2, y2, z1, Color)    
      CreateLine3D(20, x2, y1, z2, Color, x2, y2, z2, Color)
      CreateLine3D(21, x1, y1, z2, Color, x1, y2, z2, Color)
        
      
     
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  Protected.f x, z
  NbX=180
  NbZ=180
    
  xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
  ;xMin.f = -3 : yMin.f = -3: zMin.f = -3 : xMax.f = 3: yMax = 3 : zMax = 3
  ;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
  range = xMax - xMin
  step1.f = range / NbX
  x.f = xMin: z.f = zMin : y.f = yMin
  For b=0 To NbZ
    
    For a=0 To NbX
      
      ;les coordonnées de vertex
      ;y.f = ((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2 
      ;y.f = 1
      ;y.f = ATan ((z*z*z)/4 - (x*x))
      ;y.f = 1/(15*(x*x+z*z))
      ;y.f = Sin(5*x)*Cos(5*z)/5
      ;y.f = 0.75/Exp((x*5)*(x*5)*(z*5)*(z*5))
      ;y.f = 0.2
      y.f = Sin(10*(x*x+z*z))/10
      MeshVertexPosition(x, y, z) 
      MeshVertexNormal(0,1,0) 
      MeshVertexTextureCoordinate(a/NbX, b/Nbz)
      x.f + step1
    Next a
    
    x = xMin
    z.f + step1
  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(1)
  
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,3,0)
  ScaleEntity(#entity,10,13,10)
  
      
EndProcedure


Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


Tex  = LoadTexture(#PB_Any, "clouds.jpg")
Mat = CreateMaterial(#PB_Any, TextureID(Tex))
MaterialBlendingMode(Mat, #PB_Material_AlphaBlend)
SetMaterialColor(Mat, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 150))
;Create Ceiling
CreateTexture(4,32,32)
  StartDrawing(TextureOutput(4))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  ;CreateMaterial(4,TextureID(Mat))
  ;MaterialCullingMode(4, #PB_Material_NoCulling)
CreateCube(103,1)
CreateEntity(103,MeshID(103),MaterialID(Mat),0,16,0)
ScaleEntity(103,36.5,1,32)

;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure
Post Reply