OpenGLGadget inside CanvasContainer

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

OpenGLGadget inside CanvasContainer

Post by applePi »

using MathArt code posted here
http://www.purebasic.fr/english/viewtop ... 79#p319879
but inside a CanvasContainer like this one:
http://www.purebasic.fr/english/viewtop ... 77#p501777

i have used opengl lists to let the graphics persistent on the screen (it is calculated previously before entering the main loop, and stored previously in a List which then in the main loop we call the List again and again to display it
i will transform it later to use VBO like the example called " 2D Plotting with VBO example " posted here http://www.purebasic.fr/english/viewtop ... 7&p=495333
i have just bad eyes vision nowadays, so may not post too much like before.
i hope Fred add CanvasContainer to Purebasic v5.4x production line :D

Image

needes PB 5.60

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Canvas Container", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
#Button = 4
;Global flag.i = 0
Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l
Declare MathArt()
   CanvasGadget(0, 40, 40, 210, 200, #PB_Canvas_Container)
   EditorGadget(1, 0, 0, 200, 30, #PB_Editor_WordWrap)
   ;ButtonGadget(#Button, 0, 40, 60, 40, "Stop/Rot")
   OpenGLGadget(2, 0, 30, 100, 100)
   CloseGadgetList()
   
  AddGadgetItem(1, 0, "Thats Hot")
 
  ;SetActiveGadget(0)
  Global run = 1
  If StartDrawing(CanvasOutput(0))
     Circle(160, 120, 7, RGB(0, 0, 250))
     Circle(160, 150, 10, RGB(255, 0, 0))
     StopDrawing()
   EndIf
  x.f=100
  If StartDrawing(CanvasOutput(0))
  For i=0 To 1000
    y.f=Sin(x)*20
    Plot(x,y+80,RGB(255,0,0))
    x+0.1
  Next
  StopDrawing()
  EndIf
 
gluOrtho2D_(-axrng, axrng, -axrng, axrng)
glMatrixMode_ (#GL_MODELVIEW)

MathArt()
  
  Repeat
    Repeat 
    EventID = WindowEvent()
    Select EventID
      Case #PB_Event_Gadget
          ;If EventGadget() = #Button
           ; run ! 1
          ;EndIf
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until EventID = 0
  
  glCallList_(glist)
        
  SetGadgetAttribute(2, #PB_OpenGL_FlipBuffers, #True)
    
 Until Quit = #True 
  
 End
 
       
Procedure MathArt() 
      glPushMatrix_() 
         ; make the glist
         ;glGenLists_(glist.l)
        glist = glGenLists_(1)
        glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
        glLoadIdentity_()
        
        x=-axrng
        While x <= axrng
          x = x + 0.03
         
          y=-axrng
          While y <= axrng
            y = y + 0.03
            count = count + 1
            r = Cos(x) + Sin(y)
            glColor3f_(Cos(y*r), Cos(x*y*r), Sin(r*x))
            glBegin_(#GL_POINTS)
            glVertex2f_(x, y) 
            glEnd_() ; finished 
             
           Wend
         Wend
         glEndList_()
         glPopMatrix_()
         glFinish_()
                
    
EndProcedure       

 
 
note: to zoom in change gluOrtho2D_(-axrng, axrng, -axrng, axrng)
to gluOrtho2D_(-axrng/2, axrng/2, -axrng/2, axrng/2)
read about gluOrtho2D in the web
Last edited by applePi on Fri Jul 14, 2017 11:44 am, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: OpenGLGadget inside CanvasContainer

Post by applePi »

Note: the final version is at the end http://www.purebasic.fr/english/viewtop ... 50#p509039 using the glDrawArray to display graphics on the 2 OpenGLGadgets. but the following 2 examples have bugs related to the usage of opengl lists if you want to investigate
here is 2 OpenGLGadgets inside CanvasGadget (using the PB official example OpenGLGadget.pb as a base)
openglgadget number 2 display mystery curve by calling glDrawarray
openglgadget number 3 display Math Art bt calling glist

but i have noticed a flicker in the mystery curve while rotating
Image

Code: Select all

Structure vertices
  x.f
  y.f
  z.f
EndStructure

Structure colors
  r.f
  g.f
  b.f
EndStructure

Global Dim vertex.vertices(0) ; for the Mystery curve
Global Dim colorData.colors(0) ; for the Mystery curve
Global Dim index(0) ; for the Mystery curve
Global indexCount, indexsize ; for the Mystery curve

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l

Declare MathArtMake()
Declare Mystery_curve_Data()
Declare Draw_Mystery_Curve(Gadget)
Declare DrawMathArt(Gadget)

OpenWindow(0, 0, 0, 300, 300, "Canvas Container", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l
Declare SetupGL()
Declare SetupGL2()
Declare MathArt()
Declare Mystery_curve_Data()

   CanvasGadget(0, 40, 40, 210, 250, #PB_Canvas_Container)
   EditorGadget(1, 0, 0, 200, 30, #PB_Editor_WordWrap)
   OpenGLGadget(2, 0, 30, 100, 100)
   SetupGL()
   OpenGLGadget(3, 0, 135, 100, 100)
   SetupGL2()
   CloseGadgetList()
   
  AddGadgetItem(1, 0, "Thats Hot")
 
  ;SetActiveGadget(0)
  Global run = 1
  If StartDrawing(CanvasOutput(0))
     Circle(160, 120, 7, RGB(0, 0, 250))
     Circle(160, 150, 10, RGB(255, 0, 0))
     StopDrawing()
   EndIf
  x.f=100
  If StartDrawing(CanvasOutput(0))
  For i=0 To 1000
    y.f=Sin(x)*20
    Plot(x,y+80,RGB(255,0,0))
    x+0.1
  Next
  StopDrawing()
  EndIf
  
MathArtMake()
Mystery_curve_Data()

AddWindowTimer(0, 1, 16) ; about 60 fps

Repeat
  Event = WindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
      
        Draw_Mystery_Curve(2)
 
        DrawMathArt(3)
               
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow
  
 End
 
 
Procedure SetupGL() ; for the Mystery curve
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(50.0, 200/200, 1.0, 10.0) 
  
  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)
  
  glTranslatef_(0, 0, -4.0)
  
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
                              ; rendered objects are inside the z-buffer.
  
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                              ; ignored. This works only with CLOSED objects like a cube... Singles
                              ; planes surfaces will be visibles only on one side.
    
  glPointSize_(2)
  glLineWidth_(3)
  glClearColor_(1.0, 1.0, 0.5, 1.0);
  ;glShadeModel_(#GL_SMOOTH)
EndProcedure

Procedure SetupGL2() ; for the Math Art
 ;gluOrtho2D_(left, right, bottom, top)
 gluOrtho2D_(-axrng, axrng, -axrng, axrng)
 glMatrixMode_ (#GL_MODELVIEW)
 
EndProcedure

Procedure MathArtMake() 
    
      glPushMatrix_() 
      
      ; make the glist
         ;glGenLists_(glist.l)
        glist = glGenLists_(1)
        glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
        glLoadIdentity_()
        
        x=-axrng
        While x <= axrng
          x = x + 0.03
         
          y=-axrng
          While y <= axrng
            y = y + 0.03
            count = count + 1
            r = Cos(x) + Sin(y)
            glColor3f_(Cos(y*r), Cos(x*y*r), Sin(r*x))
            glBegin_(#GL_POINTS)
            glVertex2f_(x, y) 
            glEnd_() ; finished 
             
           Wend
         Wend
         glEndList_()
         glPopMatrix_()
         glFinish_()
                
    
 EndProcedure
 
Procedure DrawMathArt(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  
  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  glCallList_(glist)
  
  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
EndProcedure

Procedure Mystery_curve_Data()
      
    Protected.f x, y, z, t

  While t <= 2*#PI
  x.f = Cos(t) - Cos(6* t)/2 + Sin(14* t)/3
  y.f = Cos(14* t)/3 + Sin(t)- Sin(6* t)/2 
  z.f = 0
  
  ReDim vertex.vertices(a)
  vertex(a)\x = x
  vertex(a)\y = y
  vertex(a)\z = z

  ReDim colorData.colors(a)
  
  colorData(a)\r = Sin(t) :colorData(a)\g = Cos(t) :colorData(a)\b = 1
  a+1
  VertexCount = a
      
  ReDim index(a)
  index(a)=a
  indexCount = VertexCount ; it is not neccessary indexCount = VertexCount ;just in this shape         
       
  t + 0.005
      
  Wend

  indexsize = ArraySize(index())
EndProcedure 

Procedure Draw_Mystery_Curve(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      
  glRotatef_(1, 0, 0, 1)
  
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      glVertexPointer_(3, #GL_FLOAT, SizeOf(vertices), @vertex(0))
      glColorPointer_(3, #GL_FLOAT, SizeOf(colors), @colorData(0))
      
      ;glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
      glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
      
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
   SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
      
EndProcedure

here is something erroneous: hope someone help
i want openglgadget 2 to display the MathArt
while openglgadget 3 to display the Mystery curve
but it happlened the mystery curve only displayed in openglgadget 3 while openglgadget 2 is empty. we notice that now there is no flicker in mystery curve
where is the problem ??
note that i have reveresed the SetupGL and SetupGL2 after the definition of openglgadget 2 and 3
Image

Code: Select all

Structure vertices
  x.f
  y.f
  z.f
EndStructure

Structure colors
  r.f
  g.f
  b.f
EndStructure

Global Dim vertex.vertices(0) ; for the Mystery curve
Global Dim colorData.colors(0) ; for the Mystery curve
Global Dim index(0) ; for the Mystery curve
Global indexCount, indexsize ; for the Mystery curve

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l

Declare MathArtMake()
Declare Mystery_curve_Data()
Declare Draw_Mystery_Curve(Gadget)
Declare DrawMathArt(Gadget)

OpenWindow(0, 0, 0, 300, 300, "Canvas Container", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l
Declare SetupGL()
Declare SetupGL2()
Declare MathArt()
Declare Mystery_curve_Data()

   CanvasGadget(0, 40, 40, 210, 250, #PB_Canvas_Container)
   EditorGadget(1, 0, 0, 200, 30, #PB_Editor_WordWrap)
   OpenGLGadget(2, 0, 30, 100, 100)
   SetupGL2()
   OpenGLGadget(3, 0, 135, 100, 100)
   SetupGL()
   CloseGadgetList()
   
  AddGadgetItem(1, 0, "Thats Hot")
 
  ;SetActiveGadget(0)
  Global run = 1
  If StartDrawing(CanvasOutput(0))
     Circle(160, 120, 7, RGB(0, 0, 250))
     Circle(160, 150, 10, RGB(255, 0, 0))
     StopDrawing()
   EndIf
  x.f=100
  If StartDrawing(CanvasOutput(0))
  For i=0 To 1000
    y.f=Sin(x)*20
    Plot(x,y+80,RGB(255,0,0))
    x+0.1
  Next
  StopDrawing()
  EndIf
  
MathArtMake()
Mystery_curve_Data()

AddWindowTimer(0, 1, 16) ; about 60 fps

Repeat
  Event = WindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
      
        Draw_Mystery_Curve(3)
 
        DrawMathArt(2)
               
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow
  
 End
 
 
Procedure SetupGL() ; for the Mystery curve
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(50.0, 200/200, 1.0, 10.0) 
  
  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)
  
  glTranslatef_(0, 0, -4.0)
  
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
                              ; rendered objects are inside the z-buffer.
  
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                              ; ignored. This works only with CLOSED objects like a cube... Singles
                              ; planes surfaces will be visibles only on one side.
    
  glPointSize_(2)
  glLineWidth_(3)
  glClearColor_(1.0, 1.0, 0.5, 1.0);
  ;glShadeModel_(#GL_SMOOTH)
EndProcedure

Procedure SetupGL2() ; for the Math Art
 ;gluOrtho2D_(left, right, bottom, top)
 gluOrtho2D_(-axrng, axrng, -axrng, axrng)
 glMatrixMode_ (#GL_MODELVIEW)
 
EndProcedure

Procedure MathArtMake() 
    
      glPushMatrix_() 
      
      ; make the glist
         ;glGenLists_(glist.l)
        glist = glGenLists_(1)
        glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
        glLoadIdentity_()
        
        x=-axrng
        While x <= axrng
          x = x + 0.03
         
          y=-axrng
          While y <= axrng
            y = y + 0.03
            count = count + 1
            r = Cos(x) + Sin(y)
            glColor3f_(Cos(y*r), Cos(x*y*r), Sin(r*x))
            glBegin_(#GL_POINTS)
            glVertex2f_(x, y) 
            glEnd_() ; finished 
             
           Wend
         Wend
         glEndList_()
         glPopMatrix_()
         glFinish_()
                
    
 EndProcedure
 
Procedure DrawMathArt(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  
  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

  glMatrixMode_ (#GL_MODELVIEW)
  glCallList_(glist)
  ;glPopMatrix_()
  glFinish_()

  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
EndProcedure

Procedure Mystery_curve_Data()
      
    Protected.f x, y, z, t

  While t <= 2*#PI
  x.f = Cos(t) - Cos(6* t)/2 + Sin(14* t)/3
  y.f = Cos(14* t)/3 + Sin(t)- Sin(6* t)/2 
  z.f = 0
  
  ReDim vertex.vertices(a)
  vertex(a)\x = x
  vertex(a)\y = y
  vertex(a)\z = z

  ReDim colorData.colors(a)
  
  colorData(a)\r = Sin(t) :colorData(a)\g = Cos(t) :colorData(a)\b = 1
  a+1
  VertexCount = a
      
  ReDim index(a)
  index(a)=a
  indexCount = VertexCount ; it is not neccessary indexCount = VertexCount ;just in this shape         
       
  t + 0.005
      
  Wend

  indexsize = ArraySize(index())
EndProcedure 

Procedure Draw_Mystery_Curve(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      
  glRotatef_(1, 0, 0, 1)
  
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      glVertexPointer_(3, #GL_FLOAT, SizeOf(vertices), @vertex(0))
      glColorPointer_(3, #GL_FLOAT, SizeOf(colors), @colorData(0))
      
      ;glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
      glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
      
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
   SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
      
EndProcedure

EDIT:
note we can display the Mystery curve in openglgadhet 2 and 3 without flicker
and one rotate to the right and the other to the left

Code: Select all

ro*-1
glRotatef_(1, 0, 0, ro)
Image
hope tomorrow to convert mathArt to be displayed by glDrawArray

Code: Select all

Structure vertices
  x.f
  y.f
  z.f
EndStructure

Structure colors
  r.f
  g.f
  b.f
EndStructure

Global Dim vertex.vertices(0) ; for the Mystery curve
Global Dim colorData.colors(0) ; for the Mystery curve
Global Dim index(0) ; for the Mystery curve
Global indexCount, indexsize ; for the Mystery curve
Global ro = 1


Declare Mystery_curve_Data()
Declare Draw_Mystery_Curve(Gadget)

OpenWindow(0, 0, 0, 300, 300, "Canvas Container", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l
Declare SetupGL()
Declare SetupGL2()
Declare MathArt()
Declare Mystery_curve_Data()

   CanvasGadget(0, 40, 40, 210, 250, #PB_Canvas_Container)
   EditorGadget(1, 0, 0, 200, 30, #PB_Editor_WordWrap)
   OpenGLGadget(2, 0, 30, 100, 100)
   SetupGL()
   OpenGLGadget(3, 0, 135, 100, 100)
   SetupGL()
   CloseGadgetList()
   
  AddGadgetItem(1, 0, "Thats Hot")
 
  ;SetActiveGadget(0)
  Global run = 1
  If StartDrawing(CanvasOutput(0))
     Circle(160, 120, 7, RGB(0, 0, 250))
     Circle(160, 150, 10, RGB(255, 0, 0))
     StopDrawing()
   EndIf
  x.f=100
  If StartDrawing(CanvasOutput(0))
  For i=0 To 1000
    y.f=Sin(x)*20
    Plot(x,y+80,RGB(255,0,0))
    x+0.1
  Next
  StopDrawing()
  EndIf
  
Mystery_curve_Data()

AddWindowTimer(0, 1, 16) ; about 60 fps

Repeat
  Event = WindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
      
        Draw_Mystery_Curve(2)
        Draw_Mystery_Curve(3)
                
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow
  
 End
 
 
Procedure SetupGL() ; for the Mystery curve
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(50.0, 200/200, 1.0, 10.0) 
  
  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)
  
  glTranslatef_(0, 0, -4.0)
  
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
                              ; rendered objects are inside the z-buffer.
  
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                              ; ignored. This works only with CLOSED objects like a cube... Singles
                              ; planes surfaces will be visibles only on one side.
    
  glPointSize_(2)
  glLineWidth_(3)
  glClearColor_(1.0, 1.0, 0.5, 1.0);
  ;glShadeModel_(#GL_SMOOTH)
EndProcedure


Procedure Mystery_curve_Data()
      
    Protected.f x, y, z, t

  While t <= 2*#PI
  x.f = Cos(t) - Cos(6* t)/2 + Sin(14* t)/3
  y.f = Cos(14* t)/3 + Sin(t)- Sin(6* t)/2 
  z.f = 0
  
  ReDim vertex.vertices(a)
  vertex(a)\x = x
  vertex(a)\y = y
  vertex(a)\z = z

  ReDim colorData.colors(a)
  
  colorData(a)\r = Sin(t) :colorData(a)\g = Cos(t) :colorData(a)\b = 1
  a+1
  VertexCount = a
      
  ReDim index(a)
  index(a)=a
  indexCount = VertexCount ; it is not neccessary indexCount = VertexCount ;just in this shape         
       
  t + 0.005
      
  Wend

  indexsize = ArraySize(index())
EndProcedure 

Procedure Draw_Mystery_Curve(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      
  ro*-1
  glRotatef_(1, 0, 0, ro)
  
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      glVertexPointer_(3, #GL_FLOAT, SizeOf(vertices), @vertex(0))
      glColorPointer_(3, #GL_FLOAT, SizeOf(colors), @colorData(0))
      
      ;glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
      glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
      
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
   SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
      
EndProcedure
Last edited by applePi on Sat Jul 15, 2017 6:37 am, edited 6 times in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: OpenGLGadget inside CanvasContainer

Post by applePi »

Final version
using glDrawArray for the two OpenGLgadgets , display the MathArt and mystery Curve without troubles or flickers. i just have reduced the number of mathart points (x = x + 0.1) instead of (x = x + 0.03) line 125 to speed the array filling since i have used Redim, if you know the number of needed points then there is no problem
try x = x + 0.03 line 125 and you need to wait 5 seconds to redim 444888 times
note that

Code: Select all

 ;gluOrtho2D_(left, right, bottom, top)
 gluOrtho2D_(-axrng, axrng, -axrng, axrng)
is still working to display math art correctly
Image

Code: Select all

Structure vertices
  x.f
  y.f
  z.f
EndStructure

Structure colors
  r.f
  g.f
  b.f
EndStructure

Global Dim vertex.vertices(0) ; for the Mystery curve
Global Dim colorData.colors(0) ; for the Mystery curve

Global Dim vertArt.vertices(0) ; for the math art
Global Dim colorArt.colors(0) ; for the math art


Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0
Global glist.l

Declare MathArtMake()
Declare Mystery_curve_Data()
Declare Draw_Mystery_Curve(Gadget)
Declare DrawMathArt(Gadget)

OpenWindow(0, 0, 0, 300, 300, "Canvas Container", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Global axrng.f = 10.0
Global x.f=0: Global y.f=0: Global r.f=0: Global count.f=0

Declare SetupGL()
Declare SetupGL2()
Declare MathArt()
Declare Mystery_curve_Data()

   CanvasGadget(0, 40, 40, 210, 250, #PB_Canvas_Container)
   EditorGadget(1, 0, 0, 200, 30, #PB_Editor_WordWrap)
   OpenGLGadget(2, 0, 30, 100, 100)
   SetupGL2()
   OpenGLGadget(3, 0, 135, 100, 100)
   SetupGL()
   CloseGadgetList()
   
  AddGadgetItem(1, 0, "Thats Hot")
 
  ;SetActiveGadget(0)

  If StartDrawing(CanvasOutput(0))
     Circle(160, 120, 7, RGB(0, 0, 250))
     Circle(160, 150, 10, RGB(255, 0, 0))
     StopDrawing()
   EndIf
  x.f=100
  If StartDrawing(CanvasOutput(0))
  For i=0 To 1000
    y.f=Sin(x)*20
    Plot(x,y+80,RGB(255,0,0))
    x+0.1
  Next
  StopDrawing()
  EndIf
  
MathArtMake()
Mystery_curve_Data()

AddWindowTimer(0, 1, 16) ; about 60 fps

Repeat
  Event = WindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
      
        Draw_Mystery_Curve(3)
 
        DrawMathArt(2)
               
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow
  
 End
 
 
Procedure SetupGL() ; for the Mystery curve
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(50.0, 200/200, 1.0, 10.0) 
  
  glMatrixMode_(#GL_MODELVIEW)
  
  glTranslatef_(0, 0, -4.0)
  
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
                              ; rendered objects are inside the z-buffer.
  
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                              ; ignored. This works only with CLOSED objects like a cube... Singles
                              ; planes surfaces will be visibles only on one side.
    
  glPointSize_(2)
  glLineWidth_(3)
  glClearColor_(1.0, 1.0, 0.5, 1.0);
  ;glShadeModel_(#GL_SMOOTH)
EndProcedure

Procedure SetupGL2() ; for the Math Art
 ;gluOrtho2D_(left, right, bottom, top)
 gluOrtho2D_(-axrng, axrng, -axrng, axrng)
 glMatrixMode_ (#GL_MODELVIEW)
 
EndProcedure

Procedure MathArtMake() 
  b = 0
  x.f = 0: y.f = 0: z.f = 0
      
        x=-axrng
        While x <= axrng
          x = x + 0.1
         
          y=-axrng
          While y <= axrng
            y = y + 0.03
            ;count = count + 1
            r = Cos(x) + Sin(y)
            
            ReDim colorArt.colors(b)
            colorArt(b)\r = Cos(y*r) :colorArt(b)\g = Cos(x*y*r) :colorArt(b)\b = Sin(r*x)
            ReDim vertArt.vertices(b)
            vertArt(b)\x = x
            vertArt(b)\y = y
            vertArt(b)\z = 0
            
            b + 1
                 
           Wend


         Wend
 
 ;Debug b
      
   
 EndProcedure
 
Procedure DrawMathArt(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      glVertexPointer_(3, #GL_FLOAT, SizeOf(vertices), @vertArt(0))
      glColorPointer_(3, #GL_FLOAT, SizeOf(colors), @colorArt(0))
      
      ;glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
      glDrawArrays_(#GL_POINTS, 0, ArraySize(vertArt()))
      
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
   
  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
EndProcedure

Procedure Mystery_curve_Data()
      
  Protected.f x, y, z, t

  While t <= 2*#PI
  x.f = Cos(t) - Cos(6* t)/2 + Sin(14* t)/3
  y.f = Cos(14* t)/3 + Sin(t)- Sin(6* t)/2 
  z.f = 0
  
  ReDim vertex.vertices(a)
  vertex(a)\x = x
  vertex(a)\y = y
  vertex(a)\z = z

  ReDim colorData.colors(a)
  
  colorData(a)\r = Sin(t) :colorData(a)\g = Cos(t) :colorData(a)\b = 1
  a+1
  
  t + 0.005
      
  Wend
;Debug a
EndProcedure 

Procedure Draw_Mystery_Curve(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
      
  glRotatef_(1, 0, 0, 1)
  
      glEnableClientState_(#GL_VERTEX_ARRAY)
      glEnableClientState_(#GL_COLOR_ARRAY)
      glVertexPointer_(3, #GL_FLOAT, SizeOf(vertices), @vertex(0))
      glColorPointer_(3, #GL_FLOAT, SizeOf(colors), @colorData(0))
      
      ;glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
      glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
      
      glDisableClientState_(#GL_COLOR_ARRAY)
      glDisableClientState_(#GL_VERTEX_ARRAY)
   SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
      
EndProcedure
Post Reply