Animating Trilons

Everything related to 3D programming
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Animating Trilons

Post by chris319 »

A trilon is a three-faceted prism-shaped object.

In PB, how would one create a trilon which can rotate to expose each of its three facets to the camera/viewer? There would be text or an image on each of the facets.

Thank you.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Animating Trilons

Post by DK_PETER »

The simple way is to use the planes to form the trilon
and add a new material to a specific plane entity as needed.
Example:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

DeclareModule _Trilon
  Enumeration
    #FONT_DEFAULT
    #ALIGN_TOPLEFT
    #ALIGN_TOPCENTER
    #ALIGN_TOPRIGHT
    #ALIGN_MIDDELLEFT
    #ALIGN_MIDDLECENTER
    #ALIGN_MIDDLERIGHT
    #ALIGN_BOTTOMLEFT
    #ALIGN_BOTTOMCENTER
    #ALIGN_BOTTOMRIGHT
  EndEnumeration
  
  Declare.i CreateTrilon()
  Declare.i ChangeText(SideNum.i, Text.s, TextColor.i, Position.i = #ALIGN_MIDDLECENTER, BackGroundColor.i = $FFFFFF, Font.i = #FONT_DEFAULT)
  Declare.i ChangeImage(SideNum.i, Image.i, Position.i = #ALIGN_MIDDLECENTER, BackGroundColor.i = $FFFFFF)
EndDeclareModule

Module _Trilon
  
  #TexSize = 1024 ;Change for different texture size
  Structure _object
    ms.i
    ma.i
    tx.i
    id.i
  EndStructure
  
  Structure _Sides
    S1._object
    S2._object
    S3._object
    nod.i ;for rotation...
  EndStructure
  
  Global Trilon._Sides
  
  Procedure.i CreateTrilon()
    Protected fn.i = LoadFont(#PB_Any, "Arial", 48)
    Trilon\nod = CreateNode(#PB_Any, 0, 0, 0)
    With Trilon\S1
      \tx = CreateTexture(#PB_Any, #TexSize, #TexSize, "Base1")
      StartDrawing(TextureOutput(\tx))
      DrawingFont(FontID(fn))
      DrawingMode(#PB_2DDrawing_Gradient)
      BoxedGradient(0, 0, #TexSize, #TexSize)
      GradientColor(0.64, $FF00FF) 
      Box(0, 0, #TexSize, #TexSize)
      DrawText(0, 0, "PURPLE!!!!!", $FF0000)
      StopDrawing()
      \ma = CreateMaterial(#PB_Any, TextureID(\tx))
      MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
      MaterialCullingMode(\ma, #PB_Material_NoCulling)
      \ms = CreatePlane(#PB_Any, 60, 10, 1, 1, 1, 1)
      \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -10/2.5, 0)
      RotateEntity(\id, 180, 180, 0)
      AttachNodeObject(Trilon\nod, EntityID(\id))
    EndWith
    With Trilon\S2
      \tx = CreateTexture(#PB_Any, #TexSize, #TexSize, "Base2")
      StartDrawing(TextureOutput(\tx))
      DrawingFont(FontID(fn))
      DrawingMode(#PB_2DDrawing_Gradient)
      BoxedGradient(0, 0, #TexSize, #TexSize)
      GradientColor(0.84, $00FFFF)
      Box(0, 0, #TexSize, #TexSize)
      DrawText(0, 0, "YELLOW!!!!!",$FF0000)
      StopDrawing()
      \ma = CreateMaterial(#PB_Any, TextureID(\tx))
      MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
      MaterialCullingMode(\ma, #PB_Material_NoCulling)
      \ms = CreatePlane(#PB_Any, 60, 10, 1, 1, 1, 1)
      \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, -10/4)
      RotateEntity(\id, 120, 0, 180, #PB_Absolute)
      AttachNodeObject(Trilon\nod, EntityID(\id))
    EndWith
    With Trilon\S3
      \tx = CreateTexture(#PB_Any, #TexSize, #TexSize, "Base3")
      StartDrawing(TextureOutput(\tx))
      DrawingFont(FontID(fn))
      DrawingMode(#PB_2DDrawing_Gradient)
      BoxedGradient(0, 0, #TexSize, #TexSize)
      GradientColor(0.24, $000FFF) 
      Box(0, 0, #TexSize, #TexSize)
      DrawText(0, 0, "REDDISH!!!",$FF0000)
      StopDrawing()
      \ma = CreateMaterial(#PB_Any, TextureID(\tx))
      MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
      MaterialCullingMode(\ma, #PB_Material_NoCulling)
      \ms = CreatePlane(#PB_Any, 60, 10, 1, 1, 1, 1)
      \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 10/4)
      RotateEntity(\id, 240, 0, 180, #PB_Absolute)
      AttachNodeObject(Trilon\nod, EntityID(\id))
    EndWith
    ProcedureReturn Trilon\nod
  EndProcedure
  
  Procedure.i ChangeImage(SideNum.i, Image.i, Position.i = #ALIGN_MIDDLECENTER, BackGroundColor.i = $FFFFFF)
    Protected tex.i, mat.i
    If IsImage(Image) = 0 : ProcedureReturn #False : EndIf
    tex = CreateTexture(#PB_Any, #TexSize, #TexSize)
    StartDrawing(TextureOutput(tex))
    Box(0, 0, #TexSize, #TexSize, BackGroundColor)
    Select Position
      Case #ALIGN_BOTTOMCENTER
        DrawImage(ImageID(image), #TexSize/2 - ImageWidth(Image)/2, #TexSize - ImageHeight(image))
      Case #ALIGN_BOTTOMLEFT
        DrawImage(ImageID(Image), 0, #TexSize - ImageHeight(image))
      Case #ALIGN_BOTTOMRIGHT
        DrawImage(ImageID(Image), #TexSize - ImageWidth(image), #TexSize - ImageHeight(image))
      Case #ALIGN_MIDDELLEFT
        DrawImage(ImageID(Image), 0, #TexSize/2 - ImageHeight(Image)/2)
      Case #ALIGN_MIDDLECENTER
        DrawImage(ImageID(Image), #TexSize/2 - ImageWidth(image)/2, #TexSize/2 - ImageHeight(image)/2)
      Case #ALIGN_MIDDLERIGHT
        DrawImage(ImageID(Image), #TexSize - ImageWidth(image), #TexSize/2 - ImageHeight(image)/2)
      Case #ALIGN_TOPCENTER
        DrawImage(ImageID(Image), #TexSize/2 - ImageWidth(Image), 0)
      Case #ALIGN_TOPLEFT
        DrawImage(ImageID(Image), 0, 0)
      Case #ALIGN_TOPRIGHT
        DrawImage(ImageID(Image), #TexSize - ImageWidth(Image), 0)
    EndSelect
    StopDrawing()
    mat = CreateMaterial(#PB_Any, TextureID(tex))
    MaterialFilteringMode(mat, #PB_Material_Anisotropic, 8)
    Select SideNum
      Case 0
        SetEntityMaterial(Trilon\S1\id, MaterialID(mat))
      Case 1
        SetEntityMaterial(Trilon\S2\id, MaterialID(mat))
      Case 2
        SetEntityMaterial(Trilon\S3\id, MaterialID(mat))
    EndSelect
  EndProcedure
  
  Procedure.i ChangeText(SideNum.i, Text.s, TextColor.i, Position.i = #ALIGN_MIDDLECENTER, BackGroundColor.i = $FFFFFF, Font.i = #FONT_DEFAULT)
    Protected tex.i, mat.i
    tex = CreateTexture(#PB_Any, #TexSize, #TexSize)
    StartDrawing(TextureOutput(tex))
    DrawingMode(#PB_2DDrawing_Transparent)
    If font <> #FONT_DEFAULT : DrawingFont(FontID(font)) : EndIf
    Box(0, 0, #TexSize, #TexSize, BackGroundColor)
    Select Position
      Case #ALIGN_BOTTOMCENTER
        DrawText((#TexSize/2) - (TextWidth(Text)/2), #TexSize - TextHeight(Text), Text, TextColor)
      Case #ALIGN_BOTTOMLEFT
        DrawText(4, #TexSize - TextHeight(Text), Text, TextColor)
      Case #ALIGN_BOTTOMRIGHT
        DrawText(#TexSize - TextWidth(Text), #TexSize - TextHeight(Text), Text, TextColor)
      Case #ALIGN_MIDDELLEFT
        DrawText(4, (#TexSize/2) - TextHeight(Text)/2, Text, TextColor)
      Case #ALIGN_MIDDLECENTER
        DrawText(#TexSize/2 - TextWidth(Text)/2, #TexSize/2 - TextHeight(Text)/2, Text, TextColor)
      Case #ALIGN_MIDDLERIGHT
        DrawText(#TexSize - TextWidth(Text), #TexSize/2 - TextHeight(Text)/2, Text, TextColor)
      Case #ALIGN_TOPCENTER
        DrawText(#TexSize/2 - TextWidth(Text)/2, TextHeight(Text), Text, TextColor)
      Case #ALIGN_TOPLEFT
        DrawText(0, 0, Text, TextColor)
      Case #ALIGN_TOPRIGHT
        DrawText(#TexSize, 0, Text, TextColor)
    EndSelect
    StopDrawing()
    mat = CreateMaterial(#PB_Any, TextureID(tex))
    MaterialFilteringMode(mat, #PB_Material_Anisotropic,8)
    Select SideNum
      Case 0
        SetEntityMaterial(Trilon\S1\id, MaterialID(mat))
      Case 1
        SetEntityMaterial(Trilon\S2\id, MaterialID(mat))
      Case 2
        SetEntityMaterial(Trilon\S3\id, MaterialID(mat))
    EndSelect
  EndProcedure
EndModule

;EXAMPLE

UseJPEGImageDecoder()
UsePNGImageDecoder()
UseGIFImageDecoder()

OpenWindow(0, 0, 0, 800, 600,"Trilon test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 80)
CreateLight(0, $FFFFFF, 0, 0, 80)

image.i = CreateImage(#PB_Any, 512, 512) ;test image
StartDrawing(ImageOutput(image))
For con.i = 0 To 256 Step 20
  Box(con, con, 512-(con*2), 512-(con*2), RGB(Random(255,10), Random(255,10), Random(255,10)))
Next con
StopDrawing()

fn = LoadFont(#PB_Any, "Arial", 96, #PB_Font_Bold)
no.i = _Trilon::CreateTrilon()
el.i = ElapsedMilliseconds()

Repeat
  
  Repeat: ev = WindowEvent() : Until ev = 0
  
  RotateNode(no, 0.6, 0, 0, #PB_Relative)
  
  If ElapsedMilliseconds() - el > 1500 ;Random side test
    FontColor.i = RGB(Random(255, 10), Random(255, 10), Random(255, 10))
    color.i = RGB(Random(255, 10), Random(255, 10), Random(255, 10))
    If Random(500, 0) > 200
      _Trilon::ChangeText(Random(2), "NEW TEXT", FontColor, Random(9,1), color, fn)
    Else
      _Trilon::ChangeImage(Random(2), image, Random(9,1), color)
    EndIf
    el = ElapsedMilliseconds()
  EndIf
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)
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.
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Animating Trilons

Post by Comtois »

You can also use MeshManual

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    CreateMesh(0, #PB_Mesh_TriangleList)
    
    ;face 1
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(2,1,0)
    MeshFace(0,3,2)    
    
    ;-face 2
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(-0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(0,1,2)
    MeshFace(2,3,0) 
    
    ;-face 3
    
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(-0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(0,1,2)
    MeshFace(2,3,0)    
    
    FinishMesh(#True)
    NormalizeMesh(0) 
    
    UpdateMeshBoundingBox(0)
    
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
    CreateMaterial(1, LoadTexture(1, "DosCarte.png"))
    CreateMaterial(2, LoadTexture(2, "ValetCoeur.jpg"))
    
    
    CreateEntity(0, MeshID(0), #PB_Material_None)
    SetEntityMaterial(0,MaterialID(0), 0)
    SetEntityMaterial(0,MaterialID(1), 1)
    SetEntityMaterial(0,MaterialID(2), 2)
    
    ScaleEntity(0, 4, 2, 4)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 10, 1, #PB_Absolute)
    CameraLookAt(O, EntityX(0), EntityY(0), EntityZ(0))
    
    CreateLight(0, RGB(255,255,255), 300, 600, 50)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      ExamineKeyboard()
      
      RotateEntity(0, 1, 0, 0, #PB_Relative)
      
      RenderWorld()
      Screen3DStats()      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Please correct my english
http://purebasic.developpez.com/
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Animating Trilons

Post by chris319 »

This works well. How would I get the trilon to rotate horizontally instead of vertically?
Comtois wrote:You can also use MeshManual

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    CreateMesh(0, #PB_Mesh_TriangleList)
    
    ;face 1
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(2,1,0)
    MeshFace(0,3,2)    
    
    ;-face 2
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(-0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(0,1,2)
    MeshFace(2,3,0) 
    
    ;-face 3
    
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,0)
    
    MeshVertexPosition(-0.5,0.5,0)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(0,1)
    
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,1)
    
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0, 0, 0)
    MeshVertexColor($FFFFFF)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(0,1,2)
    MeshFace(2,3,0)    
    
    FinishMesh(#True)
    NormalizeMesh(0) 
    
    UpdateMeshBoundingBox(0)
    
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
    CreateMaterial(1, LoadTexture(1, "DosCarte.png"))
    CreateMaterial(2, LoadTexture(2, "ValetCoeur.jpg"))
    
    
    CreateEntity(0, MeshID(0), #PB_Material_None)
    SetEntityMaterial(0,MaterialID(0), 0)
    SetEntityMaterial(0,MaterialID(1), 1)
    SetEntityMaterial(0,MaterialID(2), 2)
    
    ScaleEntity(0, 4, 2, 4)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 10, 1, #PB_Absolute)
    CameraLookAt(O, EntityX(0), EntityY(0), EntityZ(0))
    
    CreateLight(0, RGB(255,255,255), 300, 600, 50)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      ExamineKeyboard()
      
      RotateEntity(0, 1, 0, 0, #PB_Relative)
      
      RenderWorld()
      Screen3DStats()      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Animating Trilons

Post by chris319 »

It still turns vertically, i.e. it rotates on the x axis.

The boundaries between the facets need to be vertical and it needs to rotate on the y axis.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Animating Trilons

Post by applePi »

add this before the Repeat line 113:

Code: Select all

RotateEntity(0, 0, 90, 0, #PB_Absolute)
but i have the impression that the side with the Geebee2 texture is big, will check it later.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Animating Trilons

Post by chris319 »

[quote="applePi"]add this before the Repeat line 113:

Code: Select all

RotateEntity(0, 0, 90, 0, #PB_Absolute)
Yup, that made it rotate horizontally. Thank you very much.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Animating Trilons

Post by Kwai chang caine »

Nice, both works well
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Animating Trilons

Post by chris319 »

Is there a way to code this so that it turns horizontally natively, without the RotateEntity()? I've studied the code and can't figure it out.

Thank you.
Post Reply