PageRenderTime 83ms CodeModel.GetById 30ms app.highlight 14ms RepoModel.GetById 36ms app.codeStats 1ms

/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/*
 37 *  stroke.c 
 38 *  This program demonstrates some characters of a 
 39 *  stroke (vector) font.  The characters are represented
 40 *  by display lists, which are given numbers which 
 41 *  correspond to the ASCII values of the characters.
 42 *  Use of glCallLists() is demonstrated.
 43 */
 44#include <GL/glut.h>
 45#include <stdlib.h>
 46#include <string.h>
 47
 48#define PT 1
 49#define STROKE 2
 50#define END 3
 51
 52typedef struct charpoint {
 53   GLfloat   x, y;
 54   int    type;
 55} CP;
 56
 57CP Adata[] = {
 58   { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT}, 
 59   {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
 60};
 61
 62CP Edata[] = {
 63   {5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE},
 64   {0, 5, PT}, {4, 5, END}
 65};
 66
 67CP Pdata[] = {
 68   {0, 0, PT}, {0, 10, PT},  {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, 
 69   {4, 5, PT}, {0, 5, END}
 70};
 71
 72CP Rdata[] = {
 73   {0, 0, PT}, {0, 10, PT},  {4, 10, PT}, {5, 9, PT}, {5, 6, PT}, 
 74   {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}
 75};
 76
 77CP Sdata[] = {
 78   {0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT}, 
 79   {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT}, 
 80   {4, 10, PT}, {5, 9, END}
 81};
 82
 83/*  drawLetter() interprets the instructions from the array
 84 *  for that letter and renders the letter with line segments.
 85 */
 86static void drawLetter(CP *l)
 87{
 88   glBegin(GL_LINE_STRIP);
 89   while (1) {
 90      switch (l->type) {
 91         case PT:
 92            glVertex2fv(&l->x);
 93            break;
 94         case STROKE:
 95            glVertex2fv(&l->x);
 96            glEnd();
 97            glBegin(GL_LINE_STRIP);
 98            break;
 99         case END:
100            glVertex2fv(&l->x);
101            glEnd();
102            glTranslatef(8.0, 0.0, 0.0);
103            return;
104      }
105      l++;
106   }
107}
108
109/*  Create a display list for each of 6 characters	*/
110static void init (void)
111{
112   GLuint base;
113
114   glShadeModel (GL_FLAT);
115
116   base = glGenLists (128);
117   glListBase(base);
118   glNewList(base+'A', GL_COMPILE); drawLetter(Adata); glEndList();
119   glNewList(base+'E', GL_COMPILE); drawLetter(Edata); glEndList();
120   glNewList(base+'P', GL_COMPILE); drawLetter(Pdata); glEndList();
121   glNewList(base+'R', GL_COMPILE); drawLetter(Rdata); glEndList();
122   glNewList(base+'S', GL_COMPILE); drawLetter(Sdata); glEndList();
123   glNewList(base+' ', GL_COMPILE); glTranslatef(8.0, 0.0, 0.0); glEndList();
124}
125
126char *test1 = "A SPARE SERAPE APPEARS AS";
127char *test2 = "APES PREPARE RARE PEPPERS";
128
129static void printStrokedString(char *s)
130{
131   GLsizei len = strlen(s);
132   glCallLists(len, GL_BYTE, (GLbyte *)s);
133}
134
135void display(void)
136{
137   glClear(GL_COLOR_BUFFER_BIT);
138   glColor3f(1.0, 1.0, 1.0);
139   glPushMatrix();
140   glScalef(2.0, 2.0, 2.0);
141   glTranslatef(10.0, 30.0, 0.0);
142   printStrokedString(test1);
143   glPopMatrix();
144   glPushMatrix();
145   glScalef(2.0, 2.0, 2.0);
146   glTranslatef(10.0, 13.0, 0.0);
147   printStrokedString(test2);
148   glPopMatrix();
149   glFlush();
150}
151
152void reshape(int w, int h)
153{
154   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
155   glMatrixMode (GL_PROJECTION);
156   glLoadIdentity ();
157   gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
158}
159
160void keyboard(unsigned char key, int x, int y)
161{
162   switch (key) {
163      case ' ':
164         glutPostRedisplay();
165         break;
166      case 27:
167         exit(0);
168   }
169}
170
171/*  Main Loop
172 *  Open window with initial window size, title bar, 
173 *  RGBA display mode, and handle input events.
174 */
175int main(int argc, char** argv)
176{
177   glutInit(&argc, argv);
178   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
179   glutInitWindowSize (440, 120);
180   glutCreateWindow ("stroke");
181   init ();
182   glutReshapeFunc(reshape);
183   glutKeyboardFunc(keyboard);
184   glutDisplayFunc(display);
185   glutMainLoop();
186   return 0;
187}