the gluOrtho2D_(-10, 10, -10, 10) projects the tiny real values to a viewport, here the coordinates will look as Cartesian coordinates (the 0,0 is at the center)
gluOrtho2D_(0, 10, 0, 10) here the (0,0) will be at the bottom Left of every viewport
using GL Lists:
Code: Select all
OpenWindow(0, 0, 0, 300, 300, "several ViewPorts with glViewPort", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Global glist.l
Declare BuildScene(xrng.f, yrng.f)
glWin = OpenGLGadget(#PB_Any , 0, 0, 300, 300)
Global xrng.f = 10.0 , yrng.f = 10.0
gluOrtho2D_(-xrng, xrng, -yrng, yrng)
;glMatrixMode_ (#GL_MODELVIEW)
;glMatrixMode_ (#GL_PROJECTION)
BuildScene(xrng, yrng)
Repeat
Repeat
EventID = WindowEvent()
Select EventID
Case #PB_Event_Gadget
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until EventID = 0
glEnable_(#GL_SCISSOR_TEST);; look descr in line 25
glViewport_(0,0,150,150)
glScissor_(0,0,150,150) ; cut part from the viewPort so clear color can be effective localy and not to the whole gl screen ;
glClearColor_(0.98, 0.59, 0.69, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
glCallList_(glist)
glViewport_(150,0,150,150)
glScissor_(150,0,150,150) ;
glClearColor_(0.3, 0.6, 0.6, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
glCallList_(glist)
glViewport_(150,150,150,150)
glScissor_(150,150,150,150) ;
glClearColor_(0.2, 0.6, 0.9, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
glCallList_(glist)
glViewport_(0,150,150,150)
glScissor_(0,150,150,150) ;
glClearColor_(0.7, 0.6, 0.6, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
glCallList_(glist)
SetGadgetAttribute(glWin, #PB_OpenGL_FlipBuffers, #True)
;glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glDisable_(#GL_SCISSOR_TEST)
Until Quit = #True
End
Procedure BuildScene(xrng.f, yrng.f)
Protected.f x, y, z
;make the glist
glist = glGenLists_(1)
glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
glColor3f_(0, 0, 1)
glBegin_(#GL_LINES) ; plot the horizontal line
glVertex2f_(-xrng, 0)
glVertex2f_(xrng, 0)
glEnd_()
glBegin_(#GL_LINES) ; plot the vertical line
glVertex2f_(0, -yrng)
glVertex2f_(0, yrng)
glEnd_()
x.f = -10
While x <= 10 ; plot the function y = x^2
y = Pow(x,2);try also Pow(2,x) or Pow(x,2)
glColor3f_(1, 1, 1)
glBegin_(#GL_POINTS)
glVertex2f_(x, y)
glEnd_()
x + 0.01
Wend
glEndList_()
glFinish_()
EndProcedure
using glDrawArray
and every viewport show the same curve but with different constants (usefull to compare several plots in one shot)
economic but slower than using glDrawArray with a fixed array
Code: Select all
Declare FillCurveData(k, s.f, s2.f)
Declare DrawCurve()
Structure Point3D
;position
x.f
y.f
z.f
;color
r.f
g.f
b.f
EndStructure
Global Dim vertex.Point3D(0)
Global.f maxX, minX, maxY, minY ; ranges to provide it to gluOrtho2D()
OpenWindow(0, 0, 0, 300, 300, "Curves ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenGLGadget(0, 0, 0, 300, 300)
FillCurveData(0, 0, 0) ; just to find minX, maxX, minY, maxY
;Debug minX: Debug maxX ;calculated from the procedure
s.f = 0.5; make small margin
gluOrtho2D_(minX-s, maxX+s, minY-s, maxY+s)
glEnable_(#GL_POINT_SMOOTH)
glEnable_(#GL_LINE_SMOOTH) ; will make the line a little thick
;glLineWidth_(2)
;Debug ArraySize(vertex())
Repeat
DrawCurve()
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
quit = #True
EndIf
Until event = 0 Or quit = #True
Until quit = #True
End
Procedure FillCurveData(k, s.f, s2.f)
b = 0
Protected.f x,y, inc, t
;k = 3: s = 1/19: s2 = 1/6 ; toon triangle
;k=3: s = 0: s2 = 1/3 ; concave triangle
;k=6: s = 0: s2 = 1/19 ; hexagon
;k=3: s = 0: s2 = 1/19 ; not perfect circle
;k=0: s = 0: s2 = 0; circle
inc = 2*#PI/2000
While t <= 2*#PI
x = -s2 *Sin(t - k*t) - s* Cos((k + 1)* t) + Cos(t)
y = -s* Sin((k + 1)* t) + s2* Cos(t - k* t) + Sin(t)
vertex(b)\r = 1 :vertex(b)\g = 0 :vertex(b)\b = 0
vertex(b)\x = x
vertex(b)\y = y
vertex(b)\z = 0
t + inc
b + 1
ReDim vertex(b)
Wend
SetWindowTitle(0, Str(b)+" vertices")
;**********************************************
;find the min and max values to provide them to gluOrtho2D_(,,,) function
For i = 1 To b
If minX > vertex(i)\x
minX = vertex(i)\x
EndIf
If maxX < vertex(i)\x
maxX = vertex(i)\x
EndIf
Next
For i = 1 To b
If minY > vertex(i)\y
minY = vertex(i)\y
EndIf
If maxY < vertex(i)\y
maxY = vertex(i)\y
EndIf
Next
;Debug b
EndProcedure
Procedure DrawCurve()
SetGadgetAttribute(0, #PB_OpenGL_SetContext, #True)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glClearColor_(0.1, 0.2, 0.1, 1.0) ; background color
glEnableClientState_(#GL_VERTEX_ARRAY)
glEnableClientState_(#GL_COLOR_ARRAY)
glVertexPointer_(3, #GL_FLOAT, SizeOf(Point3D), @vertex(0))
glColorPointer_(3, #GL_FLOAT, SizeOf(Point3D), @vertex(0)\r)
glEnable_(#GL_SCISSOR_TEST);
glViewport_(0,0,150,150)
glScissor_(0,0,150,150) ; cut part from the viewPort so clear color can be effective localy and not to the whole screen ;
glClearColor_(1.0, 0.7, 0.8, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
FillCurveData(3, 1/19, 1/6)
glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
glViewport_(150,0,150,150)
glScissor_(150,0,150,150) ;
glClearColor_(0.62, 0.81, 0.81, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
FillCurveData(3, 0, 1/3)
glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
glViewport_(150,150,150,150)
glScissor_(150,150,150,150) ;
glClearColor_(0.6, 0.8, 0.95, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
FillCurveData(6, 0, 1/19)
glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
;glDrawArrays_(#GL_LINES, 0, ArraySize(vertex()))
;glDrawArrays_(#GL_POINTS, 0, ArraySize(vertex()))
glViewport_(0,150,150,150)
glScissor_(0,150,150,150) ;
glClearColor_(0.86, 0.82, 0.82, 0)
glClear_(#GL_COLOR_BUFFER_BIT)
FillCurveData(0, 0, 0)
glDrawArrays_(#GL_LINE_STRIP, 0, ArraySize(vertex()))
glDisableClientState_(#GL_COLOR_ARRAY)
glDisableClientState_(#GL_VERTEX_ARRAY)
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
EndProcedure
use somthing like this:
Code: Select all
glLoadIdentity_()
gluOrtho2D_(0, xrng, 0, yrng)
glViewport_(0,150,150,150)