OpenGLGadget : Dashed Bezier Curve

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

OpenGLGadget : Dashed Bezier Curve

Post by eddy »

Code: Select all

Structure VECTOR3
   x.f
   y.f
   z.f
EndStructure

OpenWindow(0, 0, 0, 520, 520, "OpenGL Bezier Drawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenGLGadget(0, 10, 10, 500, 500)
   w=GadgetWidth(0) : h=GadgetHeight(0)
   glMatrixMode_(#GL_PROJECTION)
   glLoadIdentity_()
   If (w<=h)
      glOrtho_(-5.0, 5.0, -5.0*h / w, 5.0*h / w, -5.0, 5.0)
   Else
      glOrtho_(-5.0*w / h, 5.0*w / h, -5.0, 5.0, -5.0, 5.0)
   EndIf
   glMatrixMode_(#GL_MODELVIEW)
   glLoadIdentity_()
   
   ;// prepare bezier curve data and settings
   i=0
   Dim bezierPoints.VECTOR3(3)
   bezierPoints(i)\x=-4 : bezierPoints(i)\y=-4 : bezierPoints(i)\z=0 : i + 1
   bezierPoints(i)\x=-2 : bezierPoints(i)\y=4 : bezierPoints(i)\z=0 : i + 1
   bezierPoints(i)\x=2 : bezierPoints(i)\y=-4 : bezierPoints(i)\z=0 : i + 1
   bezierPoints(i)\x=4 : bezierPoints(i)\y=4 : bezierPoints(i)\z=0 : i + 1
   
   glClearColor_(0.0, 0.0, 0.0, 0.0)
   glShadeModel_(#GL_FLAT)
   glMap1f_(#GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, bezierPoints())
   glEnable_(#GL_MAP1_VERTEX_3)
   glEnable_(#GL_LINE_STIPPLE)   ; it will draw lines with pattern
   glEnable_(#GL_LINE_SMOOTH)
   glEnable_(#GL_BLEND)
   glLineWidth_(3)               ; specify line thickness
   glLineStipple_(1, $FF00)      ; specify dashed pattern   
   glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
   glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
   
   ;// The following code displays bezier curve
   i=0 : linePrecision=1000
   glClear_(#GL_COLOR_BUFFER_BIT)
   glColor3f_(1.0, 1.0, 1.0)
   glBegin_(#GL_LINE_STRIP)
   For i=0 To linePrecision
      glEvalCoord1f_(i / linePrecision)
   Next
   glEnd_()
   ;// The following code displays the control points As dots.
   glPointSize_(5.0);
   glColor3f_(1.0, 1.0, 0.0)
   glBegin_(#GL_POINTS)
   For i=0 To 3
      glVertex3fv_(bezierPoints(i))
   Next
   glEnd_()
   glFlush_()
   
   SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
EndIf

Repeat: Until WaitWindowEvent()=#PB_Event_CloseWindow
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool