Looking for information - How to create 3D animation

Everything related to 3D programming
DT2000
User
User
Posts: 15
Joined: Tue Dec 02, 2014 5:09 am

Looking for information - How to create 3D animation

Post by DT2000 »

My apologies for such a broad question.... but I am looking to find information on how to develop a 3D animated piece to run within my current project. I am not sure if I require additional software to develop a 3D mesh or structure for what I am after or whether it can be done directly using PB.

Essentially, I am looking to have floating ping pong style balls moving about on the screen in one of the windows I have written.

What do I need to use? Or is there something I can read to become better familiar with the concept and use of sprites and animation?

Unfortunately the "Help" section of the IDE does not provide a lot of clarify or insight into the workings or method to perform what I am trying to accomplish.

If possible... some insight would be very much appreciated.
"Occasionally stumped.... But never defeated!"
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Looking for information - How to create 3D animation

Post by applePi »

Hi
in the official examples there is : NodeAnimation.pb and NodeAnimation2.pb
a more complex example : download this demo from Comtois
http://herved25.free.fr/sources/magicofstonehengePB.zip
but in line 111 change 1. 5) to 1.5) i have tested the demo in PB v5.45 and v5.61 and works well, with music.
I am looking to have floating ping pong style balls moving about on the screen
i suggest this plan: create a ground with sides, several balls with physics body (CreateEntityBody) and in the main loop apply continuous force or pulses in xyz directions to each of the balls.
i have found this example (not optimized, just a showcase). there are many spheres inside a big static transparent sphere, it is giving force to the smaller spheres by illegal and unapproved method:
CreateEntityBody(#bigSphere, #PB_Entity_StaticBody , 1, 2, 2)
but when we press space key we do:
MoveEntity(#bigsphere,0,4,0)
to stop vibration press 'V' to do
MoveEntity(#bigsphere,0,0,0)
ie we are trying to move a static body, it happened it vibrates instead.

Code: Select all

Enumeration
   #Tex = 50
   #MAT
   #MAT_plane
   #plane
   #LIGHT
   #camera
   #mainwin
   #ball
   #bigSphere
EndEnumeration
Global Quit.b = #False
Global x.f = 0
Global y.f = 10
Global z.f = -30
Global zoom.f = 13
Global ballNum.l
#cameraSpeed = 0.4

Define.f KeyX, KeyY, MouseX, MouseY
Declare Gear_Make()
ExamineDesktops()
If OpenWindow(#mainwin, 0, 0, DesktopWidth(0), DesktopHeight(0), "space to start the machine ... V to stop , arrow keys + mouse: rotate camera  ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

;Initialize environment
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, DesktopWidth(0), DesktopHeight(0)-4, 0, 0, 0)
    
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
SetFrameRate(60)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Parse3DScripts()
; transparent Texture from example CreateTextureAlpha.pb in Purebasic 3D folder
    CreateTexture(#mat, 256, 256)
      StartDrawing(TextureOutput(#mat))
      DrawingMode(#PB_2DDrawing_AllChannels | #PB_2DDrawing_AlphaBlend)
      Box(0, 0, 256, 256, RGBA(0, 0, 0, 255))
      Box(0, 0, 256, 256, RGBA(100, 255, 0, 20)) 
      ;Circle(127, 127, 50, RGBA(0, 0, 0, 0))
      StopDrawing()
      CreateMaterial(#mat, TextureID(#mat))
      MaterialBlendingMode(#mat, #PB_Material_AlphaBlend)
      MaterialCullingMode(#MAT, #PB_Material_NoCulling)
      DisableMaterialLighting(#mat, 1)
      

CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Wood.jpg"))
;CreatePlane(#Mesh, TileSizeX, TileSizeZ, TileCountX, TileCountZ, TextureRepeatCountX, TextureRepeatCountZ)
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(#MAT_plane),  0, -1.5,-0.7)
RotateEntity(#plane, 6,0,0)

CreateEntityBody(#plane, #PB_Entity_StaticBody )  

CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(255,255,255))

CreateCamera(#camera, 0, 0, 100, 100)
MoveCamera(#camera, 0, 4, 13)
RotateCamera(#camera, -15, 0, 0)
;the mother of the jumping balls wich we will make 500 copies
CreateSphere(#ball, 1)
CreateEntity(#ball, MeshID(#ball),#PB_Material_None )
ScaleEntity(#ball, 0.1, 0.1, 0.1)
MoveEntity(#ball, 0,2,3) 
CreateEntityBody(#ball, #PB_Entity_SphereBody  , 1, 0.3, 1)

;material for the jumping balls
LoadTexture(0, "ground_diffuse.png")
CreateMaterial(0, TextureID(0))
DisableMaterialLighting(0, 1)
MaterialCullingMode(0, #PB_Material_NoCulling)
;make 501 small balls 
For i=0 To 500
  CopyEntity(#ball , 100+i) ; entities numbering begins from 100
      x.f = Random(1, 0)
      y.f= Random(3, 0)
      z.f = Random(2, 0)
          
      SetEntityMaterial(100+i, MaterialID(0))

  MoveEntity(100+i, x,y,z)
  CreateEntityBody(100+i, #PB_Entity_SphereBody  , 1, 0.2, 1)
Next

CreateSphere(#bigSphere, 3, 20, 20)
CreateEntity(#bigSphere, MeshID(#bigSphere), MaterialID(#MAT), 0,3.5,1 )
CreateEntityBody(#bigSphere, #PB_Entity_StaticBody  , 1, 2, 2)


EndIf
WorldGravity(-9.8)
;WorldGravity(0)

SkyBox("desert07.jpg")
;Main loop
Repeat
  
  Repeat   
    Event  = WindowEvent()
  Until Event = 0
  
If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
EndIf
      
      
  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_Escape)
      Quit = #True
    EndIf
    If KeyboardReleased(#PB_Key_Space)
      MoveEntity(#bigsphere,0,4,0)
    ElseIf KeyboardPushed(#PB_Key_V) 
      MoveEntity(#bigsphere,0,0,0)
    
    EndIf
    
    MoveCamera  (#camera, KeyX, 0, KeyY)
      RotateCamera(#camera,  MouseY, MouseX, 0, #PB_Relative)
  RenderWorld()
   
  FlipBuffers()
    Until Quit = #True Or Event = #PB_Event_CloseWindow
  
but it is better to use my first suggestion
and using thick container will prevent the spheres from escaping it. there is a discussions about this in the forum.
I am not sure if I require additional software to develop a 3D mesh or structure for what I am after or whether it can be done directly using PB.
there is example SetMeshData.pb : it creates a plane mesh then it animate it like a flag :
the method is by getting the mesh info into a structured array using GetMeshData function and then changing the array values according to a formula, after that we inject this modified array again to the mesh using SetMeshData function, this is done millions of time giving the impression of animation. this same approach can be used with any other mesh
DT2000
User
User
Posts: 15
Joined: Tue Dec 02, 2014 5:09 am

Re: Looking for information - How to create 3D animation

Post by DT2000 »

I appreciate the help applePi... This is a great start to learning to understand how the 3D aspect of coding works and how best to approach it. The example of the globe with smaller spheres is a little sloppy in look and function, obviously, but it offers some insight to how and how no to code the project I am attempting.

What I could use some clarification on is the graphics used in the examples. I assume these are stores images, such as: "CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Wood.jpg"))", the wood.jpg? This is stored in the project folder as I have found. The question becomes calling graphics from my own file folders and how best to design or make the graphics so they are 3D.

The Magic of Stone Hedge example is very well done and much more advanced than I am close to being able to accomplish but I am hoping to review the coding and learn from it to better help myself to develop what I would ultimately like to have showing in my project. The quality of this example would be my goal for my project.

If you know of any other literature I can read that would be of help learning I would be open to suggestions.

I appreciate the time given to offer the help and insight that you have given. I'm always eager to learn.
"Occasionally stumped.... But never defeated!"
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Looking for information - How to create 3D animation

Post by blueb »

DT2000 wrote: If you know of any other literature I can read that would be of help learning I would be open to suggestions.
An excellent book by John Logsdon is: Programming 2D Scrolling Games

see: http://www.mediafire.com/folder/aajnae2 ... Pure_Basic
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Looking for information - How to create 3D animation

Post by applePi »

in addition to the blueb link there is also "PureBasic - A Beginners Guide":
http://www.purebasic.fr/english/viewtop ... 14&t=37059
http://nomad.so/about/
chapter 11 about 3D graphics
even it is for PB v4 but it is useful in describing several 3D subjects

the Wood.jpg is the image file found in the folder C:\PureBasic\Examples\3D\Data\Textures\
CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Wood.jpg"))
purebasic will search for the file Wood.jpg in the directories specified in these lines:

Code: Select all

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
#PB_Compiler_Home: is where you have installed purebasic, usually c:\purebasic
it can search inside zip files for images and other things, try this experiment: add Wood.jpg to inside the file desert.zip Examples/3D/Data/Packs/desert.zip (drag and drop) and remove Wood.jpg from Examples/3D/Data/Textures, the purebasic will find it easily inside desert.zip and will use it.

about

Code: Select all

Add3DArchive(".", #PB_3DArchive_FileSystem)
it let purebasic to search the current folder of the source code
now to organize things: create a new folder called myTextures in the same folder as the source code and copy Wood.jpg to it
add this to the code

Code: Select all

Add3DArchive("myTextures", #PB_3DArchive_FileSystem)
then purebasic will find Wood.jpg in myTextures folder

you may feel the 3D subject is too vast and complex, but to let you feel better and that you are not alone, i know only 1% of the PB Ogre engine, it is sufficient for me to do what i want from the 3D engine (which is mostly plotting).
note: instead of

Code: Select all

CreateMaterial(#MAT_plane, LoadTexture(#MAT_plane, "Wood.jpg"))
....
we can use:
Global tex = LoadTexture(#PB_Any, "Wood.jpg")
Global mat_plane = CreateMaterial(#PB_Any, TextureID(tex))
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(mat_plane), 0, -1.5,-0.7)
DT2000
User
User
Posts: 15
Joined: Tue Dec 02, 2014 5:09 am

Re: Looking for information - How to create 3D animation

Post by DT2000 »

As I went over the code more intently I was able to follow the structure of where the calls where pointing in the folders for PB.

I will keep looking at the various coding to learn from and also check the book you recommended. I have a cope of the beginners manual and have had a small peek in it, but I'll jump into the deep end and get wet!

Again... I appreciate the help.
"Occasionally stumped.... But never defeated!"
Post Reply