Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / GLFontRenderer.cpp
1 /*
2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman,  pierre@codercorner.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 #include "stdafx.h"
16
17 #include <math.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <windows.h>
21
22 #include "GLFontData.h"
23 #include "GLFontRenderer.h"
24
25 bool GLFontRenderer::m_isInit=false;
26 unsigned int GLFontRenderer::m_textureObject=0;
27 int GLFontRenderer::m_screenWidth=640;
28 int GLFontRenderer::m_screenHeight=480;
29 float GLFontRenderer::m_color[4]={1.0f, 1.0f, 1.0f, 1.0f};
30
31 bool GLFontRenderer::init()
32 {
33         glGenTextures(1, &m_textureObject);
34         if(m_textureObject == 0) return false;
35
36         glBindTexture(GL_TEXTURE_2D, m_textureObject);
37         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
38         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
39
40         // expand to rgba
41         unsigned char* pNewSource = new unsigned char[OGL_FONT_TEXTURE_WIDTH*OGL_FONT_TEXTURE_HEIGHT*4];
42         for(int i=0;i<OGL_FONT_TEXTURE_WIDTH*OGL_FONT_TEXTURE_HEIGHT;i++)
43         {
44                 pNewSource[i*4+0]=255;
45                 pNewSource[i*4+1]=255;
46                 pNewSource[i*4+2]=255;
47                 pNewSource[i*4+3]=OGLFontData[i];
48         }
49         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, OGL_FONT_TEXTURE_WIDTH, OGL_FONT_TEXTURE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, pNewSource);
50         delete[] pNewSource;
51
52         return true;
53 }
54
55 void GLFontRenderer::print(float x, float y, float fontSize, const char* pString, bool forceMonoSpace, int monoSpaceWidth, bool doOrthoProj)
56 {
57 //      x = x*m_screenWidth;
58 //      y = y*m_screenHeight;
59         fontSize = fontSize*m_screenHeight;
60
61         if(!m_isInit)
62         {
63                 m_isInit = init();
64         }
65
66         unsigned int num = (unsigned int)strlen(pString);
67         if(m_isInit && num > 0)
68         {
69                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
70                 glDisable(GL_DEPTH_TEST);
71                 glDisable(GL_LIGHTING);
72
73                 glEnable(GL_TEXTURE_2D);
74                 glBindTexture(GL_TEXTURE_2D, m_textureObject);
75
76
77                 if(doOrthoProj)
78                 {
79                         glMatrixMode(GL_PROJECTION);
80                         glPushMatrix();
81                         glLoadIdentity();
82                         glOrtho(0, m_screenWidth, 0, m_screenHeight, -1, 1);
83                 }
84                 glMatrixMode(GL_MODELVIEW);
85                 glPushMatrix();
86                 glLoadIdentity();
87
88                 glEnable(GL_BLEND);
89
90                 glColor4f(m_color[0], m_color[1], m_color[2], m_color[3]);
91
92                 const float glyphHeightUV = ((float)OGL_FONT_CHARS_PER_COL)/OGL_FONT_TEXTURE_HEIGHT*2-0.01f;
93
94                 float translate = 0.0f;
95
96                 float* pVertList = new float[num*3*6];
97         float* pTextureCoordList = new float[num*2*6];
98                 int vertIndex = 0;
99         int textureCoordIndex = 0;
100
101                 float translateDown = 0.0f;
102                 unsigned int count = 0;
103
104                 for(unsigned int i=0;i<num; i++)
105                 {
106                         const float glyphWidthUV = ((float)OGL_FONT_CHARS_PER_ROW)/OGL_FONT_TEXTURE_WIDTH;
107
108                         if (pString[i] == '\n') {
109                                 translateDown-=0.005f*m_screenHeight+fontSize;
110                                 translate = 0.0f;
111                                 continue;
112                         }
113
114                         int c = pString[i]-OGL_FONT_CHAR_BASE;
115                         if (c < OGL_FONT_CHARS_PER_ROW*OGL_FONT_CHARS_PER_COL) {
116
117                                 count++;
118
119                                 float glyphWidth = (float)GLFontGlyphWidth[c];
120                                 if(forceMonoSpace){
121                                         glyphWidth = (float)monoSpaceWidth;
122                                 }
123                                 
124                                 glyphWidth = glyphWidth*(fontSize/(((float)OGL_FONT_TEXTURE_WIDTH)/OGL_FONT_CHARS_PER_ROW))-0.01f;
125
126                                 float cxUV = float((c)%OGL_FONT_CHARS_PER_ROW)/OGL_FONT_CHARS_PER_ROW+0.008f;
127                                 float cyUV = float((c)/OGL_FONT_CHARS_PER_ROW)/OGL_FONT_CHARS_PER_COL+0.008f;
128
129                                 pTextureCoordList[textureCoordIndex++] = cxUV;
130                                 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
131                                 pVertList[vertIndex++] = x+0+translate;
132                                 pVertList[vertIndex++] = y+0+translateDown;
133                                 pVertList[vertIndex++] = 0;
134
135                                 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
136                                 pTextureCoordList[textureCoordIndex++] = cyUV;
137                                 pVertList[vertIndex++] = x+fontSize+translate;
138                                 pVertList[vertIndex++] = y+fontSize+translateDown;
139                                 pVertList[vertIndex++] = 0;
140
141                                 pTextureCoordList[textureCoordIndex++] = cxUV;
142                                 pTextureCoordList[textureCoordIndex++] = cyUV;
143                                 pVertList[vertIndex++] = x+0+translate;
144                                 pVertList[vertIndex++] = y+fontSize+translateDown;
145                                 pVertList[vertIndex++] = 0;
146
147                                 pTextureCoordList[textureCoordIndex++] = cxUV;
148                                 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
149                                 pVertList[vertIndex++] = x+0+translate;
150                                 pVertList[vertIndex++] = y+0+translateDown;
151                                 pVertList[vertIndex++] = 0;
152
153                                 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
154                                 pTextureCoordList[textureCoordIndex++] = cyUV+glyphHeightUV;
155                                 pVertList[vertIndex++] = x+fontSize+translate;
156                                 pVertList[vertIndex++] = y+0+translateDown;
157                                 pVertList[vertIndex++] = 0;
158
159                                 pTextureCoordList[textureCoordIndex++] = cxUV+glyphWidthUV;
160                                 pTextureCoordList[textureCoordIndex++] = cyUV;
161                                 pVertList[vertIndex++] = x+fontSize+translate;
162                                 pVertList[vertIndex++] = y+fontSize+translateDown;
163                                 pVertList[vertIndex++] = 0;
164
165                                 translate+=glyphWidth;
166                         }
167                 }
168
169                 glEnableClientState(GL_VERTEX_ARRAY);
170                 glVertexPointer(3, GL_FLOAT, 0, pVertList);
171                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
172                 glTexCoordPointer(2, GL_FLOAT, 0, pTextureCoordList);
173                 glDrawArrays(GL_TRIANGLES, 0, count*6);
174                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
175                 glDisableClientState(GL_VERTEX_ARRAY);
176
177                 delete[] pVertList;
178                 delete[] pTextureCoordList;
179
180                 if(doOrthoProj)
181                 {
182                         glMatrixMode(GL_PROJECTION);
183                         glPopMatrix();
184                 }
185                 glMatrixMode(GL_MODELVIEW);
186                 glPopMatrix();
187                 
188                 glEnable(GL_DEPTH_TEST);
189                 glEnable(GL_LIGHTING);
190                 glDisable(GL_TEXTURE_2D);       
191                 glDisable(GL_BLEND);
192         }
193 }
194
195 void GLFontRenderer::setScreenResolution(int screenWidth, int screenHeight)
196 {
197         m_screenWidth = screenWidth;
198         m_screenHeight = screenHeight;
199 }
200
201 void GLFontRenderer::setColor(float r, float g, float b, float a)
202 {
203         m_color[0] = r;
204         m_color[1] = g;
205         m_color[2] = b;
206         m_color[3] = a;
207 }