16#include <Inventor/So.h>
24 void renderText(
const LabelText& labelText);
25 void renderLabel(
const Label& label);
26 void glDrawString(
const SbVec2f& screenPosition,
const LabelText& labelText);
27 void applyLabelOffset(GLfloat& screenX, GLfloat& screenY,
const GLfloat& textWidth,
const GLfloat& textAscender,
const LabelText& labelText);
28 void fillScreenBoundingBox(
Label& label);
30 void setupRenderState();
31 void resetRenderState();
33 void setupRasterPositionStateScreen();
34 void resetRasterPositionState();
36 GLfloat getScreenTextWidth(
const LabelText& labelText);
37 GLfloat getScreenTextAscender(
const LabelText& labelText);
39 SbVec3f getScreenCoordinate(
const SbVec3f& worldCoordinate);
40 SbVec4f getColorWithAlpha(
const SbColor& color,
float alpha) {
return SbVec4f(color[0], color[1], color[2], alpha); }
42 void setupRenderState()
45 glPushAttrib(GL_ALL_ATTRIB_BITS);
46 glDisable(GL_LIGHTING);
49 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
50 glDepthFunc(GL_LEQUAL);
52 glPushAttrib(GL_LINE_BIT);
55 void resetRenderState()
63 glEnable(GL_LIGHTING);
68 void setupRasterPositionStateScreen()
70 glMatrixMode(GL_MODELVIEW);
73 glMatrixMode(GL_PROJECTION);
75 glPushAttrib(GL_ALL_ATTRIB_BITS);
77 glDisable(GL_LIGHTING);
78 glDisable(GL_CULL_FACE);
85 glGetIntegerv(GL_VIEWPORT, viewport);
87 glOrtho(0, viewport[2], 0, viewport[3], 0.f, 1.f);
89 glMatrixMode(GL_MODELVIEW);
92 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95 void resetRasterPositionState()
99 glMatrixMode(GL_PROJECTION);
101 glMatrixMode(GL_MODELVIEW);
105 void applyLabelOffset(GLfloat& screenX, GLfloat& screenY,
const GLfloat& textWidth,
const GLfloat& textAscender,
const LabelText& labelText)
107 screenX -= (textWidth * .5f);
108 screenY -= (textAscender * .5f);
120 void renderText(
const LabelText& labelText)
124 GLboolean isValidPosition;
125 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
129 GLfloat currentRasterPosition[4];
130 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
131 auto screenX = currentRasterPosition[0];
132 auto screenY = currentRasterPosition[1];
133 const auto screenZ = currentRasterPosition[2];
135 setupRasterPositionStateScreen();
137 glTranslatef(0, 0, -screenZ);
139 const auto textWidth = getScreenTextWidth(labelText);
140 const auto textAscender = getScreenTextAscender(labelText);
142 applyLabelOffset(screenX, screenY, textWidth, textAscender, labelText);
144 const SbVec2f screenPosition(screenX, screenY);
146 glDrawString(screenPosition, labelText);
148 resetRasterPositionState();
152 void renderLabel(
const Label& label)
163 glColor4fv(tickColor.getValue());
165 glBegin(GL_LINE_STRIP);
167 glVertex3f(tickStart[0], tickStart[1], tickStart[2]);
168 glVertex3f(tickEnd[0], tickEnd[1], tickEnd[2]);
177 GLfloat currentRasterPosition[4];
178 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
179 const auto screenZ = currentRasterPosition[2];
181 setupRasterPositionStateScreen();
183 glTranslatef(0, 0, -screenZ);
187 resetRasterPositionState();
191 void fillScreenBoundingBox(
Label& label)
199 GLboolean isValidPosition;
200 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
206 GLfloat currentRasterPosition[4];
207 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
208 auto screenX = currentRasterPosition[0];
209 auto screenY = currentRasterPosition[1];
210 const auto screenZ = currentRasterPosition[2];
212 setupRasterPositionStateScreen();
214 glTranslatef(0, 0, -screenZ);
216 const auto textWidth = getScreenTextWidth(labelText);
217 const auto textAscender = getScreenTextAscender(labelText);
219 applyLabelOffset(screenX, screenY, textWidth, textAscender, labelText);
222 label.
screenBoundingBox[1].setValue(screenX + textWidth, screenY + textAscender);
224 resetRasterPositionState();
228 void glDrawString(
const SbVec2f& screenPosition,
const LabelText& labelText)
235 GLfloat getScreenTextWidth(
const LabelText& labelText)
238 return static_cast<GLfloat
>(font->getStringWidth(labelText.
fontSize,labelText.
text.c_str(), strlen(labelText.
text.c_str())));;
241 GLfloat getScreenTextAscender(
const LabelText& labelText)
244 return static_cast<GLfloat
>(font->getFontAscender(labelText.
fontSize));
247 SbVec3f getScreenCoordinate(
const SbVec3f& worldCoordinate)
249 GLdouble modelViewMatrix[16];
250 glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
252 GLdouble projectionMatrix[16];
253 glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
256 glGetIntegerv(GL_VIEWPORT, viewport);
258 GLdouble screenX, screenY, screenZ;
259 gluProject(worldCoordinate[0], worldCoordinate[1], worldCoordinate[2],
260 modelViewMatrix, projectionMatrix, viewport,
261 &screenX, &screenY, &screenZ);
263 return SbVec3f(
static_cast<float>(screenX),
static_cast<float>(screenY),
static_cast<float>(screenZ));
static GL2DFont * getDefaultSystemFont()
get the default system font (already initialized, a valid GL context is only required for the draw me...
static void disableAllClipPlanes()
disables all clip planes (needs a valid OpenGL context!)
SbVec2f textShiftFactorScreen
void initializeScreenBoundingBox()
SbVec2f screenBoundingBox[2]