/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* text.c - render strings using an X Window System bitmap * font and a stroke font * * Escape key - exit the program */ #include /* includes gl.h, glu.h */ #include #include "axes.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid drawScene( GLvoid ); GLvoid animate( GLvoid ); GLvoid visibility( int ); GLvoid reshape( GLsizei, GLsizei ); GLvoid keyboard( GLubyte, GLint, GLint ); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ static GLdouble left, right, bottom, top; static void *fixedFont, *strokeFont; static char *charStr = "A text string."; GLvoid main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( 0, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow( argv[0] ); initgfx(); glutIdleFunc( animate ); glutVisibilityFunc( visibility ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - render a bitmap font string and a " "stroke font string\n\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glShadeModel( GL_FLAT ); /* Set up two fonts to use for rendering */ fixedFont = GLUT_BITMAP_TIMES_ROMAN_24; strokeFont = GLUT_STROKE_ROMAN; } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case KEY_ESC: /* Exit whenever the Escape key is pressed */ exit(0); } } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); aspect = (GLdouble) width / (GLdouble) height; if ( aspect < 1.0 ) { left = -2.0; right = 2.0; bottom = -2.0 * ( 1.0 / aspect ); top = 2.0 * ( 1.0 / aspect ); } else { left = -2.0 * aspect; right = 2.0 * aspect; bottom = -2.0; top = 2.0; } glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( left, right, bottom, top, -1.0, 1.0 ); glMatrixMode( GL_MODELVIEW ); } GLvoid animate( GLvoid ) { /* Tell GLUT to redraw the scene */ glutPostRedisplay(); } GLvoid visibility( int state ) { if (state == GLUT_VISIBLE) { glutIdleFunc( animate ); } else { glutIdleFunc( NULL ); } } GLvoid renderBitmapString( void *font, char *string ) { int i; int len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); } } GLvoid renderStrokeString( void *font, char *string ) { int i; int len = (int) strlen(string); for (i = 0; i < len; i++) { glutStrokeCharacter(font, string[i]); } } GLvoid drawScene( GLvoid ) { char str[16]; static GLfloat whiteColor[] = { 1.0f, 1.0f, 1.0f }; static GLfloat x = 3.0f; glClear( GL_COLOR_BUFFER_BIT ); XYaxes(); /* update the x position each time we're called; * wrap when we go off the screen */ x -= 0.02f; if ( x <= left ) x = right ; glColor3fv( whiteColor ); glPushMatrix(); /* Bitmap characters ARE NOT affected by * modeling transformations */ glRotatef( 15.0f, 0.0f, 0.0f, 1.0f ); glScalef( 2.0f, 2.0f, 2.0f ); /* Set position for the left end of the baseline for * our text string */ glRasterPos2f( x, 0.0f ); renderBitmapString( fixedFont, charStr ); /* show the baseline */ glBegin( GL_LINES ); glVertex2f( x, 0.0f ); glVertex2f( x+1.0f, 0.0f ); glEnd(); glPopMatrix(); glPushMatrix(); /* Stroke characters ARE affected by * modeling transformations */ glTranslatef( -1.0f, 0.0f, 0.0f ); glRotatef( 15.0f, 0.0f, 0.0f, 1.0f ); glScalef(0.003f, 0.003f, 0.003f ); sprintf( str, "x = %5.2f", x ); renderStrokeString( strokeFont, str ); glPopMatrix(); glutSwapBuffers(); }