16 #include <Inventor/So.h>
24 void renderText(
const LabelText& axisText);
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);
51 glPushAttrib(GL_LINE_BIT);
54 void resetRenderState()
62 glEnable(GL_LIGHTING);
67 void setupRasterPositionStateScreen()
69 glMatrixMode(GL_MODELVIEW);
72 glMatrixMode(GL_PROJECTION);
74 glPushAttrib(GL_ALL_ATTRIB_BITS);
76 glDisable(GL_LIGHTING);
77 glDisable(GL_CULL_FACE);
79 ml::OpenGL::disableAllClipPlanes();
84 glGetIntegerv(GL_VIEWPORT, viewport);
86 glOrtho(0, viewport[2], 0, viewport[3], 0.f, 1.f);
88 glMatrixMode(GL_MODELVIEW);
91 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
94 void resetRasterPositionState()
98 glMatrixMode(GL_PROJECTION);
100 glMatrixMode(GL_MODELVIEW);
104 void applyLabelOffset(GLfloat& screenX, GLfloat& screenY,
const GLfloat& textWidth,
const GLfloat& textAscender,
const LabelText& labelText)
106 screenX -= (textWidth * .5f);
107 screenY -= (textAscender * .5f);
119 void renderText(
const LabelText& labelText)
123 GLboolean isValidPosition;
124 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
128 GLfloat currentRasterPosition[4];
129 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
130 auto screenX = currentRasterPosition[0];
131 auto screenY = currentRasterPosition[1];
132 const auto screenZ = currentRasterPosition[2];
134 setupRasterPositionStateScreen();
136 glTranslatef(0, 0, -screenZ);
138 const auto textWidth = getScreenTextWidth(labelText);
139 const auto textAscender = getScreenTextAscender(labelText);
141 applyLabelOffset(screenX, screenY, textWidth, textAscender, labelText);
143 const SbVec2f screenPosition(screenX, screenY);
145 glDrawString(screenPosition, labelText);
147 resetRasterPositionState();
151 void renderLabel(
const Label& label)
162 glColor4fv(tickColor.getValue());
164 glBegin(GL_LINE_STRIP);
166 glVertex3f(tickStart[0], tickStart[1], tickStart[2]);
167 glVertex3f(tickEnd[0], tickEnd[1], tickEnd[2]);
176 GLfloat currentRasterPosition[4];
177 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
178 const auto screenZ = currentRasterPosition[2];
180 setupRasterPositionStateScreen();
182 glTranslatef(0, 0, -screenZ);
186 resetRasterPositionState();
190 void fillScreenBoundingBox(
Label& label)
198 GLboolean isValidPosition;
199 glGetBooleanv(GL_CURRENT_RASTER_POSITION_VALID, &isValidPosition);
205 GLfloat currentRasterPosition[4];
206 glGetFloatv(GL_CURRENT_RASTER_POSITION, currentRasterPosition);
207 auto screenX = currentRasterPosition[0];
208 auto screenY = currentRasterPosition[1];
209 const auto screenZ = currentRasterPosition[2];
211 setupRasterPositionStateScreen();
213 glTranslatef(0, 0, -screenZ);
215 const auto textWidth = getScreenTextWidth(labelText);
216 const auto textAscender = getScreenTextAscender(labelText);
218 applyLabelOffset(screenX, screenY, textWidth, textAscender, labelText);
221 label.
screenBoundingBox[1].setValue(screenX + textWidth, screenY + textAscender);
223 resetRasterPositionState();
227 void glDrawString(
const SbVec2f& screenPosition,
const LabelText& labelText)
229 const auto font = ml::GL2DFont::getDefaultSystemFont();
234 GLfloat getScreenTextWidth(
const LabelText& labelText)
236 const auto font = ml::GL2DFont::getDefaultSystemFont();
237 return static_cast<GLfloat
>(font->getStringWidth(labelText.
fontSize,labelText.
text.c_str(), strlen(labelText.
text.c_str())));;
240 GLfloat getScreenTextAscender(
const LabelText& labelText)
242 const auto font = ml::GL2DFont::getDefaultSystemFont();
243 return static_cast<GLfloat
>(font->getFontAscender(labelText.
fontSize));
246 SbVec3f getScreenCoordinate(
const SbVec3f& worldCoordinate)
248 GLdouble modelViewMatrix[16];
249 glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
251 GLdouble projectionMatrix[16];
252 glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
255 glGetIntegerv(GL_VIEWPORT, viewport);
257 GLdouble screenX, screenY, screenZ;
258 gluProject(worldCoordinate[0], worldCoordinate[1], worldCoordinate[2],
259 modelViewMatrix, projectionMatrix, viewport,
260 &screenX, &screenY, &screenZ);
262 return SbVec3f(
static_cast<float>(screenX),
static_cast<float>(screenY),
static_cast<float>(screenZ));
SbVec2f textShiftFactorScreen
void initializeScreenBoundingBox()
SbVec2f screenBoundingBox[2]