Filled polygons [OK]

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

Filled polygons [OK]

Post by SPH »

Hi all,

I'm looking to make animations in filled polygons (another world style).
What would be the fastest method under PB (APIs allowed)

Big thanks... 8)
Last edited by SPH on Sat Nov 21, 2020 7:10 pm, edited 1 time in total.
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Filled polygons

Post by Saki »

The fastest way is to use FilterCallback to replace the colour of the polligon with an image.

It becomes difficult if automatic tilling, resizing and scrolling is to be added.

We solve this in GFX_Wizzard_BF with the function BucketFill_BF.
One line is enough to insert an image and scroll.
But you have to see for yourself what you need.

Little samples :
Image

Code: Select all

; Enhanced BucketFill function with texture support, color distance, clipping and automatic tiling
  ; This function can handle color distance with SetColorDistanceFill_BF -It's ever needed for removing mask artifacts
  ; As sample, Sprite rotating BF create ever little border artifacts, because using interpolation
  ; Hint : ColorDistanceFill_BF can influence and so break a used fill function clipping
  ; This is a BF core based function and can same all BF core based functions automatically clipping, tiling, scrolling
  ; All BF core based functions use automatic BF texture caching
  ; This caching is activated by default
  ; BF core based functions which can write from a to b so even write in themselves without changing the source permanently
  ; So write operations on the source do not show any changes when caching is activated
  ; If you want to change the source permanently, switch off temporary the caching before with NoCaching_BF(1)
  Procedure BucketFill_BF(mode, output_ID, texture_ID,
                          output_get_color_x=0,  ; Grab transparence color from sprite pos x - For preset set ..x=-1  
                          output_get_color_y=0,  ; Grab transparence color from sprite pos y - For preset set ..y= the color you want - as sample $FF00FF 
                          texture_x=0,           ; Startposition texture output x - Preset 0
                          texture_y=0,           ; Startposition texture output y - Preset 0
                          texture_width=0,       ; Endposition texture output - Preset 0 = Clipping is automatic to available width 
                          texture_height=0,      ; Endposition texture output - Preset 0 = Clipping is automatic to available height 
                          texture_clip_x=0,      ; Startposition inside the texture x - Preset 0
                          texture_clip_y=0,      ; Startposition inside the texture y - Preset 0
                          texture_clip_width=0,  ; Endposition inside the texture - Preset 0 = full texture width
                          texture_clip_height=0, ; Endposition inside the texture - Preset 0 = full texture height
                          texture_resize_x=0,    ; Pre resize texture x
                          texture_resize_y=0)    ; Pre resize texture y
                                                 ; mode = 0         - Set only a positioning marker
                                                 ;       -1         - Ignore a texture and use a color - Set as texture_ID a color, as sample $FFFF00
                                                 ;       -2         - Standard preset texture mode
                                                 ;       -3 to -256 - Texture mode with alpha blending
                                                 ; For compatibility with BucketFill this function use negative mode values, but it works at the same also with positive values
                                                 ; This function can output on images, PB sprites and canvas
Last edited by Saki on Sun Nov 22, 2020 12:19 pm, edited 2 times in total.
地球上の平和
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Filled polygons

Post by Mijikai »

Maybe the vector library is a solution :)

Image

Example:

Code: Select all

EnableExplicit

;press [SPACE] to collect vertex points (mouse position on window)
;there need to be atleast 3 recorded points before anything is drawn!

Structure VERTEX_STRUCT
  x.d
  y.f
EndStructure

Procedure.i Main()
  Protected NewList vertex.VERTEX_STRUCT()
  Protected vertex_last.VERTEX_STRUCT
  Protected mx.i
  Protected my.i
  If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    AddKeyboardShortcut(0,#PB_Shortcut_Space,666)
    If StartVectorDrawing(WindowVectorOutput(0))
      AddPathBox(0,0,WindowWidth(0),WindowHeight(0))
      VectorSourceColor($FF000000)
      FillPath()
      StopVectorDrawing()  
    EndIf
    Repeat
      Select WaitWindowEvent(16)
        Case #PB_Event_Menu
          If EventMenu() = 666
            mx = WindowMouseX(0)
            my = WindowMouseY(0)
            If Not mx = -1 And Not my = -1
              If AddElement(vertex())
                vertex()\x = mx
                vertex()\y = my
              EndIf
            EndIf
            If StartVectorDrawing(WindowVectorOutput(0))
              AddPathBox(0,0,WindowWidth(0),WindowHeight(0))
              VectorSourceColor($FF000000)
              FillPath()
              If ListSize(vertex()) > 2
                ForEach vertex()
                  AddPathLine(vertex()\x,vertex()\y)
                  AddPathCircle(vertex()\x,vertex()\y,4)
                  VectorSourceColor($FFFF00FF)
                  FillPath()
                Next
                MovePathCursor(vertex()\x,vertex()\y)
                ForEach vertex()
                  AddPathLine(vertex()\x,vertex()\y)
                Next
                VectorSourceColor($666BFFC7)
                FillPath()
              EndIf
              StopVectorDrawing()  
            EndIf
          EndIf
        Case #PB_Event_CloseWindow
          Break 
      EndSelect 
    ForEver
    CloseWindow(0)  
  EndIf  
  ProcedureReturn #Null
EndProcedure

Main()

End
Universal but probably too slow!
The best and fastest solution imho. would be Opengl :)

Code: Select all

EnableExplicit

Procedure.i Main()
  If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    If OpenGLGadget(1,0,0,1280,800)
      glOrtho_(0,1280,800,0,-1,1)
      glMatrixMode_(#GL_MODELVIEW)
      glLoadIdentity_()
      Repeat
        Repeat
          Select WindowEvent()
            Case #PB_Event_CloseWindow
              Break 2
            Case #Null
              Break
          EndSelect
        ForEver
        glClear_(#GL_DEPTH_BUFFER_BIT|#GL_COLOR_BUFFER_BIT)
        glBegin_(#GL_POLYGON)
        glVertex2f_(500,100)
        glVertex2f_(300,400)
        glVertex2f_(400,600)
        glVertex2f_(200,500)
        glVertex2f_(100,100)
        glColor4f_(1,1,1,1)
        glEnd_()
        SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
      ForEver
    EndIf
    CloseWindow(0)
  EndIf  
  ProcedureReturn #Null
EndProcedure

Main()

End
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Filled polygons

Post by Saki »

Hi Mijikai, try this

Code: Select all

EnableExplicit

;press [SPACE] to collect vertex points (mouse position on window)
;there need to be atleast 3 recorded points before anything is drawn!

Structure VERTEX_STRUCT
  x.d
  y.f
EndStructure

Procedure.i Main()
  Protected NewList vertex.VERTEX_STRUCT()
  Protected vertex_last.VERTEX_STRUCT
  Protected mx.i
  Protected my.i
  
  LoadImage (0, #PB_Compiler_Home +"examples/Sources/Data/Geebee2.bmp")
  
  If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    AddKeyboardShortcut(0,#PB_Shortcut_Space,666)
    If StartVectorDrawing(WindowVectorOutput(0))
      AddPathBox(0,0,WindowWidth(0),WindowHeight(0))
      VectorSourceColor($FF000000)
      FillPath()
      StopVectorDrawing() 
    EndIf
    Repeat
      Select WaitWindowEvent(16)
        Case #PB_Event_Menu
          If EventMenu() = 666
            mx = WindowMouseX(0)
            my = WindowMouseY(0)
            If Not mx = -1 And Not my = -1
              If AddElement(vertex())
                vertex()\x = mx
                vertex()\y = my
              EndIf
            EndIf
            If StartVectorDrawing(WindowVectorOutput(0))
              AddPathBox(0,0,WindowWidth(0),WindowHeight(0))
              VectorSourceColor($FF000000)
              FillPath()
              If ListSize(vertex()) > 2
                ForEach vertex()
                  AddPathLine(vertex()\x,vertex()\y)
                  AddPathCircle(vertex()\x,vertex()\y,4)
                  VectorSourceColor($FFFF00FF)
                  FillPath()
                Next
                MovePathCursor(vertex()\x,vertex()\y)
                ForEach vertex()
                  AddPathLine(vertex()\x,vertex()\y)
                Next
                If IsImage(0)
                  VectorSourceImage(ImageID(0), 255, ImageWidth(0), ImageHeight(0), #PB_VectorImage_Repeat)
                  FillPath()                                           
                EndIf
              EndIf
              StopVectorDrawing() 
            EndIf
          EndIf
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver
    CloseWindow(0) 
  EndIf 
  ProcedureReturn #Null
EndProcedure

Main()

End
地球上の平和
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

Re: Filled polygons

Post by SPH »

Hi,
Here is a code producing 2 polygons. Alas, they are "attached".
I was wondering how to detach them and how to assign them a color ...

Code: Select all

If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
Else
  End
EndIf


If OpenGLGadget(1,0,0,1280,800)
  glOrtho_(0,1280,800,0,-1,1)
  glMatrixMode_(#GL_MODELVIEW)
  glLoadIdentity_()
  
  ;glClear_(#GL_DEPTH_BUFFER_BIT|#GL_COLOR_BUFFER_BIT)
  glClear_(0)
  glBegin_(#GL_POLYGON)
  glVertex2f_(500,400)
  glVertex2f_(300,400)
  glVertex2f_(400,600)
  glColor4f_(1,1,1,1)
  
  glBegin_(#GL_POLYGON)
  glVertex2f_(11,11)
  glVertex2f_(22,11)
  glVertex2f_(33,755)
  
  glColor4f_(1,1,1,1)
  glEnd_()
  SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
  
EndIf

Delay(2000)
CloseWindow(0)
End
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Filled polygons

Post by Olli »

Hello Sephirut ! Try this to watch :

Code: Select all

If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
If OpenGLGadget(1,0,0,1280,800)
glOrtho_(0,1280,800,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)
glBegin_(#GL_POLYGON)
glColor4f_(1,1,1,1)
glVertex2f_(500,400)
glVertex2f_(300,400)
glVertex2f_(400,600)
glEnd_()
glBegin_(#GL_POLYGON)
glColor4f_(0.5,0.5,0.5,0.5)
glVertex2f_(11,11)
glVertex2f_(22,11)
glVertex2f_(33,755)
glEnd_()
SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
EndIf
Delay(5000)
CloseWindow(0)
EndIf
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

Re: Filled polygons

Post by SPH »

THX A LOT Olli

Olli = ollivier du forum francais ?
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Re: Filled polygons

Post by Mesa »

If you looking fo speed, sprites (or engine3D) is the solution.
2D and Vector2D functions are pretty slow.

Ex1

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 340, 285, "Triangle", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If OpenWindowedScreen(WindowID(0), 10, 10, 320, 200, 0, 0, 0)
    SpriteQuality(#PB_Sprite_BilinearFiltering)
    
    CreateSprite(0, 128, 128, #PB_Sprite_AlphaBlending)
    
    StartDrawing(SpriteOutput(0))
    DrawingMode( #PB_2DDrawing_AllChannels)
    Box(0,0,128,128,RGBA(255,255,255,255))
    LineXY(0,0,127,0, RGBA(255,0,0,128))
    LineXY(0,0,64,127, RGBA(255,0,0,128))
    LineXY(64,127,127,0, RGBA(255,0,0,128))
    FillArea(64,64,RGBA(255,0,0,128),RGBA(255,0,0,128))
    StopDrawing()
    TransparentSpriteColor(0, RGBA(255,255,255,255))
    
    CopySprite(0, 1 ,#PB_Sprite_AlphaBlending)
    RotateSprite(1,90.0,#PB_Absolute)
    
    
    Repeat
      Repeat
        ; Always process all the events to flush the queue at every frame
        Event = WindowEvent()
        
        Select Event
          Case #PB_Event_CloseWindow
            Quit = 1
            
        EndSelect
        
      Until Event = 0 ; Quit the event loop only when no more events are available
      
      ; Clear the screen and draw our sprites
      ClearScreen(RGB(255,255,255))
      
      DisplayTransparentSprite(0, 10+x, 10+y)
      RotateSprite(1,x,#PB_Relative)
      DisplayTransparentSprite(1, 120-x, 10-y)
      x+1
      y+1
      If x>320
        x=0
      EndIf 
      If y>200
        y=0
      EndIf
      
      
      FlipBuffers()       ; Inverse the buffers (the back become the front (visible)... and we can do the rendering on the back
      
    Until  Quit
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf

Ex2

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Can't open the sprite system", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 340, 285, "CLic grab button and use left/right/up/down keys", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(1, 10,  10, 100, 25, "Grab")
  ButtonGadget(2, 120,  10, 100, 25, "Button 2")
  ButtonGadget(3, 230,  10, 100, 25, "Button 3")
  TextGadget  (4, 10, 40, 300, 30, "Mouse and keyboard released")
  
  If OpenWindowedScreen(WindowID(0), 10, 70, 320, 200, 0, 0, 0)
    SpriteQuality(#PB_Sprite_BilinearFiltering)
    
    
    CreateSprite(0, 128, 128, #PB_Sprite_AlphaBlending)
    
    StartDrawing(SpriteOutput(0))
    DrawingMode( #PB_2DDrawing_AllChannels)
    Box(0,0,128,128,RGBA(255,255,255,255))
    LineXY(0,0,127,0, RGBA(255,0,0,128))
    LineXY(0,0,64,127, RGBA(255,0,0,128))
    LineXY(64,127,127,0, RGBA(255,0,0,128))
    FillArea(64,64,RGBA(255,0,0,128),RGBA(255,0,0,128))
    StopDrawing()
    TransparentSpriteColor(0, RGBA(255,255,255,255))
    
    CopySprite(0, 1 ,#PB_Sprite_AlphaBlending)
    RotateSprite(1,90.0,#PB_Absolute)
    
    direction = 1
    playerX = 1
    playerY = 1
    
    ; Start with released input
    ReleaseMouse(#True)
    InputReleased = 1
    
    Repeat
      Repeat
        ; Always process all the events to flush the queue at every frame
        Event = WindowEvent()
        
        Select Event
          Case #PB_Event_CloseWindow
            Quit = 1
            
          Case #PB_Event_Gadget
            
            ; Do the normal application management here
            Gadget = EventGadget()
            
            Select Gadget
              Case 1
                InputReleased = 0
                ReleaseMouse(#False)
                SetGadgetText(4, "Press 'F1' to ungrab keyboard and mouse")
                
              Case 2, 3
                SetGadgetText(4, "Button "+Str(Gadget)+" pressed.")
            EndSelect
            
        EndSelect
        
      Until Event = 0 ; Quit the event loop only when no more events are available
      
      ExamineKeyboard()
      
      If InputReleased = 0
        
        ExamineMouse()
        
        ; do the sprite & screen management at every frame
        If KeyboardPushed(#PB_Key_Up)    And playerY > 0   : playerY -3 : EndIf  
        If KeyboardPushed(#PB_Key_Down)  And playerY < 280 : playerY +3 : EndIf  
        If KeyboardPushed(#PB_Key_Left)  And playerX > 0   : playerX -3 : EndIf  
        If KeyboardPushed(#PB_Key_Right) And playerX < 300 : playerX +3 : EndIf  
        
        If KeyboardPushed(#PB_Key_F1)
          ReleaseMouse(#True)
          InputReleased = 1
          SetGadgetText(4, "Mouse and keyboard released");
        EndIf
      EndIf
      
      ; Clear the screen and draw our sprites
      ClearScreen(RGB(255,255,255))
      
      DisplayTransparentSprite(0, 10+playerX, 10+playerY)
      DisplayTransparentSprite(1, 120, 10)
      
      
      
      FlipBuffers()       ; Inverse the buffers (the back become the front (visible)... and we can do the rendering on the back
      
    Until  Quit Or KeyboardPushed(#PB_Key_Escape)
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
  EndIf
EndIf

M.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Filled polygons

Post by Olli »

Olli = ollivier du forum francais ?
Mais oui, ma petite truie vérolée adorée... Tu me manques... J'ai bidouillé un truc dans mon profil, et ça n'a pas fonctionné.

Translate : Yes, you are right ! Perfectly right...
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

Re: Filled polygons

Post by SPH »

Can we superimpose 2 polygons by making a transparency?

Code: Select all

If OpenWindow(0,0,0,1280,800,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
If OpenGLGadget(1,0,0,1280,800)
glOrtho_(0,1280,800,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;timer=ElapsedMilliseconds()
glBegin_(#GL_POLYGON)
glColor4f_(1,1,1,1)
glVertex2f_(-2,11)
glVertex2f_(500,66)
glVertex2f_(33,755)
glEnd_()

;For i=1 To 10000
glBegin_(#GL_POLYGON)
glColor4f_(1,0.5,0.5,0.5)
;glColor4f_(1,1,1,1)
glVertex2f_(500,400)
glVertex2f_(300,400)
glVertex2f_(400,600)
glVertex2f_(200,600)
glVertex2f_(300,100)
glEnd_()
;Next

;timer=ElapsedMilliseconds()-timer
;Debug timer


SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
EndIf
Delay(1000)
CloseWindow(0)
EndIf
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Filled polygons

Post by Mijikai »

Enable blending:

Code: Select all

glEnable_(#GL_BLEND)
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
User avatar
SPH
Enthusiast
Enthusiast
Posts: 268
Joined: Tue Jan 04, 2011 6:21 pm

Re: Filled polygons

Post by SPH »

thanks Mijikai
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 5.73LTS - 32 bits
Post Reply