once luis have issued the glfw wrapper i have found that it is very useful for who wants to learn Opengl, since we don't focus on the internals which are very complex but on the exercises. but i have always ambiguity regarding refreshing the screen. it is easy to refresh the screen if we draw a rotating rectangle by only calling the drawing code again and again and the computer will not freeze, but what if the drawing routine are time consuming such as a fractal, it will not work and will freeze the program. unless we store whats drawn before in a memory
i have tried the glGenLists then calling the generated list with glCallList, it display the stored fractal , and refreshed when we resize the window or cover it, but still have some slow refreshing performance on my machine.
the following code rotating the triangle by executing the same draw code. there is no difficulty in refreshing the screen here.
and after that the fractal draw, i have added
run = 1 in the Render procedure so the second run in the main procedure will not execute the lengthy plotting code but the generated list in the memory glCallList_(glist)
i have included OpenGL.pbi because it contains the definitions for #GL_COMPILE_AND_EXECUTE) , #GL_COMPILE , #GL_POINTS
i will try to learn the introductory OpenGL again from time to time.
rotating triangle:
Code: Select all
; *****************************************************************
; Complete wrapper for glfw 3.0.4
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem
; under OSX
; *****************************************************************
EnableExplicit
#GLFW_LINKING_STATIC = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$ = "."
#GLFW_LIBS_PATH$ = "../glfw-libs"
#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4
IncludeFile "glfw3.declares.pbi"
IncludeFile "glfw3.imports.pbi"
Global rot.f
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib"
glClear_(a.i) As "_glClear@4"
glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
glMatrixMode_(a.i) As "_glMatrixMode@4"
glLoadIdentity_() As "_glLoadIdentity@0"
glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
glBegin_(a.i) As "_glBegin@4"
glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
glEnd_() As "_glEnd@0"
EndImport
Import "Glu32.lib"
gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport
CompilerElse
Import "Opengl32.lib"
glClear_(a.i) As "glClear"
glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
glMatrixMode_(a.i) As "glMatrixMode"
glLoadIdentity_() As "glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
glBegin_(a.i) As "glBegin"
glColor3f_(a.f,b.f,c.f) As "glColor3f"
glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
glEnd_() As "glEnd"
EndImport
Import "Glu32.lib"
gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf
CompilerEndIf
CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)
ImportC "-lGL"
glClear_(a.i) As "glClear"
glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
glMatrixMode_(a.i) As "glMatrixMode"
glLoadIdentity_() As "glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
glBegin_(a.i) As "glBegin"
glColor3f_(a.f,b.f,c.f) As "glColor3f"
glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
glEnd_() As "glEnd"
EndImport
ImportC "-lGLU"
gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)
ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
glClear_(a.i) As "_glClear"
glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
glMatrixMode_(a.i) As "_glMatrixMode"
glLoadIdentity_() As "_glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
glBegin_(a.i) As "_glBegin"
glColor3f_(a.f,b.f,c.f) As "_glColor3f"
glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
glEnd_() As "_glEnd"
gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport
CompilerEndIf
Procedure Render()
Protected.f width = 450, height = 450, ratio = width / height
Protected.f rot
rot + 0.5
glViewport_(0, 0, width, height);
glClear_(#GL_COLOR_BUFFER_BIT);
glMatrixMode_(#GL_PROJECTION);
;glLoadIdentity_();
glOrtho_(-ratio, ratio, -1, 1, 1, -1);
glMatrixMode_(#GL_MODELVIEW);
glLoadIdentity_();
glRotatef_(glfwGetTime() * 50, 0, 0, 1);
;glRotatef_(rot, 0, 0, 1);
glBegin_(#GL_TRIANGLES);
glColor3f_(1, 0, 0);
glVertex3f_(-0.6, -0.4, 0);
glColor3f_(0, 1, 0);
glVertex3f_(0.6, -0.4, 0);
glColor3f_(0, 0, 1);
glVertex3f_(0, 0.6, 0);
glEnd_();
;glfwSwapBuffers(window);
;glfwPollEvents();
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
;glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
;glViewport_(0, 0, 640, 480)
;glMatrixMode_(#GL_PROJECTION)
;glLoadIdentity_()
;gluPerspective_(30.0, Abs(640.0/480.0), 0.1, 100.0)
;glMatrixMode_(#GL_MODELVIEW)
;glLoadIdentity_()
;glTranslatef_(0.0, 0.0, -8.0)
;glBegin_(#GL_TRIANGLES)
;glColor3f_ ( 1.0, 0.0, 0.0)
;glVertex3f_( 0.0, 1.0, 0.0)
;glColor3f_ ( 0.0, 1.0, 0.0)
;glVertex3f_(-1.0, -1.0, 0.0)
;glColor3f_ ( 0.0, 0.0, 1.0)
;glVertex3f_( 1.0, -1.0, 0.0)
;glEnd_()
EndProcedure
Procedure main()
Protected window
If glfwBindLibrary ("../glfw-libs")
;Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)
If glfwInit()
window = glfwCreateWindow(450, 450, "Hello World", #Null, #Null)
If window
glfwMakeContextCurrent(window)
While (glfwWindowShouldClose(window) = 0)
Render()
glfwSwapBuffers(window)
glfwPollEvents()
Wend
EndIf
glfwTerminate()
EndIf
Else
MessageRequester("glfw", "Cannot open the dynamic library.")
EndIf
EndProcedure
Main()
some Fractal:
Code: Select all
; *****************************************************************
; Complete wrapper for glfw 3.0.4
; for Win/Lin/OSX, 32/64 bits, Dynamic/Static
;
; Jan 2014 by Luis, release 1.03
; http://luis.no-ip.net
;
; Many thanks to Danilo for solving the "Application Menu" problem
; under OSX
; *****************************************************************
EnableExplicit
Global run.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
; CONFIGURE THE WRAPPER
#GLFW_LINKING_STATIC = 0 ; (0 = dynamic linking, 1 = static linking)
#GLFW_WRAPPER_PATH$ = "."
#GLFW_LIBS_PATH$ = "../glfw-libs"
#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4
;#GL_COMPILE ; can be found in OpenGL.pbi = $1300
;#GL_COMPILE_AND_EXECUTE = $1301
;#GL_POINTS = $0000
; INCLUDE THE WRAPPER
IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.declares.pbi"
IncludeFile #GLFW_WRAPPER_PATH$ + "/" + "glfw3.imports.pbi"
IncludeFile #PB_Compiler_Home + "Examples\Sources - Advanced\OpenGL Cube\OpenGL.pbi"
; IF LINKING IS DYNAMIC, THEN DO THE LATE BINDING
CompilerIf #GLFW_LINKING_STATIC = 0
If glfwBindLibrary(#GLFW_LIBS_PATH$) = 0
MessageRequester("glfw", "Cannot open the dynamic library, aborting.")
End
EndIf
CompilerEndIf
; Stuff needed for this demo
#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100
#GL_MODELVIEW = $1700
#GL_PROJECTION = $1701
#GL_TRIANGLES = 4
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
Import "Opengl32.lib"
glClear_(a.i) As "_glClear@4"
glViewport_(a.i,b.i,c.i,d.i) As "_glViewport@16"
glMatrixMode_(a.i) As "_glMatrixMode@4"
glLoadIdentity_() As "_glLoadIdentity@0"
glTranslatef_(a.f,b.f,c.f) As "_glTranslatef@12"
glBegin_(a.i) As "_glBegin@4"
glColor3f_(a.f,b.f,c.f) As "_glColor3f@12"
glVertex3f_(a.f,b.f,c.f) As "_glVertex3f@12"
glEnd_() As "_glEnd@0"
EndImport
Import "Glu32.lib"
gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective@32"
EndImport
CompilerElse
Import "Opengl32.lib"
glClear_(a.i) As "glClear"
glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
glMatrixMode_(a.i) As "glMatrixMode"
glLoadIdentity_() As "glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
glBegin_(a.i) As "glBegin"
glColor3f_(a.f,b.f,c.f) As "glColor3f"
glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
glEnd_() As "glEnd"
EndImport
Import "Glu32.lib"
gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf
CompilerEndIf
CompilerIf (#PB_Compiler_OS = #PB_OS_Linux)
ImportC "-lGL"
glClear_(a.i) As "glClear"
glViewport_(a.i,b.i,c.i,d.i) As "glViewport"
glMatrixMode_(a.i) As "glMatrixMode"
glLoadIdentity_() As "glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "glTranslatef"
glBegin_(a.i) As "glBegin"
glColor3f_(a.f,b.f,c.f) As "glColor3f"
glVertex3f_(a.f,b.f,c.f) As "glVertex3f"
glEnd_() As "glEnd"
EndImport
ImportC "-lGLU"
gluPerspective_(a.d,b.d,c.d,d.d) As "gluPerspective"
EndImport
CompilerEndIf
CompilerIf (#PB_Compiler_OS = #PB_OS_MacOS)
ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
glClear_(a.i) As "_glClear"
glViewport_(a.i,b.i,c.i,d.i) As "_glViewport"
glMatrixMode_(a.i) As "_glMatrixMode"
glLoadIdentity_() As "_glLoadIdentity"
glTranslatef_(a.f,b.f,c.f) As "_glTranslatef"
glBegin_(a.i) As "_glBegin"
glColor3f_(a.f,b.f,c.f) As "_glColor3f"
glVertex3f_(a.f,b.f,c.f) As "_glVertex3f"
glEnd_() As "_glEnd"
gluPerspective_(a.d,b.d,c.d,d.d) As "_gluPerspective"
EndImport
CompilerEndIf
Procedure Render()
run=1
;Static glist.l ; handle for the OpenGL list
glViewport_ (0, 0, 450, 450)
;gluPerspective_(30.0, Abs(640.0/480.0), 0.1, 100.0)
glMatrixMode_(#GL_PROJECTION)
gluOrtho2D_(-axrng, axrng, -axrng, axrng)
glMatrixMode_ (#GL_MODELVIEW)
;glEnable_ (#GL_DEPTH_TEST) ; Enabled, it slowdown a lot the rendering. It's to be sure than the
;glEnable_ (#GL_CULL_FACE) ; This will enhance the rendering speed as all the back face will be
glLoadIdentity_()
glPushMatrix_()
glClear_(#GL_COLOR_BUFFER_BIT);
glClearColor_(1.0, 1.0, 1.0, 1.0);
;make the glist
glist = glGenLists_(1);
glNewList_(glist, #GL_COMPILE_AND_EXECUTE)
;glNewList_(glist, #GL_COMPILE)
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))
glPointSize_(6);
glBegin_(#GL_POINTS)
glVertex2f_(x, y)
glEnd_() ; finished
Wend
Wend
glEndList_()
;glutSwapBuffers
glPopMatrix_()
glFinish_()
;SwapBuffers_(hdc)
EndProcedure
ProcedureC key_callback(window, key, scancode, action, mods)
Debug "key = " + key + ", scancode = " + scancode + ", action = " + action + ", mode = " + mods
If (key = #GLFW_KEY_ESCAPE And action = #GLFW_PRESS)
glfwSetWindowShouldClose(window, 1)
EndIf
EndProcedure
ProcedureC error_callback (err, *desc)
Debug "Error: " + err + ", " + PeekS(*desc, -1, #PB_UTF8)
EndProcedure
Procedure main()
Protected window
If glfwBindLibrary ("../glfw-libs")
;Debug PeekS(glfwGetVersionString(), -1, #PB_UTF8)
If glfwInit()
window = glfwCreateWindow(450, 450, "Hello World", #Null, #Null)
If window
glfwMakeContextCurrent(window)
glfwSetKeyCallback(window, @key_callback())
While (glfwWindowShouldClose(window) = 0)
;glfwSwapInterval( 1 );
glfwSwapBuffers(window)
glfwPollEvents()
If run=0 ; compose the fractal the first time
Render()
Else ; calling the fractal list from memory
glPushMatrix_();
glCallList_(glist);
glPopMatrix_()
EndIf
Wend
EndIf
glfwTerminate()
EndIf
Else
;MessageRequester("glfw", "Cannot open the dynamic library.")
EndIf
EndProcedure
Main()