Polygon editor

Everything related to 3D programming
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Polygon editor

Post 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 !
miso
Enthusiast
Enthusiast
Posts: 712
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Polygon editor

Post by miso »

Your examples are simple, thats why I can learn from them. Thanks for sharing.
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: Polygon editor

Post by threedslider »

Thanks for your kind of words :D

You are welcome and your good work is interesting too ;)
User avatar
SPH
Enthusiast
Enthusiast
Posts: 636
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon editor

Post by SPH »

Interesting.
Did you encounter any difficulty creating concave polygons?

!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 : 6.12LTS - 64 bits
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: Polygon editor

Post by threedslider »

SPH wrote: Wed Feb 18, 2026 9:06 am Interesting.
Did you encounter any difficulty creating concave polygons?
Nope, it avoids that :)
User avatar
SPH
Enthusiast
Enthusiast
Posts: 636
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon editor

Post 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)!!!

!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 : 6.12LTS - 64 bits
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: Polygon editor

Post by threedslider »

Ok ! If you have any change from that you can always to bright your work here if you want :)
User avatar
minimy
Addict
Addict
Posts: 894
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Polygon editor

Post 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:
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
SPH
Enthusiast
Enthusiast
Posts: 636
Joined: Tue Jan 04, 2011 6:21 pm

Re: Polygon editor

Post by SPH »

Hmm, at first glance, you also can't make concave polygons (for example: a very 'hollow' crescent moon). That's okay...

!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 : 6.12LTS - 64 bits
Post Reply