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
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
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
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