@@ -323,7 +323,7 @@ label::gl_render( const view& scene)
323323 // Compute the positions of the text in the text box, and the height of the
324324 // text box. The text positions are relative to the lower left corner of
325325 // the text box.
326- double box_height = border* 2.0 + extents. y ;
326+ double box_height = extents. y + 2.0 *border ;
327327
328328 vector text_pos ( border, box_height - border);
329329
@@ -336,6 +336,24 @@ label::gl_render( const view& scene)
336336 lst = lst * translate;
337337 }
338338 vector origin = (lst * vertex (vector (), 1.0 )).project ();
339+
340+ // It is very important to make sure that the texture is positioned
341+ // accurately at a screen pixel location, to avoid artifacts around the texture.
342+ double kx = scene.view_width /2.0 ;
343+ double ky = scene.view_height /2.0 ;
344+ if (origin.x >= 0 ) {
345+ origin.x = ((int )(kx*origin.x +0.5 ))/kx;
346+ } else {
347+ origin.x = -((int )(-kx*origin.x +0.5 ))/kx;
348+ }
349+ if (origin.y >= 0 ) {
350+ origin.y = ((int )(ky*origin.y +0.5 ))/ky;
351+ } else {
352+ origin.y = -((int )(-ky*origin.y +0.5 ))/ky;
353+ }
354+ double halfwidth = (int )(0.5 *box_width+0.5 );
355+ double halfheight = (int )(0.5 *box_height+0.5 );
356+
339357 rgb stereo_linecolor = linecolor;
340358 if (scene.anaglyph )
341359 if (scene.coloranaglyph )
@@ -356,9 +374,8 @@ label::gl_render( const view& scene)
356374 identity.gl_load ();
357375
358376 glTranslated ( origin.x , origin.y , origin.z );
359- glScaled ( 2.0 /scene.view_width , 2.0 /scene.view_height , 1.0 );
360- // At this point, all furthur translations are in direction of label
361- // space.
377+ glScaled ( 1.0 /kx, 1.0 /ky, 1.0 );
378+ // At this point, all further translations are in direction of label space.
362379 if (space && (xoffset || yoffset)) {
363380 // Move the origin away from the body.
364381 vector space_offset = vector (xoffset, yoffset).norm () * std::fabs (space);
@@ -375,45 +392,46 @@ label::gl_render( const view& scene)
375392 }
376393 if (std::fabs (xoffset) > std::fabs (yoffset)) {
377394 glTranslated (
378- xoffset + ((xoffset > 0 ) ? 0 : -box_width ),
379- yoffset - box_height* 0.5 ,
395+ xoffset + ((xoffset > 0 ) ? 0 : -2.0 *halfwidth ),
396+ yoffset - halfheight ,
380397 0 );
381398 }
382399 else {
383400 glTranslated (
384- xoffset - box_width* 0.5 ,
385- yoffset + ((yoffset > 0 ) ? 0 : -box_height ),
401+ xoffset - halfwidth ,
402+ yoffset + ((yoffset > 0 ) ? 0 : -2.0 *halfheight ),
386403 0 );
387404 }
388405 }
389406 else {
390- glTranslated ( -box_width* 0.5 , -box_height* 0.5 , 0.0 );
407+ glTranslated ( -halfwidth , -halfheight , 0.0 );
391408 }
409+
392410 if (opacity) {
393411 // Occlude objects behind the label.
394412 rgba ( background[0 ], background[1 ], background[2 ], opacity).gl_set ();
395413 glBegin ( GL_QUADS );
396414 vector ().gl_render ();
397- vector ( box_width , 0 ).gl_render ();
398- vector ( box_width, box_height ).gl_render ();
399- vector ( 0 , box_height ).gl_render ();
415+ vector ( 2.0 *halfwidth , 0 ).gl_render ();
416+ vector ( 2.0 *halfwidth, 2.0 *halfheight ).gl_render ();
417+ vector ( 0 , 2.0 *halfheight ).gl_render ();
400418 glEnd ();
401419 }
402420 if (box_enabled) {
403421 // Draw a box around the text.
404422 stereo_linecolor.gl_set (1 .0f );
405423 glBegin ( GL_LINE_LOOP );
406424 vector ().gl_render ();
407- vector ( box_width , 0 ).gl_render ();
408- vector ( box_width, box_height ).gl_render ();
409- vector ( 0 , box_height ).gl_render ();
425+ vector ( 2.0 *halfwidth , 0 ).gl_render ();
426+ vector ( 2.0 *halfwidth, 2.0 *halfheight ).gl_render ();
427+ vector ( 0 , 2.0 *halfheight ).gl_render ();
410428 glEnd ();
411429 }
412430
413431 // Render the text itself.
414432 color.gl_set (1 .0f );
415433 text_layout->gl_render (scene, text_pos);
416- } glMatrixMode ( GL_MODELVIEW ); } // Pops the matricies back off the stack
434+ } glMatrixMode ( GL_MODELVIEW ); } // Pops the matrices back off the stack
417435 list.gl_compile_end ();
418436 check_gl_error ();
419437 scene.screen_objects .insert ( std::make_pair (pos, list));
0 commit comments