GLAreaGadget - Linux Only

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

GLAreaGadget - Linux Only

Post by Guimauve »

Hello everyone,

GLAreaGadget demonstration working only in Linux (Tested Ubuntu 11.04 and Linux Mint 12 x64).

Edit : Minor corrections suggested by Idle added.

Best regards
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : GLAreaGadget
; File Name : GLAreaGadget.pb
; File version: 1.0.1
; Programmation : Almost OK
; Programmed by : Guimauve + Idle
; Date : 10-01-2012
; Last Update : 11-01-2012
; PureBasic code : 4.60
; Plateform : Linux
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- Notes
;
; 1. This code include a highly reduced GL, GLU,
;    libgtkgl header files. It's only for the
;    demonstration ! 
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- Know issues
;
; 1. In Ubuntu 11.04, The splitter gadget is not
;    redrawed correctly after a Window Restore
;    and/or Resize event. In Linux Mint 12 x64 
;    + Gnome-Shell this code work fine.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- OpenGL Partial Constants

#GL_INC                             = 1  
#GL_VERSION_1_1                     = 1  
#GL_DEPTH_BUFFER_BIT                = $00000100  
#GL_COLOR_BUFFER_BIT                = $00004000  
#GL_TRIANGLES                       = $0004  
#GL_TRUE                            = 1  
#GL_FALSE                           = 0  
#GL_NONE                            = 0  
#GL_FRONT_LEFT                      = $0400  
#GL_FRONT_RIGHT                     = $0401  
#GL_BACK_LEFT                       = $0402  
#GL_BACK_RIGHT                      = $0403  
#GL_FRONT                           = $0404  
#GL_BACK                            = $0405  
#GL_LEFT                            = $0406  
#GL_RIGHT                           = $0407  
#GL_FRONT_AND_BACK                  = $0408  
#GL_CULL_FACE                       = $0B44   
#GL_LIGHTING                        = $0B50  
#GL_COLOR_MATERIAL                  = $0B57  
#GL_DEPTH_TEST                      = $0B71  
#GL_DITHER                          = $0BD0  
#GL_PERSPECTIVE_CORRECTION_HINT     = $0C50  
#GL_POINT_SMOOTH_HINT               = $0C51  
#GL_LINE_SMOOTH_HINT                = $0C52  
#GL_POLYGON_SMOOTH_HINT             = $0C53  
#GL_FOG_HINT                        = $0C54  
#GL_DONT_CARE                       = $1100  
#GL_FASTEST                         = $1101  
#GL_NICEST                          = $1102  
#GL_LIGHT0                          = $4000  
#GL_LIGHT1                          = $4001  
#GL_AMBIENT                         = $1200  
#GL_DIFFUSE                         = $1201  
#GL_SPECULAR                        = $1202  
#GL_POSITION                        = $1203  
#GL_AMBIENT_AND_DIFFUSE             = $1602  
#GL_MODELVIEW                       = $1700  
#GL_PROJECTION                      = $1701  
#GL_TEXTURE                         = $1702  
#GL_SMOOTH                          = $1D01  

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- OpenGL Partial Import

ImportC "-lGL"  
  
  glBegin(Mode.l) 
  glClear(Mask.l)  
  glClearColor(Red.f, Green.f, Blue.f, Alpha.f) ; Values are clamped to the range [0,1]. 
  glColor3f(Red.f, Green.f, Blue.f) 
  glColorMaterial(Face.l, Mode.l) 
  glDisable(Capability.l) 
  glEnable(Capability.l) 
  glEnd() 
  glHint(Target.l, Mode.l)  
  glLightfv(Light.l, Pname.l, *params.i) 
  glLoadIdentity() 
  glMatrixMode(Mode.l)     
  glOrtho(Left.d, Right.d, Bottom.d, Top.d, Near.d, Far.d) 
  glPopMatrix() 
  glPopName() 
  glPushMatrix() 
  glRotatef(Angle.f, x.f, y.f, z.f) 
  glScalef(x.f, y.f, z.f) 
  glShadeModel(Mode.l)  
  glTranslatef(x.f, y.f, z.f) 
  glVertex3f(x.f, y.f, z.f) 
  glViewport(x.l, y.l, width.l, height.l)  
  
EndImport 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- OpenGL Utility Partial Import

ImportC "-lGLU" 
  
  gluLookAt(EyeX.d,  EyeY.d, EyeZ.d, CenterX.d, CenterY.d, CenterZ.d, UpX.d, UpY.d,  UpZ.d) 
  gluOrtho2D(Left.d, Right.d, Bottom.d, Top.d) 
  gluPerspective(FovY.d, Aspect.d, zNear.d, zFar.d) 
  
EndImport 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
;- GDK Constants

#GDK_GL_DONT_CARE = 0 
#GDK_GL_NONE      = 0 
#GDK_GL_ATTRIB_LIST_NONE = 0 
#GDK_GL_SUCCESS = 0 

#GDK_GL_USE_GL           = 1 
#GDK_GL_BUFFER_SIZE      = 2 
#GDK_GL_LEVEL            = 3 
#GDK_GL_RGBA             = 4 
#GDK_GL_DOUBLEBUFFER     = 5 
#GDK_GL_STEREO           = 6 
#GDK_GL_AUX_BUFFERS      = 7 
#GDK_GL_RED_SIZE         = 8 
#GDK_GL_GREEN_SIZE       = 9 
#GDK_GL_BLUE_SIZE        = 10 
#GDK_GL_ALPHA_SIZE       = 11 
#GDK_GL_DEPTH_SIZE       = 12 
#GDK_GL_STENCIL_SIZE     = 13 
#GDK_GL_ACCUM_RED_SIZE   = 14 
#GDK_GL_ACCUM_GREEN_SIZE = 15 
#GDK_GL_ACCUM_BLUE_SIZE  = 16 
#GDK_GL_ACCUM_ALPHA_SIZE = 17 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- GDK Structures

Structure GdkGLContext
EndStructure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- GTK Structures

Structure GtkGLArea
  DrawingArea.GtkDrawingArea
  *glcontext.GdkGLContext
EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;- GTK GL Import

ImportC "/usr/lib/libgtkgl-2.0.so" 
  
  gtk_gl_area_get_type()
  gtk_gl_area_new(*attrList)
  gtk_gl_area_share_new(*attrList,*share.GtkGLArea)
  gtk_gl_area_new_vargs(*share,*args)
  gtk_gl_area_make_current(*glarea.GtkGLArea)
  gtk_gl_area_swap_buffers(*glarea.GtkGLArea)
  gtk_widget_set_size_request(*glarea.GtkGLArea,width,height) 
  
EndImport  

Macro fillv(V,a,b,c,d)
  v(0) = a
  v(1) = b
  v(2) = c
  v(3) = d
EndMacro

ProcedureC GLAreaGadget_Init(*widget.GtkWidget)
  
  If gtk_gl_area_make_current(*widget) 
    
    Dim light0_pos.f(4) 
    Dim light0_color.f(4)
    Dim light1_pos.f(4)
    Dim light1_color.f(4) 
    
    fillv(light0_pos,-50.0, 50.0, 0.0, 0.0);
    fillv(light0_color, 0.1, 0.1, 0.1, 1.0); /* white light */
    fillv(light1_pos,50.0, 50.0, 0.0, 0.0);
    fillv(light1_color,0.4, 0.4, 1.0, 1.0);  /* cold blue light */
    
    glEnable(#GL_DEPTH_TEST)
    glEnable(#GL_CULL_FACE)
    
    ;/* speedups */
    glEnable(#GL_DITHER)
    glShadeModel(#GL_SMOOTH)
    glHint(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_FASTEST)
    glHint(#GL_POLYGON_SMOOTH_HINT, #GL_FASTEST)
    
    ;/* light */
    glLightfv(#GL_LIGHT0, #GL_POSITION, @light0_pos)
    glLightfv(#GL_LIGHT0, #GL_DIFFUSE,  @light0_color)
    glLightfv(#GL_LIGHT1, #GL_POSITION, @light1_pos)
    glLightfv(#GL_LIGHT1, #GL_DIFFUSE,  @light1_color)
    glEnable(#GL_LIGHT0)
    glEnable(#GL_LIGHT1)
    glEnable(#GL_LIGHTING)
    
    glColorMaterial(#GL_FRONT_AND_BACK,#GL_AMBIENT_AND_DIFFUSE);
    glEnable(#GL_COLOR_MATERIAL)
    
  EndIf 
  
  ProcedureReturn 1
EndProcedure 

ProcedureC GLAreaGadget_Reshape(*widget.GtkWidget, *event.GdkEventConfigure)
  
  w = *widget\parent\allocation\width
  h = *widget\parent\allocation\height
  
  If gtk_gl_area_make_current(*widget)
    
    glViewport(0, 0, w, h)
    glMatrixMode(#GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, w/h, 0.1, 1000.0)
    glMatrixMode(#GL_MODELVIEW)
    
  EndIf 
  
  ProcedureReturn 1
EndProcedure

ProcedureC GLAreaGadget_Draw(*widget.GtkWidget,*event.GdkEventExpose = #Null)
  
  Static rot.f 
  
  If Not *event 
    *widget = GetGadgetData(*widget) 
  EndIf 
  
  If gtk_gl_area_make_current(*widget) 
    
    rot+0.1  
    glClear(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glClearColor(0,0,0,1);
    glLoadIdentity();                   // Reset The View
    glTranslatef(0,0,-5);             
    glRotatef(rot, 0.0,1.0,1.0);             // Rotate The Pyramid On It's Y Axis  
    
    glbegin(#GL_TRIANGLES)
    glColor3f(255,0.0,0.0);          // Red
    glVertex3f( 0.0,1.0,0.0);            // Top Of Triangle (Front)
    glColor3f(0.0,255,0.0);          // Green
    glVertex3f(-1.0,-1.0, 1.0);          // Left Of Triangle (Front)
    glColor3f(0.0,0.0,255);          // Blue
    glVertex3f( 1.0,-1.0, 1.0);          // Right Of Triangle (Front)
    
    glColor3f(255,0.0,0.0);          // Red
    glVertex3f( 0.0, 1.0, 0.0);          // Top Of Triangle (Right)
    glColor3f(0.0,0.0,255);          // Blue
    glVertex3f( 1.0,-1.0, 1.0);          // Left Of Triangle (Right)
    glColor3f(0.0,255,0.0);          // Green
    glVertex3f( 1.0,-1.0, -1.0);         // Right Of Triangle (Right)
    
    glColor3f(255,0.0,0.0);          // Red
    glVertex3f( 0.0, 1.0,0.0);          // Top Of Triangle (Back)
    glColor3f(0.0,255,0.0);          // Green
    glVertex3f( 1.0,-1.0, -1.0);         // Left Of Triangle (Back)
    glColor3f(0.0,0.0,255);          // Blue
    glVertex3f(-1.0,-1.0, -1.0);         // Right Of Triangle (Back)
    
    glColor3f(255,0.0,0.0);          // Red
    glVertex3f( 0.0, 1.0, 0.0);          // Top Of Triangle (Left)
    glColor3f(0.0,0.0,255);          // Blue
    glVertex3f(-1.0,-1.0,-1.0);          // Left Of Triangle (Left)
    glColor3f(0.0,255,0.0);          // Green
    glVertex3f(-1.0,-1.0, 1.0);          // Right Of Triangle (Left)
    glEnd();                        // Done Drawing The Pyramid
    
    gtk_gl_area_swap_buffers(*widget);
    
  EndIf 
  
  ProcedureReturn 1
EndProcedure 

ProcedureC GLAreaGadget_Reshape_Ex(*widget.GtkWidget,*allocation.GtkAllocation, *vData)
  
  gtk_widget_set_size_request_(*vdata, *allocation\width, *allocation\height)
  
EndProcedure  

Procedure GLAreaGadget(GadgetID, x, y, w, h)
  
  Dim ats(4) : ats(0)=#GDK_GL_RGBA : ats(1)=#GDK_GL_DOUBLEBUFFER : ats(2) = #GDK_GL_DEPTH_SIZE 
  ats(3) = 1 : ats(4) = #GDK_NONE
  
  Container = ContainerGadget(GadgetID,x,y,w,h)
    
    gtk_gl_area = gtk_gl_area_new(@ats(0))
    
    gtk_widget_set_events_(gtk_gl_area,#GDK_EXPOSURE_MASK|#GDK_BUTTON_PRESS_MASK|#GDK_BUTTON_RELEASE_MASK|#GDK_POINTER_MOTION_MASK|#GDK_POINTER_MOTION_HINT_MASK)
    g_signal_connect_(gtk_gl_area, "realize", @GLAreaGadget_Init(), 0)
    g_signal_connect_(gtk_gl_area, "expose_event", @GLAreaGadget_Draw(), 0)
    g_signal_connect_(gtk_gl_area, "configure_event", @GLAreaGadget_Reshape(), 0)
    
    If GadgetID = #PB_Any
      SetGadgetData(Container, gtk_gl_area)
      g_signal_connect_(GadgetID(Container), "size-allocate", @GLAreaGadget_Reshape_Ex(), gtk_gl_area)
      gtk_container_add_(GadgetID(Container), gtk_gl_area)
    Else
      SetGadgetData(GadgetID, gtk_gl_area)
      g_signal_connect_(GadgetID(GadgetID), "size-allocate", @GLAreaGadget_Reshape_Ex(), gtk_gl_area)
      gtk_container_add_(GadgetID(GadgetID), gtk_gl_area)
    EndIf
    
  CloseGadgetList()
  
  gtk_widget_set_usize_(gtk_gl_area, w, h)
  gtk_widget_show_(gtk_gl_area)
  
  ProcedureReturn Container
EndProcedure  

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Enumeration 
  
  #GLArea1
  #GLArea2
  #Separateur1
  #Separateur2
  #Separateur3
  
EndEnumeration

If OpenWindow(0, 0, 0, 800, 600, "GLAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
  GLAreaGadget(#GLArea1, 0, 0, 0, 0)
  GLAreaGadget(#GLArea2, 0, 0, 0, 0)
  GLArea3 = GLAreaGadget(#PB_Any, 0, 0, 0, 0)
  GLArea4 = GLAreaGadget(#PB_Any, 0, 0, 0, 0)
  
  SplitterGadget(#Separateur1, 0, 0, 0, 0, #GLArea1, #GLArea2, #PB_Splitter_Separator)
  SplitterGadget(#Separateur2, 0, 0, 0, 0, GLArea3, GLArea4, #PB_Splitter_Separator)
  SplitterGadget(#Separateur3, 5, 5, 790, 590, #Separateur1, #Separateur2, #PB_Splitter_Separator|#PB_Splitter_Vertical)
  
  Repeat
    
    GLAreaGadget_Draw(#GLArea1)  
    GLAreaGadget_Draw(#GLArea2)
    GLAreaGadget_Draw(GLArea3)  
    GLAreaGadget_Draw(GLArea4)
    
    EventID = WaitWindowEvent(5)
    
    Select EventID
        
      Case #PB_Event_Menu
        
        Select EventMenu()
            
        EndSelect
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
            
        EndSelect
        
      Case #PB_Event_SizeWindow, #PB_Event_RestoreWindow 
        ResizeGadget(#Separateur3, #PB_Ignore, #PB_Ignore, WindowWidth(0) - 10, WindowHeight(0) - 10)
        
    EndSelect
    
  Until EventID = #PB_Event_CloseWindow
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Last edited by Guimauve on Wed Jan 11, 2012 5:44 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GLAreaGadget - Linux Only

Post by idle »

Image
note you may have to change the callback functions to procedureC and the GLU import to ImportC
Ubuntu 11.10
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: GLAreaGadget - Linux Only

Post by Guimauve »

idle wrote:note you may have to change the callback functions to procedureC and the GLU import to ImportC
Ubuntu 11.10
Done !

Best regards
Guimauve
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GLAreaGadget - Linux Only

Post by idle »

Wrapped it adding structure events, keyboard and mouse
Code's a bit long to post so in zip

Added
iGLAreaGadget_Init(*widget.GtkWidget)
iGLAreaGadget_Reshape(*widget.GtkWidget, *event.GdkEventConfigure)
iGLAreaGadget_Reshape_Ex(*widget.GtkWidget,*allocation.GtkAllocation, *vData)
GLAreaGadget_Number(gadget)
GLAreaGadget_ID(gadget)
GLAreaGadget_Width(gadget)
GLAreaGadget_Height(gadget)
GLAreaGadget_X(gadget)
GLAreaGadget_Y(gadget)
GLAreaGadget_GadgetID(gadget)
GLAreaGadget_KeyPressed(gadget,key)
GLAreaGadget_KeysPressed(gadget,k1,k2,k3=0,k4=0,k5=0,k6=0)
GLAreaGadget_GetKeyString(key)
GLAreaGadget_GetKey(gadget)
GLAreaGadget_MouseX(gadget)
GLAreaGadget_MouseY(gadget)
GLAreaGadget_MouseButton(gadget,button)
GLAreaGadget_MouseWheel(gadget)
GLAreaGadget_StartDrawing(gadget)
GLAreaGadget_StopDrawing(gadget)
GLAreaGadget_Draw(*widget.GtkWidget,*event.GdkEventExpose=0)
GLAreaGadget_Perspective(Gadget,angle.d,aspect.d,near.d,far.d)

user callbacks
GLAreaInit() is required
GLAreaDrawScene() optional
GLAreaEvents(gadget,event,value) optional

You can use opengl functions directly in your event loop if you get a lock with GLAreaGadget_StartDrawing()
And you can call the functions to get mouse and key states Keyboard maps both upper and lower case keys
but is only mapped for US keyboard

To use the gadget see the unit test

download
http://www.idlearts.com/GlAreaGadget.zip
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: GLAreaGadget - Linux Only

Post by Guimauve »

Hello everyone,

Ok this is the GLAreaGadget V1.0.3

Change for V 1.0.3
  • 1. Miscellaneous constants corrections
  • 2. Data Structure modified + BOP Setter and Getter added
  • 3. Global LinkedList and Global variable removed
  • 4. All constants, macros and procedures related to the GLAreaGadget has been regrouped on the same file.
http://pages.videotron.com/gsaumure/fil ... Gadget.zip

Important :
The GLAreaGadget include file no longer include a reduced OpenGL Import. Don't worry, the complete OpenGL Import file are included with a demonstration code in the zip file.

Edit : Download link correction

Best regards.
Guimauve
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: GLAreaGadget - Linux Only

Post by Guimauve »

Hello everyone,

A little update of the GLAreaGadget(), but please note this example is completely experimental so use it at your own risk.

Download link : http://pages.videotron.com/gsaumure/fil ... xample.zip

Whats new ?
  • 1. TrackBallCamera added
    2. Sharing Textures, VBO and IBO between gadgets is possible, one load/creation per gadget are no longer needed
IMPORTANT !!
  • 1. The "libgtkgl2.0-1" as to be installed.
    2. It's recommended to consider the first GLAreaGadget() created as a Parent and all others as childs. Make sure to use GLAreaGadget_MakeCurrent(#ParentGadget) if you want to load textures and/or create VBOs and IBOs because they need an OpenGL context to be created.
    3. Rendering methods 4 and 5 need shaders programs to make them to work but for the moment my programmable pipeline knowledge is near to zero but for those who know how it will work.
    4. In PureBasic 5.00 B4 it's impossible to compile this example with the debugger active. With the debugger we get a IMA on gtk_gl_area_make_current(*widget) call why, I don't know !
Best regards
Guimauve
Dear Optimist, Pessimist,
and Realist,

While you guys were
busy arguing about the
glass of water, I DRANK IT !

Sincerely,
the Opportunist
Post Reply