/opengles/redbook/stroke.c

http://ftk.googlecode.com/ · C · 187 lines · 119 code · 18 blank · 50 comment · 3 complexity · 768a781d85dfd6e4bf2571a4821e27f8 MD5 · raw file

  1. /*
  2. * License Applicability. Except to the extent portions of this file are
  3. * made subject to an alternative license as permitted in the SGI Free
  4. * Software License B, Version 1.1 (the "License"), the contents of this
  5. * file are subject only to the provisions of the License. You may not use
  6. * this file except in compliance with the License. You may obtain a copy
  7. * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8. * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9. *
  10. * http://oss.sgi.com/projects/FreeB
  11. *
  12. * Note that, as provided in the License, the Software is distributed on an
  13. * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
  14. * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
  15. * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. *
  18. * Original Code. The Original Code is: OpenGL Sample Implementation,
  19. * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
  20. * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
  21. * Copyright in any portions created by third parties is as indicated
  22. * elsewhere herein. All Rights Reserved.
  23. *
  24. * Additional Notice Provisions: The application programming interfaces
  25. * established by SGI in conjunction with the Original Code are The
  26. * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
  27. * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
  28. * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
  29. * Window System(R) (Version 1.3), released October 19, 1998. This software
  30. * was created using the OpenGL(R) version 1.2.1 Sample Implementation
  31. * published by SGI, but has not been independently verified as being
  32. * compliant with the OpenGL(R) version 1.2.1 Specification.
  33. *
  34. */
  35. /*
  36. * stroke.c
  37. * This program demonstrates some characters of a
  38. * stroke (vector) font. The characters are represented
  39. * by display lists, which are given numbers which
  40. * correspond to the ASCII values of the characters.
  41. * Use of glCallLists() is demonstrated.
  42. */
  43. #include <GL/glut.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #define PT 1
  47. #define STROKE 2
  48. #define END 3
  49. typedef struct charpoint {
  50. GLfloat x, y;
  51. int type;
  52. } CP;
  53. CP Adata[] = {
  54. { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT},
  55. {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
  56. };
  57. CP Edata[] = {
  58. {5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE},
  59. {0, 5, PT}, {4, 5, END}
  60. };
  61. CP Pdata[] = {
  62. {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT},
  63. {4, 5, PT}, {0, 5, END}
  64. };
  65. CP Rdata[] = {
  66. {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT},
  67. {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}
  68. };
  69. CP Sdata[] = {
  70. {0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT},
  71. {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT},
  72. {4, 10, PT}, {5, 9, END}
  73. };
  74. /* drawLetter() interprets the instructions from the array
  75. * for that letter and renders the letter with line segments.
  76. */
  77. static void drawLetter(CP *l)
  78. {
  79. glBegin(GL_LINE_STRIP);
  80. while (1) {
  81. switch (l->type) {
  82. case PT:
  83. glVertex2fv(&l->x);
  84. break;
  85. case STROKE:
  86. glVertex2fv(&l->x);
  87. glEnd();
  88. glBegin(GL_LINE_STRIP);
  89. break;
  90. case END:
  91. glVertex2fv(&l->x);
  92. glEnd();
  93. glTranslatef(8.0, 0.0, 0.0);
  94. return;
  95. }
  96. l++;
  97. }
  98. }
  99. /* Create a display list for each of 6 characters */
  100. static void init (void)
  101. {
  102. GLuint base;
  103. glShadeModel (GL_FLAT);
  104. base = glGenLists (128);
  105. glListBase(base);
  106. glNewList(base+'A', GL_COMPILE); drawLetter(Adata); glEndList();
  107. glNewList(base+'E', GL_COMPILE); drawLetter(Edata); glEndList();
  108. glNewList(base+'P', GL_COMPILE); drawLetter(Pdata); glEndList();
  109. glNewList(base+'R', GL_COMPILE); drawLetter(Rdata); glEndList();
  110. glNewList(base+'S', GL_COMPILE); drawLetter(Sdata); glEndList();
  111. glNewList(base+' ', GL_COMPILE); glTranslatef(8.0, 0.0, 0.0); glEndList();
  112. }
  113. char *test1 = "A SPARE SERAPE APPEARS AS";
  114. char *test2 = "APES PREPARE RARE PEPPERS";
  115. static void printStrokedString(char *s)
  116. {
  117. GLsizei len = strlen(s);
  118. glCallLists(len, GL_BYTE, (GLbyte *)s);
  119. }
  120. void display(void)
  121. {
  122. glClear(GL_COLOR_BUFFER_BIT);
  123. glColor3f(1.0, 1.0, 1.0);
  124. glPushMatrix();
  125. glScalef(2.0, 2.0, 2.0);
  126. glTranslatef(10.0, 30.0, 0.0);
  127. printStrokedString(test1);
  128. glPopMatrix();
  129. glPushMatrix();
  130. glScalef(2.0, 2.0, 2.0);
  131. glTranslatef(10.0, 13.0, 0.0);
  132. printStrokedString(test2);
  133. glPopMatrix();
  134. glFlush();
  135. }
  136. void reshape(int w, int h)
  137. {
  138. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  139. glMatrixMode (GL_PROJECTION);
  140. glLoadIdentity ();
  141. gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
  142. }
  143. void keyboard(unsigned char key, int x, int y)
  144. {
  145. switch (key) {
  146. case ' ':
  147. glutPostRedisplay();
  148. break;
  149. case 27:
  150. exit(0);
  151. }
  152. }
  153. /* Main Loop
  154. * Open window with initial window size, title bar,
  155. * RGBA display mode, and handle input events.
  156. */
  157. int main(int argc, char** argv)
  158. {
  159. glutInit(&argc, argv);
  160. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  161. glutInitWindowSize (440, 120);
  162. glutCreateWindow ("stroke");
  163. init ();
  164. glutReshapeFunc(reshape);
  165. glutKeyboardFunc(keyboard);
  166. glutDisplayFunc(display);
  167. glutMainLoop();
  168. return 0;
  169. }