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,
29 void fillScreenBoundingBox(
Label& label);
31 void setupRenderState();
32 void resetRenderState();
34 void setupRasterPositionStateScreen();
35 void resetRasterPositionState();
37 GLfloat getScreenTextWidth(
const LabelText& labelText);
38 GLfloat getScreenTextAscender(
const LabelText& labelText);
40 SbVec3f getScreenCoordinate(
const SbVec3f& worldCoordinate);
41 SbVec4f getColorWithAlpha(
const SbColor& color,
float alpha)
43 return SbVec4f(color[0], color[1], color[2], alpha);
46 void setupRenderState()
49 glPushAttrib(GL_ALL_ATTRIB_BITS);
50 glDisable(GL_LIGHTING);
53 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
54 glDepthFunc(GL_LEQUAL);
56 glPushAttrib(GL_LINE_BIT);
59 void resetRenderState()
67 glEnable(GL_LIGHTING);
72 void setupRasterPositionStateScreen()
74 glMatrixMode(GL_MODELVIEW);
77 glMatrixMode(GL_PROJECTION);
79 glPushAttrib(GL_ALL_ATTRIB_BITS);
81 glDisable(GL_LIGHTING);
82 glDisable(GL_CULL_FACE);
89 glGetIntegerv(GL_VIEWPORT, viewport);
91 glOrtho(0, viewport[2], 0, viewport[3], 0.f, 1.f);
93 glMatrixMode(GL_MODELVIEW);
96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 void resetRasterPositionState()
103 glMatrixMode(GL_PROJECTION);
105 glMatrixMode(GL_MODELVIEW);
109 void applyLabelOffset(GLfloat& screenX, GLfloat& screenY,
const GLfloat& textWidth,
const GLfloat& textAscender,
112 screenX -= (textWidth * .5f);
113 screenY -= (textAscender * .5f);
124 void renderText(
const LabelText& labelText)
128 GLboolean isValidPosition;
129 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
131 if (isValidPosition) {
132 GLfloat currentRasterPosition[4];
133 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
134 auto screenX = currentRasterPosition[0];
135 auto screenY = currentRasterPosition[1];
136 const auto screenZ = currentRasterPosition[2];
138 setupRasterPositionStateScreen();
140 glTranslatef(0, 0, -screenZ);
142 const auto textWidth = getScreenTextWidth(labelText);
143 const auto textAscender = getScreenTextAscender(labelText);
145 applyLabelOffset(screenX, screenY, textWidth, textAscender, labelText);
147 const SbVec2f screenPosition(screenX, screenY);
149 glDrawString(screenPosition, labelText);
151 resetRasterPositionState();
155 void renderLabel(
const Label& label)
165 glColor4fv(tickColor.getValue());
167 glBegin(GL_LINE_STRIP);
169 glVertex3f(tickStart[0], tickStart[1], tickStart[2]);
170 glVertex3f(tickEnd[0], tickEnd[1], tickEnd[2]);
178 GLfloat currentRasterPosition[4];
179 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
180 const auto screenZ = currentRasterPosition[2];
182 setupRasterPositionStateScreen();
184 glTranslatef(0, 0, -screenZ);
188 resetRasterPositionState();
192 void fillScreenBoundingBox(
Label& label)
200 GLboolean isValidPosition;
201 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
205 if (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)
232 font->drawString(screenPosition[0], screenPosition[1], labelText.
fontSize, textColor.getValue(),
236 GLfloat getScreenTextWidth(
const LabelText& labelText)
239 return static_cast<GLfloat
>(font->getStringWidth(labelText.
fontSize, labelText.
text.c_str()));
242 GLfloat getScreenTextAscender(
const LabelText& labelText)
245 return static_cast<GLfloat
>(font->getFontAscender(labelText.
fontSize));
248 SbVec3f getScreenCoordinate(
const SbVec3f& worldCoordinate)
250 GLdouble modelViewMatrix[16];
251 glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
253 GLdouble projectionMatrix[16];
254 glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
257 glGetIntegerv(GL_VIEWPORT, viewport);
259 GLdouble screenX, screenY, screenZ;
260 gluProject(worldCoordinate[0], worldCoordinate[1], worldCoordinate[2], 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()
static void disableAllClipPlanes()
Disables all clip planes (needs a valid OpenGL context!).
SbVec2f textShiftFactorScreen
void initializeScreenBoundingBox()
SbVec2f screenBoundingBox[2]