Bezier Curves in Opengl

Started by Charles Pegge, January 14, 2024, 11:46:35 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge


  % title = "Bezier Closed Shape Plot"
  int width  =   600
  int height =   600

  'uses OpenglSceneFrame
  uses consoleG
  BeginScript

  type vector float x,y

  function InterpLine(float f1,vector *p1,*p2,*rr)
  ================================================
  float f2=1-f1
  rr.x=p1.x*f1 + p2.x*f2
  rr.y=p1.y*f1 + p2.y*f2
  end function


  function DrawCurve(vector *r1,*rc,*r2)
  ======================================
  vector r3,r4,rr
  int    i,n
  float  q,f
  n=25   'steps
  f=1/n  'increment
  for i=1 to n+1 'plot curve
    q=i*f 'successive ooints
    InterpLine q,r1,rc,r3
    InterpLine q,rc,r2,r4
    InterpLine q,r3,r4,rr
    glVertex2f rr.x,rr.y
  next
  end function


  function DrawRoundShape(float q)
  ================================
  indexbase 0
  vector pt={ {-1,-1},{1,-1},{.5,1},{-.5,1} }
  vector r1,r2,r3,rc
  int    j
  int    j1,j2,j3
  float  q1,q2,f
  q1=q
  q2=1-q
  for j=0 to 3 'each pair of control lines
    j1=j    and 3
    j2=j1+1 and 3
    j3=j2+1 and 3
    InterpLine q1,pt[j1],pt[j2],r1
    InterpLine q2,pt[j2],pt[j3],r2
    InterpLine q1,pt[j2],pt[j3],r3
    glBegin GL_TRIANGLE_FAN
    glVertex2f rc.x,rc.y
    DrawCurve r1,pt[j2],r2
    glEnd
    'IN-FILL
    glBegin GL_TRIANGLES
    glVertex2f rc.x, rc.y
    glVertex2f r2.x, r2.y
    glVertex2f r3.x, r3.y
    glEnd
  next
  end function


  procedure main()
  ================
  cls 0.4,0.2,0.4
  'Color       0.5, 1.0, 1.0 ' color 14
  'thickness   2.0
  '
  'static int shape
  if opening
    BeginGlCompile shape
    DrawRoundShape 0.5 'curve control 0.0 to 0.5
    EndGlCompile
  endif
  pushstate
    move 10,-10,.0001
    scale 7.0
    color 1.0, 0.5, 1.0
    go shape
  popstate
  pushstate
    move 20,-10,.0002
    scale 7.0,3.5
    color 0.5, 1.0, 1.0
    go shape
  popstate
  pushstate
    move 20,-20,.0003
    rotateZ 10
    scale 3.5,8.0
    color 1.0, 1.0, 0.5
    go shape
  popstate
  end procedure

  EndScript

Frank Brübach

Thx Charles interesting stuff I Made only a Test and changed the interpline function a little to get new shapes :)