Polygon editor
Posted: Wed Feb 11, 2026 3:18 pm
Some stuff interesting here :
Click left to draw lines and click to right to close the polygon.
Enjoy !
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
Enjoy !