home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / oglgold.zip / SAMPLES / GLUT / STROKE.C < prev    next >
C/C++ Source or Header  |  1997-09-30  |  6KB  |  192 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*
  39.  *  stroke.c 
  40.  *  This program demonstrates some characters of a 
  41.  *  stroke (vector) font.  The characters are represented
  42.  *  by display lists, which are given numbers which 
  43.  *  correspond to the ASCII values of the characters.
  44.  *  Use of glCallLists() is demonstrated.
  45.  */
  46. #include <GL/gl.h>
  47. #include <GL/glu.h>
  48. #include <GL/glut.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51.  
  52. #define PT 1
  53. #define STROKE 2
  54. #define END 3
  55.  
  56. typedef struct charpoint {
  57.    GLfloat   x, y;
  58.    int    type;
  59. } CP;
  60.  
  61. CP Adata[] = {
  62.    { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, 
  63.    {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
  64. };
  65.  
  66. CP Edata[] = {
  67.    {5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE},
  68.    {0, 5, PT}, {4, 5, END}
  69. };
  70.  
  71. CP Pdata[] = {
  72.    {0, 0, PT}, {0, 10, PT},  {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, 
  73.    {4, 5, PT}, {0, 5, END}
  74. };
  75.  
  76. CP Rdata[] = {
  77.    {0, 0, PT}, {0, 10, PT},  {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, 
  78.    {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}
  79. };
  80.  
  81. CP Sdata[] = {
  82.    {0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT}, 
  83.    {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT}, 
  84.    {4, 10, PT}, {5, 9, END}
  85. };
  86.  
  87. /*  drawLetter() interprets the instructions from the array
  88.  *  for that letter and renders the letter with line segments.
  89.  */
  90. static void drawLetter(CP *l)
  91. {
  92.    glBegin(GL_LINE_STRIP);
  93.    while (1) {
  94.       switch (l->type) {
  95.          case PT:
  96.             glVertex2fv(&l->x);
  97.             break;
  98.          case STROKE:
  99.             glVertex2fv(&l->x);
  100.             glEnd();
  101.             glBegin(GL_LINE_STRIP);
  102.             break;
  103.          case END:
  104.             glVertex2fv(&l->x);
  105.             glEnd();
  106.             glTranslatef(8.0, 0.0, 0.0);
  107.             return;
  108.       }
  109.       l++;
  110.    }
  111. }
  112.  
  113. /*  Create a display list for each of 6 characters    */
  114. static void init (void)
  115. {
  116.    GLuint base;
  117.  
  118.    glShadeModel (GL_FLAT);
  119.  
  120.    base = glGenLists (128);
  121.    glListBase(base);
  122.    glNewList(base+'A', GL_COMPILE); drawLetter(Adata); glEndList();
  123.    glNewList(base+'E', GL_COMPILE); drawLetter(Edata); glEndList();
  124.    glNewList(base+'P', GL_COMPILE); drawLetter(Pdata); glEndList();
  125.    glNewList(base+'R', GL_COMPILE); drawLetter(Rdata); glEndList();
  126.    glNewList(base+'S', GL_COMPILE); drawLetter(Sdata); glEndList();
  127.    glNewList(base+' ', GL_COMPILE); glTranslatef(8.0, 0.0, 0.0); glEndList();
  128. }
  129.  
  130. char *test1 = "A SPARE SERAPE APPEARS AS";
  131. char *test2 = "APES PREPARE RARE PEPPERS";
  132.  
  133. static void printStrokedString(char *s)
  134. {
  135.    GLsizei len = strlen(s);
  136.    glCallLists(len, GL_BYTE, (GLbyte *)s);
  137. }
  138.  
  139. void display(void)
  140. {
  141.    glClear(GL_COLOR_BUFFER_BIT);
  142.    glColor3f(1.0, 1.0, 1.0);
  143.    glPushMatrix();
  144.    glScalef(2.0, 2.0, 2.0);
  145.    glTranslatef(10.0, 30.0, 0.0);
  146.    printStrokedString(test1);
  147.    glPopMatrix();
  148.    glPushMatrix();
  149.    glScalef(2.0, 2.0, 2.0);
  150.    glTranslatef(10.0, 13.0, 0.0);
  151.    printStrokedString(test2);
  152.    glPopMatrix();
  153.    glFlush();
  154. }
  155.  
  156. void reshape(int w, int h)
  157. {
  158.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  159.    glMatrixMode (GL_PROJECTION);
  160.    glLoadIdentity ();
  161.    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
  162. }
  163.  
  164. void keyboard(unsigned char key, int x, int y)
  165. {
  166.    switch (key) {
  167.       case ' ':
  168.          glutPostRedisplay();
  169.          break;
  170.       case 27:
  171.          exit(0);
  172.    }
  173. }
  174.  
  175. /*  Main Loop
  176.  *  Open window with initial window size, title bar, 
  177.  *  RGBA display mode, and handle input events.
  178.  */
  179. int main(int argc, char** argv)
  180. {
  181.    glutInit(&argc, argv);
  182.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  183.    glutInitWindowSize (440, 120);
  184.    glutCreateWindow ("stroke");
  185.    init ();
  186.    glutReshapeFunc(reshape);
  187.    glutKeyboardFunc(keyboard);
  188.    glutDisplayFunc(display);
  189.    glutMainLoop();
  190.    return 0;
  191. }
  192.