Page 1 of 1

Polygon editor

Posted: Wed Feb 11, 2026 3:18 pm
by threedslider
Some stuff interesting here :

Code: Select all

EnableExplicit

; =========================
; Init
; =========================
InitSprite()
InitMouse()
InitKeyboard()

OpenWindow(0, 0, 0, 800/DesktopResolutionX(), 600/DesktopResolutionY(), "OpenGL Polygon Draw", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)

; =========================
; OpenGL setup
; =========================
glViewport_(0, 0, 800, 600)

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
glOrtho_(0, 800, 600, 0, -1, 1)

glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()

glDisable_(#GL_DEPTH_TEST)
glDisable_(#GL_LIGHTING)

glClearColor_(0.1, 0.1, 0.1, 1.0)

; =========================
; Structures
; =========================
Structure Vertex
  x.f
  y.f
EndStructure

Global NewList Vertices.Vertex()
Global PolygonClosed = #False
Global PrevLeft, PrevRight
Global ExitProgram = #False

; =========================
; Main loop (FLUIDE)
; =========================
Repeat

  ; -------- Event pump (NON BLOQUANT)
  Repeat
    Define Event = WindowEvent()
    If Event = 0
      Break
    EndIf

    If Event = #PB_Event_CloseWindow
      ExitProgram = #True
    EndIf
  ForEver

  If ExitProgram
    Break
  EndIf

  ; -------- Input
  ExamineMouse()
  ExamineKeyboard()

  ; -------- Mouse logic
  If MouseButton(#PB_MouseButton_Left) And PrevLeft = 0 And Not PolygonClosed
    AddElement(Vertices())
    Vertices()\x = MouseX()
    Vertices()\y = MouseY()
  EndIf
  PrevLeft = MouseButton(#PB_MouseButton_Left)

  If MouseButton(#PB_MouseButton_Right) And PrevRight = 0 And ListSize(Vertices()) >= 3
    PolygonClosed = #True
  EndIf
  PrevRight = MouseButton(#PB_MouseButton_Right)

  ; -------- Render
  glClear_(#GL_COLOR_BUFFER_BIT)
  glLoadIdentity_()
  
  ; Draw vertices (RED)
  If ListSize(Vertices()) > 0
    glPointSize_(6)
    glColor3f_(1, 0, 0) ; red
    glBegin_(#GL_POINTS)
    ForEach Vertices()
      glVertex2f_(Vertices()\x, Vertices()\y)
    Next
    glEnd_()
  EndIf


  ; Draw edges (ORANGE)
  If ListSize(Vertices()) > 1
    glColor3f_(1.0, 0.5, 0.0) ; orange
    glBegin_(#GL_LINE_STRIP)
    ForEach Vertices()
      glVertex2f_(Vertices()\x, Vertices()\y)
    Next
    If PolygonClosed
      FirstElement(Vertices())
      glVertex2f_(Vertices()\x, Vertices()\y)
    EndIf
    glEnd_()
  EndIf


  ; Draw filled face
  If PolygonClosed
    glColor3f_(1, 1, 1)
    glBegin_(#GL_POLYGON)
    ForEach Vertices()
      glVertex2f_(Vertices()\x, Vertices()\y)
    Next
    glEnd_()
  EndIf

  ; Cursor cross (visible & fluide)
  glColor3f_(1, 0, 0)
  glBegin_(#GL_LINES)
  glVertex2f_(MouseX() - 6, MouseY())
  glVertex2f_(MouseX() + 6, MouseY())
  glVertex2f_(MouseX(), MouseY() - 6)
  glVertex2f_(MouseX(), MouseY() + 6)
  glEnd_()

  FlipBuffers()

Until ExitProgram Or KeyboardPushed(#PB_Key_Escape)

End
Click left to draw lines and click to right to close the polygon.

Enjoy !

Re: Polygon editor

Posted: Wed Feb 11, 2026 4:47 pm
by miso
Your examples are simple, thats why I can learn from them. Thanks for sharing.

Re: Polygon editor

Posted: Fri Feb 13, 2026 3:53 pm
by threedslider
Thanks for your kind of words :D

You are welcome and your good work is interesting too ;)

Re: Polygon editor

Posted: Wed Feb 18, 2026 9:06 am
by SPH
Interesting.
Did you encounter any difficulty creating concave polygons?

Re: Polygon editor

Posted: Wed Feb 18, 2026 2:43 pm
by threedslider
SPH wrote: Wed Feb 18, 2026 9:06 am Interesting.
Did you encounter any difficulty creating concave polygons?
Nope, it avoids that :)

Re: Polygon editor

Posted: Wed Feb 18, 2026 9:40 pm
by SPH
I'm using an instruction that only creates convex polygons. I'll look at the difference between my instruction and yours (and its execution speed)!!!

Re: Polygon editor

Posted: Wed Feb 18, 2026 11:44 pm
by threedslider
Ok ! If you have any change from that you can always to bright your work here if you want :)

Re: Polygon editor

Posted: Thu Feb 19, 2026 10:55 pm
by minimy
Thanks for share threeslider, good code. Nice to learn how work GL.
I add this before the repeat glLineWidth_(5) for fat lines, and change glPointSize to glPointSize_(10). Very small for my old and tired eyes :lol:

Re: Polygon editor

Posted: Sat Feb 28, 2026 9:50 am
by SPH
Hmm, at first glance, you also can't make concave polygons (for example: a very 'hollow' crescent moon). That's okay...