gui_chung an easy way to make gui windows

User projects written in or related to FreeBASIC.
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Postby chung » Oct 08, 2010 21:58

yes, you are right .
its because this source comes from a larger program where drawing was done at each loop and guiscan was used instead of guiwait ...
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Postby chung » Oct 09, 2010 16:04

(09/10/2010) guiloadtexture + example with loadobj.dll

guiloadtexture (bmp,jpg,gif,ico of any size) , loadobj load .obj objects in openGL viewer example.

(10/10/2010) trapmovemouse, trapkeyboard added

Image
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Postby chung » Oct 10, 2010 20:06

example with irrlicht openGL

you can find it there => http://www.mediafire.com/?cl0q60c3otzkf41

Image
applepi
Posts: 19
Joined: Oct 27, 2008 15:50

Postby applepi » Nov 29, 2010 15:51

hello chung
your gui is great, especialy the gui_chung_opengl for displaying opengl on window, i have found a great 3D fractal written in freebasic opengl , it is displaying 3d mandelbrot or julia set upon choosing value for mand=1 or mand=0, and can be rotated by mouse, but since it is using glut i can't included it in your program, if possible to look at the code, appreciated too much and a great lesson.
the program are using display lists, and it is constructing the dots through a loop for foo = 0 to 1000000*incr , the default of incr is 5 but can be increased to increase dots. the program are using the mouse to rotate the graphic by moving the mouse only without clicking
note: the purpose of the affinity matrix aff() is to provide a way for the graphics rotation so as if it is tracking the mouse movement
thanks

Code: Select all

'Quaternion Fractal program
'by stan blank
#include "GL/gl.bi"
#include "GL/glu.bi"
#include "GL/glut.bi"
#include "crt.bi"

declare sub doMain( )
declare sub calcit( )
declare function calcleng( x as GLfloat, y as GLfloat, z as GLfloat) as GLfloat

randomize timer

dim shared as GLfloat dx, dy, x, y, z
dim shared as Long wd, ht, i, count, n, iter
dim shared as GLfloat mouseX, mouseY
dim shared as GLfloat aff(15)
dim shared as GLfloat w, leng, tx, ty, tz, tem
dim shared as GLfloat cr, ci, cj, ck, wk, incr, dist
dim shared as Long mand, zval
dim shared as GLuint quat

for i = 0 to 15
    if i mod 5 = 0 then
        aff(i) = 1.0
    else
        aff(i) = 0.0
    end if
next i
                       
wd = 500
ht = 500

'defines the shape of the Julia Set
cr = -0.200
ci = 0.800
cj = 0.000
ck = 0.000
wk = 0.000

'mand = 0 is Julia Set
'mand = 1 is Mandelbrot 3D
mand = 1

'zval = 1 shows entire set
'zval = 0 cuts set in half
'zval = 0 is an interesting effect
zval = 1

'for a more precise set increase
'iter.  However, too precise may
'not plot anything!  Recommend 25
iter = 5

'increments of 1 million points
'20 = 20 million random points
'tested for inclusion in the set
'try 100, but be patient!
incr = 5

doMain

sub doInitGL
   glClearColor 0.0, 0.0, 0.0, 0.0
   glEnable GL_DEPTH_TEST
    calcit()
end sub

sub doReshapeGL CDECL ( byval w as integer, _
                        byval h as integer )
   
    glMatrixMode GL_PROJECTION
    glLoadIdentity
    glViewport 0, 0, w, h
   
    gluPerspective  45.0, 1.0, 1.0, 1000.0
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
end sub

'creates the display list
sub calcit
    dim as GLfloat zz
    dim as GLint foo
    count = 0
   
    if zval = 0 then
        zz = 2.0
    else
        zz = 4.0
    end if
   
    quat = glGenLists(1)
    glNewList(quat, GL_COMPILE)
    glBegin(GL_POINTS)
    for foo = 0 to 1000000*incr
        x = 4.0*rnd-2.0
        y = 4.0*rnd-2.0
        z = zz*rnd-2.0
       
        'calls the quaternion calculation
        leng = calcleng(x,y,z)
       
        if leng < 4 then
            glColor3f(x+cos(15*leng),y+sin(1-leng)*cos(5*leng),z+sin(0.75*leng))
            glVertex3f(x,y,z)
        end if
    next foo
    glEnd()
    glEndList()
end sub

'quaternion calculations
function calcleng( x as GLfloat,  y as GLfloat,  z as GLfloat) as GLfloat
    dim as GLfloat w, kr, ki, kj, kk
    w = wk
    n = 0
    if mand = 1 then 'full Mandelbrot set
        kr = x
        ki = y
        kj = z
        kk = 0
    else                'Julia Set
        kr = cr
        ki = ci
        kj = cj
        kk = ck
    end if
   
    do
        tem = x+x
        x = x*x-y*y-z*z-w*w+kr
        y = tem*y + ki
        z = tem*z + kj
        w = tem*w + kk
       
        n+=1
        dist = x*x+y*y+z*z+w*w
       
        if dist > 4 then n = iter
    loop while n < iter
       
    return dist
end function

sub doRender cdecl
   
    glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
    glMatrixMode GL_MODELVIEW
   
    glPushMatrix
    glLoadIdentity
    glMultMatrixf @aff(0)
   
    glCallList(quat)
   
    glPopMatrix           
    glutSwapBuffers
   
end sub

sub mousemotion CDECL(byval x as integer, byval y as integer)
    mouseX = x
    mouseY = y
end sub

sub doInput CDECL ( byval kbcode as unsigned byte, _
              byval mousex as integer, _
              byval mousey as integer )
             
    if ( kbcode = 27 ) then
        'doShutdown
        end 0
    end if

end sub

sub chaptrack
    dx = (mouseX - wd/2)/256.0
    dy = (mouseY - ht/2)/256.0
    glMatrixMode GL_TEXTURE
    glPushMatrix
    glLoadIdentity
    glRotatef(dx, 0.0, 1.0, 0.0)
    glRotatef(dy, 1.0, 0.0, 0.0)
    glMultMatrixf @aff(0)
    glGetFloatv GL_TEXTURE_MATRIX, @aff(0)
    glPopMatrix
   
end sub

sub doIdle cdecl
    chaptrack
    glutPostRedisplay
end sub

sub doMain
   
    glutInit 1, strptr( " " )   
   
    glutInitWindowPosition 0, 0
    glutInitWindowSize wd, ht
    glutInitDisplayMode GLUT_RGBA or GLUT_DOUBLE or GLUT_DEPTH
    glutCreateWindow "FreeBASIC OpenGL example"
   
    doInitGL
   
    glutDisplayFunc  @doRender
    glutIdleFunc     @doIdle
    glutReshapeFunc  @doReshapeGL
    glutKeyboardFunc @doInput
    glutPassiveMotionFunc @mousemotion
   
    glutMainLoop

end sub


chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Postby chung » Nov 29, 2010 20:43

i have tested a version of your program with gui_chung.
maybe it does the same thing as the original one with glut (i didnt succed to make it work...)

Code: Select all

'Quaternion Fractal program
'by stan blank
'adapted with gui_chung
#include "GL/gl.bi"
#include "GL/glu.bi"
'#include "GL/glut.bi"
#include "crt.bi"
#Include "gui_chung.bi"

declare sub doMain( )
declare sub calcit( )
declare function calcleng( x as GLfloat, y as GLfloat, z as GLfloat) as GLfloat

randomize timer

dim shared as GLfloat dx, dy, x, y, z
dim shared as Long wd, ht, i, count, n, iter
dim shared as GLfloat mouseX, mouseY
dim shared as GLfloat aff(15)
dim shared as GLfloat w, leng, tx, ty, tz, tem
dim shared as GLfloat cr, ci, cj, ck, wk, incr, dist
dim shared as Long mand, zval
dim shared as GLuint quat

for i = 0 to 15
    if i mod 5 = 0 then
        aff(i) = 1.0
    else
        aff(i) = 0.0
    end if
next i
                       
wd = 500
ht = 500

'defines the shape of the Julia Set
cr = -0.200
ci = 0.800
cj = 0.000
ck = 0.000
wk = 0.000

'mand = 0 is Julia Set
'mand = 1 is Mandelbrot 3D
mand = 1

'zval = 1 shows entire set
'zval = 0 cuts set in half
'zval = 0 is an interesting effect
zval = 1

'for a more precise set increase
'iter.  However, too precise may
'not plot anything!  Recommend 25
iter = 5

'increments of 1 million points
'20 = 20 million random points
'tested for inclusion in the set
'try 100, but be patient!
incr = 5

doMain

sub doInitGL
   glClearColor 0.0, 0.0, 0.0, 0.0
        glEnable GL_DEPTH_TEST
    calcit()
end sub

sub doReshapeGL CDECL ( byval w as integer, _
                        byval h as integer )
   
    glMatrixMode GL_PROJECTION
    glLoadIdentity
    glViewport 0, 0, w, h
   
    gluPerspective  45.0, 1.0, 1.0, 1000.0
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
end sub

'creates the display list
sub calcit
    dim as GLfloat zz
    dim as GLint foo
    count = 0
   
    if zval = 0 then
        zz = 2.0
    else
        zz = 4.0
    end if
   
    quat = glGenLists(1)
    glNewList(quat, GL_COMPILE)
    glBegin(GL_POINTS)
    for foo = 0 to 1000000*incr
        x = 4.0*rnd-2.0
        y = 4.0*rnd-2.0
        z = zz*rnd-2.0
       
        'calls the quaternion calculation
        leng = calcleng(x,y,z)
       
        if leng < 4 then
            glColor3f(x+cos(15*leng),y+sin(1-leng)*cos(5*leng),z+sin(0.75*leng))
            glVertex3f(x,y,z)
        end if
    next foo
    glEnd()
    glEndList()
end sub

'quaternion calculations
function calcleng( x as GLfloat,  y as GLfloat,  z as GLfloat) as GLfloat
    dim as GLfloat w, kr, ki, kj, kk
    w = wk
    n = 0
    if mand = 1 then 'full Mandelbrot set
        kr = x
        ki = y
        kj = z
        kk = 0
    else                'Julia Set
        kr = cr
        ki = ci
        kj = cj
        kk = ck
    end if
   
    do
        tem = x+x
        x = x*x-y*y-z*z-w*w+kr
        y = tem*y + ki
        z = tem*z + kj
        w = tem*w + kk
       
        n+=1
        dist = x*x+y*y+z*z+w*w
       
        if dist > 4 then n = iter
    loop while n < iter
       
    return dist
end function

sub doRender cdecl
   
    glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
    glMatrixMode GL_MODELVIEW
   
    glPushMatrix
    glLoadIdentity
    glMultMatrixf @aff(0)
   
    glCallList(quat)
   
    glPopMatrix           
    'glutSwapBuffers
    guirefreshopengl
end sub

sub mousemotion CDECL(byval x as integer, byval y as integer)
    mouseX = x
    mouseY = y
end sub
Sub submouse
   mouseX=guimousex
   mouseY=guimousey
End Sub
sub doInput CDECL ( byval kbcode as unsigned byte, _
              byval mousex as integer, _
              byval mousey as integer )
             
    if ( kbcode = 27 ) then
        'doShutdown
        end 0
    end if

end sub

sub chaptrack
    dx = (mouseX - wd/2)/256.0
    dy = (mouseY - ht/2)/256.0
    glMatrixMode GL_TEXTURE
    glPushMatrix
    glLoadIdentity
    glRotatef(dx, 0.0, 1.0, 0.0)
    glRotatef(dy, 1.0, 0.0, 0.0)
    glMultMatrixf @aff(0)
    glGetFloatv GL_TEXTURE_MATRIX, @aff(0)
    glPopMatrix
   
end sub

sub doIdle cdecl
    chaptrack
    'glutPostRedisplay
End sub

sub doMain
   
/'    glutInit 1, strptr( " " )   
   
    glutInitWindowPosition 0, 0
    glutInitWindowSize wd, ht
    glutInitDisplayMode GLUT_RGBA or GLUT_DOUBLE or GLUT_DEPTH
    glutCreateWindow "FreeBASIC OpenGL example"
   
    doInitGL
   
    glutDisplayFunc  @doRender
    glutIdleFunc     @doIdle
    glutReshapeFunc  @doReshapeGL
    glutKeyboardFunc @doInput
    glutPassiveMotionFunc @mousemotion
   
    glutMainLoop
'/
    graphicbox("win.graph",0,0,wd,ht,"opengl")
    openwindow("win","my window",100,100,wd+10,ht+10)
   
    trapmovemouse("win.graph",@submouse)
   
    guistartOpenGL("win.graph")
    doInitGL
    doReshapeGL(wd,ht)
   
    While guitestkey(vk_escape)=0 And guitestkey(vk_space)=0
     guiwait
     doRender
     doIdle       
    Wend
    guicloseOpenGL()
    guiclose
    guiquit
end sub


 
applepi
Posts: 19
Joined: Oct 27, 2008 15:50

Postby applepi » Nov 30, 2010 5:56

thank you chung very much for trying the example, indeed this is the same problem with mouse movement faced in this link:
http://www.thinbasic.com/community/show ... elbrot-Set
the mouse are not moved correctly without using the mouse glut function, and until now the problem are not solved, it seems the:
glutPassiveMotionFunc @mousemotion
are getting x,y coordinates just when the mouse begin to move, so it goes to the:
sub mousemotion CDECL(byval x as integer, byval y as integer)
mouseX = x
mouseY = y
end sub

also may be other factors interfere. seems the glutPassiveMotionFunc are doing a great job.

there is wm_mousemove in windows which capturing x,y upon mouse movement, it is used in the above link without success.

some of the related codes in C from the original authors are here:
http://new.math.uiuc.edu/math198/notes/skelpak02/

thank you and best wishes
Last edited by applepi on Dec 01, 2010 14:07, edited 1 time in total.
applepi
Posts: 19
Joined: Oct 27, 2008 15:50

2D Draw

Postby applepi » Nov 30, 2010 19:29

the above example by chung show 3D graphics on a windows (we can increase the form to add buttons and other controls).
now here is a 2D Graphics showing a 2D plotting using gui_chung_openGL, its picture like this:
Image


it is adapted from mathart program page 102 in the book "Python Programming in OpenGL" by stan blank, it is a free book " PyOpenGL.pdf " google it you will find it.
since the graphics are going from -10 to -10 in xy coordinates the most important thing is to use :
gluOrtho2D(-10, 10, -10, 10)
it is projecting your virtual world (-10,10) on the real world (500,500) (the window size)

Code: Select all

#include "GL/gl.bi"
#include "GL/glu.bi"
#Include "gui_chung.bi"

declare sub doMain( )
Declare sub calcit( )
Dim shared as GLdouble axrng,x, y, z,r
dim shared as Long w, h, i,tot
dim shared as GLuint glist
axrng = 10.0
w = 600
h = 600

graphicbox("win.graph",0,0,500,500,"opengl")
openwindow("win","Press ESC to exit",0,0,500,500)
guistartOpenGL("win.graph")

doMain

sub doInitGL
   glClearColor 0.0, 0.0, 0.0, 0.0
   glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    gluOrtho2D(-axrng, axrng, -axrng, axrng)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    calcit()
end sub

Sub doReshapeGL CDECL ( byval w as integer, _
                        byval h as integer )
   
   
    glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
end sub

'creates the display list
sub calcit
    glPushMatrix()
    glClearColor(1.0, 1.0, 1.0, 1.0)
    glist = glGenLists(1)
    glNewList(glist, GL_COMPILE)
    glLoadIdentity()
   
    for x = -axrng to axrng Step 0.03
         x += 0.01
         for y = -axrng to axrng Step 0.03
          r = cos(x) + sin(y)
          glColor3f(cos(y*r), cos(x*y*r), sin(r*x))
          glBegin(GL_POINTS)
            glVertex2f(x, y)
          glEnd
         
         Next y
    Next x
   
    glEndList()
    glPopMatrix()
end sub

sub doRender cdecl
    glPushMatrix
    glLoadIdentity
   
    glCallList(glist)
   
    glPopMatrix           
    guirefreshopengl
end sub

sub doMain
   
    doInitGL
    doReshapeGL(w,h)
   
    While guitestkey(vk_escape)=0 And guitestkey(vk_space)=0
     guiwait
     doRender
    Wend
   
    guicloseOpenGL()
    guiclose
    guiquit
end sub


 


we can optimize the code, expanding the window to add buttons, text box, to add interactive features, it is just a proof of concept
Edit: Look at the following attempts
Last edited by applepi on Dec 19, 2010 7:43, edited 1 time in total.
applepi
Posts: 19
Joined: Oct 27, 2008 15:50

Postby applepi » Dec 02, 2010 20:02

Hi Chung
when i change the number in the text box and click Redraw button it takes longer time than what supposed to even the number is 5 instead of the default 10, in the redraw click event i write:
guicloseOpenGL()
guistartOpenGL("win.graph")

is this okay to do, since without that the draw will be smaller and smaller at every click.
regards

Code: Select all

#include "GL/gl.bi"
#include "GL/glu.bi"
#Include Once "windows.bi"
#Include "gui_chung.bi"

declare sub doMain( )
Declare sub calcit( )
Dim shared as GLdouble axrng,x, y, z,r
dim shared as Long w, h
dim shared as GLuint glist
axrng = 10.0

w = 600
h = 600

Sub redraw
   Dim As String text
   text=guigettext("win.text1")
   axrng = Val(text)
   
   glDeleteLists(glist,1)
   glClearColor 0.0, 0.0, 0.0, 0.0
   glClear(GL_COLOR_BUFFER_BIT)
   
   guicloseOpenGL()
   guistartOpenGL("win.graph")
   
   doMain
End Sub
button("win.button1","Redraw",@redraw,510,30,50,30)
edittext("win.text1","10",NULL,510,70,50,30)
graphicbox("win.graph",0,0,500,500,"opengl")
openwindow("win","Press ESC to exit",0,0,580,500)
guistartOpenGL("win.graph")

doMain

sub doInitGL
   glClearColor 0.0, 0.0, 0.0, 0.0
   glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    gluOrtho2D(-axrng, axrng, -axrng, axrng)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    calcit()
end sub

Sub doReshapeGL CDECL ( byval w as integer, _
                        byval h as integer )
   
   
    glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
end sub

'creates the display list
sub calcit
    glPushMatrix()
    glClearColor(1.0, 1.0, 1.0, 1.0)
    glist = glGenLists(1)
    glNewList(glist, GL_COMPILE)
    glLoadIdentity()
   
    for x = -axrng to axrng Step 0.03
         x += 0.01
         for y = -axrng to axrng Step 0.03
          r = cos(x) + sin(y)
          glColor3f(cos(y*r), cos(x*y*r), sin(r*x))
          glBegin(GL_POINTS)
            glVertex2f(x, y)
          glEnd
         
         Next y
    Next x
   
    glEndList()
    glPopMatrix()
   
end sub
sub doRender cdecl
    glPushMatrix
    glLoadIdentity
   
    glCallList(glist)
   
    glPopMatrix           
    guirefreshopengl
end sub

sub doMain
    doInitGL
    doReshapeGL(w,h)
    While guitestkey(vk_escape)=0 And guitestkey(vk_space)=0
     guiwait
     doRender
    Wend
    guicloseOpenGL()
    guiclose
    guiquit
end Sub

 

Image
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Postby chung » Dec 03, 2010 8:14

it is not recommanded to call the main program in a sub , except if you want special effects , but you may get stackoverflow too.

the button sub is expected to give back control (end sub) to the main program , otherwise i dont know.
applepi
Posts: 19
Joined: Oct 27, 2008 15:50

Postby applepi » Dec 03, 2010 13:55

thanks for the valuable hint, i have changed the code so i think now it is working correctly; thanks for the versatile examples inside the gui_chung

Code: Select all

#include "GL/gl.bi"
#include "GL/glu.bi"
#Include Once "windows.bi"
#Include "gui_chung.bi"

declare sub doMain( )
Declare sub calcit( )
Declare sub redraw( )
Dim shared as GLdouble axrng,x, y, z,r
dim shared as Long w, h,flag
Dim Shared As Integer quit=0,restart=0
dim shared as GLuint glist
axrng = 10.0

w = 600
h = 600
Sub mysubquit
   quit=1
End Sub
sub doRender cdecl
    glPushMatrix
    glLoadIdentity
   
    glCallList(glist)
   
    glPopMatrix           
    guirefreshopengl
end Sub
sub doInitGL
   glClearColor 0.0, 0.0, 0.0, 0.0
   glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    gluOrtho2D(-axrng, axrng, -axrng, axrng)
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    calcit()
end Sub

debut:
guibackgroundcolor(205,242,254)
quit=0
button("win.button1","Redraw",@redraw,520,30,50,30)

edittext("win.text1",Str(axrng),NULL,520,140,40,30)
statictext("win.text2","axrng",520,175,40,30)

graphicbox("win.graph",0,0,500,500,"opengl")
openwindow("win","Press ESC to exit",0,0,580,500)
guistartOpenGL("win.graph")
trapclose("win",@mysubquit)

Sub redraw
   quit=1
   restart=1
   Dim As String text
   text=guigettext("win.text1")
   axrng = Val(text)
   glDeleteLists(glist,1)
   glClearColor 0.0, 0.0, 0.0, 0.0
   
End Sub
doInitGL

While quit=0 And Not guitestKey(vk_escape)
   guiscan  'scan for input and continue
   'guiwait  'wait for msg
   doRender
   guirefreshopenGL
   Sleep 20 
Wend

guicloseOpenGL
guiclose
If restart=1 Then
   restart=0
   guibackgroundcolor(Rnd*255,Rnd*255,Rnd*255)
   guireset:GoTo debut
EndIf
guiquit
End

Sub doReshapeGL CDECL ( byval w as integer, _
                        byval h as integer )
   
   
    glLoadIdentity
    glViewport 0, 0, 500, 500
    glMatrixMode GL_PROJECTION
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
   
end sub

'creates the display list
sub calcit
    glPushMatrix()
    glClearColor(1.0, 1.0, 1.0, 1.0)
    glist = glGenLists(1)
    glNewList(glist, GL_COMPILE)
    glLoadIdentity()
   
    for x = -axrng to axrng Step 0.03
         x += 0.01
         for y = -axrng to axrng Step 0.03
          r = cos(x) + sin(y)
          glColor3f(cos(y*r), cos(x*y*r), sin(r*x))
          glBegin(GL_POINTS)
            glVertex2f(x, y)
          glEnd
         
         Next y
    Next x
   
    glEndList()
    glPopMatrix()
   
end sub

 
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Re: gui_chung an easy way to make gui windows

Postby chung » Jan 22, 2012 11:17

(22/01/2012) controlsubs launched on vk_return key if it has focus (buttons,edittext)
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Re: gui_chung an easy way to make gui windows

Postby chung » Feb 21, 2012 10:07

(21/02/2012) gethinstance changed -> gui_chung can be used in submodules or .dlls
chung
Posts: 629
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Re: gui_chung an easy way to make gui windows

Postby chung » Aug 22, 2016 9:03

(22/08/2016) gui_chung_openGLEXT.bas render to texture framebuffer using glext and gui_chung example test added

you can find it there => https://sourceforge.net/projects/guichung/

Code: Select all

' gui_chung_openGLEXT.bas render to texture framebuffer test with gui_chung and GLext (2016)
'compiled with freebasic 1.04.0-win32
'glext.dll and source available at sourceforge.net
#Define GL_GLEXT_PROTOTYPES
#Include "gui_chung.bi"
#Include "gl_chung.bi"
#Include Once "\gl\gl.bi"
#Include Once "\gl\glu.bi"
#INCLUDE Once "\gl\glext.bi"

#Inclib "glext"


Dim Shared As Integer quit=0
Sub subquit
   quit=1
End Sub
Dim Shared As Integer xmax=600,ymax=400
Sub initgl   
   glMatrixMode GL_PROJECTION
   glLoadIdentity
   '              anglevue    xmax/ymax   mxmin,mxmax
   gluPerspective   47.0,    xmax/ymax,  5.0, 50000.0
   glMatrixMode GL_MODELVIEW
   glLoadIdentity
   
   glShadeModel GL_SMOOTH
   glClearColor 0.0, 0.0, 0.0, 1.0
   glClearDepth 1.0
   glEnable GL_DEPTH_TEST
   glDepthFunc GL_LEQUAL
   glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
End Sub

graphicbox("win.graph",10,10,xmax,ymax,"opengl")
openwindow("win","gui_chung glext test",10,10,xmax+40,ymax+45)

trapclose("win",@subquit)
guistartOpenGL("win.graph")
initgl

/'Dim As Integer i
Dim As GlHandleARB Vertex_Shader, Shader_Program

Declare Sub Init_Vertex_Shader( Vertex_Shader As GlHandleARB, Shader_Program As GlHandleARB )
Init_Vertex_Shader( Vertex_Shader, Shader_Program )
'/
Dim Shared As uint mytexture
mytexture=guiloadtexture("objects/caisse.jpg")

Dim As Gluint texture, Frame_Buffer
Dim As Integer textureWidth = 1024, textureHeight = 1024

'create texture
glGenTextures (1, @texture)
glBindTexture (GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)

'create framebuffer object and attach texture
glGenFramebuffersEXT (1, @Frame_Buffer)
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, Frame_Buffer)
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0)

'glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, frame_buffer)
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, frame_buffer)

'glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)

  glViewport 0, 0, textureWidth, textureHeight
  glClearColor 0.7,0,0,1
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
      glLoadIdentity
      glTranslatef 0, 0.0, -60.0
      glColor3f 1.0,1.0, 1.0
      glenable(gl_texture_2D)
      glbindtexture(gl_texture_2D,mytexture)
      glscalef(1,ymax/xmax,1)
      gltexcarre 20
      'guirefreshopenGL()

glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
'glBindTexture (GL_TEXTURE_2D, 0)
'glbindrenderbufferext(gl_renderbuffer_ext,0)
glViewport 0, 0, xmax,ymax
Var time0=Timer

While quit=0 And guitestkey(vk_escape)=0
  guiscan  'scan for input don't forget it !
  glClearColor 0.7,0.7,0.7,1

      glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
      
      glLoadIdentity
      glTranslatef 0, 0.0, -60.0
      glColor3f 1.0,1.0, 1.0
      glRotatef (Timer-time0)*5, 0, 0, 1
      glenable(gl_texture_2D)
      glbindtexture(gl_texture_2D,texture)
      'gltexsphere 20

      Var dx=20.0
      Var s=0.5,t=0.5,ss=0.5,tt=0.5
   glbegin(gl_quads)
   glTexCoord2f(0.5-s,0.5-t)
   glvertex3f(-dx,-dx,0)
   gltexcoord2f(0.5+ss,0.5-t)
   glvertex3f(dx,-dx,0)
   glTexCoord2f(0.5+ss,0.5+tt)
   glvertex3f(dx,dx,0)
   gltexcoord2f(0.5-s,0.5+tt)
   glvertex3f(-dx,dx,0)
   glend()
   
 
  guirefreshopenGL()
  Sleep 30
Wend

guicloseOpenGL()

guiclose()
guiquit()

End

/'
Const As String crlf=Chr(13)+Chr(10)
Sub Init_Vertex_Shader( Vertex_Shader As GlHandleARB, Shader_Program As GlHandleARB )
   
    Dim As Integer i, Line_Cnt
    Dim As String Shader_Text, tString
    Dim As Gluint Shader_Compile_Success
   
    Read Line_Cnt
    For i = 1 To Line_Cnt
        Read tString
        Shader_Text + = tString + Chr( 13, 10 )
    Next
   
    Dim As GLcharARB Ptr table(0) => { Strptr( Shader_Text ) }
    Vertex_Shader = glCreateShaderObjectARB( GL_VERTEX_SHADER )
    glShaderSourceARB( Vertex_Shader, 1, @table(0), 0 )
    glCompileShaderARB( Vertex_Shader )

    glGetObjectParameterivARB(Vertex_Shader, GL_OBJECT_COMPILE_STATUS_ARB, @Shader_Compile_Success )
    If Shader_Compile_Success = 0 Then
        Dim As Gluint infologsize
        glGetObjectParameterivARB( Vertex_Shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, @infoLogSize)
        Dim As GlByte infolog(InfoLogSize)
        glGetInfoLogARB(Vertex_Shader, InfoLogSize, 0, @infoLog(0))
        tString=""
        For i = 0 To InfoLogSize-1
            tString+=Chr(InfoLog(i))
        Next
       
        Var msg="Vertex Shader Infolog error message:"+crlf
        guinotice(msg+tString)
    End If
   
   
    Shader_Program = GlCreateProgramObjectARB()
    glAttachObjectARB( Shader_Program, Vertex_Shader )
   
   
    glLinkProgramARB( Shader_Program )
    GlValidateProgramARB( Shader_Program )
   
   
    glGetObjectParameterivARB( Shader_Program, GL_OBJECT_VALIDATE_STATUS_ARB, @Shader_Compile_Success )
    If Shader_Compile_Success = 0 Then
        'Beep
        guinotice "GLSL program failed to compile!"
        'Sleep 1000
    End If
   
End Sub


Data 10
Data "void main(void)"
Data "{"
Data "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
Data "vec4 V = gl_ModelViewMatrix * gl_Vertex;"
Data "gl_FrontColor = gl_Color;"
Data "float ptSize = length(V);"
Data "ptSize *= ptSize;"
Data "gl_PointSize = 32.0 - (ptSize / 64.0);"
Data "gl_FrontColor += (gl_PointSize / 64.0);"
Data "}"
'/
/'Dim Shared glCreateShaderObjectARB         As PFNglCreateShaderObjectARBPROC
Dim Shared glShaderSourceARB               As PFNglShaderSourceARBPROC
Dim Shared glGetShaderSourceARB            As PFNglGetShaderSourceARBPROC
Dim Shared glCompileShaderARB              As PFNGLCompileShaderARBPROC
Dim Shared glDeleteObjectARB               As PFNGLDeleteObjectARBPROC
Dim Shared glCreateProgramObjectARB        As PFNglCreateProgramObjectARBPROC
Dim Shared glAttachObjectARB               As PFNglAttachObjectARBPROC
Dim Shared glUseProgramObjectARB           As PFNglUseProgramObjectARBPROC
Dim Shared glLinkProgramARB                As PFNglLinkProgramARBPROC
Dim Shared glValidateProgramARB            As PFNglValidateProgramARBPROC
Dim Shared glGetObjectParameterivARB       As PFNglGetObjectParameterivARBPROC
Dim Shared glGetInfoLogARB                 As PFNglGetInfoLogARBPROC

Dim Shared glGenFramebuffersEXT            As PFNglGenFramebuffersEXTPROC
Dim Shared glDeleteFramebuffersEXT         As PFNglDeleteFramebuffersEXTPROC
Dim Shared glBindFramebufferEXT            As PFNglBindFramebufferEXTPROC
Dim Shared glFramebufferTexture2DEXT       As PFNglFramebufferTexture2DEXTPROC
Dim Shared glFramebufferRenderbufferEXT    As PFNglFramebufferRenderbufferEXTPROC
Dim Shared glGenerateMipmapEXT             As PFNglGenerateMipmapEXTPROC
'/

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 2 guests