more animated 3D Curves

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

more animated 3D Curves

Post by applePi »

just another animated curves exhibition using Comtois example posted here http://www.purebasic.fr/english/viewtop ... 05#p415252 i have added a mirror to reflect the animated curve using purebasic cube mapping, it is made from a thin sphere i haven't yet tried a thin cube .the mirror can catch the eyes of viewers more than the original curve, a great unexpected new features.
the second code is for me for a future reference to enable myself to visualize the sequence of vertices when we draw something using a plane ,it is a plane only 4X4 and it has 25 vertices just press v to add 0.3 to the specified vertex ,look line 72 to 75
at last is a classic example: just deforming a sphere created by CreateSphere.
but in the first example why we don't use CreatePlane instead of a plane generator code i am still studying this since from the first impression it does not animate but can be deformed at first and then can't be deformed again.
use as a meterial this script save it as cubemap.material neccessary for the mirror working

Code: Select all

material CubeMapMaterial
  {
    technique
    {
      pass
      {
        texture_unit
        {
          cubic_texture CubeMapTexture combinedUVW
          tex_address_mode clamp
          env_map cubic_reflection
        }
      }
    }
  }
use these two textures for the first example
snake.png
Image
silver.jpg
Image


Image

Image

Code: Select all

Enumeration
  #mainwin = 10
  #mat
  #sphere
  #sphereMat
  #mirror
  
EndEnumeration

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Define.Vector3 Normal, v1, v2, v3

#CameraSpeed = 1
#NbX=30
#NbZ=30

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Declare UpdateMatrix()
Declare CreateMatrix()

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press W for wire frame, press S for Solid ,____ zoom by keyboard ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)   
   
   Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(".", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;-Material 
    ;GetScriptMaterial(1, "Scene/GroundBlend")
    CreateMaterial(1, LoadTexture(0, "snake.png"))
    DisableMaterialLighting(1, 1)
    MaterialCullingMode(1, 1)
    ;MaterialShadingMode(1, #PB_Material_Wireframe)
    SetMaterialColor(1, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))

    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,70,120, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(90, 0, 0))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 5, 15, 5)
    AmbientColor(RGB(255, 255, 255))
    CameraBackColor(0, RGB(0,100,100))

    CreateSphere(#sphere, 4)
    GetScriptMaterial(#sphereMat, "Color/Red")
    DisableMaterialLighting(#spheremat, 0)
    CreateEntity(#sphere, MeshID(#sphere), MaterialID(#spheremat))
    
  MyCubeMap = CreateCubeMapTexture(#PB_Any, 256, 256, "CubeMapTexture")
  GetScriptMaterial(2,"CubeMapMaterial")
  SetMaterialColor(2, #PB_Material_SelfIlluminationColor, $FFFFFF)
  
  Mirror = CreateSphere(#PB_Any, 20)
  Entity = CreateEntity(#PB_Any,MeshID(Mirror),MaterialID(2))
  ScaleEntity(Entity,1,2,0.2)
  RotateEntity(Entity, 0,-210,0)
  EntityCubeMapTexture(MyCubeMap, Entity)
  MoveEntity(Entity, 70,20,-50) 
  
    CreateMaterial(4, LoadTexture(4, "silver.jpg"))
    AddMaterialLayer(2, TextureID(4) , #PB_Material_Modulate)

    Repeat
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_W)
          MaterialShadingMode(1, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_S)
          MaterialShadingMode(1, #PB_Material_Solid)
          
        ElseIf KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
          ;Debug(MeshVertexCount(0, 0))
        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
      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
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure DrawMatrix()
  Protected.l a, b, c, Nb
  Protected.w P1, P2, P3, P4
    
  For b=0 To #Nbz
    For a=0 To #NbX
    dtime.f=ElapsedMilliseconds()/600
      
      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, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  CreateEntity(0, MeshID(0), #PB_Material_None)
  ScaleEntity(0, 3, 3, 3)
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0, 0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  
EndProcedure

Procedure UpdateMatrix()
  Protected a, b, c, i, j
  Protected N.Vector3, V1.Vector3, V2.Vector3, V3.Vector3
  Protected.f magSq, oneOverMag
  dtime.f=ElapsedMilliseconds()/600 ; reduce 600 to get speedier animation
  
  For b=0 To #Nbz
    For a=0 To #NbX
      xa.f=MeshData(c)\x
      zb.f=MeshData(c)\z
      ;yy.f= MeshData(c)\y
      ;new y vertex coordinates
      MeshData(c)\y=Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*2
      c + 1 ; vertex number
      ;xa.f=MeshData(481)\x
      ;zb.f=MeshData(481)\z
      ;yy.f = Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*2
      yy.f=Cos(dtime)*8
      MoveEntity(#sphere, 0 , yy, 0 ,#PB_Absolute)
    Next a
  Next b
  
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(0, 0)-1)
EndProcedure
testing the vertex
Image

Code: Select all

Enumeration
  #mainwin = 10
  
EndEnumeration

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Define.Vector3 Normal, v1, v2, v3
#CameraSpeed = 3
#NbX=4
#NbZ=4

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global ind.l

Declare UpdateMatrix()
Declare CreateMatrix()

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press V to displace a vertex , press W for wire frame, press S for Solid ,____ zoom by keyboard ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)   
   
   Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(".", #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;-Material 
    ;GetScriptMaterial(1, "Scene/GroundBlend")
    CreateMaterial(1, LoadTexture(0, "grass.jpg"))
    DisableMaterialLighting(1, 1)
    MaterialCullingMode(1, 1)
    MaterialShadingMode(1, #PB_Material_Wireframe)
    SetMaterialColor(1, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))

    ;-Mesh
    CreateMatrix()
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0,0,10,10, #PB_Absolute)
    CameraLookAt(0, 0, 0, 0)
    CameraBackColor(0, RGB(255,244,255))
    
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 5, 15, 5)
    AmbientColor(RGB(255, 255, 255))
    
  
    Repeat
      
      If ExamineKeyboard()
        
        If KeyboardReleased(#PB_Key_W)
          MaterialShadingMode(1, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_S)
          MaterialShadingMode(1, #PB_Material_Solid)
        ElseIf KeyboardReleased(#PB_Key_V) 
          MeshData(ind)\y = 0.3:ind+1
          If ind=MeshVertexCount(0, 0):ind=0:EndIf
          SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(0, 0)-1)
        
        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
      MoveCamera  (0, KeyX, 0, KeyY)
      RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative) 
            
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure DrawMatrix()
  Protected.l a, b, c, Nb
  Protected.w P1, P2, P3, P4
    
  For b=0 To #Nbz
    For a=0 To #NbX
    dtime.f=ElapsedMilliseconds()/600
      
      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, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
  
  CreateNode(0)
  CreateEntity(0, MeshID(0), #PB_Material_None)
  ScaleEntity(0, 3, 3, 3)
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0, 0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  
EndProcedure

sphere deform:

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA
   #mainwin
   #tex
   #sp
 EndEnumeration
Structure Vector3
  x.f
  y.f
  z.f
EndStructure
Define.Vector3 v1, v2, v3

Global x.f = 0
Global y.f = 10
Global z.f = -30
Global Dim MeshData.PB_MeshVertex(0)

Global amplitude.f = 0.01
Global frequency.f = 3.0
;Global offset.f

ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press W for wireframw, press S for Solid ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)


Define.f KeyX, KeyY

Declare Deform()

If InitEngine3D()
 
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Parse3DScripts()
 
  InitSprite()
  InitKeyboard()
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)
   
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
    DisableMaterialLighting(0, 1)
    SetMaterialColor(0, #PB_Material_DiffuseColor, RGB(255,255,255))
    
    MaterialShadingMode(0, #PB_Material_Wireframe     )
    MaterialCullingMode(0, #PB_Material_NoCulling)
    
    CreateSphere(#sp,4,64,64)
    CreateEntity(#sp,  MeshID(#sp), MaterialID(0))
    ;;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 8, #PB_Absolute)
    CameraFOV(0, 70)
    CameraBackColor(0, $330000)
    CameraLookAt(0,0,0,0)
       
    CreateLight(0, RGB(255,255,255), 5, 0, 10)
    AmbientColor(RGB(90, 90, 90))
    GetMeshData(#sp,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#sp)-1)   
    Repeat
      Event = WindowEvent()
      
      Deform()           
      If ExamineKeyboard()
       
        If KeyboardReleased(#PB_Key_W)
          MaterialShadingMode(0, #PB_Material_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_S)
          MaterialShadingMode(0, #PB_Material_Solid)
        EndIf
       
      EndIf
      rot.f+0.6
      
      RotateEntity(#sp,0,rot,0)
      
      RenderWorld()
      FlipBuffers()
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure Deform()
  ;warping a sphere
  dtime.f=ElapsedMilliseconds()/1000
  offset.f
  countvertices = MeshVertexCount(#sp)
  For i=0 To countvertices-1
    ;--------------------------------
        
    vx.f = MeshData(i)\x
    vy.f = MeshData(i)\y
    vz.f = MeshData(i)\z
    ;--------------------------------
    offset = amplitude * Sin((dtime + vx)*frequency)
   
    ;vx + offset
    vy + offset
    vz + offset
    
    MeshData(i)\y = vy
    MeshData(i)\z = vz     
  Next
  SetMeshData(#sp,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(#sp)-1)
  
   EndProcedure    
    
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: more animated 3D Curves

Post by Comtois »

added normals :)

Code: Select all

Enumeration
  #mainwin = 10
  #mat
  #sphere
  #sphereMat
  #mirror
  
EndEnumeration

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Macro SubVector3(n, v, w)
  n\x = v\x - w\x
  n\y = v\y - w\y
  n\z = v\z - w\z
EndMacro

Define.Vector3 Normal, v1, v2, v3

#CameraSpeed = 1
#NbX=30
#NbZ=30

Define.f KeyX, KeyY, MouseX, MouseY

Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)

Declare UpdateMatrix()
Declare CreateMatrix()

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "press W for wire frame, press S for Solid ,____ zoom by keyboard ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-60, 0, 0, 0)   
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  ;-Material
  CreateMaterial(1, LoadTexture(0, "snake.png"))
  MaterialCullingMode(1, 1)
  
  ;-Mesh
  CreateMatrix()
  
  ;-Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0,0,70,120, #PB_Absolute)
  CameraLookAt(0, 0, 0, 0)
  CameraBackColor(0, RGB(0,100,100))
  
  ;-Light
  CreateLight(0, RGB(255, 255, 255), 5, 25, 5)
  AmbientColor(RGB(30, 30, 30))
  
  
  CreateSphere(#sphere, 4)
  GetScriptMaterial(#sphereMat, "Color/Red")
  DisableMaterialLighting(#spheremat, 0)
  CreateEntity(#sphere, MeshID(#sphere), MaterialID(#spheremat))
  
  MyCubeMap = CreateCubeMapTexture(#PB_Any, 256, 256, "CubeMapTexture")
  GetScriptMaterial(2,"CubeMapMaterial")
  SetMaterialColor(2, #PB_Material_SelfIlluminationColor, $FFFFFF)
  
  Mirror = CreateSphere(#PB_Any, 20)
  Entity = CreateEntity(#PB_Any,MeshID(Mirror),MaterialID(2))
  ScaleEntity(Entity,1,2,0.2)
  RotateEntity(Entity, 0,-210,0)
  EntityCubeMapTexture(MyCubeMap, Entity)
  MoveEntity(Entity, 70,20,-50)
  
  CreateMaterial(4, LoadTexture(4, "silver.jpg"))
  AddMaterialLayer(2, TextureID(4) , #PB_Material_Modulate)
  
  Repeat
    
    If ExamineKeyboard()
      
      If KeyboardReleased(#PB_Key_W)
        MaterialShadingMode(1, #PB_Material_Wireframe)
      ElseIf KeyboardReleased(#PB_Key_S)
        MaterialShadingMode(1, #PB_Material_Solid)
        
      ElseIf KeyboardPushed(#PB_Key_Left)
        KeyX = -#CameraSpeed
        ;Debug(MeshVertexCount(0, 0))
      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
    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
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure DrawMatrix()
  Protected.l a, b, c, Nb
  Protected.w P1, P2, P3, P4
  
  For b=0 To #Nbz
    For a=0 To #NbX
      dtime.f=ElapsedMilliseconds()/600
      
      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, #True)
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
  
  ;CreateNode(0)
  CreateEntity(0, MeshID(0), #PB_Material_None)
  ScaleEntity(0, 3, 3, 3)
  
  GetMeshData(0,0, MeshData(), #PB_Mesh_Vertex, 0, MeshVertexCount(0, 0)-1)
  GetMeshData(0,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(0, 0)-1)
  
EndProcedure

Procedure NormalFace(*n.Vector3, *v1.Vector3, *v2.Vector3, *v3.Vector3)
  Protected.Vector3 v2v1, v3v1
  SubVector3(v2v1, *v2, *v1)
  SubVector3(v3v1, *v3, *v1)
  
  *n\x = v2v1\y * v3v1\z - v2v1\z * v3v1\y
  *n\y = v2v1\z * v3v1\x - v2v1\x * v3v1\z
  *n\z = v2v1\x * v3v1\y - v2v1\y * v3v1\x
EndProcedure  

Procedure NormalMesh()
  Protected a, b, c, i, j
  Protected N.Vector3, V1.Vector3, V2.Vector3, V3.Vector3
  Protected.f magSq, oneOverMag
  
  
  
  ;Calcul les normales
  For j=0 To ArraySize(MeshDataInd())-2 Step 3
    
    V1\x = MeshData(MeshDataInd(j  )\Index)\x
    V1\y = MeshData(MeshDataInd(j  )\Index)\y
    V1\z = MeshData(MeshDataInd(j  )\Index)\z
    V2\x = MeshData(MeshDataInd(j+1)\Index)\x
    V2\y = MeshData(MeshDataInd(j+1)\Index)\y
    V2\z = MeshData(MeshDataInd(j+1)\Index)\z 
    V3\x = MeshData(MeshDataInd(j+2)\Index)\x
    V3\y = MeshData(MeshDataInd(j+2)\Index)\y
    V3\z = MeshData(MeshDataInd(j+2)\Index)\z   
    NormalFace(@N, @V1, @V2, @V3)
    ;Normalize(@N)
    MeshData(MeshDataInd(j  )\Index)\NormalX + N\x
    MeshData(MeshDataInd(j  )\Index)\NormalY + N\y
    MeshData(MeshDataInd(j  )\Index)\NormalZ + N\z
    MeshData(MeshDataInd(j+1)\Index)\NormalX + N\x
    MeshData(MeshDataInd(j+1)\Index)\NormalY + N\y
    MeshData(MeshDataInd(j+1)\Index)\NormalZ + N\z 
    MeshData(MeshDataInd(j+2)\Index)\NormalX + N\x
    MeshData(MeshDataInd(j+2)\Index)\NormalY + N\y
    MeshData(MeshDataInd(j+2)\Index)\NormalZ + N\z     
  Next
  
  For i=0 To ArraySize(MeshData())
    
    magSq = MeshData(i)\NormalX * MeshData(i)\NormalX +
            MeshData(i)\NormalY * MeshData(i)\NormalY +
            MeshData(i)\NormalZ * MeshData(i)\NormalZ
    
    If magsq > 0
      oneOverMag = 1.0 / Sqr(magSq)
      MeshData(i)\NormalX * oneOverMag
      MeshData(i)\NormalY * oneOverMag
      MeshData(i)\NormalZ * oneOverMag
    EndIf
    
  Next
  
EndProcedure

Procedure UpdateMatrix()
  Protected a, b, c, i, j
  Protected N.Vector3, V1.Vector3, V2.Vector3, V3.Vector3
  Protected.f magSq, oneOverMag
  dtime.f=ElapsedMilliseconds()/600 ; reduce 600 to get speedier animation
  
  For b=0 To #Nbz
    For a=0 To #NbX
      xa.f=MeshData(c)\x
      zb.f=MeshData(c)\z
      ;yy.f= MeshData(c)\y
      ;new y vertex coordinates
      MeshData(c)\y=Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*2
      ;Init les normales
      MeshData(c)\NormalX = 0
      MeshData(c)\NormalY = 0
      MeshData(c)\NormalZ = 0
      c + 1 ; vertex number
      ;xa.f=MeshData(481)\x
      ;zb.f=MeshData(481)\z
      ;yy.f = Cos(dtime + (Sqr(Pow(0.5*xa,2)+Pow(0.5*zb,2))))*2
      yy.f=Cos(dtime)*8
      MoveEntity(#sphere, 0 , yy, 0 ,#PB_Absolute)
    Next a
  Next b
  
  NormalMesh()
  SetMeshData(0,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(0, 0)-1)
EndProcedure
Please correct my english
http://purebasic.developpez.com/
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: more animated 3D Curves

Post by applePi »

yes better scene thanks, in reality i was escaping the normals. those demos and the new 3D plotting features can be used in educational purposes in schools and universities, and i'm sure they will be great. i know one case (from a post) for this usage in an eastern europe country.
Olby
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Jan 12, 2009 10:33 am
Contact:

Re: more animated 3D Curves

Post by Olby »

applePi wrote:yes better scene thanks, in reality i was escaping the normals. those demos and the new 3D plotting features can be used in educational purposes in schools and universities, and i'm sure they will be great. i know one case (from a post) for this usage in an eastern europe country.
Which one if you don't mind telling?
Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: more animated 3D Curves

Post by applePi »

sorry olby , but it is a university thesis project to demonstrate differential geometry issue. the student find by googling the subject of Möbius strip and Klein bottle and he impressed by the graphics and its texturing. so he based his plotting routines for the differential geometry project on the 3D functions in PB . this is only what i was aware about .
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: more animated 3D Curves

Post by DK_PETER »

Lovely examples and easy to follow.

Thank you! :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Post Reply