Fitting 2D graphics nicely in the graphics window

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Fitting 2D graphics nicely in the graphics window

Post by applePi »

Fitting 2D graphics nicely in the graphics window with Orthographic
suppose your graphics window is 500*500

Code: Select all

#screenwidth = 500
#screenheight = 500
OpenWindowedScreen(WindowID(0), 0, 0, #screenwidth, #screenheight)
suppose you want to draw a function from -1 to 1 in x and y directions, ie we have a square of size 2*2. we need to project this square to the computer graphics window.
instead of multiplying x and y with a factor to make it bigger so we can see it on the screen, use this function:

Code: Select all

Width = 2: Height = 2
CameraProjectionMode(0, #PB_Camera_Orthographic, Width, Height)
and then we draw points without distracting ourselves to the process of projecting our real plotting within small or big intervals to the computer screen.
here is a graphics to show a circle and a "Cayley's_sextic" curve
Image
use this screen ruler http://www.spadixbd.com/freetools/?referrer=JRulerUser
to measure the graphics in pixels or in centimeters, right click on the ruler to choose actions and options

needs PB 5.62

Code: Select all

 #screenwidth = 500
 #screenheight = 500

Declare Curve()
Define.f KeyX, KeyY, MouseX, MouseY

InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  OpenWindow(0, 0, 0, 800, 600, "#PB_Camera_Orthographic ... Cayley's_sextic Curve + .... Circle", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 130, 40, #screenwidth, #screenheight)

     
    ;- Material
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
       ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 10, #PB_Absolute)
    ;CameraFOV(0, 40)
    CameraLookAt(0, 0,  0,  0)
    CameraBackColor(0, RGB(250, 250, 240))
    
    ;glEnable_(#GL_POINT_SMOOTH) ; effective only when compiling with opengl subsystem
    
    CreateLine3D(1, -1, 0, 0, RGB(255,0,0), 1, 0, 0, RGB(255,0,0))
    CreateLine3D(2, 0, -1, 0, RGB(255,0,0), 0, 1, 0, RGB(255,0,0))
    
    Width.f = 2.5 * (#screenwidth / #screenheight); width of the plotting x range from -xMin to xMax : -1.25 to 1.25
    Height.f = 2.5; from -1.25 to 1.125
    CameraProjectionMode(0, #PB_Camera_Orthographic, Width, Height)
    
    CreateText3D(5, "-1") : ScaleText3D(5, 0.2,0.2,0.2,#PB_Absolute)
    Text3DColor(5, RGBA(255, 0, 0, 255))
    MoveText3D(5, -1, 0, 0, #PB_Absolute)
      
    Curve()
    
    SetMeshMaterial(0, MaterialID(0))
    Global Stars = CreateNode(#PB_Any)
    AttachNodeObject(Stars, MeshID(0)) ; attach the graphics mesh to a node so when we move or rotate the node the graphics will follow it
  
    Repeat
      Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
    EndSelect
    Until event = 0
    
      ExamineKeyboard()
      
      ;RotateNode(Stars, 0, 0, 1, #PB_Relative)
      
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
    End
    
 Procedure Curve()
   ;CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
   CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
      
    t.f = -1
    While t <= 2*#PI
      
      ;Cayley's_sextic curve parametric equations
      x.f = Pow(Cos(t),3)* Cos(3*t)
      y.f = Pow(Cos(t),3)* Sin(3*t)
      MeshVertexPosition(x, y, 0)
      MeshVertexColor(RGB(255,0,0))
      
      ; Circle parametric equations
      x.f=Cos(t)*1
      y.f=Sin(t)*1
      MeshVertexPosition(x, y, 0)
      MeshVertexColor(RGB(0,150,255))
        
      t+0.001
    Wend
    

    FinishMesh(#False)
    EndProcedure


applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Fitting 2D graphics nicely in the graphics window

Post by applePi »

an equivalent 2D for OpenGl Gadget using gluOrtho2D_(left, right, bottom, top) with glDrawArrays_(). we fill the arrays with data, and then we plot the arrays with glDrawArrays_().
gluOrtho2D_(-1, 1, -1, 1) will project the small Cartesian graphics windows to your opengl gadget screen say (500,500). the plotting will be like in the usual school mathematics with 0,0 is at the center

here we draw a Cayley's_sextic Curve, a circle. xy axis, a grid, all at the same time. note we can change the length of lines by glLineWidth_ , size of points with glPointSize_
Image

Code: Select all

Declare FillCurveData()
Declare FillCircleData()
Declare AxisData()
Declare drawGrid(width.f, height.f, grid_width.f)

Declare DrawCurve()

Structure vertices
  x.f
  y.f
  r.f
  g.f
  b.f
  a.f
EndStructure


Global Dim vert.vertices(500000) ; for the CurveData
Global Dim vert2.vertices(500000) ; for the CircleData()
Global Dim vert3.vertices(4)      ; for the x-y axis
Global Dim vert4.vertices(1000)      ; for the Grid

Global v1.vertices
Global v2.vertices



OpenWindow(0, 0, 0, 500, 500, "Curves ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenGLGadget(0, 25, 25, 450, 450)
     
FillCurveData()
FillCircleData()
AxisData()
drawGrid(5.0, 3.0, 0.1) ; drawGrid(width.f, height.f, grid_width.f)

;gluOrtho2D_(left, right, bottom, top)
;gluOrtho2D_(-#PI, #PI, -#PI, #PI)
gluOrtho2D_(-1, 1, -1, 1)
;gluOrtho2D_(-1.0, 2, -1.0, 2)
glMatrixMode_ (#GL_PROJECTION)
;glMatrixMode_ (#GL_MODELVIEW)

glEnable_(#GL_POINT_SMOOTH);
;glHint_(#GL_POINT_SMOOTH_HINT, #GL_NICEST);

;glEnable_(#GL_BLEND);
;glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA);   
   
 glEnable_(#GL_LINE_WIDTH)
 glEnable_(#GL_LINE_SMOOTH);
 glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
   
Repeat
  
Repeat
  event = WindowEvent()
  
    If event = #PB_Event_CloseWindow
      quit = #True
    EndIf
    key = GetGadgetAttribute(0,#PB_OpenGL_Key )
    If key = #PB_Shortcut_Escape ;  Esc key to exit
      quit = #True
    EndIf
      
  Until event = 0 Or quit = #True
  
  DrawCurve()  
    
  ;SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
  Delay(10)
Until Event = #PB_Event_CloseWindow Or quit = #True


Procedure FillCurveData() 
  b = 0
  Protected.f x, y, inc, t ,q
      
  inc = 0.001
   t.f = -1
    While t <= 2*#PI
      ;Cayley's_sextic curve parametric equations
      x.f = Pow(Cos(t),3)* Cos(3*t)
      y.f = Pow(Cos(t),3)* Sin(3*t)
             
             
       vert(b)\x = x ; position
       vert(b)\y = y
      
       vert(b)\r = 1 ; color red
       vert(b)\g = 0 
       vert(b)\b = 0
         
       t + inc 
       b + 1

     Wend
         
     ReDim vert.vertices(b) 
    
     SetWindowTitle(0, Str(b)+" vertices in the Red curve")
           
EndProcedure
   
Procedure FillCircleData()
 b = 0
 Protected.f x, y, inc, t 
 inc = 0.001
 
 While t<=2*#PI
   vert2(b)\r = 1 :vert2(b)\g = 1 :vert2(b)\b = 0
  ; Circle parametric equations
   vert2(b)\x = Cos(t)*1
   vert2(b)\y = Sin(t)*1  
   
    t + inc 
    b + 1
       
  Wend
 
  
 ReDim vert2.vertices(b) 

EndProcedure

Procedure AxisData()

     ;Data For the perpendicular coordinates x-y
     ; Horizontal Blue Line 
      vert3(2)\r = 0.2 :vert3(2)\g = 0.5 : vert3(2)\b = 1
      vert3(0)\x = -#PI: vert3(0)\y = 0
      vert3(2)\r = 0.2 :vert3(2)\g = 0.6 : vert3(2)\b = 1
      vert3(1)\x = #PI: vert3(1)\y = 0
      
      ;Vertical Blue Line
      vert3(2)\r = 0 :vert3(2)\g = 0 : vert3(2)\b = 1
      vert3(2)\x = 0: vert3(2)\y = -#PI
      vert3(3)\r = 0 :vert3(3)\g = 0 : vert3(3)\b = 1
      vert3(3)\x = 0: vert3(3)\y = #PI
EndProcedure

Procedure drawGrid(width.f, height.f, grid_width.f)
  ;horizontal lines
  ;For(float i=-height; i<height; i+=grid_width){
  b=0
   i.f = -height
   While i < height 
     ;Vertex v1 = {-width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
     v1\x = -width: v1\y= i 
      v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
      ;Vertex v2 = {width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
      v2\x = width: v2\y= i 
      v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
      
      ;drawLineSegment(@v1, @v2, 1) 
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v1\x
       vert4(b)\y = v1\y
       b+1
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v2\x
       vert4(b)\y = v2\y
   
    b + 1;
      i + grid_width
   Wend
   
   ;vertical lines
   ;for(float i=-width; i<width; i+=grid_width){
   i.f = -width
  
   While i < width 
     ;Vertex v1 = {i, -height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
     v1\x = i: v1\y= -height
      v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
      ;Vertex v2 = {i, height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
      v2\x = i: v2\y= height
      v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
      
      ;drawLineSegment(@v1, @v2, 1) 
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v1\x
       vert4(b)\y = v1\y
       b+1
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v2\x
       vert4(b)\y = v2\y
       
       i + grid_width
       b+1
   Wend
   ReDim vert4.vertices(b) 
EndProcedure


 
Procedure DrawCurve()
  SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
  ;glClearColor_(0.2, 0.2, 0.1, 1.0) ; background color
  glClearColor_(0.5, 0.8, 0.9, 0.0) ; background color
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
    
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      
           
      glDisable_(#GL_LINE_SMOOTH)
      ;draw the Grid
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert4(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert4(0)\r)
      glLineWidth_(1)
      glDrawArrays_(#GL_LINES, 0, ArraySize(vert4()))
      
      glEnable_(#GL_LINE_WIDTH)
      glEnable_(#GL_LINE_SMOOTH);
      glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
      ;plot the perpendicular x-y Lines
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert3(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert3(0)\r)
      glLineWidth_(2)
      glDrawArrays_(#GL_LINES, 0, ArraySize(vert3()))
      
      ;circle
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert2(0)\x)
       glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert2(0)\r)
       glLineWidth_(2)
       glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert2()))
       ;glDrawArrays_(#GL_LINES, 0, ArraySize(vert2()))
       ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert2()))
       
       ;Cayley's_sextic Curve
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert(0)\r)
      glLineWidth_(1)
      glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert()))
      ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert()))
                  
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
      
   
  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
EndProcedure



note: for the same approach but for 3D graphics look my primitive STL models reader here viewtopic.php?f=36&t=72545#p539553
Last edited by applePi on Thu Jul 25, 2019 9:52 am, edited 1 time in total.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Fitting 2D graphics nicely in the graphics window

Post by RSBasic »

@applePi
The window flickers extremely. (Windows 10)
Why are you still using Windows XP after 17 years?
Image
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Fitting 2D graphics nicely in the graphics window

Post by applePi »

Thanks RSBasic for reporting. sorry i haven't tried it on windows 7+ before posting.
now i am inside windows 7 x64 on the same machine. yes it is flickering too much. after too much experimenting i have commenting line 71:
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
and now it is not flickering. (as noticed below there was erroneously 2 occurences of SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
i'm still using xp because i find it more comfortable to my eyes (limited vision). i have done too much efforts to make win7 better (in classic mode) using different fonts steels but no way.
Last edited by applePi on Thu Jul 25, 2019 1:43 pm, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Fitting 2D graphics nicely in the graphics window

Post by applePi »

i have found the reason: the SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
is written twice one in line 71 the other in line 243. so removing a one will make graphics not flicker in windows 7+.
i thought first the SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True) is not needed and thats wrong
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Fitting 2D graphics nicely in the graphics window

Post by RSBasic »

Yes, it looks good now. Thank you Image
Image
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Fitting 2D graphics nicely in the graphics window

Post by applePi »

Thanks RSBasic ,
Now how to add Text to the scene using OpenglGadget ?, i have used the functions posted by hagibaba in Nehe lesson 13 viewtopic.php?f=12&t=25317
to let that example works in PB 5.70 just comment lines 11 + 15 , to switch to windowed screen press F1. the example itself is a treasure and fun for the windows experts in the forum.
it is hard to mix text with graphics, here is my last attempt.
to draw data from -1 to +1 vhange line 143 to glOrtho_(-1,1,-1,1,-1,1)
for -3 to +3 change it to glOrtho_(-3,3,-3,3,-3,3)
Image
tested with PB 5.70 LTS win7_x64

Code: Select all

EnableExplicit

Declare FillCurveData()
Declare FillCircleData()
Declare AxisData()
Declare drawGrid(width.f, height.f, grid_width.f)

Declare DrawCurve()

Structure vertices
  x.f
  y.f
  z.f
  r.f
  g.f
  b.f
  a.f
EndStructure


Global Dim vert.vertices(500000) ; for the CurveData
Global Dim vert2.vertices(500000) ; for the CircleData()
Global Dim vert3.vertices(4)      ; for the x-y axis
Global Dim vert4.vertices(1000)   ; for the Grid

Global v1.vertices
Global v2.vertices

Global ro, ro2, event, rot

Global hDC.l ;Private GDI Device Context
Global hRC.l ;Permanent Rendering Context
Global hWnd.l ;Holds Our Window Handle
Global hInstance.l ;Holds The Instance Of The Application

Global Dim keys.b(256) ;Array Used For The Keyboard Routine
Global active.b=#True ;Window Active Flag Set To TRUE By Default
Global fullscreen.b=#True ;Fullscreen Flag Set To Fullscreen Mode By Default

Global base.l ;Base Display List For The Font Set

Global swidth.l ;screen width (Note: added code to print window size)
Global sheight.l ;screen height
Global width.l = 600
Global height.l = 600
swidth = width
sheight = height

OpenWindow(0, 0, 0, width, height, "OpenGL fonts", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(100,100,40))
OpenGLGadget(0, 25, 15, 550, 550, #PB_OpenGL_Keyboard)

hDC=GetDC_(WindowID(0))

FillCurveData()
FillCircleData()
AxisData()
drawGrid(5.0, 3.0, 0.1) ; drawGrid(width.f, height.f, grid_width.f)

;wingdi.h constants
#ANTIALIASED_QUALITY=4 ;for CreateFont_()
#DM_BITSPERPEL=$40000
#DM_PELSWIDTH=$80000
#DM_PELSHEIGHT=$100000


Procedure BuildFont(name.s,height.l,bold.l,italic.b,symbol.l) ;Build Our Bitmap Font
 ;BuildFont("Courier New",24,1,0,0) ;Build The Font
 Protected font.l ;Windows Font ID
 Protected oldfont.l ;Used For Good House Keeping
 
 If bold : bold=#FW_BOLD : Else : bold=#FW_NORMAL : EndIf ;font weight
 If symbol : symbol=#SYMBOL_CHARSET : Else : symbol=#ANSI_CHARSET : EndIf ;character set
 
 base=glGenLists_(96) ;Storage For 96 Characters
  
 ;CreateFont_(Height, Width, Angle Of Escapement, Orientation Angle, Weight, Italic, Underline, Strikeout, Character Set, Output Precision, Clipping Precision, Output Quality, Family And Pitch, Name)
 font=CreateFont_(-height,0,0,0,bold,italic,#False,#False,symbol,#OUT_TT_PRECIS,#CLIP_DEFAULT_PRECIS,#ANTIALIASED_QUALITY,#FF_DONTCARE | #DEFAULT_PITCH,name)
 
 oldfont=SelectObject_(hDC,font) ;Selects The Font We Want
 wglUseFontBitmaps_(hDC,32,96,base) ;Builds 96 Characters Starting At Character 32
 SelectObject_(hDC,oldfont) ;reselect the old font again
 DeleteObject_(font) ;Delete The Font
 
EndProcedure

Procedure KillFont() ;Delete The Font List

 glDeleteLists_(base,96) ;Delete All 96 Characters
 
EndProcedure

Procedure glPrint(text.s) ;Custom GL "Print" Routine

 If text="" ;If There's No Text
  ProcedureReturn #False ;Do Nothing
 EndIf
 
 glPushAttrib_(#GL_LIST_BIT) ;Pushes The Display List Bits
  glListBase_(base-32) ;Sets The Base Character to 32
  glCallLists_(Len(text),#GL_UNSIGNED_BYTE,text) ;Draws The Display List Text
 glPopAttrib_() ;Pops The Display List Bits
 
EndProcedure

Procedure.l InitGL() ;All Setup For OpenGL Goes Here

 ;BuildFont(name,height,bold,italic,symbol)
 BuildFont("Courier New",24,1,0,0) ;Build The Font
 
 glShadeModel_(#GL_SMOOTH) ;Enable Smooth Shading
 glClearColor_(0.0,0.0,0.0,0.5) ;Black Background
 glClearDepth_(1.0) ;Depth Buffer Setup
 glEnable_(#GL_DEPTH_TEST) ;Enables Depth Testing
 glDepthFunc_(#GL_LEQUAL) ;The Type Of Depth Testing To Do
 glHint_(#GL_PERSPECTIVE_CORRECTION_HINT,#GL_NICEST) ;Really Nice Perspective Calculations 
 
 ProcedureReturn #True ;Initialization Went OK
 
EndProcedure

InitGL()
 
glEnable_(#GL_POINT_SMOOTH);
glHint_(#GL_POINT_SMOOTH_HINT, #GL_NICEST);
glEnable_(#GL_LINE_WIDTH)
glEnable_(#GL_LINE_SMOOTH);
glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
 
Repeat

 event = WindowEvent()
  
 rot+1
 ;glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
 glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)  
   
 glMatrixMode_(#GL_PROJECTION)
 glLoadIdentity_()
 ;gluOrtho2D_(-1, 1, -1, 1)
 ;gluOrtho2D_(-2, 2, -2, 2)
 ;glOrtho_(-1,1,-1,1,-1,1) 
 glOrtho_(-2,2,-2,2,-2,2) 
 ;glOrtho_(-3,3,-3,3,-3,3) 
 gluPerspective_(90.0, 1, 0.1, 100.0) ; essential to see the text
 glMatrixMode_(#GL_MODELVIEW)
  ;============================================
  
  glLoadIdentity_() 
  glPushMatrix_() 
  ;glTranslatef_(0, 0, -1)
  DrawCurve() 
  glPopMatrix_()
  ;----------------------------------------------
 glPushMatrix_() 
 glLoadIdentity_() 
 glTranslatef_(0.0,0.0,-10) ;Move 10 Units Into The Screen
 
 glColor3f_(0,0,1)
 glRasterPos2f_(-8, 1)
 glPrint("Active OpenGL Text With NeHe -                              ") ;Print GL Text To The Screen
 
 glColor3f_(1,0,1)
 glRasterPos2f_(2,-3) ;position text 2 across, -3 down
 glPrint(Str(swidth)+"x"+Str(sheight)+"        ") ;print window size at 2,-3
 
 glColor3f_(1,0,0)
 glRasterPos2f_(-12, 0)
 glPrint("-1  ")
 
 glPopMatrix_()
  
 Delay(10)
 
 SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Until event = #PB_Event_CloseWindow
KillFont() 


Procedure FillCurveData() 
  Protected.l b = 0
  Protected.f x, y, inc, t ,q
      
  inc = 0.001
   t.f = -1
    While t <= 2*#PI
      ;Cayley's_sextic curve parametric equations
      x.f = Pow(Cos(t),3)* Cos(3*t)
      y.f = Pow(Cos(t),3)* Sin(3*t)
             
             
       vert(b)\x = x ; position
       vert(b)\y = y
      
       vert(b)\r = 1 ; color red
       vert(b)\g = 0 
       vert(b)\b = 0
         
       t + inc 
       b + 1

     Wend
         
     ReDim vert.vertices(b) 
    
     SetWindowTitle(0, Str(b)+" vertices in the Red curve")
           
EndProcedure
   
Procedure FillCircleData()
 Protected b = 0
 Protected.f x, y, inc, t 
 inc = 0.001
 
 While t<=2*#PI
   vert2(b)\r = 1 :vert2(b)\g = 1 :vert2(b)\b = 0
  ; Circle parametric equations
   vert2(b)\x = Cos(t)*1
   vert2(b)\y = Sin(t)*1  
   
    t + inc 
    b + 1
       
  Wend
 
  
 ReDim vert2.vertices(b) 

EndProcedure

Procedure AxisData()

     ;Data For the perpendicular coordinates x-y
     ; Horizontal Blue Line 
      vert3(0)\r = 1.0 :vert3(0)\g = 0.6 : vert3(0)\b = 0
      vert3(0)\x = -#PI: vert3(0)\y = 0
      vert3(1)\r = 1.0 :vert3(1)\g = 0.6 : vert3(1)\b = 0
      vert3(1)\x = #PI: vert3(1)\y = 0
      
      ;Vertical Blue Line
      vert3(2)\r = 1 :vert3(2)\g = 0.6 : vert3(2)\b = 0
      vert3(2)\x = 0: vert3(2)\y = -#PI
      vert3(3)\r = 1 :vert3(3)\g = 0.6 : vert3(3)\b = 0
      vert3(3)\x = 0: vert3(3)\y = #PI
EndProcedure

Procedure drawGrid(width.f, height.f, grid_width.f)
  ;horizontal lines
  ;For(float i=-height; i<height; i+=grid_width){
  Protected b=0
   Protected.f i = -height
   While i < height 
     ;Vertex v1 = {-width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
     v1\x = -width: v1\y= i 
      v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
      ;Vertex v2 = {width, i, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
      v2\x = width: v2\y= i 
      v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
      
      ;drawLineSegment(@v1, @v2, 1) 
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v1\x
       vert4(b)\y = v1\y
       b+1
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v2\x
       vert4(b)\y = v2\y
   
    b + 1;
      i + grid_width
   Wend
   
   ;vertical lines
   ;for(float i=-width; i<width; i+=grid_width){
   i.f = -width
  
   While i < width 
     ;Vertex v1 = {i, -height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
     v1\x = i: v1\y= -height
      v1\r=1 : v1\g=1: v1\b =1 : v1\a = 1
      ;Vertex v2 = {i, height, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f};
      v2\x = i: v2\y= height
      v2\r=1 : v2\g=1: v2\b =1 : v2\a = 1
      
      ;drawLineSegment(@v1, @v2, 1) 
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v1\x
       vert4(b)\y = v1\y
       b+1
       vert4(b)\r = v1\r :vert4(b)\g = v1\g :vert4(b)\b = v1\b
       vert4(b)\x = v2\x
       vert4(b)\y = v2\y
       
       i + grid_width
       b+1
   Wend
   ReDim vert4.vertices(b) 
EndProcedure
   
 Procedure DrawCurve()
  ;SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
  ;glClearColor_(0.2, 0.2, 0.1, 1.0) ; background color
  glClearColor_(0.5, 0.8, 0.9, 0.0) ; background color
   gluLookAt_(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
    
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      
           
      glDisable_(#GL_LINE_SMOOTH)
      ;draw the Grid
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert4(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert4(0)\r)
      glLineWidth_(1)
      glDrawArrays_(#GL_LINES, 0, ArraySize(vert4()))
      
      glEnable_(#GL_LINE_WIDTH)
      glEnable_(#GL_LINE_SMOOTH);
      glHint_(#GL_LINE_SMOOTH_HINT, #GL_NICEST)
      ;plot the perpendicular x-y Lines
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert3(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert3(0)\r)
      glLineWidth_(2)
      glDrawArrays_(#GL_LINES, 0, ArraySize(vert3()))
      
      ;circle
       glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert2(0)\x)
       glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert2(0)\r)
       glLineWidth_(2)
       glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert2()))
       ;glDrawArrays_(#GL_LINES, 0, ArraySize(vert2()))
       ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert2()))
       

      ro+1 
      glRotatef_(ro, 0, 0, 1) 
       ;Cayley's_sextic Curve
      glVertexPointer_(2, #GL_FLOAT, SizeOf(vertices), @vert(0)\x)
      glColorPointer_(4, #GL_FLOAT, SizeOf(vertices), @vert(0)\r)
      glLineWidth_(1)
      glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vert()))
      ;glDrawArrays_(#GL_POINTS, 0, ArraySize(vert()))
                  
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)

EndProcedure



Post Reply