G2: OpenGL 2D Library (Very Easy to Use!!)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

G2: OpenGL 2D Library (Very Easy to Use!!)

Post by KristopherWindsor »

Based on the code at http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL, I have made a small library for hardware accelerated 2D graphics!

It has commands similar to FBGFX's Line, Circle, Pset, Draw String, Put, and Screen, and can create textures directly from a bitmap or FBGFX buffer.
It has full support for alpha blending through rgba() in the primitive functions, and an alpha value argument in g2Put().

Image

This uses the texture making code from
http://www.freebasic.net/forum/viewtopic.php?t=8591
And the font reading code from D.J. Peters' code here:
http://www.freebasic.net/forum/viewtopic.php?t=10351

You can download the library and three samples here:
http://fusionware.ourproject.org/itech/ ... /g2/g2.zip

EDIT:
- Now comes with FBGL (my version). FBGL is a set of macros that can be set to either use G2 or FBGFX (at compile time). This is an optional extension to G2 that works with most of its features.
- Now comes with FBGFXE, a set of 3 functions (Multiput, Loadimage, and DrawString) that adds to the functionality of FBGFX. This is only used with FBGL when adjusted to work without hardware acceleration.

The download includes the program shown above, and a modified version of this program:
http://www.freebasic.net/forum/viewtopic.php?t=10384

Here is a sample usage (but not from the above screen shot):

Code: Select all

' G2 Demo!
' By Kristopher Windsor

#include once "g2.bas"

Using g2

Dim As Double x, y, xv, yv

g2Screen 800, 600

Randomize Timer

xv = Rnd
If xv < .6 Then xv = .6
xv *= 12

yv = Rnd
If yv < .6 Then yv = .6
yv *= 12

Do
  x += xv
  If x < 0 Then
    x = 0
    xv *= -1
  End If
  If x > 799 Then
    x = 799
    xv *= -1
  End If
  
  y += yv
  If y < 0 Then
    y = 0
    yv *= -1
  End If
  If y > 599 Then
    y = 599
    yv *= -1
  End If
  
  g2Cls
  g2CircleF x, y, 100, &HFF88FF88
  g2DrawString 4, 4, Time, &HFFFFFFFF, &H888888FF
  g2Flip
  
  Sleep 10
Loop Until Inkey = Chr(27)
:-)

Tags: kristopherwindsor_program_library kristopherwindsor_feature_program
Last edited by KristopherWindsor on Mar 22, 2008 6:40, edited 4 times in total.
phycowelder
Posts: 74
Joined: Dec 19, 2007 6:55

Post by phycowelder »

ill definitely look in to this! but after i get some were on Newton!

looks good!
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Well since you decided to go through with this, I have a suggestion. I've been thinking on writing a library for FBgfx-type OpenGL, where unlike other GL libraries released on the forums, it can be more easily configured to use software if necessary.

This is done by using the exact same API for both FBgfx and OpenGL commands, and simply having a gfx.setLib("OpenGL") or gfx.setLib("GfxLib2") call in the code.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

That's an interesting idea; if I could easily add a feature to my library to draw to textures instead of just the screen, it could essentially have all of the FBGFX features. :-)

The main reason why I have not tried this is the software mode would only run on older computers such as the P3. But if the P3 can run it in software, of course the newer computers can, too, so why use hardware at all?
Or at the other extreme, the main benefit of this library is that it can run programs that would not be fast enough in software, even on a new computer.
There is some use for an optional hardware / software switch, but I think the only advantage with such a library would be support for older computers, if they have a 3D card.
The amount of computers that are < 1.5GHz and > .5GHz and have good hardware is probably quite small. (Especially since people who had a 3D card on their P3 have probably bought a new computer since then.)

If you want to try this (I don't feel like doing it ;-)), it should be easy since the library would just have to call the code in this library, or FBGFX.
I think it would be better just to choose this or FBGFX depending on the amount of GFX used, before writing the program.
I also think that on new computers in the Particle Intensity program, the CPU may be the bottleneck. The program can call over 20 million GL functions per second on my best computer (200 particle rings * 64 graphics per ring * 14 GL calls per drawn texture * 130 FPS (the max on my best computer)). So adding code to check if the mode is set to OpenGL might noticeably slow it down on a new computer, even though it does not make any additional hardware calls.

So you can try this project, but it will not be an enhancement on most computers (I think), and it will be hard to get the OpenGL functions to exactly match the FBGFX functions.

I'm not too concerned with supporting old hardware, but anyway, Lachie will be jealous to know, I can get 20 - 60 FPS on the Particle Intensity program with a mere 600MHz and 32MB of graphics RAM. ;-)
badmrbox
Posts: 664
Joined: Oct 27, 2005 14:40
Location: Sweden
Contact:

Post by badmrbox »

Hey, this looks nice :)
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

Looks nice so far...
Once it'll be better than FB HGE (or ogremagic), I'll start using it
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Post by h4tt3n »

This is very nice. I've got a few ideas for improvement, but It'll have to wait for tonight (european time).

Cheers Michael
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

do you plan to add rotation and scale? shader support maybe?

how will rotation/scale be handled?

like

Code: Select all

put(x, y, rot, xscale, yscale)
or like

Code: Select all

setrotation 45
setscale 1, 2
put(x, y)
endscale
endrotation
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

I thought this was self-explanatory. ;-)

Code: Select all

  Declare Sub g2MultiPut (Byval x As Integer, Byval y As Integer, Byval graphic As Uinteger, Byval scale As Double = 1, _
    Byval angle As Double = 0, Byval alpha As Integer = 255)
Of course scale and rotation are added. :-)
In the currently posted version, this called g2Put(), but I have renamed it to g2Multiput(), and added g2Put() to work without rotation.
I'll post this version later today, along with an attempt to solve anonymous1337's suggestion with a few macros.
Of course, I am not attempting to compete with HGE, but this is an easy-to-use library, so more people can try it. ;-)
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

Code: Select all

#define fbgl_soft
'#define fbgl_hard
I made something like anonymous1337 suggested, but I decided to use compile-time macros instead of real-time tests.
It was easier that way. ;-)

Code: Select all

#macro fbglCircleF (x, y, r, c)
  #ifdef fbgl_soft
    Circle (x, y), r, c,,,, F
  #Endif
  #ifdef fbgl_hard
    g2.g2CircleF x, y, r, c
  #Endif
#endmacro
There are a few things it doesn't support, such as a background color for Draw String, and the hardware and software versions use a different font size, but it could work well for some programs.

Here is the demo of FBGL (which I forgot to put in the download :-P):

Code: Select all

' FBGL Demo!
' By Kristopher Windsor

#include once "fbgl.bas"

Dim As Double x, y, xv, yv

fbglScreen(800, 600, fbgl_null)

Randomize Timer

xv = Rnd
If xv < .6 Then xv = .6
xv *= 12

yv = Rnd
If yv < .6 Then yv = .6
yv *= 12

Do
  x += xv
  If x < 0 Then
    x = 0
    xv *= -1
  End If
  If x > 799 Then
    x = 799
    xv *= -1
  End If
  
  y += yv
  If y < 0 Then
    y = 0
    yv *= -1
  End If
  If y > 599 Then
    y = 599
    yv *= -1
  End If
  
  fbglScreenLock()
  fbglCls(fbgl_null)
  fbglCircleF(x, y, 100, &HFF88FF88)
  fbglDrawString(4, 4, Time, &HFFFFFFFF)
  fbglScreenUnlock()
  
  Sleep 10
Loop Until Inkey = Chr(27)
Note that passing fbgl_null to the macro for certain parameters means the macro should not use that optional parameter. (See fbglCls() for an example.)

Using FBGL, I was able to compile the Intense Particles program to run in FBGFX mode with Multiput():

Image

Since this slowed down to about 2 - 5 FPS with the particles, but the OpenGL version can keep > 100 FPS, I conclude that my 6800 video card can match Multiput() on a 50 - 100 GHz computer, with a bit of extra re-sampling. xD

While I might upgrade G2 to support even more of FBGFX's features, and consider it a library, I would probably start over if I tried to expand FBGL, so I will label this a proof of concept.
Still, it can work for small programs like the one pictured here, where CPU won't keep a good framerate, or can be tweaked to meet the needs of a larger program.

The download is in the first post. :-)
duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Post by duke4e »

Sorry, my bad. Didn't see that "scale". Great work, keep it up ;)
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Awesome! I'm glad you took a liking to the FBGL idea.

This should probably be used by more people now that it has software/hardware support. Just a simple recompile and people like Lachie will stop complaining >.>;;
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Post by sir_mud »

no they won't
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

I like this idea very much, and will gladly use it. I would like it more if it were more like fbgfx, though; I use image buffers a lot and those would be really helpful - probably the most important thing so far. (On a side note, does anyone have routines for converting between OpenGL textures and fbgfx Image buffers? Because that might be useful with this now...)

Also, clipping. So far I don't see clipping. Right now I'm working on a GUI library, for example, and I really need clipping support for the windows. It would be nice to be able to make a GUI with hardware acceleration, but without clipping it's not possible.

I've long wanted to be able to use hardware acceleration but because I don't have the time to learn to use OpenGL I can't. I'll watch this project with interest, and I encourage you to keep improving on it :)
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

notthecheatr wrote:Does anyone have routines for converting between OpenGL textures and fbgfx Image buffers?
Yes, this is the g2ImageCreate function. It's not exactly equivalent to the FBGFX function of the same name, but there is no point in creating an empty texture if you can't edit it. ;-)

Clipping should be possible because I can specify the points on the texture that correspond to the points on the polygon, but I haven't tried it yet.

Someday I will write a list of FBGFX functions and attempt to support all of them in GL. ;-)
Post Reply