source: trunk/packages/vizservers/nanovis/R2/src/R2Fonts.cpp @ 2953

Last change on this file since 2953 was 2953, checked in by ldelgass, 12 years ago

Remove unused global origin, make default transfer function a bit more
sensible (used to have full opacity at 0). Fix HeightMap? dtor to use delete[]
instead of free() on array allocated with new[]. Document data response in
protocol.

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <stdarg.h>
3#include <string.h>
4
5#include <fstream>
6
7#include <GL/glew.h>
8
9#include <R2/R2Fonts.h>
10#include <R2/R2FilePath.h>
11
12// constants
13const int c_nFileMagicHeader = 6666;
14
15R2Fonts::R2Fonts() :
16    _fontIndex(-1),
17    _screenWidth(512),
18    _screenHeight(512)
19{
20}
21
22R2Fonts::~R2Fonts()
23{
24    for (unsigned index = 0; index < _fonts.size(); ++index) {
25        glDeleteLists(_fonts[index]._displayLists, 256);
26        glDeleteTextures(1, &(_fonts[_fontIndex]. _fontTextureID));
27    }
28}
29
30void
31R2Fonts::setFont(const char *fontName)
32{
33    if (fontName != NULL) {
34        unsigned int i;
35        for (i = 0; i < _fonts.size(); ++i) {
36            if (strcmp(_fonts[i]._fontName, fontName) == 0) {
37                _fontIndex = i;
38                break;
39            }
40        }
41    }
42}
43
44void
45R2Fonts::addFont(const char *fontName, const char *fontFileName)
46{
47    R2FontAttributes sFont;
48   
49    loadFont(fontName, fontFileName, sFont);
50    initializeFont(sFont);
51    _fonts.push_back(sFont);
52}
53
54void
55R2Fonts::draw(const char *pString, ...) const
56{
57    va_list vlArgs;
58    char szVargsBuffer[1024];
59
60    va_start(vlArgs, pString);
61    vsprintf(szVargsBuffer, pString, vlArgs);
62
63    if (_fontIndex != -1) {
64        int length = strlen(szVargsBuffer);
65
66        glListBase(_fonts[_fontIndex]._displayLists);
67        glCallLists(length, GL_UNSIGNED_BYTE,
68                    reinterpret_cast<const GLvoid*>(szVargsBuffer));
69    }
70}
71
72void
73R2Fonts::begin()
74{
75    glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT);
76    glEnable(GL_TEXTURE_2D);
77    glBindTexture(GL_TEXTURE_2D, _fonts[_fontIndex]._fontTextureID);
78
79    glMatrixMode(GL_PROJECTION);
80    glPushMatrix();
81    glLoadIdentity();
82
83    gluOrtho2D(0.0f, _screenWidth, _screenHeight, 0.0f);
84
85    glMatrixMode(GL_MODELVIEW);
86    glPushMatrix();
87    glLoadIdentity();
88
89    glEnable(GL_BLEND);
90    glDisable(GL_DEPTH_TEST);
91}
92
93void
94R2Fonts::end()
95{
96    glBindTexture(GL_TEXTURE_2D, 0);
97
98    glMatrixMode(GL_PROJECTION);
99    glPopMatrix();
100
101    glMatrixMode(GL_MODELVIEW);
102    glPopMatrix();
103
104    glPopAttrib();
105}
106
107void
108R2Fonts::initializeFont(R2FontAttributes& attr)
109{
110    attr._displayLists = glGenLists(256);
111
112    int index;
113    for (index = 0; index < 256; ++index) {
114        R2FontAttributes::R2CharInfo& charInfo = attr._chars[index];
115        glNewList(attr._displayLists + index, GL_COMPILE);
116        if (charInfo._valid) {
117            glBegin(GL_TRIANGLE_STRIP);
118           
119            glTexCoord2f(charInfo._left, charInfo._top);
120            glVertex2i(0, 0);
121           
122            glTexCoord2f(charInfo._left, charInfo._bottom);
123            glVertex2i(0, (GLint)attr._fontHeight);
124           
125            glTexCoord2f(charInfo._right, charInfo._top);
126            glVertex2i((GLint)charInfo._width, 0);
127           
128            glTexCoord2f(charInfo._right,  charInfo._bottom);
129            glVertex2i((GLint)charInfo._width, (GLint)attr._fontHeight);
130           
131            glEnd();
132            glTranslatef(charInfo._width, 0.0f, 0.0f);
133        }
134        glEndList();
135    }
136}
137
138bool
139R2Fonts::loadFont(const char *fontName, const char *fontFileName,
140                  R2FontAttributes& sFont)
141{
142    bool bSuccess = false;
143
144    const char *path = R2FilePath::getInstance()->getPath(fontFileName);
145    if (path == NULL) {
146        return false;
147    }
148    std::ifstream fsInput(path, std::ios::binary);
149    if (fsInput) {
150        sFont._fontName = new char [strlen(fontName)+1];
151        strcpy(sFont._fontName, fontName);
152
153        // make sure this file is the correct type by checking the header
154        unsigned int uiFileId = 0;
155        fsInput.read(reinterpret_cast<char*>(&uiFileId), sizeof(unsigned int));
156        if (uiFileId == (unsigned int)c_nFileMagicHeader) {
157            // read general font/texture dimensions
158            unsigned int uiTextureWidth, uiTextureHeight, uiFontHeight;
159            uiTextureWidth = uiTextureHeight = uiFontHeight = 0;
160            fsInput.read(reinterpret_cast<char*>(&sFont._textureWidth),
161                         sizeof(unsigned int));
162            fsInput.read(reinterpret_cast<char*>(&sFont._textureHeight),
163                         sizeof(unsigned int));
164            fsInput.read(reinterpret_cast<char*>(&sFont._fontHeight),
165                         sizeof(unsigned int));
166
167            // read dimensions for each character in 256-char ASCII chart
168            for (int i = 0; i < 256; ++i) {
169                unsigned int uiSize = 0;
170
171                // top
172                fsInput.read(reinterpret_cast<char*>(&uiSize),
173                             sizeof(unsigned int));
174                sFont._chars[i]._top = static_cast<float>(uiSize) / sFont._textureHeight;
175                // left
176                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
177                sFont._chars[i]._left = static_cast<float>(uiSize) / sFont._textureWidth;
178                // bottom
179                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
180                sFont._chars[i]._bottom = static_cast<float>(uiSize) / sFont._textureHeight;
181                // right
182                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
183                sFont._chars[i]._right = static_cast<float>(uiSize) / sFont._textureWidth;
184                // enabled
185                fsInput.read(reinterpret_cast<char*>(&uiSize), sizeof(unsigned int));
186                sFont._chars[i]._valid = (uiSize != 0);
187                // width factor
188                float fWidthFactor = 1.0f;
189                fsInput.read(reinterpret_cast<char*>(&fWidthFactor), sizeof(float));
190                sFont._chars[i]._width = fWidthFactor * sFont._fontHeight;
191            }
192        }
193
194        // allocate and read the texture map
195        if (!fsInput.eof() && !fsInput.fail()) {
196            unsigned int uiArea = sFont._textureWidth * sFont._textureHeight;
197            unsigned char *pRawMap = new unsigned char[uiArea];
198            fsInput.read(reinterpret_cast<char *>(pRawMap), uiArea);
199
200            // we've only read the luminance values, but we need a luminance +
201            // alpha buffer, so we make a new buffer and duplicate the
202            // luminance values
203            unsigned char *pTexMap = new unsigned char[2 * uiArea];
204            unsigned char *pMap = pTexMap;
205            for (unsigned int i = 0; i < uiArea; ++i) {
206                *pMap++ = pRawMap[i];
207                *pMap++ = pRawMap[i];
208            }
209            delete[] pRawMap;
210            pRawMap = NULL;
211
212            // make texture map out of new buffer
213            glGenTextures(1, &sFont._fontTextureID);
214            glBindTexture(GL_TEXTURE_2D, sFont._fontTextureID);
215            glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
216                         sFont._textureWidth, sFont._textureHeight, 0,
217                         GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pTexMap);
218            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
219            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
220            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
221            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
222            glBindTexture(GL_TEXTURE_2D, 0);
223
224            delete[] pTexMap;
225            pTexMap = NULL;
226
227            bSuccess = true;
228        }
229
230        fsInput.close();
231    }
232    delete [] path;
233    return bSuccess;
234}
235
236void
237R2Fonts::resize(int width, int height)
238{
239    _screenWidth = width;
240    _screenHeight = height;
241}
242
Note: See TracBrowser for help on using the repository browser.