Tutorials for 2D OpenGL

Game development specific discussions.
Lster
Posts: 73
Joined: May 25, 2006 14:53

Tutorials for 2D OpenGL

Postby Lster » Oct 20, 2006 14:06

I am wondering if OpenGL has libraries and functions for 2D drawing like (line, circle etc...). (Ive used OpenGL for 3D a bit though.) If so... anyone know a good tutorial on the internet?

Thanks,
Lster
ikkejw
Posts: 258
Joined: Jan 15, 2006 15:51
Location: Fryslân, the Netherlands
Contact:

Postby ikkejw » Oct 20, 2006 14:31

I'm working on a lib which can do line, put, circle etc in OpenGL :) so you don't need to do it yourself.

I haven't finished it yet, but as soon as I got rid of this bug, I will post it in the Projects forum.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Oct 20, 2006 14:33

OpenGL can naitively do points, lines, line loops, triangles and triangle strips.

From these, you can make other primitives such as circles, rectangles, n-gons, etc. There are lots of examples on the web. Try google, it's a good source.
D.J.Peters
Posts: 8203
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Oct 20, 2006 18:53

The best 2D OpenGl FreeBASIC tutorial are from stormy.
http://other.storm-master.de/ogl/index.html
It's in german but if you take a look on the pictures and line of codes
i'm sure you will find it usefull too.

Can be that the translation in english are in progress please ask stormy about that.

Joshy
TbbW
Posts: 348
Joined: Aug 19, 2005 10:08
Contact:

Postby TbbW » Oct 21, 2006 19:06

Argh!
an tutorial made in a diffrent language... lucky germans, poor non germans.
D.J.Peters
Posts: 8203
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Oct 21, 2006 20:11

TbbW wrote:Argh!
an tutorial made in a diffrent language... lucky germans, poor non germans.

The best and only FreeBASIC 2D OpenGl tutorial.

Joshy
TbbW
Posts: 348
Joined: Aug 19, 2005 10:08
Contact:

Postby TbbW » Oct 21, 2006 23:02

mjeah... babelfish got owned by that page...
google translation translated good but only half the page.
( but google #%$@ up the blocks with code in em... the linefeed got removed for some odd reson )

someone who knows german suld translate that page and source comments.
hartnell
Posts: 170
Joined: Aug 15, 2006 3:21
Contact:

Postby hartnell » Oct 21, 2006 23:20

You might get some insights on using OpenGL with FreeBASIC by checking out Basic4GL. It's not FreeBASIC, but it's a BASIC dialect that has OpenGL support built in.

You also might want to consider using Allegro rather than OpenGL.

-hartnell
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » Oct 22, 2006 2:24

Translated version.

Go to Google, type in search "storm-master ogl"

find link for site, click on "translate this page"

Very nice tutorial.

JohnB
Lster
Posts: 73
Joined: May 25, 2006 14:53

Postby Lster » Oct 22, 2006 9:16

Thanks everyone for your help... Nows the hard bit: learning OpenGL...
Lster
Posts: 73
Joined: May 25, 2006 14:53

Postby Lster » Oct 29, 2006 12:48

Ive been following the tutorial by Stormy, which is very good. Although the translation by Google is bad.

Anyway, without using any other libraries, how can I use textures.

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"
#include "inc/2d_opengl.bi"

screen 20, 32, , &h2 OR &h1

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glViewport(0.0, 0.0, 1024, 768)
glOrtho(0, 1024, 768, 0, -128, 128)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_TEXTURE_2D)
glLoadIdentity()

glDepthFunc(GL_LESS)
glEnable(GL_ALPHA_TEST)
glAlphaFunc(0, 1)

' Load texture

do
    glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT)
   
    glBindTexture GL_TEXTURE_2D, texture
    glBegin GL_QUADS
        glTexCoord2f 0, 0
        glVertex2i 0, 32
       
        glTexCoord2f 1, 0
        glVertex2i 32, 32
       
        glTexCoord2f 1, 1
        glVertex2i 32, 0
       
        glTexCoord2f 0, 1
        glVertex2i 0, 0
    glEnd
   
    flip
    screenSync
loop until multikey(&h1)


Thanks,
Lster
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Oct 29, 2006 17:39

Well, you need to load a texture and tell GL about it.

The easiest method is to use the CreateTexture function from lillo.

Code: Select all

'lillo
Function CreateTexture( Byval buffer As Any Ptr, Byval flags As Integer = 0 ) As GLuint
                Redim dat(0) As Uinteger
                Dim p As Uinteger Ptr
                Dim As Integer w, h, x, y, col
                Dim tex As GLuint
                Dim As GLenum format, minfilter, magfilter
               
                CreateTexture = 0
               
                w = cptr(Short Ptr, buffer)[0] Shr 3
                h = cptr(Short Ptr, buffer)[1]
               
                If( (w < 64) Or (h < 64) ) Then
                        End -11'Exit Function
                End If
                If( (w And (w-1)) Or (h And (h-1)) ) Then
                        'Width/height not powers of 2
            'Exit Function
            End -12
                End If
               
                Redim Dat(w * h) As Uinteger
                p = @Dat(0)
               
                glGenTextures 1, @tex
                glBindTexture GL_TEXTURE_2D, tex
               
                For y = h-1 To 0 Step -1
                        For x = 0 To w-1
                                col = Point(x, y, buffer)
                                '' Swap R and B so we can use the GL_RGBA texture format
                                col = RGBA(col And &hFF, (col Shr 8) And &hFF, (col Shr 16) And &hFF, col Shr 24)
               
                If( flags And TEX_HASALPHA ) Then
                                        *p = col
                                Else
                                        If( (flags And TEX_MASKED) And (col = &hFF00FF) ) Then
                                                *p = 0
                                        Else
                                                *p = col Or &hFF000000
                                        End If
                                End If
                                p += 1
                        Next x
                Next y
               
                If( flags And ( TEX_MASKED Or TEX_HASALPHA ) ) Then
                        format = GL_RGBA
                Else
                        format = GL_RGB
                End If
               
                If( flags And TEX_NOFILTER ) Then
                        magfilter = GL_NEAREST
                Else
                        magfilter = GL_LINEAR
                End If
               
                If( flags And TEX_MIPMAP ) Then
                        gluBuild2DMipmaps GL_TEXTURE_2D, format, w, h, GL_RGBA, GL_UNSIGNED_BYTE, @dat(0)
                        If( flags And TEX_NOFILTER ) Then
                                minfilter = GL_NEAREST_MIPMAP_NEAREST
                        Else
                                minfilter = GL_LINEAR_MIPMAP_LINEAR
                        End If
                Else
                        glTexImage2D GL_TEXTURE_2D, 0, format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, @dat(0)
                        minfilter = magfilter
                End If
                glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter
                glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter
               
                CreateTexture = tex
       
End Function
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Oct 30, 2006 3:41

doesn't that only work with old style buffers?
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Oct 30, 2006 3:54

hrm, it does need to be updated, doesn't it?

Try this instead, works with the old and new buffers.

Code: Select all

#Define   TEX_MASKED           &h1
#Define   TEX_MIPMAP           &h2
#Define   TEX_NOFILTER         &h4
#Define   TEX_HASALPHA         &h8




#IfNDef _OLD_HEADER
type _OLD_HEADER field = 1
   bpp : 3 as ushort
   width : 13 as ushort
   height as ushort
end type
#EndIf




#IfNDef PUT_HEADER
type PUT_HEADER field = 1
   union
      old as _OLD_HEADER
      type as uinteger
   end union
   bpp as integer
   width as uinteger
   height as uinteger
   pitch as uinteger
   _reserved(1 to 12) as ubyte
end type
#EndIf




#IfNDef PUT_HEADER_NEW
#define PUT_HEADER_NEW      &h7
#EndIf




Function CreateTexture _
   (   _
   Byval Buffer As PUT_HEADER Ptr       , _
   Byval Flags  As Uinteger       = NULL  _
   ) As GLuint
   
   
   Redim As Uinteger           dat( 0 )
   Dim As Uinteger Ptr         pPixels
   Dim As Integer              w, h, x, y, col, i
   Dim As GLuint               rTex
   Dim As GLenum               format, minfilter, magfilter
   
   
   If ( Buffer->Type = PUT_HEADER_NEW ) Then
     
      w = Buffer->Width
      h = Buffer->Height
     
   Else
     
      w = Buffer->Old.Width
      h = Buffer->Old.Height
     
   End If
   
   
   If ( ( w < 8 ) Or ( h < 8 ) ) Then AbortCode( ERROR_TEXTURE Or ERROR_BAD_SIZE, TextureCreate_Abort )
   If ( ( w And ( w - 1 ) ) Or ( h And ( h - 1 ) ) ) Then AbortCode( ERROR_TEXTURE Or ERROR_BAD_SIZE, TextureCreate_Abort )
   
   Redim As Uinteger           dat( w * h )
   pPixels = @dat( 0 )
   
   
   glGenTextures       1, @rTex
   AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
   
   glBindTexture       GL_TEXTURE_2D, rTex
   AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
   
   
   i = 0
   For y = ( h - 1 ) To 0 Step -1
      For x = 0 To ( w - 1 )
         
         col = Point( x, y, Buffer )
         
         col = BGRA_To_RGBA( col )
         
         If ( Flags And TEX_HASALPHA ) Then
            pPixels[ i ] = col
         Else
            If ( ( Flags And TEX_MASKED ) And ( ( col And &HFFFFFF ) = &hFF00FF ) ) Then
               pPixels[ i ] = 0
            Else
               pPixels[ i ] = col Or &hFF000000
            End If
         End If
         
         i += 1
         
      Next x
   Next y
   
   
   If ( Flags And ( TEX_MASKED Or TEX_HASALPHA ) ) Then
      format = GL_RGBA
   Else
      format = GL_RGB
   End If
   
   
   If ( Flags And TEX_NOFILTER ) Then
      magfilter = GL_NEAREST
   Else
      magfilter = GL_LINEAR
   End If
   
   
   If ( Flags And TEX_MIPMAP ) Then
     
      gluBuild2DMipmaps GL_TEXTURE_2D, format, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pPixels
      AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
     
      If ( Flags And TEX_NOFILTER ) Then
         minfilter = GL_NEAREST_MIPMAP_NEAREST
      Else
         minfilter = GL_LINEAR_MIPMAP_LINEAR
      End If
     
   Else
     
      glTexImage2D GL_TEXTURE_2D, 0, format, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pPixels
      AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
     
      minfilter = magfilter
     
   End If
   
   
   glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter
   AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
   glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter
   AbortGL( ERROR_TEXTURE, TextureCreate_Abort, __LINE__ )
   
   
   Return rTex
   
   
   TextureCreate_Abort:
   
   
   If ( rTex ) Then glDeleteTextures 1, @rTex
   
   Return NULL
   
   
End Function





Edit: Added in checks for structures for older versions of fb so they won't puke.
Last edited by 1000101 on Oct 30, 2006 22:04, edited 1 time in total.
Lster
Posts: 73
Joined: May 25, 2006 14:53

Postby Lster » Oct 30, 2006 13:10

...
Last edited by Lster on Nov 06, 2007 20:46, edited 1 time in total.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 3 guests