Qbasicnews.com

Full Version: OpenGL Text doesn't work...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok, this code ALMOST works... I get a pink thick stripe on the screen that's aprox 300 pixels wide (it shoudl be text instead 8) )

Here's the code: (If someone could fix I would kiss that persons ... hmm, nevermind) :lol:

Code:
defint a-z

#include "SDL/sdl.bi"
#include "GL/gl.bi"
#include "GL/glu.bi"

Type Model3d
    m_nIndices as integer
    m_nVertices as integer
    m_pVertex as GlFloat ptr
    m_pTexCoord as GlFloat ptr
    m_pIndex as GLuint ptr
end type

Declare sub Load_Font_Texture ()
Declare sub Build_Fonts ()
Declare sub KillFont ()
Declare sub glPrint(byval x as GLint, byval  y as GLint, byref xstring  as string , byval xset as integer)
Declare sub Init_OpenGL (SDLscreen as SDL_Surface ptr)
Declare sub Draw_OpenGL_Scene (vb as Model3d ptr)

Dim shared font_texture as GLuint
Dim shared floop        as GLuint
Dim shared fbase        as GLuint
Dim vpage               as SDL_Surface ptr
Dim Finished as integer

Init_OpenGL vpage
Build_Fonts

Finished = 0

Do
   Draw_OpenGL_Scene vb
   SDL_GL_SwapBuffers
   SDL_PumpEvents
Loop Until inkey$ <> ""
SDL_Quit
End

Sub Init_OpenGL (SDLscreen as SDL_Surface ptr)
  
   Dim Result  as unsigned integer
   dim SDL_flags as integer
   Dim FOVy    as double
   Dim Aspect  as double
   Dim znear   as double
   Dim zfar    as double
  
   SDL_flags = SDL_HWSURFACE
   SDL_flags = SDL_flags or SDL_OPENGL
   SDL_flags = SDL_flags or SDL_FULLSCREEN
  
   Result = SDL_Init(SDL_INIT_EVERYTHING)
   If Result <> 0 Then
      End 1
   End if
  
   SDLscreen = SDL_SetVideoMode(800, 600, 32, SDL_flags)
   IF SDLscreen = 0 then
      SDL_Quit
   End if
  
   SDL_ShowCursor SDL_ENABLE
   SDL_WM_SetCaption "OpenGL Text", ""
      
   glViewport 0, 0, 800, 600
   glMatrixMode GL_PROJECTION
   glLoadIdentity
  
   FOVy = 80/2
   Aspect = 800 / 600
   znear = 1
   zfar = 200
  
   gluPerspective FOVy, Aspect, znear, zfar
   glMatrixMode GL_MODELVIEW
   glLoadIdentity
  
   glShadeModel GL_SMOOTH
   glClearColor 0.0, 0.0, 0.0, 0.0
   glClearDepth 1.0
   glEnable GL_DEPTH_TEST
  
   glDepthFunc GL_LEQUAL
   glBlendFunc GL_SRC_ALPHA, GL_ONE
   glEnable GL_COLOR_MATERIAL
   glEnable GL_TEXTURE_2D
   glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
End Sub

Sub Draw_OpenGL_Scene (vb as Model3d ptr)
  
   glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
   glPushMatrix
   glLoadIdentity
  
   glColor3f 1.0, 0.5, 1.0
   glPrint 0, 460, "Text in OpenGL mode", 1
   glColor4f 1.0, 1.0, 1.0, 1.0
  
   glFlush
End Sub

Sub Load_Font_Texture ()
   Dim Filename$
   Dim Font_Image as SDL_Surface ptr
  
   Filename$ = "font24inv.bmp"
   font_image = SDL_LoadBMP(Filename$)
   If font_image = 0 then
      SDL_FreeSurface font_image
      SDL_Quit
      Screen 13
      Print "Error"
      Sleep
      End -1
   End If
  
   Dim ptrbyte as ubyte ptr
   Dim itextsize as integer
   Dim iptr as integer
  
   ptrBye = font_image->Pixels
   iTextSize = (font_image->W * font_image->H * 3) - 1
   for iPtr = 0 to iTextSize step 3
      swap *(ptrByte + iPtr), *(ptrByte + iPtr + 2)
   next iPtr
  
   glGenTextures 1, @font_texture
  
   glBindTexture GL_TEXTURE_2D, @font_texture
   glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
   glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
   glTexImage2D GL_TEXTURE_2D, 0, 3, font_image->W, font_image->H, 0, GL_RGB, GL_UNSIGNED_BYTE, font_image->Pixels
  
   Build_Fonts
  
   SDL_FreeSurface font_image
End Sub

Sub Build_Fonts ()
  
   Dim cx      as single
   Dim cy      as single
   dim floop   as integer
  
   const Factor = 1/16
  
   fbase = glGenLists(256)
   glBindTexture GL_TEXTURE_2D, @font_texture
  
   For floop = 0 to 255
      cx = (floop mod 16) / 16.0
      cy = (floop \ 16)   / 16.0
      
      glNewList fbase + floop, GL_COMPILE
         glBegin GL_QUADS
            glTexCoord2f cx, (1 - cy) - Factor
            glVertex2i 0, 0
            glTexCoord2f cx + Factor, (1 - cy) - Factor
            glVertex2i 16, 0
            glTexCoord2f cx + Factor, 1 - cy
            glVertex2i 16,16
            glTexCoord2f cx, 1 - cy
            glVertex2i 0, 16
         glEnd
         glTranslatef 16,0,0
      glEndList
   next floop
End Sub

Sub KillFont ()
   glDeleteLists fbase, 256
End Sub

Sub glPrint(BYVAL x as GLint, BYVAL y as GLint, BYREF xstring as string, BYVAL xset as integer)
   Dim leng as integer
   leng = len(xstring)
   Dim SomeString as byte ptr
   SomeString = StrPtr(xstring)
   Dim i as integer
   Dim chars(leng) as unsigned byte
   for i = 0 to Leng
      chars(i) = SomeString[i] - 32
   next i
  
   if xset > 1 then
      xset = 1
   end if
  
   glBindTexture GL_TEXTURE_2D, @font_texture
   glDisable GL_DEPTH_TEST
   glMatrixMode GL_PROJECTION
   glPushMatrix
   glLoadIdentity
   glOrtho 0, 800, 0, 600, -1, 1
   glMatrixMode GL_MODELVIEW
   glPushMatrix
   glLoadIdentity
   glTranslated x, y, 0
   glEnable GL_BLEND
   glListBase fbase + (128 * xset)
   glCallLists Leng, GL_UNSIGNED_BYTE, @chars(0)
   glDisable GL_BLEND
   glMatrixMode GL_PROJECTION
   glPopMatrix
   glEnable GL_DEPTH_TEST
End Sub
Hi

You must call Load_Font_Texture () instead of Build_Fonts.
Also this code :

ptrBye = font_image->Pixels
iTextSize = (font_image->W * font_image->H * 3) - 1
for iPtr = 0 to iTextSize step 3
swap *(ptrByte + iPtr), *(ptrByte + iPtr + 2)
next iPtr

crashes my puter; Some crazy pointer obviously.
And on the main loop you must use the SDL way :

Finished = 0
do
SDL_GetEvents(Finished)
keys = SDL_GetKeyState(0)
if peek(keys + SDLK_UP) then Finished = -1

.....

SDL_PumpEvents
loop until Finished

Not inkey()

(Look at the GLFont.bas example)

Greets
Tio Bit
I figured out the problem myself before you posted, but thanks anyway...

I didn't know FB was case-sensitive? I changed this
Code:
ptrBye = Font_Image->Pixels
iTextSize = (Font_Image->W * Font_Image->H * 3) - 1

to this:

Code:
ptrBye = font_image->Pixels
iTextSize = (font_image->W * font_image->H * 3) - 1

...and it worked. And I also added
Load_Font_Textures
Build_Fonts
to the main code...
http://www.freebasic.net/about.php?section=features#1
Quote:FreeBASIC is case-insensitive

Sure you dident change anything else?
No FB is not case-sensitive, the problem it that "ptrBye" should be "ptrByte", always use OPTION EXPLICIT when using libraries, a single typo and nothing will work and it becomes quite hard to find where the problem is.

In QB it was worse, there was no EXPLICIT and also if there was a function name typo, QB would "guess" you were trying to access an undeclared array and would allocate it automagically..
I will always use Option Explicit from now on... :wink: