FBGFX to OpenGL (Use FBGFX along with OpenGL)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

FBGFX to OpenGL (Use FBGFX along with OpenGL)

Postby relsoft » Feb 24, 2010 9:56

This was requested by vdecampo. What this does a way to eliminate the "stutter" bug when the "hit and miss" thread problem manifests itself when using pure FBGFX.

I hope some of you would test this on vista and win7 and post results.

Thanks!


Code: Select all

''   This demo shows how to draw using FBGFX
''   but render the buffer to an OpenGL context
''   Someone requestred this so...
''   Pros:
''   1. Eliminate the GFX stutter when flipping FBGFX screens (multithread bug I think)
''   2. Free filtering of your buffer using openGL
''   3. Fast whole buffer rotations
''   4. You don't need to change much of your existing code to use this.
''   5. You can freely use FBGFX commands along with OpenGL commsnds
''   6. full screen scaling

''   
''   Caveats:
''   1. Needs OpenGL (Who doesn't have that nowadays?)
''   2. A good GFX card (this worked on a 64 mb NVIDIA)
''   
''   
''   Richard Eric M Lope BSN RN (Relsoft)
''   http://rel.betterwebber.com
''
''  ** Note that this demo is not very well done and not encapsulated that much.
''     It's for the user to provide his own way of im`plementation as he see fit.
''   ** You can also use any other windowing system if you don't like GLFW such
''      as SDL, SFML, or even pure windows api.


#include once "/gl/gl.bi"
#include once "/gl/glu.bi"
#include once "/gl/glfw.bi"
#include once "/fbgfx.bi"

#ifndef false
   const as integer FALSE = 0
   const as integer TRUE = NOT false
#endif

'' some constants that we need
'' You could also encapsulate this inside your
'' Class if you want.

const as integer SCR_WIDTH = 640      '' Actual screen dimensions
const as integer SCR_HEIGHT = 480
const as integer BITSPP = 32

const as integer BUFF_WIDTH = 320      '' The dimensions of our buffer where we draw FBGFX
const as integer BUFF_HEIGHT = 240      '' I used 320x480 since I used a pretty old PC to code this.

const as integer TEX_WIDTH = 512      '' Our texture dimensions where we transfe the FB image
const as integer TEX_HEIGHT = 512       '' to an OpenGL context
 


'' This is a Type I use to make making GLFW context easier
'' You can forget about this unless you want to learn GLFW
Type Tdisplay
    w          as uinteger
    h          as uinteger
    r_bits      as uinteger
    g_bits      as uinteger
    b_bits      as uinteger
    a_bits      as uinteger
    d_bits      as uinteger
    s_bits      as uinteger
    mode      as uinteger
    Glver       As Zstring Ptr
    as single FOVy, aspect, znear, zfar
End Type



'' Our main Class
type Tfbgfx_2_GL
   
   declare constructor()
   declare destructor()
   
   public:   
   '' properties
   declare property image() as any ptr
   
   '' methods
   declare sub init_gl_screen()      '' Initializes our GL parameters
   declare sub view_ortho()         '' Used for 2d rendering
   declare sub view_perspective()      '' Used to go back to 3d
   declare sub render()            '' FBGFX drawing and FBGFX->GL conversion
   
   private:
   s_display         as Tdisplay    '' GLFW thingy above
   texture_id        as GLuint      '' Our texture where we upload the FBGFX buffer
   p_image            as any ptr      '' GET/PUT image where we have to draw stuff   
   p_image_texture      as any ptr      '' GET/PUT image to be uploaded to opengl     
    p_texture_data       as GLuint  ptr   '' pixel data address of the GET/PUT image    
    
end type



'' Some Useless but nice looking gfx demos
'' You can forget about this.
declare sub twisted(byval buffer as any ptr)
declare sub init_cube(byref CubeDL as GLuint)
declare sub draw_cube(byref CubeDL as GLuint)


''=============================================================================
''
''  Implementation
''
''=============================================================================   

constructor Tfbgfx_2_GL()

   '' init an FBGFX 320x240 screen
   '' GFX_NULL so that we can use FBGFX along with OGL
   screenres BUFF_WIDTH,BUFF_HEIGHT,32,1,fb.GFX_NO_SWITCH  or fb.GFX_NULL
   
   
   '' Set up our image buffers
   '' p_image is the buffer we draw FBGFX (320 x 240)
   '' p_image_texture is the buffer we need to upload to the GPU
   ''  p_image_texture needs to be power of 2 and bigger than p_image
   
   p_image = Imagecreate(BUFF_WIDTH,BUFF_HEIGHT,0)
   p_image_texture   = Imagecreate(TEX_WIDTH,TEX_HEIGHT,0)
   
   
   '' initialize OpenGL screen
    if glfwInit() Then
        'Successful!
    else   
        print "Failed to initialize GLFW!"
        sleep 1000
        end
    end if

   
   '' set up GLFW parameters
   s_display.w       = SCR_WIDTH
   s_display.h         = SCR_HEIGHT
   s_display.r_bits   = 8   
   s_display.g_bits   = 8
   s_display.b_bits   = 8   
   s_display.a_bits   = 8
   s_display.d_bits   = 8
   s_display.s_bits   = 8
   s_display.mode      = GLFW_WINDOW   
   
    '' Open a GLFW window   
    if  glfwOpenWindow( _
          s_display.w     , _
          s_display.h     , _
          s_display.r_bits, _
          s_display.g_bits, _
          s_display.b_bits, _
          s_display.a_bits, _
          s_display.d_bits, _
          s_display.s_bits, _
          s_display.mode  ) _
    then
   
          glfwSwapInterval 1
          s_display.GlVer = glGetString(GL_VERSION)

    else
           glfwTerminate()
           end
    end if
   
    ''center screen
    dim xvid As GLFWvidmode
    glfwGetDesktopMode( @xvid )
    glfwSetWindowPos( (xvid.width-s_display.w)\2, (xvid.height-s_display.h)\2 )
   glfwSetWindowTitle("FBGFX_2_GL by relsoft 2010")

   '' Initialize OpenGL patamaters
   init_gl_screen()

   
   
      
    '' init texture
   
    dim as uinteger ptr pixdata
   dim as integer res,wid,hei,bpp,pitch,imgsize
   
   '' get GET/PUT data from header
   res = imageinfo(p_image_texture, wid, hei, bpp, pitch, pixdata, imgsize)
   
   '' Poinnt our texture buffer to the pixeldata
   p_texture_data = pixdata
   
   
   '' Enables OGL texturing
    glEnable (GL_TEXTURE_2D)
   
    '' Generate 1 texture for our FB image buffer,
    '' Bind it and set up parameters
    glGenTextures(1, @texture_id)
    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0,_
                 GL_RGBA, GL_UNSIGNED_BYTE, p_texture_data)
                 

   '' This is the parameter to use normally
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

   '' This one would give you a bi-filter for free
   '' Try to uncomment the 2 lines below.

'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

End Constructor

destructor Tfbgfx_2_GL()

   '' free our texture
   glDeleteTextures(1, @texture_id)
   
   '' close GLFW
   glfwTerminate()
   
   '' destroy our image buffers
   imagedestroy (p_image)
   imagedestroy (p_image_texture)
   
End Destructor


''============================================================================
''
''   returns the FBGFX buffer where we can draw FBGFX commands
''   
''============================================================================
property Tfbgfx_2_GL.image() as any ptr

   property = p_image
   
end property
   

''============================================================================
''
''   Initializes our openGL screen
''   
''============================================================================

sub Tfbgfx_2_GL.init_gl_screen()

   'screen information
   dim w as integer, h as integer
   'OpenGL params for gluerspective
   dim FOVy as double            'Field of view angle in Y
   dim Aspect as double          'Aspect of screen
   dim znear as double           'z-near clip distance
   dim zfar as double            'z-far clip distance

   'using screen info w and h as params
   glViewport(0, 0, s_display.w, s_display.h)
   
   'Set current Mode to projection(ie: 3d)
   glMatrixMode(GL_PROJECTION)
   
   'Load identity matrix to projection matrix
   glLoadIdentity()

   'Set gluPerspective params
   FOVy = 80/2                                     '45 deg fovy
   Aspect = s_display.w/ s_display.h
   znear = 1                                       'Near clip
   zfar = 500                                      'far clip
   
   'use glu Perspective to set our 3d frustum dimension up
   gluPerspective(FOVy, aspect, znear, zfar)
   
   'Modelview mode
   'ie. Matrix that does things to anything we draw
   'as in lines, points, tris, etc.
   glMatrixMode(GL_MODELVIEW)
   'load identity(clean) matrix to modelview
   glLoadIdentity()
   
   glShadeModel(GL_SMOOTH)                 'set shading to smooth(try GL_FLAT)
   glClearColor(0.0, 0.0, 0.0, 1.0)        'set Clear color to BLACK
   glClearDepth(1.0)                       'Set Depth buffer to 1(z-Buffer)
   glEnable(GL_DEPTH_TEST)                 'Enable Depth Testing so that our z-buffer works
   
   'compare each incoming pixel z value with the z value present in the depth buffer
   'LEQUAL means than pixel is drawn if the incoming z value is less than
   'or equal to the stored z value
   glDepthFunc(GL_LEQUAL)
   
   'have one or more material parameters track the current color
   'Material is your 3d model
   glEnable(GL_COLOR_MATERIAL)


    'Enable Texturing
    glEnable(GL_TEXTURE_2D)
   

    'Set blending parameters
    glBlendFunc(GL_SRC_ALPHA, GL_ONE)


   'Tell openGL that we want the best possible perspective transform
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
   
   '' Tell OpenGL that we need to fill polygons
   glPolygonMode(GL_FRONT, GL_FILL)
   
   
   
end sub


''============================================================================
''
''   Permits us to use an OpenGL screen in 2d mode
''   
''============================================================================

sub Tfbgfx_2_GL.view_ortho()
    glMatrixMode(GL_PROJECTION)   
    glPushMatrix()
    glLoadIdentity()
    glOrtho(0, s_display.w, 0,s_display.h, -1, 1)
    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glLoadIdentity()
end sub


''============================================================================
''
''   Switches OpenGL to 3d mode
''   
''============================================================================

sub Tfbgfx_2_GL.view_perspective() 
    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glPopMatrix()
end sub


''============================================================================
''
''   The meat of the demo
''  This makes use of an FBGFX image uploaded to
''  an OGL context so eliminate the FBGFX "stutter" bug.
''   
''============================================================================

sub Tfbgfx_2_GL.render()
   

   screenlock ()
   '' Put the 320 x 240 buffer to the 512 x 512 buffer
   put p_image_texture,(0,0),p_image,pset

   
   screenunlock ()
   
   '' Upload the 512 x 512 buffer to the GPU
   '' You can also use glTexImage2D()
   glTexSubImage2D (GL_TEXTURE_2D,0, 0, 0, TEX_WIDTH, TEX_HEIGHT,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8, p_texture_data)
   
    ''OpenGL drawing time     
    GlMatrixMode (GL_MODELVIEW)
    glloadidentity ()
    glClear  (GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT)

   '' These are texture coords for
   '' The quad we draw on the screen.
    dim as single sx
    dim as single sy
    dim as single sw
    dim as single sh

   '' Much like the screen coords
   '' But we need to normalize sw,sh as
   '' Our actual useable area is greater than what needs
   '' To be drawn  on screen.
    sx = 0
    sy = 0
    sw = BUFF_WIDTH/TEX_WIDTH
    sh = BUFF_HEIGHT/TEX_HEIGHT
   
    '' Set up color to pure white at full alpha
    glColor4f(1.0f,1.0f,1.0f,1.0f)
    glPolygonMode(GL_FRONT, GL_FILL)      '' need to fill the quad
    glPolygonMode(GL_BACK, GL_FILL)         '' Just to make sure even if you messed up your winding
    glDisable (GL_BLEND)                '' ditch blending
    glDisable (GL_DEPTH_TEST)            '' as well as distance testing
    glEnable  (GL_TEXTURE_2D)            '' enable texturing ot we don't see anything
   
    '' draw the quad
    glPushMatrix()       
        view_ortho()      '' go to 2d mode                   
            glBegin(GL_QUADS)     
              glTexCoord2f(sx, sy):   glVertex2i(0, s_display.h - 1)
              glTexCoord2f(sw, sy):   glVertex2i(s_display.w - 1, s_display.h - 1)
              glTexCoord2f(sw, sh):   glVertex2i(s_display.w - 1, 0)
              glTexCoord2f(sx, sh):   glVertex2i(0,0)         
            glEnd()                     
        view_perspective()   '' back 3d mode   
    glPopMatrix()

   
end sub








''=============================================================================
''
''  Test
''
''=============================================================================   


dim as Tfbgfx_2_GL video      '' open an OpenGL context

dim as GLuint cube            '' Display list for the eye candy
init_cube(cube)                '' init the cube model


dim as single time_start = 0

do

   '' Draw FBGFX stuff here
   
   '' Oh yes, screenlock works
   screenlock ()
   
   '' draw some stupid pixel effect
   twisted(video.image)

   '' Just to show all of FBGFX's drawing funks work ;*)
   draw string video.image,(10,40),"Hello from FBGFX!",rgb(rnd*255,rnd*255,rnd*255)
   draw string video.image,(10,60),"The swirly xor texture",rgb(200,100,0)
   draw string video.image,(10,80),"is rendered using FBGFX.",rgb(255,0,100)   
   draw string video.image,(10,100),"That white moving shadow",rgb(0,255,255)
   draw string video.image,(10,120),"is rendered by OpenGL.",rgb(255,0,255)

   '' Draw a border for kicks
   line video.image,(0,0)-(BUFF_WIDTH-1,BUFF_HEIGHT-1),rgb(200,100,255),b
   
   screenunlock()
   
   '' set up OGL and clear the frame buffer   
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT)
   
    '' Render FBGFX_2_GL
    video.render
   
    '' Draw some openGL eyecandy
    draw_cube(cube)
   
    '' limit frames
    '' Too lazy to do a timebased demo
    do             
    loop until glfwgettime() - time_start >= (1/60)
    time_start =  glfwgettime()
   
    '' flip
    GlfwSwapBuffers ()
   

Loop Until glfwGetKey( GLFW_KEY_ESC )


gldeleteLists(cube,1)

end



''=============================================================================
''
''  Useless but nice looking eye candies
''
''=============================================================================   



sub twisted(byval buffer as any ptr)
 
   dim as integer x, y, tx, ty, cx, cy
   dim as single angle, radius
   
   static as single twister = 0.02
   static as single twist_delta = 0.001
   
   dim as integer tw_x = 150, tw_y = 110
   dim as integer tw_radius = 100, tw_radius_div2= 150\2

    twister += twist_delta
    if twister> 0.07 then
        twist_delta = -twist_delta
    elseif twister< -0.07 then
        twist_delta = -twist_delta
    end if 
   
    dim as integer warp_value
       
    for y = 0 to BUFF_HEIGHT - 1
       for x = 0 to BUFF_WIDTH - 1
         cx = x - tw_x
         cy = y - tw_y
         radius = sqr( cx^2  + cy^2 )           
         if radius < tw_radius then
             angle = atan2(cy, cx)                       
             angle = angle + (tw_radius-radius )*twister
             tx = cos(angle)*radius
             ty = sin(angle)*radius 
             warp_value = (tw_x + tx) xor (tw_y + ty)         
             pset buffer,(x, y),rgb(warp_value,0,warp_value)           
         else
             pset buffer,(x, y),rgb(x xor y,0,x xor y)
         end if
       next x   
    next y
   
end sub


sub init_cube(byref CubeDL as GLuint)
 
  dim as single col = 0.03
  CubeDL =glGenLists(1)
  glNewList(CubeDL, GL_COMPILE)
  glBegin(GL_QUADS)
    ' Front Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0,  1.0)
    ' Back Face
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    ' Top Face
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    ' Bottom Face
    glColor3f(col, col,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    ' Right face
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    ' Left Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
  glEnd()
  glEndList()

end sub


sub draw_cube(byref CubeDL as GLuint)
   
   dim i as integer
   static as integer theta = 0
   dim elapsed_time as single
   dim as single ti, t, dt
   
    elapsed_time = glfwGetTime()
    ti = glfwGetTime() * 0.001208
    t = ti * 180.5 * (sin(elapsed_time/3150) + 360 * cos(elapsed_time/4150) + 90 * sin(elapsed_time/3140))
    dt = 0.80130 + 2.0124 * (sin(elapsed_time/40) * sin(elapsed_time/240) * cos(elapsed_time/540))
 
 
    glPolygonMode(GL_FRONT, GL_LINE)
    glPolygonMode(GL_BACK, GL_LINE)
    glEnable (GL_BLEND)
    glblendfunc (GL_ONE, GL_ONE)
    glDisable (GL_DEPTH_TEST)
    gldisable (GL_TEXTURE_2D)
   
    glTranslatef (0.0,0.0,-5.0)   
    glPushMatrix ()       
        glTranslatef (sin(elapsed_time)* 1,cos(elapsed_time)* 1,cos(elapsed_time)+ sin(elapsed_time)* 1)       
        theta = (theta + 1) mod 360
        dim as single scale = 0.01
        glRotatef (theta, 0,1,0)
        glRotatef (theta, 0,0,1)
        glScalef (scale,scale,scale)
        for i = 0 to 250
            glpushmatrix ()   
               t = t + dt
               glRotatef ((t), 1,0,0)
               glRotatef ((t), 0,1,0)
               glRotatef ((t), 0,0,1)   
               glcalllist (CubeDL)
               glpopmatrix ()
            glScalef (1.0187,1.0187,1.0187)
        next i
    glpopmatrix ()      
   
end sub




This is a 32bit to 32bit direct conversion. For those who would want to know how to implement an 8bit to 32 bit system here's an old game I made. The converter is in display.bas.

http://rel.betterwebber.com/index.php?a ... s_twilight
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Feb 24, 2010 15:27

Thanks relsoft! :)

-Vince
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Postby joseywales72 » Feb 25, 2010 5:06

I tested your code on Win 7 64 Ultimate, 1 Gb Ati Radeon 3850. It is running smoothly and without problems.
I'll try it later at work on 32 Bit Linux with a on board Nvidia 6100.
FBC 0.20 is the compiler.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Feb 25, 2010 5:19

It is working fine on Vista x64, but I'll try on my laptop later, where the real FBGFX stuttering is apparent.
I'd appreciate it if you could make a library of this, just to fix the FBGFX stutter. :]
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Feb 25, 2010 8:28

Could you summarize how this works? Does it write into a texture or...?
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Feb 25, 2010 18:53

I tried this on my laptop (Vista 32). I'm not 100% sure, but think it doesn't fix the problem.

I added a bouncing ball, because panning seems to show the issue the best. I removed the special effects because I was only get ~35FPS with them. And I added a sleep(1, 1) in the idle loop because I'm not going to make a game that uses 100% CPU.

Code: Select all

''        This demo shows how to draw using FBGFX
''        but render the buffer to an OpenGL context
''        Someone requestred this so...
''        Pros:
''        1. Eliminate the GFX stutter when flipping FBGFX screens (multithread bug I think)
''        2. Free filtering of your buffer using openGL
''        3. Fast whole buffer rotations
''        4. You don't need to change much of your existing code to use this.
''        5. You can freely use FBGFX commands along with OpenGL commsnds
''        6. full screen scaling

''       
''        Caveats:
''        1. Needs OpenGL (Who doesn't have that nowadays?)
''        2. A good GFX card (this worked on a 64 mb NVIDIA)
''       
''       
''        Richard Eric M Lope BSN RN (Relsoft)
''        http://rel.betterwebber.com
''
''  ** Note that this demo is not very well done and not encapsulated that much.
''     It's for the user to provide his own way of im`plementation as he see fit.
''        ** You can also use any other windowing system if you don't like GLFW such
''           as SDL, SFML, or even pure windows api.


#include once "/gl/gl.bi"
#include once "/gl/glu.bi"
#include once "/gl/glfw.bi"
#include once "/fbgfx.bi"

#ifndef false
        Const As Integer FALSE = 0
        Const As Integer TRUE = Not false
#endif

'' some constants that we need
'' You could also encapsulate this inside your
'' Class if you want.

Const As Integer SCR_WIDTH = 640                '' Actual screen dimensions
Const As Integer SCR_HEIGHT = 480
Const As Integer BITSPP = 32

Const As Integer BUFF_WIDTH = 320                '' The dimensions of our buffer where we draw FBGFX
Const As Integer BUFF_HEIGHT = 240                '' I used 320x480 since I used a pretty old PC to code this.

Const As Integer TEX_WIDTH = 512                '' Our texture dimensions where we transfe the FB image
Const As Integer TEX_HEIGHT = 512       '' to an OpenGL context
 


'' This is a Type I use to make making GLFW context easier
'' You can forget about this unless you want to learn GLFW
Type Tdisplay
    w                         As Uinteger
    h                         As Uinteger
    r_bits                As Uinteger
    g_bits                As Uinteger
    b_bits                As Uinteger
    a_bits                As Uinteger
    d_bits                As Uinteger
    s_bits                As Uinteger
    mode                As Uinteger
    Glver                 As Zstring Ptr
    As Single FOVy, aspect, znear, zfar
End Type



'' Our main Class
Type Tfbgfx_2_GL
       
        Declare Constructor()
        Declare Destructor()
       
        Public:       
        '' properties
        Declare Property image() As Any Ptr
       
        '' methods
        Declare Sub init_gl_screen()                '' Initializes our GL parameters
        Declare Sub view_ortho()                        '' Used for 2d rendering
        Declare Sub view_perspective()                '' Used to go back to 3d
        Declare Sub render()                                '' FBGFX drawing and FBGFX->GL conversion
       
        Private:
        s_display                        As Tdisplay         '' GLFW thingy above
        texture_id                  As GLuint                '' Our texture where we upload the FBGFX buffer
        p_image                                As Any Ptr                '' GET/PUT image where we have to draw stuff       
        p_image_texture                As Any Ptr                '' GET/PUT image to be uploaded to opengl     
    p_texture_data                 As GLuint  Ptr        '' pixel data address of the GET/PUT image       
         
End Type



'' Some Useless but nice looking gfx demos
'' You can forget about this.
Declare Sub twisted(Byval buffer As Any Ptr)
Declare Sub init_cube(Byref CubeDL As GLuint)
Declare Sub draw_cube(Byref CubeDL As GLuint)


''=============================================================================
''
''  Implementation
''
''=============================================================================       

Constructor Tfbgfx_2_GL()

        '' init an FBGFX 320x240 screen
        '' GFX_NULL so that we can use FBGFX along with OGL
        screenres BUFF_WIDTH,BUFF_HEIGHT,32,1,fb.GFX_NO_SWITCH  Or fb.GFX_NULL
       
       
        '' Set up our image buffers
        '' p_image is the buffer we draw FBGFX (320 x 240)
        '' p_image_texture is the buffer we need to upload to the GPU
        ''  p_image_texture needs to be power of 2 and bigger than p_image
       
        p_image = Imagecreate(BUFF_WIDTH,BUFF_HEIGHT,0)
        p_image_texture        = Imagecreate(TEX_WIDTH,TEX_HEIGHT,0)
       
       
        '' initialize OpenGL screen
    If glfwInit() Then
        'Successful!
    Else   
        Print "Failed to initialize GLFW!"
        Sleep 1000
        End
    End If

       
        '' set up GLFW parameters
        s_display.w                 = SCR_WIDTH
        s_display.h                        = SCR_HEIGHT
        s_display.r_bits        = 8       
        s_display.g_bits        = 8
        s_display.b_bits        = 8       
        s_display.a_bits        = 8
        s_display.d_bits        = 8
        s_display.s_bits        = 8
        s_display.mode                = GLFW_WINDOW   
   
    '' Open a GLFW window   
    If  glfwOpenWindow( _
                    s_display.w     , _
                    s_display.h     , _
                    s_display.r_bits, _
                    s_display.g_bits, _
                    s_display.b_bits, _
                    s_display.a_bits, _
                    s_display.d_bits, _
                    s_display.s_bits, _
                    s_display.mode  ) _
    Then
   
                    glfwSwapInterval 1
                    s_display.GlVer = glGetString(GL_VERSION)

    Else
                glfwTerminate()
                End
    End If
   
    ''center screen
    Dim xvid As GLFWvidmode
    glfwGetDesktopMode( @xvid )
    glfwSetWindowPos( (xvid.width-s_display.w)\2, (xvid.height-s_display.h)\2 )
        glfwSetWindowTitle("FBGFX_2_GL by relsoft 2010")

        '' Initialize OpenGL patamaters
        init_gl_screen()

       
       
           
    '' init texture
   
    Dim As Uinteger Ptr pixdata
        Dim As Integer res,wid,hei,bpp,pitch,imgsize
       
        '' get GET/PUT data from header
        res = imageinfo(p_image_texture, wid, hei, bpp, pitch, pixdata, imgsize)
       
        '' Poinnt our texture buffer to the pixeldata
        p_texture_data = pixdata
       
       
        '' Enables OGL texturing
    glEnable (GL_TEXTURE_2D)
   
    '' Generate 1 texture for our FB image buffer,
    '' Bind it and set up parameters
    glGenTextures(1, @texture_id)
    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0,_
                 GL_RGBA, GL_UNSIGNED_BYTE, p_texture_data)
                 

        '' This is the parameter to use normally
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

        '' This one would give you a bi-filter for free
        '' Try to uncomment the 2 lines below.

'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

End Constructor

Destructor Tfbgfx_2_GL()

        '' free our texture
        glDeleteTextures(1, @texture_id)
       
        '' close GLFW
        glfwTerminate()
       
        '' destroy our image buffers
        imagedestroy (p_image)
        imagedestroy (p_image_texture)
       
End Destructor


''============================================================================
''
''        returns the FBGFX buffer where we can draw FBGFX commands
''       
''============================================================================
Property Tfbgfx_2_GL.image() As Any Ptr

        Property = p_image
       
End Property
       

''============================================================================
''
''        Initializes our openGL screen
''       
''============================================================================

Sub Tfbgfx_2_GL.init_gl_screen()

        'screen information
        Dim w As Integer, h As Integer
        'OpenGL params for gluerspective
        Dim FOVy As Double            'Field of view angle in Y
        Dim Aspect As Double          'Aspect of screen
        Dim znear As Double           'z-near clip distance
        Dim zfar As Double            'z-far clip distance

        'using screen info w and h as params
        glViewport(0, 0, s_display.w, s_display.h)
       
        'Set current Mode to projection(ie: 3d)
        glMatrixMode(GL_PROJECTION)
       
        'Load identity matrix to projection matrix
        glLoadIdentity()

        'Set gluPerspective params
        FOVy = 80/2                                     '45 deg fovy
        Aspect = s_display.w/ s_display.h
        znear = 1                                       'Near clip
        zfar = 500                                      'far clip
       
        'use glu Perspective to set our 3d frustum dimension up
        gluPerspective(FOVy, aspect, znear, zfar)
       
        'Modelview mode
        'ie. Matrix that does things to anything we draw
        'as in lines, points, tris, etc.
        glMatrixMode(GL_MODELVIEW)
        'load identity(clean) matrix to modelview
        glLoadIdentity()
       
        glShadeModel(GL_SMOOTH)                 'set shading to smooth(try GL_FLAT)
        glClearColor(0.0, 0.0, 0.0, 1.0)        'set Clear color to BLACK
        glClearDepth(1.0)                       'Set Depth buffer to 1(z-Buffer)
        glEnable(GL_DEPTH_TEST)                 'Enable Depth Testing so that our z-buffer works
       
        'compare each incoming pixel z value with the z value present in the depth buffer
        'LEQUAL means than pixel is drawn if the incoming z value is less than
        'or equal to the stored z value
        glDepthFunc(GL_LEQUAL)
       
        'have one or more material parameters track the current color
        'Material is your 3d model
        glEnable(GL_COLOR_MATERIAL)


    'Enable Texturing
    glEnable(GL_TEXTURE_2D)
   

    'Set blending parameters
    glBlendFunc(GL_SRC_ALPHA, GL_ONE)


        'Tell openGL that we want the best possible perspective transform
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
       
        '' Tell OpenGL that we need to fill polygons
        glPolygonMode(GL_FRONT, GL_FILL)
   
   
   
End Sub


''============================================================================
''
''        Permits us to use an OpenGL screen in 2d mode
''       
''============================================================================

Sub Tfbgfx_2_GL.view_ortho()
    glMatrixMode(GL_PROJECTION)   
    glPushMatrix()
    glLoadIdentity()
    glOrtho(0, s_display.w, 0,s_display.h, -1, 1)
    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glLoadIdentity()
End Sub


''============================================================================
''
''        Switches OpenGL to 3d mode
''       
''============================================================================

Sub Tfbgfx_2_GL.view_perspective()
    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glPopMatrix()
End Sub


''============================================================================
''
''        The meat of the demo
''  This makes use of an FBGFX image uploaded to
''  an OGL context so eliminate the FBGFX "stutter" bug.
''       
''============================================================================

Sub Tfbgfx_2_GL.render()
       

        screenlock ()
        '' Put the 320 x 240 buffer to the 512 x 512 buffer
        Put p_image_texture,(0,0),p_image,Pset

       
        screenunlock ()
       
        '' Upload the 512 x 512 buffer to the GPU
        '' You can also use glTexImage2D()
        glTexSubImage2D (GL_TEXTURE_2D,0, 0, 0, TEX_WIDTH, TEX_HEIGHT,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8, p_texture_data)
   
    ''OpenGL drawing time     
    GlMatrixMode (GL_MODELVIEW)
    glloadidentity ()
    glClear  (GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)

        '' These are texture coords for
        '' The quad we draw on the screen.
    Dim As Single sx
    Dim As Single sy
    Dim As Single sw
    Dim As Single sh

        '' Much like the screen coords
        '' But we need to normalize sw,sh as
        '' Our actual useable area is greater than what needs
        '' To be drawn  on screen.
    sx = 0
    sy = 0
    sw = BUFF_WIDTH/TEX_WIDTH
    sh = BUFF_HEIGHT/TEX_HEIGHT
   
    '' Set up color to pure white at full alpha
    glColor4f(1.0f,1.0f,1.0f,1.0f)
    glPolygonMode(GL_FRONT, GL_FILL)                '' need to fill the quad
    glPolygonMode(GL_BACK, GL_FILL)                        '' Just to make sure even if you messed up your winding
    glDisable (GL_BLEND)                                         '' ditch blending
    glDisable (GL_DEPTH_TEST)                                '' as well as distance testing
    glEnable  (GL_TEXTURE_2D)                                '' enable texturing ot we don't see anything
   
    '' draw the quad
    glPushMatrix()       
        view_ortho()                '' go to 2d mode                   
            glBegin(GL_QUADS)     
              glTexCoord2f(sx, sy):   glVertex2i(0, s_display.h - 1)
              glTexCoord2f(sw, sy):   glVertex2i(s_display.w - 1, s_display.h - 1)
              glTexCoord2f(sw, sh):   glVertex2i(s_display.w - 1, 0)
              glTexCoord2f(sx, sh):   glVertex2i(0,0)         
            glEnd()                     
        view_perspective()        '' back 3d mode   
    glPopMatrix()

       
End Sub








''=============================================================================
''
''  Test
''
''=============================================================================       


Dim As Tfbgfx_2_GL video                '' open an OpenGL context

Dim As GLuint cube                                '' Display list for the eye candy
init_cube(cube)                                    '' init the cube model


Dim As Single time_start = 0
dim as double t=timer,fc=0
dim as integer x, dx=5, y, dy=4
Do

        '' Draw FBGFX stuff here
       fc+=1
        '' Oh yes, screenlock works
        screenlock ()
       
        '' draw some stupid pixel effect
        'twisted(video.image)
        Line video.image,(0,0)-(BUFF_WIDTH-1,BUFF_HEIGHT-1),rgb(0,0,0),bf
       

        '' Just to show all of FBGFX's drawing funks work ;*)
        Draw String video.image,(10,40),"Hello from FBGFX!" & int(fc/(timer-t)),rgb(200, 100, 0)
        Draw String video.image,(10,60),"The swirly xor texture",rgb(200,100,0)
        Draw String video.image,(10,80),"is rendered using FBGFX.",rgb(255,0,100)       
        Draw String video.image,(10,100),"That white moving shadow",rgb(0,255,255)
        Draw String video.image,(10,120),"is rendered by OpenGL.",rgb(255,0,255)

        '' Draw a border for kicks
        Line video.image,(0,0)-(BUFF_WIDTH-1,BUFF_HEIGHT-1),rgb(200,100,255),b
       
        x += dx
        if x = 0 or x = 300 then dx*=-1
        y+=dy
        if y = 0 or y = 160 then dy*=-1
        circle video.image, (x, y), 10, &HFFFFFFFF,,, 1, F
       
        screenunlock()
       
        '' set up OGL and clear the frame buffer       
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
   
    '' Render FBGFX_2_GL
    video.render
   
    '' Draw some openGL eyecandy
    'draw_cube(cube)
   
    '' limit frames
    '' Too lazy to do a timebased demo
    Do             :sleep(1,1)
    Loop Until glfwgettime() - time_start >= (1/60)
    time_start =  glfwgettime()
   
    '' flip
    GlfwSwapBuffers ()
       

Loop Until glfwGetKey( GLFW_KEY_ESC )


gldeleteLists(cube,1)

End



''=============================================================================
''
''  Useless but nice looking eye candies
''
''=============================================================================       



Sub twisted(Byval buffer As Any Ptr)
 
        Dim As Integer x, y, tx, ty, cx, cy
        Dim As Single angle, radius
       
        Static As Single twister = 0.02
        Static As Single twist_delta = 0.001
       
        Dim As Integer tw_x = 150, tw_y = 110
        Dim As Integer tw_radius = 100, tw_radius_div2= 150\2

    twister += twist_delta
    If twister> 0.07 Then
        twist_delta = -twist_delta
    Elseif twister< -0.07 Then
        twist_delta = -twist_delta
    End If
   
    Dim As Integer warp_value
       
    For y = 0 To BUFF_HEIGHT - 1
            For x = 0 To BUFF_WIDTH - 1
                        cx = x - tw_x
                        cy = y - tw_y
                        radius = Sqr( cx^2  + cy^2 )           
                        If radius < tw_radius Then
                            angle = atan2(cy, cx)                       
                            angle = angle + (tw_radius-radius )*twister
                            tx = Cos(angle)*radius
                            ty = Sin(angle)*radius
                            warp_value = (tw_x + tx) Xor (tw_y + ty)         
                            Pset buffer,(x, y),rgb(warp_value,0,warp_value)           
                        Else
                            Pset buffer,(x, y),rgb(x Xor y,0,x Xor y)
                        End If
            Next x   
    Next y
       
End Sub


Sub init_cube(Byref CubeDL As GLuint)
 
  Dim As Single col = 0.03
  CubeDL =glGenLists(1)
  glNewList(CubeDL, GL_COMPILE)
  glBegin(GL_QUADS)
    ' Front Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0,  1.0)
    ' Back Face
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    ' Top Face
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    ' Bottom Face
    glColor3f(col, col,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    ' Right face
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    ' Left Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
  glEnd()
  glEndList()

End Sub


Sub draw_cube(Byref CubeDL As GLuint)
       
        Dim i As Integer
        Static As Integer theta = 0
        Dim elapsed_time As Single
        Dim As Single ti, t, dt
       
    elapsed_time = glfwGetTime()
    ti = glfwGetTime() * 0.001208
    t = ti * 180.5 * (Sin(elapsed_time/3150) + 360 * Cos(elapsed_time/4150) + 90 * Sin(elapsed_time/3140))
    dt = 0.80130 + 2.0124 * (Sin(elapsed_time/40) * Sin(elapsed_time/240) * Cos(elapsed_time/540))
 
 
    glPolygonMode(GL_FRONT, GL_LINE)
    glPolygonMode(GL_BACK, GL_LINE)
    glEnable (GL_BLEND)
    glblendfunc (GL_ONE, GL_ONE)
    glDisable (GL_DEPTH_TEST)
    gldisable (GL_TEXTURE_2D)
   
    glTranslatef (0.0,0.0,-5.0)   
    glPushMatrix ()       
        glTranslatef (Sin(elapsed_time)* 1,Cos(elapsed_time)* 1,Cos(elapsed_time)+ Sin(elapsed_time)* 1)       
        theta = (theta + 1) Mod 360
        Dim As Single scale = 0.01
        glRotatef (theta, 0,1,0)
        glRotatef (theta, 0,0,1)
        glScalef (scale,scale,scale)
        For i = 0 To 250
            glpushmatrix ()       
                    t = t + dt
                    glRotatef ((t), 1,0,0)
                    glRotatef ((t), 0,1,0)
                    glRotatef ((t), 0,0,1)       
                    glcalllist (CubeDL)
                    glpopmatrix ()
            glScalef (1.0187,1.0187,1.0187)
        Next i
    glpopmatrix ()         
   
End Sub


 


It still seems to stutter a bit, although I'm not sure if there is still a shearing issue.
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

Postby super_castle » Feb 27, 2010 13:33

he, wonderfull. (i'm german)

this image is freebasicscreen and little openglscreen (free-ogl):
http://www.roboternetz.de/phpBB2/album_cat.php?cat_id=1

I would like to freebasicscreen in:
---print " hello" write--
not in the opengl. screen.
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

Postby super_castle » Feb 27, 2010 13:57

i'm one picture from gl-screen to freebasic-gfx ?
http://www.roboternetz.de/phpBB2/album_cat.php?cat_id=1

why it is?

gruss
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

Postby super_castle » Feb 28, 2010 16:30

he, activate left mini-windows with the left mouse-click.

Code: Select all


#Include once "/gl/gl.bi"
#include once "/gl/glu.bi"
#include once "/gl/glfw.bi"
#include once "/fbgfx.bi"
USING FB

#ifndef false
        Const As Integer FALSE = 0
        Const As Integer TRUE = Not false
#endif

'' some constants that we need
'' You could also encapsulate this inside your
'' Class if you want.

Const As Integer SCR_WIDTH = 640                '' Actual screen dimensions
Const As Integer SCR_HEIGHT = 480
Const As Integer BITSPP = 32

Const As Integer BUFF_WIDTH = 320                '' The dimensions of our buffer where we draw FBGFX
Const As Integer BUFF_HEIGHT = 240                '' I used 320x480 since I used a pretty old PC to code this.

Const As Integer TEX_WIDTH = 512                '' Our texture dimensions where we transfe the FB image
Const As Integer TEX_HEIGHT = 512       '' to an OpenGL context
 
 
Dim shared as Integer aa,new_x, new_y

'' This is a Type I use to make making GLFW context easier
'' You can forget about this unless you want to learn GLFW
Type Tdisplay
    w                         As Uinteger
    h                         As Uinteger
    r_bits                As Uinteger
    g_bits                As Uinteger
    b_bits                As Uinteger
    a_bits                As Uinteger
    d_bits                As Uinteger
    s_bits                As Uinteger
    mode                As Uinteger
    Glver                 As Zstring Ptr
    As Single FOVy, aspect, znear, zfar
End Type



'' Our main Class
Type Tfbgfx_2_GL
       
        Declare Constructor()
        Declare Destructor()
       
        Public:       
        '' properties
        Declare Property image() As Any Ptr
       
        '' methods
        Declare Sub init_gl_screen()                '' Initializes our GL parameters
        Declare Sub view_ortho()                        '' Used for 2d rendering
        Declare Sub view_perspective()                '' Used to go back to 3d
        Declare Sub render()                                '' FBGFX drawing and FBGFX->GL conversion
       
        Private:
        s_display                        As Tdisplay         '' GLFW thingy above
        texture_id                  As GLuint                '' Our texture where we upload the FBGFX buffer
        p_image                                As Any Ptr                '' GET/PUT image where we have to draw stuff       
        p_image_texture                As Any Ptr                '' GET/PUT image to be uploaded to opengl     
    p_texture_data                 As GLuint  Ptr        '' pixel data address of the GET/PUT image       
         
End Type



'' Some Useless but nice looking gfx demos
'' You can forget about this.
Declare Sub twisted(Byval buffer As Any Ptr)
Declare Sub init_cube(Byref CubeDL As GLuint)
Declare Sub draw_cube(Byref CubeDL As GLuint)


''=============================================================================
''
''  Implementation
''
''=============================================================================       

Constructor Tfbgfx_2_GL()

        '' init an FBGFX 320x240 screen
        '' GFX_NULL so that we can use FBGFX along with OGL
        ScreenRes BUFF_WIDTH,BUFF_HEIGHT,32
       sCREENCONTROL SET_WINDOW_POS, 10,200
       
   
        '' Set up our image buffers
        '' p_image is the buffer we draw FBGFX (320 x 240)
        '' p_image_texture is the buffer we need to upload to the GPU
        ''  p_image_texture needs to be power of 2 and bigger than p_image
       
        p_image = Imagecreate(BUFF_WIDTH,BUFF_HEIGHT,0)
        p_image_texture = Imagecreate(TEX_WIDTH,TEX_HEIGHT,0)
       
       
        '' initialize OpenGL screen
    If glfwInit() Then
        'Successful!
    Else   
        Print "Failed to initialize GLFW!"
        Sleep 1000
        End
    End If

       
        '' set up GLFW parameters
        s_display.w                 = SCR_WIDTH
        s_display.h                        = SCR_HEIGHT
        s_display.r_bits        = 8       
        s_display.g_bits        = 8
        s_display.b_bits        = 8       
        s_display.a_bits        = 8
        s_display.d_bits        = 8
        s_display.s_bits        = 8
        s_display.mode                = GLFW_WINDOW   
   
    '' Open a GLFW window   
    If  glfwOpenWindow( _
                    s_display.w     , _
                    s_display.h     , _
                    s_display.r_bits, _
                    s_display.g_bits, _
                    s_display.b_bits, _
                    s_display.a_bits, _
                    s_display.d_bits, _
                    s_display.s_bits, _
                    s_display.mode  ) _
    Then
   
                    glfwSwapInterval 1
                    s_display.GlVer = glGetString(GL_VERSION)

    Else
                glfwTerminate()
                End
    End If
   
    ''center screen
    Dim xvid As GLFWvidmode
    glfwGetDesktopMode( @xvid )
    glfwSetWindowPos( (xvid.width-s_display.w)\2, (xvid.height-s_display.h)\2 )
        glfwSetWindowTitle("FBGFX_2_GL by relsoft 2010")

        '' Initialize OpenGL patamaters
        init_gl_screen()

       
       
           
    '' init texture
   
    Dim As Uinteger Ptr pixdata
        Dim As Integer res,wid,hei,bpp,pitch,imgsize
       
        '' get GET/PUT data from header
        res = imageinfo(p_image_texture, wid, hei, bpp, pitch, pixdata, imgsize)
       
        '' Poinnt our texture buffer to the pixeldata
        p_texture_data = pixdata
       
       
        '' Enables OGL texturing
    glEnable (GL_TEXTURE_2D)
   
    '' Generate 1 texture for our FB image buffer,
    '' Bind it and set up parameters
    glGenTextures(1, @texture_id)
    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0,_
                 GL_RGBA, GL_UNSIGNED_BYTE, p_texture_data)
                 

        '' This is the parameter to use normally
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

        '' This one would give you a bi-filter for free
        '' Try to uncomment the 2 lines below.

'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
'    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

End Constructor

Destructor Tfbgfx_2_GL()

        '' free our texture
        glDeleteTextures(1, @texture_id)
       
        '' close GLFW
        glfwTerminate()
       
        '' destroy our image buffers
        imagedestroy (p_image)
        imagedestroy (p_image_texture)
       
End Destructor


''============================================================================
''
''        returns the FBGFX buffer where we can draw FBGFX commands
''       
''============================================================================
Property Tfbgfx_2_GL.image() As Any Ptr

        Property = p_image
       
End Property
       

''============================================================================
''
''        Initializes our openGL screen
''       
''============================================================================

Sub Tfbgfx_2_GL.init_gl_screen()

        'screen information
        Dim w As Integer, h As Integer
        'OpenGL params for gluerspective
        Dim FOVy As Double            'Field of view angle in Y
        Dim Aspect As Double          'Aspect of screen
        Dim znear As Double           'z-near clip distance
        Dim zfar As Double            'z-far clip distance

        'using screen info w and h as params
        glViewport(0, 0, s_display.w, s_display.h)
       
        'Set current Mode to projection(ie: 3d)
        glMatrixMode(GL_PROJECTION)
       
        'Load identity matrix to projection matrix
        glLoadIdentity()

        'Set gluPerspective params
        FOVy = 80/2                                     '45 deg fovy
        Aspect = s_display.w/ s_display.h
        znear = 1                                       'Near clip
        zfar = 500                                      'far clip
       
        'use glu Perspective to set our 3d frustum dimension up
        gluPerspective(FOVy, aspect, znear, zfar)
       
        'Modelview mode
        'ie. Matrix that does things to anything we draw
        'as in lines, points, tris, etc.
        glMatrixMode(GL_MODELVIEW)
        'load identity(clean) matrix to modelview
        glLoadIdentity()
       
        glShadeModel(GL_SMOOTH)                 'set shading to smooth(try GL_FLAT)
        glClearColor(0.0, 0.0, 0.0, 1.0)        'set Clear color to BLACK
        glClearDepth(1.0)                       'Set Depth buffer to 1(z-Buffer)
        glEnable(GL_DEPTH_TEST)                 'Enable Depth Testing so that our z-buffer works
       
        'compare each incoming pixel z value with the z value present in the depth buffer
        'LEQUAL means than pixel is drawn if the incoming z value is less than
        'or equal to the stored z value
        glDepthFunc(GL_LEQUAL)
       
        'have one or more material parameters track the current color
        'Material is your 3d model
        glEnable(GL_COLOR_MATERIAL)


    'Enable Texturing
    glEnable(GL_TEXTURE_2D)
   

    'Set blending parameters
    glBlendFunc(GL_SRC_ALPHA, GL_ONE)


        'Tell openGL that we want the best possible perspective transform
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
       
        '' Tell OpenGL that we need to fill polygons
        glPolygonMode(GL_FRONT, GL_FILL)
   
   
   
End Sub


''============================================================================
''
''        Permits us to use an OpenGL screen in 2d mode
''       
''============================================================================

Sub Tfbgfx_2_GL.view_ortho()
    glMatrixMode(GL_PROJECTION)   
    glPushMatrix()
    glLoadIdentity()
    glOrtho(0, s_display.w, 0,s_display.h, -1, 1)
    glMatrixMode(GL_MODELVIEW)
    glPushMatrix()
    glLoadIdentity()
End Sub


''============================================================================
''
''        Switches OpenGL to 3d mode
''       
''============================================================================

Sub Tfbgfx_2_GL.view_perspective()
    glMatrixMode(GL_PROJECTION)
    glPopMatrix()
    glMatrixMode(GL_MODELVIEW)
    glPopMatrix()
End Sub


''============================================================================
''
''        The meat of the demo
''  This makes use of an FBGFX image uploaded to
''  an OGL context so eliminate the FBGFX "stutter" bug.
''       
''============================================================================

Sub Tfbgfx_2_GL.render()
       

        screenlock ()
        '' Put the 320 x 240 buffer to the 512 x 512 buffer
        Put p_image_texture,(0,0),p_image,PSet
        Put (0,0),p_image,Pset

       
        screenunlock ()
       
        '' Upload the 512 x 512 buffer to the GPU
        '' You can also use glTexImage2D()
        glTexSubImage2D (GL_TEXTURE_2D,0, 0, 0, TEX_WIDTH, TEX_HEIGHT,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8, p_texture_data)
   
    ''OpenGL drawing time     
    GlMatrixMode (GL_MODELVIEW)
    glloadidentity ()
    glClear  (GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)

        '' These are texture coords for
        '' The quad we draw on the screen.
    Dim As Single sx
    Dim As Single sy
    Dim As Single sw
    Dim As Single sh

        '' Much like the screen coords
        '' But we need to normalize sw,sh as
        '' Our actual useable area is greater than what needs
        '' To be drawn  on screen.
    sx = 0
    sy = 0
    sw = BUFF_WIDTH/TEX_WIDTH
    sh = BUFF_HEIGHT/TEX_HEIGHT
   
    '' Set up color to pure white at full alpha
    glColor4f(1.0f,1.0f,1.0f,1.0f)
    glPolygonMode(GL_FRONT, GL_FILL)                '' need to fill the quad
    glPolygonMode(GL_BACK, GL_FILL)                        '' Just to make sure even if you messed up your winding
    glDisable (GL_BLEND)                                         '' ditch blending
    glDisable (GL_DEPTH_TEST)                                '' as well as distance testing
    glEnable  (GL_TEXTURE_2D)                                '' enable texturing ot we don't see anything
   
    '' draw the quad
    glPushMatrix()       
        view_ortho()                '' go to 2d mode                   
            glBegin(GL_QUADS)     
              glTexCoord2f(sx, sy):   glVertex2i(0, s_display.h - 1)
              glTexCoord2f(sw, sy):   glVertex2i(s_display.w - 1, s_display.h - 1)
              glTexCoord2f(sw, sh):   glVertex2i(s_display.w - 1, 0)
              glTexCoord2f(sx, sh):   glVertex2i(0,0)         
            glEnd()                     
        view_perspective()        '' back 3d mode   
    glPopMatrix()

       
End Sub








''=============================================================================
''
''  Test
''
''=============================================================================       


Dim As Tfbgfx_2_GL video                '' open an OpenGL context

Dim As GLuint cube                                '' Display list for the eye candy
init_cube(cube)                                    '' init the cube model


Dim As Single time_start = 0
Dim As Double t=Timer,fc=0
Dim As Integer x, dx=5, y, dy=4
Do

        '' Draw FBGFX stuff here
       fc+=1
        '' Oh yes, screenlock works
        screenlock ()
       
        '' draw some stupid pixel effect
        'twisted(video.image)
        Line video.image,(0,0)-(BUFF_WIDTH-1,BUFF_HEIGHT-1),rgb(0,0,0),bf
       
        aa=aa+1
        '' Just to show all of FBGFX's drawing funks work ;*)
        Draw String video.image,(10,40),"Hello from FBGFX!  " & aa,rgb(200, 100, 0)
        Draw String video.image,(10,60),"The swirly xor texture",rgb(200,100,0)
        Draw String video.image,(10,80),"is rendered using FBGFX.",rgb(255,0,100)       
        Draw String video.image,(10,100),"That white moving shadow",rgb(0,255,255)
        Draw String video.image,(10,120),"is rendered by OpenGL.",rgb(255,0,255)

        '' Draw a border for kicks
        Line video.image,(0,0)-(BUFF_WIDTH-1,BUFF_HEIGHT-1),rgb(200,100,255)
       
        x += dx
        If x = 0 Or x > 310 Then dx*=-1
        y+=dy
        If y = 0 Or y > 235 Then dy*=-1
       
        getmouse new_x, new_y
        Circle video.image, (new_x, new_y), 10, &HFFFFFFFF,,, 1, F
       
        screenunlock()
       
        '' set up OGL and clear the frame buffer       
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
   
    '' Render FBGFX_2_GL
    video.render
   
    '' Draw some openGL eyecandy
    draw_cube(cube)
   
    '' limit frames
    '' Too lazy to do a timebased demo
    Do             :Sleep(1,1)
    Loop Until glfwgettime() - time_start >= (1/60)
    time_start =  glfwgettime()
   
    '' flip
    GlfwSwapBuffers ()
       

Loop Until glfwGetKey( GLFW_KEY_ESC )


gldeleteLists(cube,1)

End



''=============================================================================
''
''  Useless but nice looking eye candies
''
''=============================================================================       



Sub twisted(Byval buffer As Any Ptr)
 
        Dim As Integer x, y, tx, ty, cx, cy
        Dim As Single angle, radius
       
        Static As Single twister = 0.02
        Static As Single twist_delta = 0.001
       
        Dim As Integer tw_x = 150, tw_y = 110
        Dim As Integer tw_radius = 100, tw_radius_div2= 150\2

    twister += twist_delta
    If twister> 0.07 Then
        twist_delta = -twist_delta
    Elseif twister< -0.07 Then
        twist_delta = -twist_delta
    End If
   
    Dim As Integer warp_value
       
    For y = 0 To BUFF_HEIGHT - 1
            For x = 0 To BUFF_WIDTH - 1
                        cx = x - tw_x
                        cy = y - tw_y
                        radius = Sqr( cx^2  + cy^2 )           
                        If radius < tw_radius Then
                            angle = atan2(cy, cx)                       
                            angle = angle + (tw_radius-radius )*twister
                            tx = Cos(angle)*radius
                            ty = Sin(angle)*radius
                            warp_value = (tw_x + tx) Xor (tw_y + ty)         
                            Pset buffer,(x, y),rgb(warp_value,0,warp_value)           
                        Else
                            Pset buffer,(x, y),rgb(x Xor y,0,x Xor y)
                        End If
            Next x   
    Next y
       
End Sub


Sub init_cube(Byref CubeDL As GLuint)
 
  Dim As Single col = 0.03
  CubeDL =glGenLists(1)
  glNewList(CubeDL, GL_COMPILE)
  glBegin(GL_QUADS)
    ' Front Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0,  1.0)
    ' Back Face
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    ' Top Face
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    ' Bottom Face
    glColor3f(col, col,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    ' Right face
    glColor3f(col, 0.0,col): glVertex3f( 1.0, -1.0, -1.0)
    glColor3f(col, col,col): glVertex3f( 1.0,  1.0, -1.0)
    glColor3f(0.0, col,col): glVertex3f( 1.0,  1.0,  1.0)
    glColor3f(0.0, 0.0,col): glVertex3f( 1.0, -1.0,  1.0)
    ' Left Face
    glColor3f(0.0, 0.0,col): glVertex3f(-1.0, -1.0, -1.0)
    glColor3f(col, 0.0,col): glVertex3f(-1.0, -1.0,  1.0)
    glColor3f(col, col,col): glVertex3f(-1.0,  1.0,  1.0)
    glColor3f(0.0, col,col): glVertex3f(-1.0,  1.0, -1.0)
  glEnd()
  glEndList()

End Sub


Sub draw_cube(Byref CubeDL As GLuint)
       
        Dim i As Integer
        Static As Integer theta = 0
        Dim elapsed_time As Single
        Dim As Single ti, t, dt
       
    elapsed_time = glfwGetTime()
    ti = glfwGetTime() * 0.001208
    t = ti * 180.5 * (Sin(elapsed_time/3150) + 360 * Cos(elapsed_time/4150) + 90 * Sin(elapsed_time/3140))
    dt = 0.80130 + 2.0124 * (Sin(elapsed_time/40) * Sin(elapsed_time/240) * Cos(elapsed_time/540))
 
 
    glPolygonMode(GL_FRONT, GL_LINE)
    glPolygonMode(GL_BACK, GL_LINE)
    glEnable (GL_BLEND)
    glblendfunc (GL_ONE, GL_ONE)
    glDisable (GL_DEPTH_TEST)
    gldisable (GL_TEXTURE_2D)
   
    glTranslatef (0.0,0.0,-5.0)   
    glPushMatrix ()       
        glTranslatef (Sin(elapsed_time)* 1,Cos(elapsed_time)* 1,Cos(elapsed_time)+ Sin(elapsed_time)* 1)       
        theta = (theta + 1) Mod 360
        Dim As Single scale = 0.01
        glRotatef (theta, 0,1,0)
        glRotatef (theta, 0,0,1)
        glScalef (scale,scale,scale)
        For i = 0 To 250
            glpushmatrix ()       
                    t = t + dt
                    glRotatef ((t), 1,0,0)
                    glRotatef ((t), 0,1,0)
                    glRotatef ((t), 0,0,1)       
                    glcalllist (CubeDL)
                    glpopmatrix ()
            glScalef (1.0187,1.0187,1.0187)
        Next i
    glpopmatrix ()         
   
End Sub


Oz
Posts: 585
Joined: Jul 02, 2005 14:21
Location: Waterloo, Ontario, Canada
Contact:

Postby Oz » Feb 28, 2010 16:40

Rel, you are truely awesome. I appreciate your work. Good job!

-Oz
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

Postby super_castle » Feb 28, 2010 17:34

help me:

one picture from opengl-screen to freebasic-screen with pointer?

gruss
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Postby relsoft » Mar 03, 2010 9:05

super_castle wrote:help me:

one picture from opengl-screen to freebasic-screen with pointer?

gruss


The only thing I can think of that would work is.

1. Render to Texture (instead of the framebuffer render OpenGL primitives in a texture)
2. Read the texture using glReadPixels and pset those pixels to an FBGFX get/put image.

Can't make a demo since I can't use my laptop for a while. This pc doesn't have a GFX card.
super_castle
Posts: 289
Joined: Oct 10, 2006 7:19

Postby super_castle » Mar 04, 2010 18:46

yes , i can....
Irrlichtscreen in the pointer and then pu in the Freebasicscfreen.

Gruss
lassar
Posts: 300
Joined: Jan 17, 2006 1:35
Contact:

Re: FBGFX to OpenGL (Use FBGFX along with OpenGL)

Postby lassar » Jul 26, 2013 1:36

I tried it and got a error on compile.

opengl_22.bas(369) error 40: Variable not declared, GL_UNSIGNED_INT_8_8_8_8 in 'glTexSubImage2D (GL_TEXTURE_2D,0, 0, 0, TEX_WIDTH, TEX_HEIGHT,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8, p_texture_data)'
bobfresh
Posts: 27
Joined: Sep 18, 2009 13:50

Re: FBGFX to OpenGL (Use FBGFX along with OpenGL)

Postby bobfresh » Oct 25, 2013 21:06

I get the same error as lassar. Im using the latest dist of FB as of todays date. I would love to try this. any suggestions?

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests