Class: FontStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gl_tail/font_store.rb

Class Method Summary collapse

Class Method Details

.generate_fontObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gl_tail/font_store.rb', line 65

def self.generate_font
  self.generate_textures
  return

  # Ignore the rest for now, used only to create font.bin

#    glPushMatrix
#    glViewport(0, 0, 8, 15)
#    glMatrixMode(GL_PROJECTION)
#    glLoadIdentity()

#    glOrtho(0, 8, 13, 0, -1.0, 1.0)

#    glMatrixMode(GL_MODELVIEW)
#    glLoadIdentity()
#    glTranslate(0, 0, 0)

#    glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ( [1.0, 1.0, 1.0, 1.0]  ) )
#    32.upto(255) do |c|
#      glClearColor(0.0, 0.0, 0.0, 1.0)
#      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

#      glRasterPos(0,0)
#      begin
#        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c)
#      rescue RangeError
#        glutBitmapCharacterX(c)
#      end

#      glBindTexture(GL_TEXTURE_2D, @textures[c])
#      glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 13, 8, 13, 0)


#      glBindTexture(GL_TEXTURE_2D, 0)
#    end
#    glClearColor(0.0, 0.0, 0.0, 1.0)
#    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

#    glPopMatrix

end

.generate_texturesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gl_tail/font_store.rb', line 8

def self.generate_textures
  if @font_texture.nil?
    @font_texture = glGenTextures(1)[0]

    File.open("#{File.dirname(__FILE__)}/font.bin") do |f|
      @font = Marshal.load(f)
    end

    font_data = []

    # Add missing pixels to increase height by 3
    32.upto(255) do |c|
      @font[c] += @font[c] + [0,0,0].pack("C*") * 24
    end

    0.upto(196607) do |i|
      font_data[i] = 0
    end

    # Re-order bitmap data into one 256x256 texture
    32.upto(255) do |c|
      row = (c - 32) / 16
      col = (c - 32) % 16

      offset = row * 256*16*3 + col*8*3
      0.upto(15) do |y|
        0.upto(7) do |x|
          font_data[offset + y*256*3 + x*3 +0] = @font[c][y*8*3 + x*3 + 0]
          font_data[offset + y*256*3 + x*3 +1] = @font[c][y*8*3 + x*3 + 1]
          font_data[offset + y*256*3 + x*3 +2] = @font[c][y*8*3 + x*3 + 2]
        end
      end

    end

#      File.open("lib/font_256.bin", "wb") do |f|
#        Marshal.dump(font_data, f)
#      end

    font_data = font_data.pack("C*")

    glBindTexture(GL_TEXTURE_2D, @font_texture)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
#      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
#      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
#      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
#      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    #    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
#    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, font_data)

    glBindTexture(GL_TEXTURE_2D, 0)
  end
end

.render_char(engine, c, pos) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gl_tail/font_store.rb', line 107

def self.render_char(engine, c, pos)
  char_size = engine.char_size

  base = c-32

  offsetx = ((base%16) ) / (32.0)
  offsety = ((base/16) * 16) / (256.0)

  width   =  8 / 256.0
  height  = 13 / 256.0

  pos_offset = char_size * pos

  glTexCoord2f(offsetx,offsety)
  glNormal3f(1.0, 1.0, 0.0)
  glVertex3f(pos_offset, 0, 0.0)

  glTexCoord2f(offsetx+width,offsety)
  glNormal3f(1.0, 1.0, 0.0)
  glVertex3f(pos_offset + char_size, 0.0, 0.0)

  glTexCoord2f(offsetx+width,offsety + height)
  glNormal3f(1.0, 1.0, 0.0)
  glVertex3f(pos_offset + char_size, engine.line_size, 0.0)

  glTexCoord2f(offsetx,offsety + height)
  glNormal3f(1.0, 1.0, 0.0)
  glVertex3f(pos_offset, engine.line_size, 0.0)
end

.render_string(engine, txt) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gl_tail/font_store.rb', line 138

def self.render_string(engine, txt)
  glPushMatrix
  glEnable(GL_BLEND)
  glBlendFunc(GL_ONE, GL_ONE)
  glBindTexture(GL_TEXTURE_2D, @font_texture)

  unless BlobStore.has(txt)
    list = glGenLists(1)
    glNewList(list, GL_COMPILE)
    glBegin(GL_QUADS)
    pos = 0
    txt.each_byte do |c|
      self.render_char(engine, c, pos)
      pos += 1
    end
    glEnd
    glEndList
    BlobStore.put(txt,list)
  end
  glCallList(BlobStore.get(txt))

  glBindTexture(GL_TEXTURE_2D, 0)
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  glDisable(GL_BLEND)
  glPopMatrix
end