HUG PICTURE using glarea
Jun 17, 2023 15:52:22 GMT 1
Post by alexfish on Jun 17, 2023 15:52:22 GMT 1
Hi All
Picture in GLarea Canvas
the code has more bits than Necessary , was playing around trying to get the bits working
Read through the code:: my be able to do better
But this is a inroad to using hug.bac for 3d projection
Possible area of research is the ClientState :: but not sure if this can be applied here since
previous work done was on the ES , that worked with both standard drawing and textures on RPI2&3
BR
Alex
Need BaConEyes.png in home directory
Should also work with CANVAS GL , but will need to Use Swap Buffers :: check hug.bac Canvas for details
Picture in GLarea Canvas
the code has more bits than Necessary , was playing around trying to get the bits working
Read through the code:: my be able to do better
But this is a inroad to using hug.bac for 3d projection
Possible area of research is the ClientState :: but not sure if this can be applied here since
previous work done was on the ES , that worked with both standard drawing and textures on RPI2&3
BR
Alex
Need BaConEyes.png in home directory
INCLUDE hug.bac
HUGOPTIONS("NOSCALING")
HUGOPTIONS("CANVASGLAREA")
'=========================
HOME$ = GETENVIRON$("HOME")
'========================
LOCAL hug_Gdk$
hug_Gdk$ = hug_Get_Gdkpixbuf$()
LOCAL gl_LIB$
gl_LIB$ = hug_Get_Gl$()
'========================================================================================
IMPORT "glTexCoord2i(int,int)" FROM gl_LIB$ TYPE void
IMPORT "glGetIntegerv(int,int*)" FROM gl_LIB$ TYPE void
IMPORT "glGenTextures(int,int*)" FROM gl_LIB$ TYPE void
IMPORT "glBindTexture(int,int)" FROM gl_LIB$ TYPE void
IMPORT "glTexParameteri(int,int,int)" FROM gl_LIB$ TYPE void
IMPORT "glTexImage2D(int,int,int,int,int,int,int,int,const void * )" FROM gl_LIB$ TYPE void
IMPORT "glBlendFunc(int,int)" FROM gl_LIB$ TYPE void
IMPORT "glEnable(int)" FROM gl_LIB$ TYPE void
IMPORT "glDisable(int)" FROM gl_LIB$ TYPE void
IMPORT "glVertex2f(float,float)" FROM gl_LIB$ TYPE void
IMPORT "gdk_pixbuf_get_width(int)" FROM hug_Gdk$ TYPE int
IMPORT "gdk_pixbuf_get_height(int)" FROM hug_Gdk$ TYPE int
IMPORT "gdk_pixbuf_flip ( const long , int ) " FROM hug_Gdk$ TYPE long
IMPORT "glFlush" FROM gl_LIB$ TYPE void
CONST GL_TEXTURE_1D = 0x0DE0
CONST GL_TEXTURE_BINDING_2D = 0x8069
CONST GL_TEXTURE_2D = 0x0DE1
CONST GL_TEXTURE_MAG_FILTER = 0x2800
CONST GL_TEXTURE_MIN_FILTER = 0x2801
CONST GL_UNSIGNED_BYTE = 0x1401
CONST GL_LINEAR = 0x2601
CONST GL_BLEND = 0x0BE2
CONST GL_SRC_ALPHA = 0x0302
CONST GL_ONE_MINUS_SRC_ALPHA = 0x0303
CONST GL_RGB = 0x1907
CONST GL_RGBA = 0x1908
CONST GL_QUADS = 0x0007
DECLARE texture TYPE unsigned int
'========================================================================================
SUB LoadGLTexture(hug_file$)
LOCAL pix
LOCAL boundTexture = 0 TYPE unsigned int
LOCAL xx,yy,channels
LOCAL data TYPE unsigned char*
pix = gdk_pixbuf_new_from_file(hug_file$,0)
xx= gdk_pixbuf_get_width(pix)
yy = gdk_pixbuf_get_height(pix)
channels = gdk_pixbuf_get_n_channels(pix)
data=(unsigned char*) gdk_pixbuf_get_pixels(pix)
IF channels = 4 THEN
glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTexture)
glGenTextures(1, &texture)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xx, yy, 0, GL_RGBA, GL_UNSIGNED_BYTE,(void *) data)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
END IF
IF channels = 3 THEN
glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTexture)
glGenTextures(1, &texture)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xx, yy, 0, GL_RGB, GL_UNSIGNED_BYTE,(void *) data)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
END IF
g_object_unref(pix)
END SUB
SUB PICKY(hug_xpos,hug_ypos,hug_xsize,hug_ysize)
LOCAL context, pix, x, y, ebox, glcontext, gldraw, i
REM Get the associated canvas
pix = hug_gui_properties.canvas
ebox = hug_widget_ebox(STR$(pix))
gtk_gl_area_make_current(hug_gui_properties.canvas)
IF texture = 0 THEN
'' Image must be 3/4 channel
LoadGLTexture(HOME$ & "/BaConEyes.png")
END IF
glLoadIdentity
glMatrixMode(GL_PROJECTION)
glLoadIdentity
glColor3ub(255, 255, 255)
glOrtho(0, hug_widget_xsize(STR$(ebox)), hug_widget_ysize(STR$(ebox)), 0, 0, 1)
glMatrixMode(GL_MODELVIEW)
'glLoadIdentity
glTranslatef(0.375, 0.375, 0)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)
glBegin(GL_QUADS)
'baconeyes 0,0,128,128
glTexCoord2i(0, 0); glVertex2i(hug_xpos,hug_ypos)
glTexCoord2i(0, 1); glVertex2i(hug_xpos,hug_ypos + hug_ysize)
glTexCoord2i(1, 1); glVertex2i(hug_xpos + hug_xsize,hug_ypos + hug_ysize)
glTexCoord2i(1, 0); glVertex2i(hug_xpos + hug_xsize,hug_ypos)
glEnd()
gtk_gl_area_swap_buffers(hug_gui_properties.canvas)
glDisable(GL_TEXTURE_2D)
END SUB
'========================================================================================
SUB Draw()
' Drawing other geometry function Very Verry Ify
' Use all texture
LOCAL context, pix, x, y, ebox, glcontext, gldraw, i
REM Get the associated canvas
pix = hug_gui_properties.canvas
ebox = hug_widget_ebox(STR$(pix))
gtk_gl_area_make_current(hug_gui_properties.canvas)
glLoadIdentity
glClear(GL_COLOR_BUFFER_BIT)
glClearColor(1, 1, 1, 1)
PICKY(100,100,128,128)
PICKY(228,100,128*2,128*2)
END SUB
'========================================================================================
win = WINDOW("Hug GL Picky",806,606)
but = BUTTON("DRAW",-1,30)
ATTACH(win,but,3,3)
can = CANVAS(800,550)
ATTACH(win,can,3,35)
CALLBACK(but,Draw)
DISPLAY
Should also work with CANVAS GL , but will need to Use Swap Buffers :: check hug.bac Canvas for details