[Gambas-user] Rendering text with OpenGL example

terco IDE tercoide at ...67...
Mon Jan 13 12:35:24 CET 2014



> >> Yes (using OpenGL), but it's rather involved. I had an epic failure of
> >> hardware and human logic this week and am just wrapping up restoring
> >> sanity to my server and workstation, but give me a day or so and I'll
> >> give you the source code.
> >>
> >> It's my understanding that OpenGL has no native support of rendering
> >> text, that it must be done manually. I can also provide you with a
> >> couple of "fonts" that I created from ttf.
> >>
> >> Basically you take a ttf, type out all the characters you want in GIMP
> >> (I recommend keeping them in line with an ASCII table), then save each
> >> character into a separate png file with alpha. You then load these into
> >> OpenGL textures and render them as quads. The horrible bit (as if it
> >> weren't horrible enough) is getting the spacing right for different
> >> combinations of characters. You'll want to add drop shadows, beveling,
> >> etc. in GIMP as I don't know of a way to do this in OpenGL other than
> >> perhaps by using shaders (zero knowledge there).
> >>
> >> Using Gambas's native software text rendering libraries is slower but
> >> easier.
> >>
> >> Kevin
> > Why slower? You can use Gambas to create the font characters texture,
> > and maintain a texture cache to not do that each time a character is drawn.
> >
> That's a good point. It's actually exactly what I do with the the 
> landscape tiles for my game using the DrawAlpha function. Composite each 
> tile using Gambas software operations, then offload it to an array of 
> OpenGL textures for on-screen rendering. I never thought the same could 
> be done with text.
> 
> In any case, let me know Martin if you're interested in my text 
> rendering code and bitmaps. I finally got everything up and running here.
> 
> -- 

Of course I am!
And I need no speed as my goal is a static view of a structure. I managed to get this code worrking but results are odd, because I cant make the text match the size of everithing else:

Public Sub txtRendering2D(texto As String, p As Punto3d, Optional Altura As Single = 12, Optional _color As Long = Color.Blue, _BackColor As Long = -1)
  
  Dim rectangulo As RectF
  Dim imagen As New Image, pR As New Punto3d
 
  imagen.Resize(200, 200) 'so Paint.Begin gives no error
  Paint.Begin(imagen)
    Paint.Font.Size = Altura
    Paint.Brush = Paint.Color(_color)
    rectangulo = Paint.TextSize(texto)
  Paint.End
  
  'this can't go into the Paint loop
  imagen.resize(rectangulo.w, rectangulo.h)
  imagen.Transparent(Color.Black)
  If _backcolor > 0 Then imagen.Fill(_BackColor)
  
  
  Paint.Begin(imagen)
    Paint.Font.Size = Altura
    Paint.Brush = Paint.Color(_color)
    
    
    Paint.Text(texto, 0, rectangulo.h)
    Paint.Fill
  Paint.End
  'imagen.Save("imagen.png")  ' this is to check the Paint worked (and works!)
 
  
  rotar3d(p, pr)  ' custom rotation
  gl.Enable(gl.TEXTURE_2D)                     
  Gl.TexImage2D(imagen)
  Glu.Build2DMipmaps(imagen)
  Gl.TexParameteri(Gl.TEXTURE_2D, Gl.TEXTURE_MIN_FILTER, Gl.LINEAR_MIPMAP_NEAREST)
  Gl.TexParameteri(Gl.TEXTURE_2D, Gl.TEXTURE_MAG_FILTER, Gl.LINEAR)
  gl.Enable(gl.TEXTURE_2D)                  

   

  gl.BindTexture(gl.TEXTURE_2D, hText[0])        
  Gl.Begin(Gl.QUADS)
  gl.color3f(1, 1, 1) 
  ' Bottom Left OF The Texture AND Quad
  Gl.TexCoordf(0.0, 1.0)
  Gl.Vertex3f(pr.x, pr.y, pr.z)
  ' Bottom Right OF The Texture AND Quad
  Gl.TexCoordf(1.0, 1.0)
  Gl.Vertex3f(pr.x + rectangulo.w, pr.y, pr.z)
  ' Top Right OF The Texture AND Quad
  Gl.TexCoordf(1.0, 0.0)
  Gl.Vertex3f(pr.x + rectangulo.w, pr.y + rectangulo.h, pr.z)
  ' Top Left OF The Texture AND Quad
  Gl.TexCoordf(0.0, 0.0)
  Gl.Vertex3f(pr.x, pr.y + rectangulo.h, pr.z)

 
  Gl.End()

 		 	   		  


More information about the User mailing list