Tutorials for 2D OpenGL
Tutorials for 2D OpenGL
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
Thanks,
Lster
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.
I haven't finished it yet, but as soon as I got rid of this bug, I will post it in the Projects forum.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
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
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
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
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.
Thanks,
Lster
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)
Lster
Well, you need to load a texture and tell GL about it.
The easiest method is to use the CreateTexture function from lillo.
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
hrm, it does need to be updated, doesn't it?
Try this instead, works with the old and new buffers.
Edit: Added in checks for structures for older versions of fb so they won't puke.
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.