Polygon "crescent moon"

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

Polygon "crescent moon"

Post by SPH »

Hey, I just found an illogical thing: we can't make "hollow" polygons ...
Try making a crescent moon for example!
(example : http://xmas.free.fr/lune.bmp )
What instruction should I add to correct this bug?

Thanks 8)

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

win=OpenWindow(0, 0, 0,1920,1080, "Polygons_Maker",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  Debug ("OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(1,0,0,1920,1080)
If screenGL=0
  Beep_(500,250) : Delay(150) : Beep_(500,250)
  Debug ("OpenGLGadget() impossible")
  End
EndIf
screenW=GadgetWidth(1)
screenH=GadgetHeight(1)
;SetupGL(screenW, screenH)

;;;;;;;;;;;;;;;;;;

glOrtho_(0,1920,1080,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

; timer=ElapsedMilliseconds()
; timer=ElapsedMilliseconds()-timer


AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666)
AddKeyboardShortcut(0,  #PB_Shortcut_H, 8) ; hide/show

AddKeyboardShortcut(0,  #PB_Shortcut_Add, 100) ; alpha+0.01
AddKeyboardShortcut(0,  #PB_Shortcut_Subtract, 101) ; alpha-0.01
AddKeyboardShortcut(0,  #PB_Shortcut_Multiply, 102) ; alpha=0.5
AddKeyboardShortcut(0,  #PB_Shortcut_Divide, 103)   ; NO alpha

AddKeyboardShortcut(0,  #PB_Shortcut_Pad1, 110) ; rouge-0.02
AddKeyboardShortcut(0,  #PB_Shortcut_Pad2, 111) ; rouge+0.02
AddKeyboardShortcut(0,  #PB_Shortcut_Pad4, 112) ; vert-0.02
AddKeyboardShortcut(0,  #PB_Shortcut_Pad5, 113) ; vert+0.02
AddKeyboardShortcut(0,  #PB_Shortcut_Pad7, 114) ; bleu-0.02
AddKeyboardShortcut(0,  #PB_Shortcut_Pad8, 115) ; bleu+0.02

AddKeyboardShortcut(0,  #PB_Shortcut_Pad3, 116) ; rouge=0.5
AddKeyboardShortcut(0,  #PB_Shortcut_Pad6, 117) ; vert=0.5
AddKeyboardShortcut(0,  #PB_Shortcut_Pad9, 118) ; bleu=0.5



hide=0
alpha.f=0.5
rouge.f=0.5
vert.f=0.5
bleu.f=0.5
no_alpha=0

Dim poly_x(1000)
Dim poly_y(1000)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Repeat
  
  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  ;- polygone non alpha
  glBegin_(#GL_POLYGON)
  glColor4f_(1,1,1,1)
  glVertex2f_(22,11)
  glVertex2f_(500,66)
  glVertex2f_(33,755)
  glEnd_()
  
  ;- polygone alpha
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
 
  glBegin_(#GL_POLYGON)
  glColor4f_(0,1,1,0.5)
  ;glColor4f_(1,1,1,1)
  glVertex2f_(400,600)
  glVertex2f_(200,600)
  glVertex2f_(300,100)
  glEnd_()
  
  
  ;;;;;;;;;;;;;;;;;;;
  
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
          Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
          ;;;;;;;          
          Select EventType()
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_LeftClick
              Beep_(1500,55) ;Debug "Clic avec le bouton gauche de la souris"
              poly_x(0)+1
              poly_x(poly_x(0))=Resx 
              poly_y(poly_x(0))=Resy
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            Case #PB_EventType_RightDoubleClick 
              If poly_x(0)>0
                Beep_(500,400) ;: Debug "Double-clic avec le bouton droit de la souris"
                Dim poly_x(1000)
                Dim poly_y(1000)
              EndIf
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_RightClick
              If poly_x(0)>0
                Beep_(500,55) ;Debug "Clic avec le bouton droit de la souris"
                poly_x(0)-1
              EndIf
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
            Case #PB_EventType_MiddleButtonDown
              If poly_x(0)>2
                Beep_(3000,55) : Delay(150) : Beep_(3000,55) ;Debug "Clic avec le bouton centre de la souris"
                
              EndIf
              ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
          EndSelect
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;        
        Case 666
          End
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 8
          hide+1 : hide%2
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 100
          alpha+0.01
          If alpha>1
            alpha=1
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 101
          alpha-0.01
          If alpha<0
            alpha=0
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 102
          alpha=0.5
          no_alpha=0
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 103
          no_alpha+1 : no_alpha%2          
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 110
          rouge-0.02
          If rouge<0
            rouge=0
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 111
          rouge+0.02
          If rouge>1
            rouge=1
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 112
          vert-0.02
          If vert<0
            vert=0
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 113
          vert+0.02
          If vert>1
            vert=1
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 114
          bleu-0.02
          If bleu<0
            bleu=0
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 115
          bleu+0.02
          If bleu>1
            bleu=1
          EndIf
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 116
          rouge=0.5
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 117
          vert=0.5
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
        Case 118
          bleu=0.5
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;         
      EndSelect
  EndSelect
  
  ;;;;;;;;;;;;;;;;
  ;;;;;;;;;;;;;;;;
  
  ;;;;;;;;;;;;;;;;
  ;;;;;;;;;;;;;;;;
  
  
  If poly_x(0)>1; And 0=1
    glBegin_(#GL_POLYGON)
    If no_alpha=1
      glColor4f_(rouge,vert,bleu,1)
    Else
      glColor4f_(rouge,vert,bleu,alpha)
    EndIf
    
    For i=1 To poly_x(0)
      glVertex2f_(poly_x(i),poly_y(i))
    Next
    If hide=0
      glVertex2f_(Resx,Resy) ;: Debug Resx : Debug Resy : Debug "----------"
    EndIf
    
    glEnd_()
  EndIf
  
  SetGadgetAttribute(1,#PB_OpenGL_FlipBuffers,#True)
  
ForEver

;SPH : 38ms
;Falsam : 695ms



Example concave polygons :

Code: Select all

InitSprite()
InitMouse()
InitKeyboard()

ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)

OpenScreen(ddw,ddh,32,"Test polygones")
;Global Dim pp.w(8290,2500)
;Global dw.w,dh.w,SPH_Z,SPH_NOMBRE

Dim points.Point(5)

points(0)\x=550
points(0)\y=200
points(1)\x=0
points(1)\y=820
points(2)\x=1000
points(2)\y=500
points(3)\x=1900
points(3)\y=850
points(4)\x=100
points(4)\y=1000
points(5)\x=1500
points(5)\y=400


hdc=StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_Default)
If hdc
    Box(0,0,0,0,RGB(255,255,2))

  Polygon_(hdc,points(),5)
EndIf
StopDrawing()   

FlipBuffers()

Delay(2000)
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: Polygon "crescent moon"

Post by Olli »

Please read the answer here.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Polygon "crescent moon"

Post by falsam »

Code: Select all

EnableExplicit

Global Scene, ww = 800, wh = 600

; Summary
Declare Start()
Declare Render()
Declare DrawMoon(x.f, y.f, Width.f, Height.f, Fullness.f = 1, Color.i = $FFFFFFFF, Angle.f = 0)
Declare Exit()

Start()

Procedure Start() 
  OpenWindow(#PB_Any, 0, 0, ww, wh, "OpenGL : Draw Moon", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
 
  ; OpenGL drawing context
  Scene = OpenGLGadget(#PB_Any, 0, 0, ww, wh, #PB_OpenGL_NoDepthBuffer)
 
  ; Properties of the camera
  glMatrixMode_(#GL_PROJECTION)
  glOrtho_(0, ww, wh, 0, -1, 1)
 
  ; Background color
  ; Each RGBA component is divided by 255
  glClearColor_(0, 0, 112/255, 1.0)
   
  ; Opacity
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)

  ; Events 
  BindEvent(#PB_Event_CloseWindow, @Exit())
 
  ; Render Loop
  Render()
EndProcedure

; Render
Procedure Render() 
  Repeat
    WindowEvent()
   
    ; ClearScreen
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
       
    ; Draw moon
    DrawMoon(600, 150, 100, 100, -0.90, RGBA(255, 255, 212, 255))
   
    ; FlipBuffers
    SetGadgetAttribute(Scene, #PB_OpenGL_FlipBuffers, #True)
  ForEver
EndProcedure

; Draw Moon
Procedure DrawMoon(x.f, y.f, Width.f, Height.f, Fullness.f = 1, Color.i = $FFFFFFFF, Angle.f = 0)
  Protected Finesse.f = 0.1
  Protected n.f = Finesse
 
  Protected SinAngle.f
  Protected CosAngle.f
 
  Protected x0.f
  Protected y0.f
 
  glMatrixMode_(#GL_MODELVIEW)   
  glPushMatrix_();   
 
  ; Location
  glTranslatef_(x, y, 0)
 
  ; Rotate
  glRotatef_(Angle, 0, 0, 1)
 
  ; Draw Moon
  glBegin_(#GL_QUAD_STRIP)
  glColor4f_(Red(Color)/255, Green(Color)/255, Blue(Color)/255, Alpha(Color)/255)
 
  While n < #PI
    SinAngle.f = Sin(n)
    CosAngle.f = Cos(n)
   
    x0 = (Width * SinAngle)
    y0 = (Height * CosAngle)
   
    glVertex2f_(x0, y0);
    glVertex2f_(-Fullness * x0, y0)
    n + Finesse
  Wend   
  glEnd_()
 
  glPopMatrix_()
EndProcedure

Procedure Exit() 
  End
EndProcedure
■ Fullness: Roundness of the moon.
Default 1 the moon is full
-0.99 The crescent moon is very thin.
-0.95 The crescent moon is a little less fine
etc ...

Enjoy :mrgreen:

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon "crescent moon"

Post by SPH »

It's OK now :idea:

Thanks for yor immense patiente :P
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: Polygon "crescent moon"

Post by Olli »

?
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon "crescent moon"

Post by SPH »

Olli wrote:?
Dis : pkoi tu n'as plus d'ordi ? :?:
Tu es en galere ?? :|
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: Polygon "crescent moon"

Post by Olli »

@SPH

Your question recalls me old questions about << signes extérieurs de richesse >>. These questions 30 years ago were << Why have not you NIKE AIR shoes ? >>.

Do remember the hangry is absolutely not solved in the world, even if there is 1 000 000 000 (= 1000000000) times more "flops" (computing units) in the world than 30 years...

Do remember this numeric power is not used in order to solve rightly the true problems if it is for bad ways : look at the SARS-COV-2, as fool as 1918, 1919 and 1920...

So (I apologize to you, falsam and the others who could feel guilty to read me, as it is not the goal), your subject starts with a bitmap BMP and I am surprised you consider "OK" when you do not consider PNG and alpha transparency used on OPENGL.

I mention in the other subject you search in the several blend modes. Please read it.

Because, here, you start your subject with two triangles (1 quad) to show the moon, and it seems that you "close" it with one hundred of triangles to show the same moon...

In this way, just use the native SPRITES LIBRARY, or study the blends of OpenGL offered through GL_Blend(), recording PNG files instead of BMP files.

An other way, keeping BMP files is consider a transparency color (pure black - 0 -, or pure white - 0x0FFFFFF -). You are talking about "polygons" in the off-topic, as an old famous game, 30 years ago.

It seems that the video hardware was specific to gain video memory. And the use of polygon was a very good method adapted to this hardware.

The paradox is, that if you want to repeat the same hardware mechanism, now you should use sprites (native library, DirectX or OpenGL) to show polygons, even if, 30 years ago, polygons was used to show the sprites were not the unique hardware availability.

If you have 4096 colors, you should prepare 4096 sets of flat sprites. And if you have 320 pixels in totality in one row on the screen, you should prepare 4096*320 sprites to imitate the old Amiga hardware video that represents 1 310 720 sprites stored in memory. And it requires 210 megabytes to all that to make the HAM-6.



Concluding :

I think you should however use the native sprite library and "compile" every line from HAM-6 to a flat sprite (320*1 pixel) on the fly, this will set the use of video memory onto 300 kilobytes only, and keep a very very good ratio of preformance.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon "crescent moon"

Post by SPH »

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: Polygon "crescent moon"

Post by Olli »

In the way you need OpenGL, (3rd version of suggesting), look at the GL_Blend() function overview. There is the same process on the native function SpriteAlphablending().
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon "crescent moon"

Post by SPH »

Olli, it's hard to follow you. I don't understand anything about what you're talking about ...
You didn't answer me: why don't you have a computer anymore?
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: Polygon "crescent moon"

Post by Olli »

I answer you there is SpriteBlendingMode you should master, and glBlend() is its equivalent.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 269
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon "crescent moon"

Post by SPH »

:idea: lol, you are a bot :idea:
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: Polygon "crescent moon"

Post by Olli »

If you want to speak french and off-topic, longer, we have this bistrot.

But for gL, sprites and color blend, sure the questions are welcome here again.

I remember test all the color blend by creating two sprites containing white, black, red and blue colored squares, and checked by a code seems like

Code: Select all

for i= 0 to 9
   for j= 0 To 9
      SpriteBlendingMode(i, j)
      x= i*20
      y= j*20
      DisplayTransparentSprite(1, x, y)
     DisplayTransparentSprite(2, x+ 4, y+ 4)
   Next
Next
Post Reply